Advertisement
otorp2

tetris

Jul 15th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1.  
  2.  
  3. extends Control
  4.  
  5. # Simple Tetris-like demo, (c) 2012 Juan Linietsky
  6. # Implemented by using a regular Control and drawing on it during the _draw() callback.
  7. # The drawing surface is updated only when changes happen (by calling update())
  8.  
  9.  
  10. var score = 0
  11. var score_label=null
  12.  
  13. const MAX_SHAPES = 7
  14.  
  15. var block = preload("block.png")
  16.  
  17. var block_colors=[
  18. Color(1,0.5,0.5),
  19. Color(0.5,1,0.5),
  20. Color(0.5,0.5,1),
  21. Color(0.8,0.4,0.8),
  22. Color(0.8,0.8,0.4),
  23. Color(0.4,0.8,0.8),
  24. Color(0.7,0.7,0.7)]
  25.  
  26. var block_shapes=[
  27. [ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I
  28. [ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O
  29. [ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S
  30. [ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z
  31. [ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L
  32. [ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J
  33. [ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T
  34.  
  35.  
  36. var block_rotations=[
  37. Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ),
  38. Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ),
  39. Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ),
  40. Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() )
  41. ]
  42.  
  43.  
  44. var width=0
  45. var height=0
  46.  
  47. var cells={}
  48.  
  49. var piece_active=false
  50. var piece_shape=0
  51. var piece_pos=Vector2()
  52. var piece_rot=0
  53.  
  54.  
  55. func piece_cell_xform(p,er=0):
  56. var r = (4+er+piece_rot)%4
  57. return piece_pos+block_rotations[r].xform(p)
  58.  
  59. func _draw():
  60.  
  61. var sb = get_stylebox("bg","Tree") # use line edit bg
  62. draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3))
  63.  
  64. var bs = block.get_size()
  65. for y in range(height):
  66. for x in range(width):
  67. if (Vector2(x,y) in cells):
  68. draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]])
  69.  
  70. if (piece_active):
  71.  
  72. for c in block_shapes[piece_shape]:
  73. draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape])
  74.  
  75.  
  76. func piece_check_fit(ofs,er=0):
  77.  
  78. for c in block_shapes[piece_shape]:
  79. var pos = piece_cell_xform(c,er)+ofs
  80. if (pos.x < 0):
  81. return false
  82. if (pos.y < 0):
  83. return false
  84. if (pos.x >= width):
  85. return false
  86. if (pos.y >= height):
  87. return false
  88. if (pos in cells):
  89. return false
  90.  
  91. return true
  92.  
  93. func new_piece():
  94.  
  95. piece_shape = randi() % MAX_SHAPES
  96. piece_pos = Vector2(width/2,0)
  97. piece_active=true
  98. piece_rot=0
  99. if (piece_shape==0):
  100. piece_pos.y+=1
  101.  
  102. if (not piece_check_fit(Vector2())):
  103. #game over
  104. #print("GAME OVER!")
  105. game_over()
  106.  
  107. update()
  108.  
  109.  
  110. func test_collapse_rows():
  111. var accum_down=0
  112. for i in range(height):
  113. var y = height - i - 1
  114. var collapse = true
  115. for x in range(width):
  116. if (Vector2(x,y) in cells):
  117. if (accum_down):
  118. cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)]
  119. else:
  120. collapse=false
  121. if (accum_down):
  122. cells.erase( Vector2(x,y+accum_down) )
  123.  
  124. if (collapse):
  125. accum_down+=1
  126.  
  127.  
  128. score+=accum_down*100
  129. score_label.set_text(str(score))
  130.  
  131.  
  132. func game_over():
  133.  
  134. piece_active=false
  135. get_node("gameover").set_text("Game Over")
  136. update()
  137.  
  138.  
  139. func restart_pressed():
  140.  
  141. score=0
  142. score_label.set_text("0")
  143. cells.clear()
  144. get_node("gameover").set_text("")
  145. piece_active=true
  146. get_node("../restart").release_focus()
  147. update()
  148.  
  149.  
  150.  
  151. func piece_move_down():
  152.  
  153. if (!piece_active):
  154. return
  155. if (piece_check_fit(Vector2(0,1))):
  156. piece_pos.y+=1
  157. update()
  158. else:
  159.  
  160. for c in block_shapes[piece_shape]:
  161. var pos = piece_cell_xform(c)
  162. cells[pos]=piece_shape
  163. test_collapse_rows()
  164. new_piece()
  165.  
  166.  
  167. func piece_rotate():
  168.  
  169. var adv = 1
  170. if (not piece_check_fit(Vector2(),1)):
  171. return
  172. piece_rot = (piece_rot + adv) % 4
  173. update()
  174.  
  175.  
  176.  
  177. func _input(ie):
  178.  
  179.  
  180. if (not piece_active):
  181. return
  182. if (!ie.is_pressed()):
  183. return
  184.  
  185. if (ie.is_action("move_left")):
  186. if (piece_check_fit(Vector2(-1,0))):
  187. piece_pos.x-=1
  188. update()
  189. elif (ie.is_action("move_right")):
  190. if (piece_check_fit(Vector2(1,0))):
  191. piece_pos.x+=1
  192. update()
  193. elif (ie.is_action("move_down")):
  194. piece_move_down()
  195. elif (ie.is_action("rotate")):
  196. piece_rotate()
  197.  
  198.  
  199. func setup(w,h):
  200. width=w
  201. height=h
  202. set_size( Vector2(w,h)*block.get_size() )
  203. new_piece()
  204. get_node("timer").start()
  205.  
  206.  
  207. func _ready():
  208. # Initalization here
  209.  
  210. setup(10,20)
  211. score_label = get_node("../score")
  212.  
  213. set_process_input(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement