diff options
Diffstat (limited to 'demos/2d/tetris')
-rw-r--r-- | demos/2d/tetris/block.png | bin | 0 -> 556 bytes | |||
-rw-r--r-- | demos/2d/tetris/engine.cfg | 17 | ||||
-rw-r--r-- | demos/2d/tetris/grid.gd | 216 | ||||
-rw-r--r-- | demos/2d/tetris/grid.xml | 153 | ||||
-rw-r--r-- | demos/2d/tetris/tetris.png | bin | 0 -> 789 bytes | |||
-rw-r--r-- | demos/2d/tetris/tetris.xml | 171 |
6 files changed, 557 insertions, 0 deletions
diff --git a/demos/2d/tetris/block.png b/demos/2d/tetris/block.png Binary files differnew file mode 100644 index 0000000000..b7759b35b4 --- /dev/null +++ b/demos/2d/tetris/block.png diff --git a/demos/2d/tetris/engine.cfg b/demos/2d/tetris/engine.cfg new file mode 100644 index 0000000000..b80a736c39 --- /dev/null +++ b/demos/2d/tetris/engine.cfg @@ -0,0 +1,17 @@ +[application] + +name="Tetris" +main_scene="res://tetris.xml" +icon="res://tetris.png" + +[display] + +width=400 +height=400 + +[input] + +move_left=[key(Left)] +move_right=[key(Right)] +move_down=[key(Down)] +rotate=[key(Space)] diff --git a/demos/2d/tetris/grid.gd b/demos/2d/tetris/grid.gd new file mode 100644 index 0000000000..dc89300881 --- /dev/null +++ b/demos/2d/tetris/grid.gd @@ -0,0 +1,216 @@ + + +extends Control + +# Simple Tetris-like demo, (c) 2012 Juan Linietsky +# Implemented by using a regular Control and drawing on it during the _draw() callback. +# The drawing surface is updated only when changes happen (by calling update()) + + +var score = 0 +var score_label=null + +const MAX_SHAPES = 7 + +var block = preload("block.png") + +var block_colors=[ + Color(1,0.5,0.5), + Color(0.5,1,0.5), + Color(0.5,0.5,1), + Color(0.8,0.4,0.8), + Color(0.8,0.8,0.4), + Color(0.4,0.8,0.8), + Color(0.7,0.7,0.7)] + +var block_shapes=[ + [ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I + [ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O + [ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S + [ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z + [ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L + [ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J + [ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T + + +var block_rotations=[ + Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ), + Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ), + Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ), + Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() ) +] + + +var width=0 +var height=0 + +var cells={} + +var piece_active=false +var piece_shape=0 +var piece_pos=Vector2() +var piece_rot=0 + + +func piece_cell_xform(p,er=0): + var r = (4+er+piece_rot)%4 + return piece_pos+block_rotations[r].xform(p) + +func _draw(): + + var sb = get_stylebox("bg","Tree") # use line edit bg + draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3)) + + var bs = block.get_size() + for y in range(height): + for x in range(width): + if (Vector2(x,y) in cells): + draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]]) + + if (piece_active): + + for c in block_shapes[piece_shape]: + draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape]) + + +func piece_check_fit(ofs,er=0): + + for c in block_shapes[piece_shape]: + var pos = piece_cell_xform(c,er)+ofs + if (pos.x < 0): + return false + if (pos.y < 0): + return false + if (pos.x >= width): + return false + if (pos.y >= height): + return false + if (pos in cells): + return false + + return true + +func new_piece(): + + piece_shape = randi() % MAX_SHAPES + piece_pos = Vector2(width/2,0) + piece_active=true + piece_rot=0 + if (piece_shape==0): + piece_pos.y+=1 + + if (not piece_check_fit(Vector2())): + #game over + #print("GAME OVER!") + game_over() + + update() + + +func test_collapse_rows(): + var accum_down=0 + for i in range(height): + var y = height - i - 1 + var collapse = true + for x in range(width): + if (Vector2(x,y) in cells): + if (accum_down): + cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)] + else: + collapse=false + if (accum_down): + cells.erase( Vector2(x,y+accum_down) ) + + if (collapse): + accum_down+=1 + + + score+=accum_down*100 + score_label.set_text(str(score)) + + +func game_over(): + + piece_active=false + get_node("gameover").set_text("Game Over") + update() + + +func restart_pressed(): + + score=0 + score_label.set_text("0") + cells.clear() + get_node("gameover").set_text("") + piece_active=true + update() + + + +func piece_move_down(): + + if (!piece_active): + return + if (piece_check_fit(Vector2(0,1))): + piece_pos.y+=1 + update() + else: + + for c in block_shapes[piece_shape]: + var pos = piece_cell_xform(c) + cells[pos]=piece_shape + test_collapse_rows() + new_piece() + + +func piece_rotate(): + + var adv = 1 + if (not piece_check_fit(Vector2(),1)): + return + piece_rot = (piece_rot + adv) % 4 + update() + + + +func _input(ie): + + + if (not piece_active): + return + if (!ie.is_pressed()): + return + + if (ie.is_action("move_left")): + if (piece_check_fit(Vector2(-1,0))): + piece_pos.x-=1 + update() + elif (ie.is_action("move_right")): + if (piece_check_fit(Vector2(1,0))): + piece_pos.x+=1 + update() + elif (ie.is_action("move_down")): + piece_move_down() + elif (ie.is_action("rotate")): + piece_rotate() + + +func setup(w,h): + width=w + height=h + set_size( Vector2(w,h)*block.get_size() ) + new_piece() + get_node("timer").start() + + +func _ready(): + # Initalization here + + setup(10,20) + score_label = get_node("../score") + + set_process_input(true) + + + + diff --git a/demos/2d/tetris/grid.xml b/demos/2d/tetris/grid.xml new file mode 100644 index 0000000000..072ffb5e75 --- /dev/null +++ b/demos/2d/tetris/grid.xml @@ -0,0 +1,153 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<resource_file type="PackedScene" subresource_count="2" version="0.99" version_name="Godot Engine v0.99.3037-pre-beta"> + <ext_resource path="res://grid.gd" type="GDScript"></ext_resource> + <main_resource> + <string name="resource/name"> "" </string> + <dictionary name="_bundled"> + <string> "names" </string> + <string_array len="57"> + <string> "Grid" </string> + <string> "Control" </string> + <string> "process/process" </string> + <string> "process/fixed_process" </string> + <string> "process/input" </string> + <string> "process/unhandled_input" </string> + <string> "process/mode" </string> + <string> "visibility/visible" </string> + <string> "visibility/toplevel" </string> + <string> "visibility/opacity" </string> + <string> "visibility/self_opacity" </string> + <string> "visibility/on_top" </string> + <string> "visibility/blend_mode" </string> + <string> "transform/notify" </string> + <string> "anchor/left" </string> + <string> "anchor/top" </string> + <string> "anchor/right" </string> + <string> "anchor/bottom" </string> + <string> "margin/left" </string> + <string> "margin/top" </string> + <string> "margin/right" </string> + <string> "margin/bottom" </string> + <string> "hint/tooltip" </string> + <string> "focus_neighbour/left" </string> + <string> "focus_neighbour/top" </string> + <string> "focus_neighbour/right" </string> + <string> "focus_neighbour/bottom" </string> + <string> "focus/ignore_mouse" </string> + <string> "focus/stop_mouse" </string> + <string> "size_flags/horizontal" </string> + <string> "size_flags/vertical" </string> + <string> "size_flags/stretch_ratio" </string> + <string> "script/script" </string> + <string> "__meta__" </string> + <string> "timer" </string> + <string> "Timer" </string> + <string> "wait_time" </string> + <string> "one_shot" </string> + <string> "autostart" </string> + <string> "gameover" </string> + <string> "Label" </string> + <string> "custom_colors/font_color" </string> + <string> "custom_constants/shadow_offset_x" </string> + <string> "custom_constants/shadow_offset_y" </string> + <string> "range/min" </string> + <string> "range/max" </string> + <string> "range/step" </string> + <string> "range/page" </string> + <string> "range/value" </string> + <string> "range/exp_edit" </string> + <string> "rounded_values" </string> + <string> "text" </string> + <string> "align" </string> + <string> "valign" </string> + <string> "autowrap" </string> + <string> "piece_move_down" </string> + <string> "timeout" </string> + </string_array> + <string> "version" </string> + <int> 1 </int> + <string> "conn_count" </string> + <int> 1 </int> + <string> "node_count" </string> + <int> 3 </int> + <string> "variants" </string> + <array len="14"> + <bool> False </bool> + <int> 0 </int> + <bool> True </bool> + <real> 1 </real> + <real> 0 </real> + <real> 40 </real> + <string> "" </string> + <node_path> "" </node_path> + <int> 2 </int> + <resource resource_type="GDScript" path="res://grid.gd"> </resource> + <dictionary> + <string> "__editor_plugin_states__" </string> + <dictionary> + <string> "Script" </string> + <dictionary> + <string> "current" </string> + <int> 0 </int> + <string> "sources" </string> + <array len="1"> + <string> "res://grid.gd" </string> + </array> + </dictionary> + <string> "2D" </string> + <dictionary> + <string> "zoom" </string> + <real> 1 </real> + <string> "ofs" </string> + <vector2> -69, -22 </vector2> + </dictionary> + <string> "3D" </string> + <dictionary> + <string> "zfar" </string> + <real> 500 </real> + <string> "fov" </string> + <real> 45 </real> + <string> "window_mode" </string> + <int> 0 </int> + <string> "window_0" </string> + <dictionary> + <string> "distance" </string> + <real> 4 </real> + <string> "default_light" </string> + <bool> True </bool> + <string> "x_rot" </string> + <real> 0.337 </real> + <string> "y_rot" </string> + <real> -0.575 </real> + <string> "show_grid" </string> + <bool> True </bool> + <string> "show_origin" </string> + <bool> True </bool> + <string> "pos" </string> + <vector3> 0, 0, 0 </vector3> + </dictionary> + <string> "znear" </string> + <real> 0.1 </real> + </dictionary> + </dictionary> + <string> "__editor_run_settings__" </string> + <dictionary> + <string> "custom_args" </string> + <string> "-l $scene" </string> + <string> "run_mode" </string> + <int> 0 </int> + </dictionary> + <string> "__editor_plugin_screen__" </string> + <string> "Script" </string> + </dictionary> + <resource name=""></resource> <int> 1 </int> + <color> 0, 0, 0, 1 </color> + </array> + <string> "nodes" </string> + <int_array len="193"> -1, -1, 1, 0, -1, 32, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 7, 2, 8, 0, 9, 3, 10, 3, 11, 2, 12, 1, 13, 0, 14, 1, 15, 1, 16, 1, 17, 1, 18, 4, 19, 4, 20, 5, 21, 5, 22, 6, 23, 7, 24, 7, 25, 7, 26, 7, 27, 0, 28, 2, 29, 8, 30, 8, 31, 3, 32, 9, 33, 10, 0, 0, 0, 35, 34, -1, 9, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 36, 3, 37, 0, 38, 0, 32, 11, 0, 0, 0, 40, 39, -1, 45, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 7, 2, 8, 0, 9, 3, 10, 3, 11, 2, 12, 1, 13, 0, 14, 1, 15, 1, 16, 12, 17, 12, 18, 4, 19, 4, 20, 4, 21, 4, 22, 6, 23, 7, 24, 7, 25, 7, 26, 7, 27, 2, 28, 2, 29, 8, 30, 1, 31, 3, 41, 13, 42, 12, 43, 12, 44, 4, 45, 3, 46, 3, 47, 3, 48, 4, 49, 0, 50, 0, 51, 6, 52, 12, 53, 12, 54, 0, 32, 11, 0 </int_array> + <string> "conns" </string> + <int_array len="6"> 1, 0, 56, 55, 2, 0 </int_array> + </dictionary> + <resource name="script/script"></resource> + </main_resource> +</resource_file>
\ No newline at end of file diff --git a/demos/2d/tetris/tetris.png b/demos/2d/tetris/tetris.png Binary files differnew file mode 100644 index 0000000000..8b63977bfe --- /dev/null +++ b/demos/2d/tetris/tetris.png diff --git a/demos/2d/tetris/tetris.xml b/demos/2d/tetris/tetris.xml new file mode 100644 index 0000000000..1b5e5afeb0 --- /dev/null +++ b/demos/2d/tetris/tetris.xml @@ -0,0 +1,171 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<resource_file type="PackedScene" subresource_count="2" version="0.99" version_name="Godot Engine v0.99.3037-pre-beta"> + <ext_resource path="res://grid.xml" type="PackedScene"></ext_resource> + <main_resource> + <string name="resource/name"> "" </string> + <dictionary name="_bundled"> + <string> "names" </string> + <string_array len="59"> + <string> "Tetris" </string> + <string> "Panel" </string> + <string> "process/process" </string> + <string> "process/fixed_process" </string> + <string> "process/input" </string> + <string> "process/unhandled_input" </string> + <string> "process/mode" </string> + <string> "visibility/visible" </string> + <string> "visibility/toplevel" </string> + <string> "visibility/opacity" </string> + <string> "visibility/self_opacity" </string> + <string> "visibility/on_top" </string> + <string> "visibility/blend_mode" </string> + <string> "transform/notify" </string> + <string> "anchor/left" </string> + <string> "anchor/top" </string> + <string> "anchor/right" </string> + <string> "anchor/bottom" </string> + <string> "margin/left" </string> + <string> "margin/top" </string> + <string> "margin/right" </string> + <string> "margin/bottom" </string> + <string> "hint/tooltip" </string> + <string> "focus_neighbour/left" </string> + <string> "focus_neighbour/top" </string> + <string> "focus_neighbour/right" </string> + <string> "focus_neighbour/bottom" </string> + <string> "focus/ignore_mouse" </string> + <string> "focus/stop_mouse" </string> + <string> "size_flags/horizontal" </string> + <string> "size_flags/vertical" </string> + <string> "size_flags/stretch_ratio" </string> + <string> "script/script" </string> + <string> "__meta__" </string> + <string> "Grid" </string> + <string> "Control" </string> + <string> "Label" </string> + <string> "range/min" </string> + <string> "range/max" </string> + <string> "range/step" </string> + <string> "range/page" </string> + <string> "range/value" </string> + <string> "range/exp_edit" </string> + <string> "rounded_values" </string> + <string> "text" </string> + <string> "align" </string> + <string> "valign" </string> + <string> "autowrap" </string> + <string> "score" </string> + <string> "restart" </string> + <string> "Button" </string> + <string> "disabled" </string> + <string> "toggle_mode" </string> + <string> "click_on_press" </string> + <string> "icon" </string> + <string> "flat" </string> + <string> "clip_text" </string> + <string> "restart_pressed" </string> + <string> "pressed" </string> + </string_array> + <string> "version" </string> + <int> 1 </int> + <string> "conn_count" </string> + <int> 1 </int> + <string> "node_count" </string> + <int> 5 </int> + <string> "variants" </string> + <array len="30"> + <bool> False </bool> + <int> 0 </int> + <bool> True </bool> + <real> 1 </real> + <real> 0 </real> + <real> 400 </real> + <string> "" </string> + <node_path> "" </node_path> + <int> 2 </int> + <resource name=""></resource> <dictionary> + <string> "__editor_plugin_states__" </string> + <dictionary> + <string> "Script" </string> + <dictionary> + <string> "current" </string> + <int> 0 </int> + <string> "sources" </string> + <array len="1"> + <string> "res://grid.gd" </string> + </array> + </dictionary> + <string> "2D" </string> + <dictionary> + <string> "zoom" </string> + <real> 1.360374 </real> + <string> "ofs" </string> + <vector2> -44.5757, -54.344 </vector2> + </dictionary> + <string> "3D" </string> + <dictionary> + <string> "zfar" </string> + <real> 500 </real> + <string> "fov" </string> + <real> 45 </real> + <string> "window_mode" </string> + <int> 0 </int> + <string> "window_0" </string> + <dictionary> + <string> "distance" </string> + <real> 4 </real> + <string> "default_light" </string> + <bool> True </bool> + <string> "x_rot" </string> + <real> 0.337 </real> + <string> "y_rot" </string> + <real> -0.575 </real> + <string> "show_grid" </string> + <bool> True </bool> + <string> "show_origin" </string> + <bool> True </bool> + <string> "pos" </string> + <vector3> 0, 0, 0 </vector3> + </dictionary> + <string> "znear" </string> + <real> 0.1 </real> + </dictionary> + </dictionary> + <string> "__editor_run_settings__" </string> + <dictionary> + <string> "custom_args" </string> + <string> "-l $scene" </string> + <string> "run_mode" </string> + <int> 0 </int> + </dictionary> + <string> "__editor_plugin_screen__" </string> + <string> "2D" </string> + </dictionary> + <resource resource_type="PackedScene" path="res://grid.xml"> </resource> + <real> 40 </real> + <real> 35 </real> + <real> 80 </real> + <real> 75 </real> + <real> 243 </real> + <real> 36 </real> + <real> 283 </real> + <real> 49 </real> + <string> "Score:" </string> + <real> 252 </real> + <real> 55 </real> + <real> 293 </real> + <real> 68 </real> + <string> "0" </string> + <real> 292 </real> + <real> 303 </real> + <real> 311 </real> + <string> "Restart" </string> + </array> + <string> "nodes" </string> + <int_array len="351"> -1, -1, 1, 0, -1, 32, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 7, 2, 8, 0, 9, 3, 10, 3, 11, 2, 12, 1, 13, 0, 14, 1, 15, 1, 16, 1, 17, 1, 18, 4, 19, 4, 20, 5, 21, 5, 22, 6, 23, 7, 24, 7, 25, 7, 26, 7, 27, 0, 28, 2, 29, 8, 30, 8, 31, 3, 32, 9, 33, 10, 0, 0, 0, 35, 34, 11, 4, 18, 12, 19, 13, 20, 14, 21, 15, 0, 0, 0, 36, 36, -1, 42, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 7, 2, 8, 0, 9, 3, 10, 3, 11, 2, 12, 1, 13, 0, 14, 1, 15, 1, 16, 1, 17, 1, 18, 16, 19, 17, 20, 18, 21, 19, 22, 6, 23, 7, 24, 7, 25, 7, 26, 7, 27, 2, 28, 2, 29, 8, 30, 1, 31, 3, 37, 4, 38, 3, 39, 3, 40, 3, 41, 4, 42, 0, 43, 0, 44, 20, 45, 1, 46, 1, 47, 0, 32, 9, 0, 0, 0, 36, 48, -1, 42, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 7, 2, 8, 0, 9, 3, 10, 3, 11, 2, 12, 1, 13, 0, 14, 1, 15, 1, 16, 1, 17, 1, 18, 21, 19, 22, 20, 23, 21, 24, 22, 6, 23, 7, 24, 7, 25, 7, 26, 7, 27, 2, 28, 2, 29, 8, 30, 1, 31, 3, 37, 4, 38, 3, 39, 3, 40, 3, 41, 4, 42, 0, 43, 0, 44, 25, 45, 1, 46, 1, 47, 0, 32, 9, 0, 0, 0, 50, 49, -1, 38, 2, 0, 3, 0, 4, 0, 5, 0, 6, 1, 7, 2, 8, 0, 9, 3, 10, 3, 11, 2, 12, 1, 13, 0, 14, 1, 15, 1, 16, 1, 17, 1, 18, 16, 19, 26, 20, 27, 21, 28, 22, 6, 23, 7, 24, 7, 25, 7, 26, 7, 27, 0, 28, 2, 29, 8, 30, 8, 31, 3, 51, 0, 52, 0, 53, 0, 44, 29, 54, 9, 55, 0, 56, 0, 32, 9, 0 </int_array> + <string> "conns" </string> + <int_array len="6"> 4, 1, 58, 57, 2, 0 </int_array> + </dictionary> + <resource name="script/script"></resource> + </main_resource> +</resource_file>
\ No newline at end of file |