飞道的博客

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)

470人阅读  评论(0)

导语

警报警报!听说CSDN游戏专区火了火了~竟然是因为各种形状的方块。

对!各种游戏都快烂大街了,俄罗斯方块咋滴就不能火一把了?

Python版俄罗斯方块 等你来战!

所有文章完整的素材+源码都在👇👇

粉丝白嫖源码福利,请移步至CSDN社区或文末公众hao即可免费。

——小Tips

俄罗斯方块,作为是一款家喻户晓的游戏,陪伴70、80甚至90后,度过无忧的儿时岁月

它上手简单能自由组合、拼接技巧也很多。

你知道么,最原始的俄罗斯方块,是长这样婶儿的~

是不是很有童年的味道?今天小编还要给大家,介绍一个全新版本——

程序员的版本,期待期待👇👇👇👇👇👇

正文

自从俄罗斯猫被制裁以后,很多人不禁担心起俄罗斯方块的命运。

虽然名字的含俄量很高,但这款游戏圈抗衰老神话肯定不会遭殃,因为它的版权归美国人

有,跟俄罗斯没半毛钱关系。很多玩了半辈子俄罗斯方块的铁子现在多少能理解乔峰当年的心

情了吧~

算起来,俄罗斯方块都快39岁高龄了,圈子里比它老的游戏没它好玩,比它好玩的游戏没它

老。所以这一款为人类带来无数欢乐的游戏,值得我们更深入的了解。

一、运行环境

小编使用的环境:Python3、Pycharm社区版、,部分自带的就不一一 展示啦。本文主要是一个

Turtle版本的。

 模块安装:pip install -i https://pypi.douban.com/simple/+模块名 

二、代码展示


  
  1. import turtle
  2. import random
  3. class Block:
  4. def __init__(self, color, tiles):
  5. self.color = color
  6. self.tiles = tiles
  7. I = Block( "cyan", [ [ [ 1, 0, 0, 0 ],
  8. [ 1, 0, 0, 0 ],
  9. [ 1, 0, 0, 0 ],
  10. [ 1, 0, 0, 0 ] ],
  11. [ [ 0, 0, 0, 0 ],
  12. [ 0, 0, 0, 0 ],
  13. [ 0, 0, 0, 0 ],
  14. [ 1, 1, 1, 1 ] ] ])
  15. J = Block( "blue", [ [ [ 0, 1, 0 ],
  16. [ 0, 1, 0 ],
  17. [ 1, 1, 0 ] ],
  18. [ [ 0, 0, 0 ],
  19. [ 1, 1, 1 ],
  20. [ 0, 0, 1 ] ],
  21. [ [ 1, 1, 0 ],
  22. [ 1, 0, 0 ],
  23. [ 1, 0, 0 ] ],
  24. [ [ 0, 0, 0 ],
  25. [ 1, 0, 0 ],
  26. [ 1, 1, 1 ] ] ])
  27. L = Block( "orange", [ [ [ 1, 0, 0 ],
  28. [ 1, 0, 0 ],
  29. [ 1, 1, 0 ] ],
  30. [ [ 0, 0, 0 ],
  31. [ 0, 0, 1 ],
  32. [ 1, 1, 1 ] ],
  33. [ [ 0, 1, 1 ],
  34. [ 0, 0, 1 ],
  35. [ 0, 0, 1 ] ],
  36. [ [ 0, 0, 0 ],
  37. [ 1, 1, 1 ],
  38. [ 1, 0, 0 ] ] ])
  39. S = Block( "lime", [ [ [ 0, 0, 0 ],
  40. [ 0, 1, 1 ],
  41. [ 1, 1, 0 ] ],
  42. [ [ 1, 0, 0 ],
  43. [ 1, 1, 0 ],
  44. [ 0, 1, 0 ] ] ])
  45. Z = Block( "red", [ [ [ 0, 0, 0 ],
  46. [ 1, 1, 0 ],
  47. [ 0, 1, 1 ] ],
  48. [ [ 0, 1, 0 ],
  49. [ 1, 1, 0 ],
  50. [ 1, 0, 0 ] ] ])
  51. O = Block( "yellow", [ [ [ 1, 1 ],
  52. [ 1, 1 ] ] ])
  53. T = Block( "magenta", [ [ [ 0, 0, 0 ],
  54. [ 0, 1, 0 ],
  55. [ 1, 1, 1 ] ],
  56. [ [ 0, 1, 0 ],
  57. [ 1, 1, 0 ],
  58. [ 0, 1, 0 ] ],
  59. [ [ 0, 0, 0 ],
  60. [ 1, 1, 1 ],
  61. [ 0, 1, 0 ] ],
  62. [ [ 1, 0, 0 ],
  63. [ 1, 1, 0 ],
  64. [ 1, 0, 0 ] ] ])
  65. tile_size = 25
  66. map_rows = 20
  67. map_cols = 10
  68. map_x = - 125
  69. map_y = 250
  70. map_turtle = turtle. Turtle()
  71. map_turtle. hideturtle()
  72. map_turtle. up()
  73. game_map = [[ "" for _ in range(map_cols)] for _ in range(map_rows)]
  74. active_block = None
  75. active_block_row = 0
  76. active_block_col = 0
  77. active_block_index = 0
  78. block_turtle = turtle. Turtle()
  79. block_turtle. hideturtle()
  80. block_turtle. up()
  81. game_update_interval = 250
  82. score = 0
  83. score_turtle = turtle. Turtle()
  84. score_turtle. hideturtle()
  85. score_turtle. up()
  86. score_turtle. goto( 170, 210)
  87. score_turtle. write( "Score: " + str(score), font=( "Calibri", 20, "bold"))
  88. game_over_turtle = turtle. Turtle()
  89. game_over_turtle. hideturtle()
  90. game_over_turtle. color( "red")
  91. def draw_box(t, width, height, pencolor, fillcolor):
  92. t. color(pencolor, fillcolor)
  93. t. down()
  94. t. begin_fill()
  95. for _ in range( 2):
  96. t. forward(width)
  97. t. right( 90)
  98. t. forward(height)
  99. t. right( 90)
  100. t. end_fill()
  101. t. up()
  102. def draw_map():
  103. map_turtle. clear()
  104. for row in range(map_rows):
  105. for col in range(map_cols):
  106. map_turtle. goto(map_x + tile_size * col, map_y - tile_size * row)
  107. draw_box(map_turtle, tile_size, tile_size, "black", game_map[row][col].color if game_map[row][col] else "mintcream")
  108. def make_new_block():
  109. global active_block
  110. global active_block_row, active_block_col
  111. global active_block_index
  112. active_block = random. choice((I, J, L, S, Z, O, T))
  113. active_block_row = 0
  114. active_block_col = 4
  115. active_block_index = 0
  116. def draw_block():
  117. block_turtle. clear()
  118. # Find the x and y position of the block
  119. x = map_x + active_block_col * tile_size
  120. y = map_y - active_block_row * tile_size
  121. block_tiles = active_block.tiles[active_block_index]
  122. block_color = active_block.color
  123. for row in range( len(block_tiles)):
  124. for col in range( len(block_tiles[row])):
  125. if block_tiles[row][col] == 1:
  126. block_turtle. goto(x+col*tile_size, y-row*tile_size)
  127. draw_box(block_turtle, tile_size, tile_size, "black", block_color)
  128. def is_valid_block(block_type, block_row, block_col, block_index):
  129. block_tiles = block_type.tiles[block_index]
  130. for row in range( len(block_tiles)):
  131. for col in range( len(block_tiles[row])):
  132. if block_tiles[row][col] == 1:
  133. if block_row + row not in range( 0, map_rows):
  134. return False
  135. if block_col + col not in range( 0, map_cols):
  136. return False
  137. if game_map[block_row + row][block_col + col] != "":
  138. return False
  139. return True
  140. def set_block_on_map():
  141. block_tiles = active_block.tiles[active_block_index]
  142. for row in range( len(block_tiles)):
  143. for col in range( len(block_tiles[row])):
  144. if block_tiles[row][col] == 1:
  145. game_map[active_block_row + row][active_block_col + col] = active_block
  146. draw_map()
  147. r = 0
  148. def remove_completed_rows():
  149. global game_map
  150. global score
  151. global game_update_interval
  152. global r
  153. new_map = []
  154. for row in range( len(game_map)):
  155. game_row = game_map[row]
  156. if "" in game_row:
  157. new_map. append(game_row)
  158. else:
  159. score += 10
  160. score_turtle. clear()
  161. score_turtle. write( "Score: " + str(score), font=( "Calibri", 20, "bold"))
  162. r += 1
  163. if r == 5:
  164. game_update_interval = int(game_update_interval/ 1.1)
  165. r = 0
  166. for row in range( 0, map_rows - len(new_map)):
  167. game_row = [ "" for _ in range(map_cols)]
  168. new_map. insert( 0, game_row)
  169. game_map = new_map
  170. draw_map()
  171. # Task: increase the score and difficulty when a row is completed
  172. pause = False
  173. def game_loop():
  174. global active_block, active_block_row
  175. if active_block is None:
  176. make_new_block()
  177. if not is_valid_block(active_block, active_block_row, active_block_col, active_block_index):
  178. active_block = None
  179. game_over_turtle. write( "Game over!", align= "center", font=( "Calibri", 60, "bold"))
  180. return
  181. draw_block()
  182. else:
  183. if is_valid_block(active_block, active_block_row + 1, active_block_col, active_block_index):
  184. if not pause:
  185. active_block_row += 1
  186. draw_block()
  187. else:
  188. set_block_on_map()
  189. active_block = None
  190. remove_completed_rows()
  191. turtle. update()
  192. # Set the next update
  193. turtle. ontimer(game_loop, game_update_interval)
  194. # Set up the turtle window
  195. turtle. setup( 800, 600)
  196. turtle. title( "Tetris")
  197. turtle. bgcolor( "navajowhite")
  198. turtle. up()
  199. turtle. hideturtle()
  200. turtle. tracer(False)
  201. # Draw the background border around the map
  202. turtle. goto(map_x - 10, map_y + 10)
  203. draw_box(turtle, tile_size * map_cols + 20, tile_size * map_rows + 20, \
  204. "", "lightslategray")
  205. # Draw the empty map in the window
  206. draw_map()
  207. turtle. update()
  208. # Set up the game loop
  209. turtle. ontimer(game_loop, game_update_interval)
  210. def rotate():
  211. global active_block_index
  212. if active_block is None:
  213. return
  214. new_block_index = (active_block_index + 1) % len(active_block.tiles)
  215. if is_valid_block(active_block, active_block_row, active_block_col, new_block_index):
  216. active_block_index = new_block_index
  217. draw_block()
  218. turtle. onkeypress(rotate, "Up")
  219. def move_left():
  220. global active_block_col
  221. if active_block is None:
  222. return
  223. if is_valid_block(active_block, active_block_row, active_block_col - 1, active_block_index):
  224. active_block_col -= 1
  225. draw_block()
  226. turtle. onkeypress(move_left, "Left")
  227. def move_right():
  228. global active_block_col
  229. if active_block is None:
  230. return
  231. if is_valid_block(active_block, active_block_row, active_block_col + 1, active_block_index):
  232. active_block_col += 1
  233. draw_block()
  234. turtle. onkeypress(move_right, "Right")
  235. def drop():
  236. global active_block_row
  237. if active_block is None:
  238. return
  239. while is_valid_block(active_block, active_block_row + 1, active_block_col, active_block_index):
  240. active_block_row += 1
  241. draw_block()
  242. turtle. onkeypress(drop, "Down")
  243. def pause_game():
  244. global pause
  245. pause = not pause
  246. turtle. onkeypress(pause_game, "space")
  247. def change_block_type():
  248. global active_block
  249. global active_block_index
  250. new_block = random. choice((I, J, L, S, Z, O, T))
  251. new_block_index = 0
  252. if is_valid_block(new_block, active_block_row, active_block_col, new_block_index):
  253. active_block = new_block
  254. active_block_index = new_block_index
  255. draw_block()
  256. turtle. onkeypress(change_block_type, "c")
  257. turtle. listen()
  258. turtle. done()

三、效果展示

1)游戏开始

2)方块儿截图game over

​普通玩家:                                                              高手玩家:

 总结

有颜色的方块儿还是玩起来有意思些,空格暂停、方向键移动的哈。不管是高手玩家还是普通玩家

都是我可望不可及的程序,仰望.jpg 看我40分的总成绩就知道了!哈哈哈

🎯完整的免费源码领取处:找我吖!文末公众hao可自行领取,滴滴我也可!

🔨推荐往期文章——

项目1.0  超级玛丽

​​​​​​程序员自制游戏:超级玛丽100%真实版,能把你玩哭了~【附源码】

项目1.1   扫雷

 Pygame实战:据说这是史上最难扫雷游戏,没有之一,你们感受下......

项目1.2   魂斗罗

Pygame实战:多年后“魂斗罗”像素风归来 不止是经典与情怀@全体成员

项目1.4  水果忍者

【Pygame实战】风靡全球的切水果游戏升级版“水果忍者”上线啦,你敢来PK嘛?

项目1.0  病毒版蛇蛇大作战

【Pygame实战】这游戏有毒,刷爆朋友圈:小编已与病毒版贪吃蛇大战了三百回合,最高分339?

🎄文章汇总——

汇总合集  Python—2022 |已有文章汇总 | 持续更新,直接看这篇就够了

(更多内容+源码都在✨文章汇总哦!!欢迎阅读喜欢的文章🎉~)


转载:https://blog.csdn.net/weixin_55822277/article/details/128453138
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场