当前位置:   article > 正文

如何用Python编写俄罗斯方块Tetris游戏?_pygame tetris

pygame tetris

在本文中,我们将用Python代码构建一个令人惊叹的项目:俄罗斯方块游戏。在这个项目中,我们将使用pygame库来构建游戏。要创建此项目,请确保您的系统中安装了最新版本的Python。让我们开始吧!

Pygame是一组跨平台的Python模块,用于创建视频游戏。它由设计用于Python编程语言的计算机图形和声音库组成。Pygame适合创建客户端应用程序,这些应用程序可能被封装在独立的可执行文件中。要学习pygame,需要具备Python的基本知识。

要安装pygame,请在终端中执行以下代码:

pip install pygame

我们已经完成了项目的先决条件。让我们来看看这个项目中的一些重要功能。

这种update_graphics方法用于设计游戏界面,并在游戏执行过程中进行必要的更新。该函数设置背景颜色和要显示的文本,并设置边框和宽度。它显示当前分数和时间。它绘制一个小屏幕来显示下一个块。为块设置瓷砖,每行20个瓷砖/平方,即19条水平线,每列10个瓷砖/广场,即9条垂直线。

draw_small_screen方法用于设计在游戏执行期间显示下一个块的相同屏幕界面。它设置背景、边框和文本,并显示下一个块。

manage_events函数用于在游戏执行期间处理块。

Python中俄罗斯方块游戏的完整代码:

我们可以在Pygame的帮助下用Python构建俄罗斯方块游戏,因为它有很多功能。现在,让我们从实现部分开始。在注释的帮助下,您可以逐行理解代码。

现在创建两个python文件,并将它们保存为util.py和tetris.py:

util.py文件:

  1. #Import libraries
  2. import pygame
  3. import sys
  4. import random
  5. import time
  6. # Define important global variables
  7. pygame.init()
  8. clock = pygame.time.Clock()
  9. best_score = 0
  10. longest_time = 0
  11. width = 700
  12. height = 750
  13. DISPLAY_SCREEN = pygame.display.set_mode((width, height))
  14. pygame.display.set_caption(" Tetris")
  15. off_set_x = 10
  16. off_set_y = 80
  17. playing_field_width = 330 #330 / 10 = 33 width per tile
  18. playing_field_height = 660 #600 / 20 = 33 height per tile
  19. tile_length = 33 # tile is a square
  20. #colors
  21. blue = (0, 0, 255)
  22. white = (255, 255, 255)
  23. black = (0, 0, 0)
  24. gray = (95, 95, 96)
  25. orange = (249, 87, 0)
  26. cobalt_blue = (3, 65, 174)
  27. green_apple = (114, 203, 59)
  28. cyber_yellow= (255, 213, 0)
  29. beer = (255, 151, 28)
  30. ryb_red = (255, 50, 19)
  31. purple = (128, 0, 128)
  32. # colors of Tetris blocks
  33. block_colors = (cobalt_blue, blue, green_apple, purple, cyber_yellow, beer, ryb_red)
  34. # shapes of Tetris blocks
  35. shapes = ("i_block", "l_block", "j_block", "o_block", "s_block", "t_block", "z_block")
  36. directions = ("vertical_1", "vertical_2", "horizontal_1", "horizontal_2")
  37. background_img = pygame.image.load("resources/images/background_img.jpg")
  38. instructions_img = pygame.image.load("resources/images/instructions_img.jpg")
  39. icon_img = pygame.image.load("resources/images/icon.png")
  40. pygame.display.set_icon(icon_img)
  41. class Button:
  42. def __init__(self, button_color, button_hover_over_color, x, y, width, height, text_size, text_color, text_hover_over_color = None, text_str=""):
  43. self.button_color = button_color
  44. self.button_hover_over_color = button_hover_over_color
  45. self.x = x
  46. self.y = y
  47. self.width = width
  48. self.height = height
  49. self.text_size = text_size
  50. self.text_color = text_color
  51. if text_hover_over_color:
  52. self.text_hover_over_color = text_hover_over_color
  53. else:
  54. self.text_hover_over_color = text_color
  55. self.text_str = text_str
  56. def blit(self, display_screen, outline_color=None):
  57. if outline_color:
  58. pygame.draw.rect(display_screen, outline_color, (self.x-3, self.y-3, self.width+6, self.height+6))
  59. pygame.draw.rect(display_screen, self.button_color, (self.x, self.y, self.width, self.height))
  60. if self.text_str != "":
  61. font = pygame.font.Font("freesansbold.ttf", self.text_size)
  62. text = font.render(self.text_str, True, self.text_color)
  63. # to center the text in the middle of the button based on the size of the button
  64. text_position = (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2))
  65. display_screen.blit(text, text_position)
  66. def is_hovered_over(self, mouse_position):
  67. if self.x < mouse_position[0] < self.x+self.width and self.y < mouse_position[1] < self.y+self.height:
  68. return True
  69. return False
  70. def blit_hovered_over(self, display_screen):
  71. pygame.draw.rect(display_screen, self.button_hover_over_color, (self.x, self.y, self.width, self.height))
  72. if self.text_str != "":
  73. font = pygame.font.Font("freesansbold.ttf", self.text_size)
  74. text = font.render(self.text_str, True, self.text_hover_over_color)
  75. # to center the text in the middle of the button based on the size of the button
  76. text_position = (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2))
  77. display_screen.blit(text, text_position)
  78. def is_clicked(self, mouse_position, event):
  79. if self.is_hovered_over(mouse_position):
  80. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  81. return True
  82. return False
  83. class Tile:
  84. def __init__(self, x, y, color = black):
  85. self.x = x
  86. self.y = y
  87. self.color = color
  88. self.empty = True
  89. def draw_tile(self):
  90. pygame.draw.rect(DISPLAY_SCREEN , self.color, (self.x, self.y, tile_length, tile_length) )
  91. class PlayingField():
  92. def __init__(self):
  93. #y coordinate of first row = (80) off_set_y
  94. self.tiles = {
  95. "row1": {80: []},
  96. "row2": {113: []},
  97. "row3": {146: []},
  98. "row4": {179: []},
  99. "row5": {212: []},
  100. "row6": {245: []},
  101. "row7": {278: []},
  102. "row8": {311: []},
  103. "row9": {344: []},
  104. "row10": {377: []},
  105. "row11": {410: []},
  106. "row12": {443: []},
  107. "row13": {476: []},
  108. "row14": {509: []},
  109. "row15": {542: []},
  110. "row16": {575: []},
  111. "row17": {608: []},
  112. "row18": {641: []},
  113. "row19": {674: []},
  114. "row20": {707: []},
  115. }
  116. self.__init_field()
  117. def __init_field(self):
  118. y = off_set_y
  119. for i in range(20): #rows
  120. x = off_set_x
  121. for j in range(10): #cols
  122. tile_to_add = Tile(x, y)
  123. self.tiles["row"+str(i+1)][y].append(tile_to_add)
  124. x += tile_length
  125. y += tile_length
  126. def destory_full_row(self, player):
  127. times = 0
  128. y = off_set_y
  129. for i in range(20):
  130. for tile in self.tiles["row"+str(i+1)][y]:
  131. if tile.empty: break
  132. elif tile.x == off_set_x+playing_field_width-tile_length:
  133. times += 1
  134. for j in range(800): #just for flashing the row
  135. if j%2 == 0:
  136. pygame.draw.rect(DISPLAY_SCREEN , black, (self.tiles["row"+str(i+1)][y][0].x+1, self.tiles["row"+str(i+1)][y][0].y+1, playing_field_width-2, tile_length-2) )
  137. else:
  138. for tile in self.tiles["row"+str(i+1)][y]:
  139. pygame.draw.rect(DISPLAY_SCREEN , tile.color, (tile.x, tile.y, tile_length, tile_length) )
  140. pygame.draw.line(DISPLAY_SCREEN , white, (off_set_x, y), (playing_field_width+off_set_x-1, y)) # horizontal line
  141. pygame.display.update()
  142. # let's destory this full row
  143. self.destroy_and_replace(i+1, y)
  144. player.score += 10*times
  145. y += tile_length
  146. def destroy_and_replace(self, row_number, row_y):
  147. for i in range (row_number, 1, -1):
  148. prev_row_number = i-1
  149. prev_y = row_y-tile_length
  150. self.tiles["row"+str(i)][row_y].clear() #current_row.clear()
  151. temp_x = off_set_x
  152. for j in range(10):
  153. empty_tile = Tile(temp_x, row_y)
  154. temp_x += tile_length
  155. self.tiles["row"+str(i)][row_y].append(empty_tile)
  156. if prev_y < 80:
  157. break
  158. for j in range(10):
  159. old_tile = self.tiles["row"+str(i)][row_y][j]
  160. new_tile = self.tiles["row"+str(prev_row_number)][prev_y][j]
  161. old_tile.x = new_tile.x
  162. old_tile.color = new_tile.color
  163. old_tile.empty = new_tile.empty
  164. row_y -= tile_length
  165. class Block:
  166. def __init__(self, shape:str, color = black):
  167. self.shape = shape
  168. self.color = color
  169. self.direction = directions[0] #vertical_1
  170. # tile1 , tile2 , tile3 , tile4
  171. self.tiles = [ Tile(off_set_x+playing_field_width/2-tile_length, off_set_y, self.color), Tile(0, 0, color), Tile(0, 0, color), Tile(0, 0, color)]
  172. self.__init_shape()
  173. for tile in self.tiles:
  174. tile.empty = False
  175. def __init_shape(self):
  176. if self.shape == "i_block":
  177. self.tiles[1] = Tile(self.tiles[0].x, self.tiles[0].y-tile_length, self.color)
  178. self.tiles[2] = Tile(self.tiles[0].x, self.tiles[1].y-tile_length, self.color)
  179. self.tiles[3] = Tile(self.tiles[0].x, self.tiles[2].y-tile_length, self.color)
  180. elif self.shape == "l_block":
  181. self.tiles[1] = Tile(self.tiles[0].x+tile_length, self.tiles[0].y, self.color)
  182. self.tiles[2] = Tile(self.tiles[0].x-tile_length, self.tiles[0].y, self.color)
  183. self.tiles[3] = Tile(self.tiles[2].x, self.tiles[2].y-tile_length, self.color)
  184. elif self.shape == "j_block":
  185. self.tiles[1] = Tile(self.tiles[0].x+tile_length, self.tiles[0].y, self.color)
  186. self.tiles[2] = Tile(self.tiles[0].x-tile_length, self.tiles[0].y, self.color)
  187. self.tiles[3] = Tile(self.tiles[1].x, self.tiles[1].y-tile_length, self.color)
  188. elif self.shape == "o_block":
  189. self.tiles[1] = Tile(self.tiles[0].x+tile_length, self.tiles[0].y, self.color)
  190. self.tiles[2] = Tile(self.tiles[0].x, self.tiles[0].y-tile_length, self.color)
  191. self.tiles[3] = Tile(self.tiles[1].x, self.tiles[1].y-tile_length, self.color)
  192. elif self.shape == "s_block":
  193. self.tiles[1] = Tile(self.tiles[0].x-tile_length, self.tiles[0].y, self.color)
  194. self.tiles[2] = Tile(self.tiles[0].x, self.tiles[0].y-tile_length, self.color)
  195. self.tiles[3] = Tile(self.tiles[2].x+tile_length, self.tiles[2].y, self.color)
  196. elif self.shape == "t_block":
  197. self.tiles[1] = Tile(self.tiles[0].x+tile_length, self.tiles[0].y, self.color)
  198. self.tiles[2] = Tile(self.tiles[0].x-tile_length, self.tiles[0].y, self.color)
  199. self.tiles[3] = Tile(self.tiles[0].x, self.tiles[0].y-tile_length, self.color)
  200. elif self.shape == "z_block":
  201. self.tiles[1] = Tile(self.tiles[0].x+tile_length, self.tiles[0].y, self.color)
  202. self.tiles[2] = Tile(self.tiles[0].x, self.tiles[0].y-tile_length, self.color)
  203. self.tiles[3] = Tile(self.tiles[2].x-tile_length, self.tiles[2].y, self.color)
  204. else:
  205. print("Error: wrong block name.")
  206. pygame.quit()
  207. sys.exit()
  208. def complete_block(self):
  209. self.__init_shape()
  210. def can_fall(self, next_block, playing_field, player):
  211. from tetris import manage_events, update_graphics
  212. manage_events(self, next_block, playing_field, player)
  213. #check borders
  214. for block_tile in self.tiles:
  215. if block_tile.y >= playing_field_height+off_set_y-tile_length:
  216. return False
  217. #check already existed tiles
  218. for block_tile in self.tiles:
  219. y = off_set_y
  220. for i in range(20):
  221. for tile in playing_field.tiles["row"+str(i+1)][y]:
  222. if not tile.empty and block_tile.y+tile_length == tile.y and block_tile.x == tile.x:
  223. return False
  224. y += tile_length
  225. return True
  226. def block_is_falling(self, next_block, playing_field, player, faster=None):
  227. from tetris import manage_events, update_graphics
  228. manage_events(self,next_block, playing_field, player)
  229. if self.can_fall(next_block, playing_field, player):
  230. for tile in self.tiles:
  231. tile.y += tile_length
  232. manage_events(self, next_block, playing_field, player)
  233. update_graphics(self, next_block, playing_field, player)
  234. if faster:
  235. clock.tick(40)
  236. self.block_is_falling( next_block, playing_field, player)
  237. else:
  238. clock.tick(5)
  239. manage_events(self, next_block, playing_field, player)
  240. update_graphics(self, next_block, playing_field, player)
  241. def get_new_block(self, next_block, playing_field, player):
  242. if self.can_fall(next_block, playing_field, player): return (self, next_block, False)
  243. #if the block has falled completely
  244. for block_tile in self.tiles:
  245. found = False
  246. y = off_set_y
  247. for i in range(20):
  248. if not found:
  249. for j in range(10):
  250. if block_tile.x == playing_field.tiles["row"+str(i+1)][y][j].x and block_tile.y == playing_field.tiles["row"+str(i+1)][y][j].y:
  251. playing_field.tiles["row"+str(i+1)][y][j].color = block_tile.color
  252. playing_field.tiles["row"+str(i+1)][y][j].empty = False
  253. found = True
  254. break
  255. y += tile_length
  256. else:
  257. break
  258. new_block = next_block
  259. next_rand_index1 = random.randint(0, 6)
  260. next_rand_index2 = random.randint(0, 6)
  261. new_next_block = Block(shapes[next_rand_index1], block_colors[next_rand_index2])
  262. clock.tick(2)
  263. return (new_block, new_next_block, True)
  264. def move_left(self, playing_field):
  265. if self.can_move_left(playing_field):
  266. for tile in self.tiles:
  267. tile.x -= tile_length
  268. def move_right(self, playing_field):
  269. if self.can_move_right(playing_field):
  270. for tile in self.tiles:
  271. tile.x += tile_length
  272. def can_move_left(self, playing_field):
  273. # whether inside the playing field or not
  274. for tile in self.tiles:
  275. if tile.x <= off_set_x:
  276. return False
  277. # whether adjacent field_tiles are occupied or not
  278. for block_tile in self.tiles:
  279. y = off_set_y
  280. for i in range(20):
  281. for tile in playing_field.tiles["row"+str(i+1)][y]:
  282. if not tile.empty and block_tile.x-tile_length == tile.x and block_tile.y == tile.y:
  283. return False
  284. y += tile_length
  285. return True
  286. def can_move_right(self, playing_field):
  287. # whether inside the playing field or not
  288. for tile in self.tiles:
  289. if tile.x + tile_length >= off_set_x+playing_field_width:
  290. return False
  291. # whether adjacent field_tiles are occupied or not
  292. for block_tile in self.tiles:
  293. y = off_set_y
  294. for i in range(20):
  295. for tile in playing_field.tiles["row"+str(i+1)][y]:
  296. if not tile.empty and block_tile.x+tile_length == tile.x and block_tile.y == tile.y:
  297. return False
  298. y += tile_length
  299. return True
  300. def rotate(self, next_block, playing_field, player):
  301. from tetris import manage_events, update_graphics
  302. manage_events(self, next_block, playing_field, player)
  303. if self.shape == "i_block":
  304. self.rotate_i_block(playing_field)
  305. elif self.shape == "l_block":
  306. self.rotate_l_block(playing_field)
  307. elif self.shape == "j_block":
  308. self.rotate_j_block(playing_field)
  309. elif self.shape == "o_block":
  310. return
  311. #no rotation for o_block.
  312. elif self.shape == "s_block":
  313. self.rotate_s_block(playing_field)
  314. elif self.shape == "t_block":
  315. self.rotate_t_block(playing_field)
  316. elif self.shape == "z_block":
  317. self.rotate_z_block(playing_field)
  318. else:
  319. print("Error: wrong block name.")
  320. pygame.quit()
  321. sys.exit()
  322. manage_events(self, next_block, playing_field, player)
  323. update_graphics(self, next_block, playing_field, player)
  324. def rotate_i_block(self, playing_field): #done
  325. temp_rotated_i = Block("i_block", self.color)
  326. temp_rotated_i.tiles = self.tiles.copy()
  327. if self.direction == directions[0] or self.direction == directions[1]:
  328. # ----
  329. temp_rotated_i.tiles[0] = Tile(temp_rotated_i.tiles[1].x, temp_rotated_i.tiles[0].y, temp_rotated_i.color)
  330. temp_rotated_i.tiles[1] = Tile(temp_rotated_i.tiles[0].x-tile_length, temp_rotated_i.tiles[0].y, temp_rotated_i.color)
  331. temp_rotated_i.tiles[2] = Tile(temp_rotated_i.tiles[0].x+tile_length, temp_rotated_i.tiles[0].y, temp_rotated_i.color)
  332. temp_rotated_i.tiles[3] = Tile(temp_rotated_i.tiles[2].x+tile_length, temp_rotated_i.tiles[0].y, temp_rotated_i.color)
  333. temp_rotated_i.direction = directions[2] # "horizontal_1"
  334. elif self.direction == directions[2] or self.direction == directions[3]:
  335. # |
  336. # |
  337. # |
  338. # |
  339. temp_rotated_i.tiles[1] = Tile(temp_rotated_i.tiles[0].x, temp_rotated_i.tiles[0].y-tile_length, temp_rotated_i.color)
  340. temp_rotated_i.tiles[2] = Tile(temp_rotated_i.tiles[1].x, temp_rotated_i.tiles[1].y-tile_length, temp_rotated_i.color)
  341. temp_rotated_i.tiles[3] = Tile(temp_rotated_i.tiles[2].x, temp_rotated_i.tiles[2].y-tile_length, temp_rotated_i.color)
  342. temp_rotated_i.direction = directions[0] #"vertical_1"
  343. for block_tile in temp_rotated_i.tiles:
  344. if block_tile.x <= off_set_x or block_tile.x >= playing_field_width:
  345. return
  346. y = off_set_y
  347. for i in range(20):
  348. for tile in playing_field.tiles["row"+str(i+1)][y]:
  349. if not tile.empty and block_tile.x == tile.x and block_tile.y == tile.y:
  350. return
  351. y += tile_length
  352. self.direction = temp_rotated_i.direction
  353. self.tiles = temp_rotated_i.tiles
  354. def rotate_l_block(self, playing_field): #done
  355. temp_rotated_l = Block("l_block", self.color)
  356. temp_rotated_l.tiles = self.tiles.copy()
  357. if self.direction == directions[0]:
  358. # after rotating, the block should look like this ↓
  359. # _
  360. # |
  361. # |
  362. # |
  363. temp_rotated_l.tiles[0] = Tile(temp_rotated_l.tiles[0].x, temp_rotated_l.tiles[0].y, temp_rotated_l.color)
  364. temp_rotated_l.tiles[1] = Tile(temp_rotated_l.tiles[0].x, temp_rotated_l.tiles[0].y-tile_length, temp_rotated_l.color)
  365. temp_rotated_l.tiles[2] = Tile(temp_rotated_l.tiles[1].x, temp_rotated_l.tiles[1].y-tile_length, temp_rotated_l.color)
  366. temp_rotated_l.tiles[3] = Tile(temp_rotated_l.tiles[2].x+tile_length, temp_rotated_l.tiles[2].y, temp_rotated_l.color)
  367. temp_rotated_l.direction = directions[2] # "horizontal_1"
  368. elif self.direction == directions[2]:
  369. # after rotating, the block should look like this ↓
  370. # ---
  371. # |
  372. temp_rotated_l.tiles[0] = Tile(temp_rotated_l.tiles[3].x, temp_rotated_l.tiles[0].y, temp_rotated_l.color)
  373. temp_rotated_l.tiles[1] = Tile(temp_rotated_l.tiles[0].x, temp_rotated_l.tiles[0].y-tile_length, temp_rotated_l.color)
  374. temp_rotated_l.tiles[2] = Tile(temp_rotated_l.tiles[1].x-tile_length, temp_rotated_l.tiles[1].y, temp_rotated_l.color)
  375. temp_rotated_l.tiles[3] = Tile(temp_rotated_l.tiles[2].x-tile_length, temp_rotated_l.tiles[2].y, temp_rotated_l.color)
  376. temp_rotated_l.direction = directions[1] #"vertical_2"
  377. elif self.direction == directions[1]:
  378. # after rotating, the block should look like this ↓
  379. # |
  380. # |
  381. # _|
  382. temp_rotated_l.tiles[0] = Tile(temp_rotated_l.tiles[3].x, temp_rotated_l.tiles[0].y, temp_rotated_l.color)
  383. temp_rotated_l.tiles[1] = Tile(temp_rotated_l.tiles[0].x+tile_length, temp_rotated_l.tiles[0].y, temp_rotated_l.color)
  384. temp_rotated_l.tiles[2] = Tile(temp_rotated_l.tiles[1].x, temp_rotated_l.tiles[1].y-tile_length, temp_rotated_l.color)
  385. temp_rotated_l.tiles[3] = Tile(temp_rotated_l.tiles[2].x, temp_rotated_l.tiles[2].y-tile_length, temp_rotated_l.color)
  386. temp_rotated_l.direction = directions[3] #"horizontal_2"
  387. elif self.direction == directions[3]:
  388. # after rotating, the block should look like this ↓
  389. # |
  390. # ---
  391. temp_rotated_l.tiles[0] = Tile(temp_rotated_l.tiles[1].x, temp_rotated_l.tiles[0].y, temp_rotated_l.color)
  392. temp_rotated_l.tiles[1] = Tile(temp_rotated_l.tiles[0].x+tile_length, temp_rotated_l.tiles[0].y, temp_rotated_l.color)
  393. temp_rotated_l.tiles[2] = Tile(temp_rotated_l.tiles[0].x-tile_length, temp_rotated_l.tiles[1].y, temp_rotated_l.color)
  394. temp_rotated_l.tiles[3] = Tile(temp_rotated_l.tiles[2].x, temp_rotated_l.tiles[2].y-tile_length, temp_rotated_l.color)
  395. temp_rotated_l.direction = directions[0] #"vertical_1"
  396. for block_tile in temp_rotated_l.tiles:
  397. if block_tile.x <= off_set_x or block_tile.x >= playing_field_width:
  398. return
  399. y = off_set_y
  400. for i in range(20):
  401. for tile in playing_field.tiles["row"+str(i+1)][y]:
  402. if not tile.empty and block_tile.x == tile.x and block_tile.y == tile.y:
  403. return
  404. y += tile_length
  405. self.direction = temp_rotated_l.direction
  406. self.tiles = temp_rotated_l.tiles
  407. def rotate_j_block(self, playing_field): #done
  408. temp_rotated_j = Block("j_block", self.color)
  409. temp_rotated_j.tiles = self.tiles.copy()
  410. if self.direction == directions[0]:
  411. temp_rotated_j.tiles[0] = Tile(temp_rotated_j.tiles[1].x, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  412. temp_rotated_j.tiles[1] = Tile(temp_rotated_j.tiles[0].x-tile_length, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  413. temp_rotated_j.tiles[2] = Tile(temp_rotated_j.tiles[1].x, temp_rotated_j.tiles[1].y-tile_length, temp_rotated_j.color)
  414. temp_rotated_j.tiles[3] = Tile(temp_rotated_j.tiles[2].x, temp_rotated_j.tiles[2].y-tile_length, temp_rotated_j.color)
  415. temp_rotated_j.direction = directions[2] # "horizontal_1"
  416. elif self.direction == directions[2]:
  417. temp_rotated_j.tiles[0] = Tile(temp_rotated_j.tiles[1].x-tile_length, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  418. temp_rotated_j.tiles[1] = Tile(temp_rotated_j.tiles[0].x, temp_rotated_j.tiles[0].y-tile_length, temp_rotated_j.color)
  419. temp_rotated_j.tiles[2] = Tile(temp_rotated_j.tiles[1].x+tile_length, temp_rotated_j.tiles[1].y, temp_rotated_j.color)
  420. temp_rotated_j.tiles[3] = Tile(temp_rotated_j.tiles[2].x+tile_length, temp_rotated_j.tiles[2].y, temp_rotated_j.color)
  421. temp_rotated_j.direction = directions[1] #"vertical_2"
  422. elif self.direction == directions[1]:
  423. temp_rotated_j.tiles[0] = Tile(temp_rotated_j.tiles[2].x, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  424. temp_rotated_j.tiles[1] = Tile(temp_rotated_j.tiles[0].x, temp_rotated_j.tiles[0].y-tile_length, temp_rotated_j.color)
  425. temp_rotated_j.tiles[2] = Tile(temp_rotated_j.tiles[1].x, temp_rotated_j.tiles[1].y-tile_length, temp_rotated_j.color)
  426. temp_rotated_j.tiles[3] = Tile(temp_rotated_j.tiles[2].x-tile_length, temp_rotated_j.tiles[2].y, temp_rotated_j.color)
  427. temp_rotated_j.direction = directions[3] #"horizontal_2"
  428. elif self.direction == directions[3]: #back to normal:
  429. temp_rotated_j.tiles[0] = Tile(temp_rotated_j.tiles[0].x, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  430. temp_rotated_j.tiles[1] = Tile(temp_rotated_j.tiles[0].x+tile_length, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  431. temp_rotated_j.tiles[2] = Tile(temp_rotated_j.tiles[0].x-tile_length, temp_rotated_j.tiles[0].y, temp_rotated_j.color)
  432. temp_rotated_j.tiles[3] = Tile(temp_rotated_j.tiles[1].x, temp_rotated_j.tiles[1].y-tile_length, temp_rotated_j.color)
  433. temp_rotated_j.direction = directions[0] #"vertical_1"
  434. for block_tile in temp_rotated_j.tiles:
  435. if block_tile.x <= off_set_x or block_tile.x >= playing_field_width:
  436. return
  437. y = off_set_y
  438. for i in range(20):
  439. for tile in playing_field.tiles["row"+str(i+1)][y]:
  440. if not tile.empty and block_tile.x == tile.x and block_tile.y == tile.y:
  441. return
  442. y += tile_length
  443. self.direction = temp_rotated_j.direction
  444. self.tiles = temp_rotated_j.tiles
  445. def rotate_s_block(self, playing_field): #done
  446. temp_rotated_s = Block("s_block", self.color)
  447. temp_rotated_s.tiles = self.tiles.copy()
  448. if self.direction == directions[0] or self.direction == directions[1]:
  449. temp_rotated_s.tiles[0] = Tile(temp_rotated_s.tiles[3].x, temp_rotated_s.tiles[0].y, temp_rotated_s.color)
  450. temp_rotated_s.tiles[1] = Tile(temp_rotated_s.tiles[0].x, temp_rotated_s.tiles[0].y-tile_length, temp_rotated_s.color)
  451. temp_rotated_s.tiles[2] = Tile(temp_rotated_s.tiles[1].x-tile_length, temp_rotated_s.tiles[1].y, temp_rotated_s.color)
  452. temp_rotated_s.tiles[3] = Tile(temp_rotated_s.tiles[2].x, temp_rotated_s.tiles[2].y-tile_length, temp_rotated_s.color)
  453. temp_rotated_s.direction = directions[2] # "horizontal_1"
  454. elif self.direction == directions[2] or self.direction == directions[3]
  455. temp_rotated_s.tiles[0] = Tile(temp_rotated_s.tiles[2].x, temp_rotated_s.tiles[0].y, temp_rotated_s.color)
  456. temp_rotated_s.tiles[1] = Tile(temp_rotated_s.tiles[0].x-tile_length, temp_rotated_s.tiles[0].y, temp_rotated_s.color)
  457. temp_rotated_s.tiles[2] = Tile(temp_rotated_s.tiles[0].x, temp_rotated_s.tiles[0].y-tile_length, temp_rotated_s.color)
  458. temp_rotated_s.tiles[3] = Tile(temp_rotated_s.tiles[2].x+tile_length, temp_rotated_s.tiles[2].y, temp_rotated_s.color)
  459. temp_rotated_s.direction = directions[0] #"vertical_1"
  460. for block_tile in temp_rotated_s.tiles:
  461. if block_tile.x <= off_set_x or block_tile.x >= playing_field_width:
  462. return
  463. y = off_set_y
  464. for i in range(20):
  465. for tile in playing_field.tiles["row"+str(i+1)][y]:
  466. if not tile.empty and block_tile.x == tile.x and block_tile.y == tile.y:
  467. return
  468. y += tile_length
  469. self.direction = temp_rotated_s.direction
  470. self.tiles = temp_rotated_s.tiles
  471. def rotate_t_block(self, playing_field): #done
  472. temp_rotated_t = Block("j_block", self.color)
  473. temp_rotated_t.tiles = self.tiles.copy()
  474. if self.direction == directions[0]:
  475. temp_rotated_t.tiles[0] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y, temp_rotated_t.color)
  476. temp_rotated_t.tiles[1] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y-tile_length, temp_rotated_t.color)
  477. temp_rotated_t.tiles[2] = Tile(temp_rotated_t.tiles[1].x, temp_rotated_t.tiles[1].y-tile_length, temp_rotated_t.color)
  478. temp_rotated_t.tiles[3] = Tile(temp_rotated_t.tiles[1].x+tile_length, temp_rotated_t.tiles[1].y, temp_rotated_t.color)
  479. temp_rotated_t.direction = directions[2] # "horizontal_1"
  480. elif self.direction == directions[2]:
  481. temp_rotated_t.tiles[0] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y, temp_rotated_t.color)
  482. temp_rotated_t.tiles[1] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y-tile_length, temp_rotated_t.color)
  483. temp_rotated_t.tiles[2] = Tile(temp_rotated_t.tiles[1].x-tile_length, temp_rotated_t.tiles[1].y, temp_rotated_t.color)
  484. temp_rotated_t.tiles[3] = Tile(temp_rotated_t.tiles[1].x+tile_length, temp_rotated_t.tiles[2].y, temp_rotated_t.color)
  485. temp_rotated_t.direction = directions[1] #"vertical_2"
  486. elif self.direction == directions[1]:
  487. temp_rotated_t.tiles[0] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y, temp_rotated_t.color)
  488. temp_rotated_t.tiles[1] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y-tile_length, temp_rotated_t.color)
  489. temp_rotated_t.tiles[2] = Tile(temp_rotated_t.tiles[1].x, temp_rotated_t.tiles[1].y-tile_length, temp_rotated_t.color)
  490. temp_rotated_t.tiles[3] = Tile(temp_rotated_t.tiles[1].x-tile_length, temp_rotated_t.tiles[1].y, temp_rotated_t.color)
  491. temp_rotated_t.direction = directions[3] #"horizontal_2"
  492. elif self.direction == directions[3]: #back to normal:
  493. temp_rotated_t.tiles[0] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y, temp_rotated_t.color)
  494. temp_rotated_t.tiles[1] = Tile(temp_rotated_t.tiles[0].x+tile_length, temp_rotated_t.tiles[0].y, temp_rotated_t.color)
  495. temp_rotated_t.tiles[2] = Tile(temp_rotated_t.tiles[0].x-tile_length, temp_rotated_t.tiles[0].y, temp_rotated_t.color)
  496. temp_rotated_t.tiles[3] = Tile(temp_rotated_t.tiles[0].x, temp_rotated_t.tiles[0].y-tile_length, temp_rotated_t.color)
  497. temp_rotated_t.direction = directions[0] #"vertical_1"
  498. for block_tile in temp_rotated_t.tiles:
  499. if block_tile.x <= off_set_x or block_tile.x >= playing_field_width:
  500. return
  501. y = off_set_y
  502. for i in range(20):
  503. for tile in playing_field.tiles["row"+str(i+1)][y]:
  504. if not tile.empty and block_tile.x == tile.x and block_tile.y == tile.y:
  505. return
  506. y += tile_length
  507. self.direction = temp_rotated_t.direction
  508. self.tiles = temp_rotated_t.tiles
  509. def rotate_z_block(self, playing_field): #done
  510. temp_rotated_z = Block("z_block", self.color)
  511. temp_rotated_z.tiles = self.tiles.copy()
  512. if self.direction == directions[0] or self.direction == directions[1]:
  513. temp_rotated_z.tiles[0] = Tile(temp_rotated_z.tiles[3].x, temp_rotated_z.tiles[0].y, temp_rotated_z.color)
  514. temp_rotated_z.tiles[1] = Tile(temp_rotated_z.tiles[0].x, temp_rotated_z.tiles[0].y-tile_length, temp_rotated_z.color)
  515. temp_rotated_z.tiles[2] = Tile(temp_rotated_z.tiles[1].x+tile_length, temp_rotated_z.tiles[1].y, temp_rotated_z.color)
  516. temp_rotated_z.tiles[3] = Tile(temp_rotated_z.tiles[2].x, temp_rotated_z.tiles[2].y-tile_length, temp_rotated_z.color)
  517. temp_rotated_z.direction = directions[2] # "horizontal_1"
  518. elif self.direction == directions[2] or self.direction == directions[3]:
  519. temp_rotated_z.tiles[0] = Tile(temp_rotated_z.tiles[3].x, temp_rotated_z.tiles[0].y, temp_rotated_z.color)
  520. temp_rotated_z.tiles[1] = Tile(temp_rotated_z.tiles[0].x+tile_length, temp_rotated_z.tiles[0].y, temp_rotated_z.color)
  521. temp_rotated_z.tiles[2] = Tile(temp_rotated_z.tiles[0].x, temp_rotated_z.tiles[0].y-tile_length, temp_rotated_z.color)
  522. temp_rotated_z.tiles[3] = Tile(temp_rotated_z.tiles[2].x-tile_length, temp_rotated_z.tiles[2].y, temp_rotated_z.color)
  523. temp_rotated_z.direction = directions[0] #"vertical_1"
  524. for block_tile in temp_rotated_z.tiles:
  525. if block_tile.x <= off_set_x or block_tile.x >= playing_field_width:
  526. return
  527. y = off_set_y
  528. for i in range(20):
  529. for tile in playing_field.tiles["row"+str(i+1)][y]:
  530. if not tile.empty and block_tile.x == tile.x and block_tile.y == tile.y:
  531. return
  532. y += tile_length
  533. self.direction = temp_rotated_z.direction
  534. self.tiles = temp_rotated_z.tiles
  535. def fall_completely(self, next_block, playing_field, player):
  536. from tetris import update_graphics
  537. fall= True
  538. while fall:
  539. for block_tile in self.tiles:
  540. if block_tile.y >= playing_field_height+off_set_y-tile_length:
  541. fall = False
  542. break
  543. #check already existed tiles
  544. for block_tile in self.tiles:
  545. y = off_set_y
  546. for i in range(20):
  547. for tile in playing_field.tiles["row"+str(i+1)][y]:
  548. if not tile.empty and block_tile.y+tile_length == tile.y and block_tile.x == tile.x:
  549. fall = False
  550. break
  551. y += tile_length
  552. if not fall:
  553. break
  554. for tile in self.tiles:
  555. tile.y += tile_length
  556. update_graphics(self, next_block, playing_field, player)
  557. clock.get_rawtime()
  558. clock.tick(50)
  559. class Player:
  560. def __init__(self, start_time):
  561. self.start_time = start_time
  562. self.time_since_start = 0
  563. self.score = 0

tetris.py文件:

  1. #import libraries
  2. import pygame
  3. from util import *
  4. def update_graphics(block, next_block, playing_field, player):
  5. #Sets black background and text
  6. DISPLAY_SCREEN.blit(background_img, (0, 0))
  7. pygame.draw.rect(DISPLAY_SCREEN , black, (off_set_x, off_set_y, playing_field_width, playing_field_height) )
  8. font = pygame.font.SysFont("comicsansms", 48)
  9. rendered_text = font.render("Tetris", 1, orange)
  10. DISPLAY_SCREEN.blit(rendered_text, (width/2-80, 10))
  11. #Displays Current score and time
  12. player.time_since_start = pygame.time.get_ticks() - player.start_time
  13. font = pygame.font.SysFont("comicsansms", 20)
  14. rendered_text_time = font.render("Time: " + str(player.time_since_start), 1, orange)
  15. DISPLAY_SCREEN.blit(rendered_text_time, (playing_field_width+tile_length*2, playing_field_height-80))
  16. rendered_text_score = font.render("Score: " + str(player.score), 1, orange)
  17. DISPLAY_SCREEN.blit(rendered_text_score, (playing_field_width+tile_length*2, playing_field_height-50))
  18. #Draw the small screen for the next block
  19. draw_small_screen(next_block)
  20. #Set tiles
  21. y = off_set_y
  22. for i in range(20):
  23. for tile in playing_field.tiles["row"+str(i+1)][y]:
  24. tile.draw_tile()
  25. y += tile_length
  26. #Blocks while falling
  27. for tile in block.tiles:
  28. if tile.y >= off_set_y:
  29. tile.draw_tile()
  30. #Sets borders
  31. pygame.draw.line(DISPLAY_SCREEN , blue, (off_set_x-2, off_set_y-3), (playing_field_width+off_set_x+1, off_set_y-3), 4) # horizontal line top
  32. pygame.draw.line(DISPLAY_SCREEN , blue, (off_set_x-2, off_set_y+playing_field_height+1), (playing_field_width+off_set_x+1, off_set_y+playing_field_height+1), 4) # horizontal line bottom
  33. pygame.draw.line(DISPLAY_SCREEN , blue, (off_set_x-3, off_set_y-3), (off_set_x-3, off_set_y+playing_field_height+1), 4) # vertical line left
  34. pygame.draw.line(DISPLAY_SCREEN , blue, (playing_field_width+off_set_x+1, off_set_y-3), (playing_field_width+off_set_x+1, off_set_y+playing_field_height+1), 4) # vertical line right
  35. #Sets Grid
  36. current_y_horizontal_lines = off_set_y
  37. current_x_vertical_lines = off_set_x
  38. for i in range(19):
  39. current_y_horizontal_lines += 33
  40. pygame.draw.line(DISPLAY_SCREEN , white, (off_set_x, current_y_horizontal_lines), (playing_field_width+off_set_x-1, current_y_horizontal_lines)) # horizontal line top
  41. for j in range(9):
  42. current_x_vertical_lines += 33
  43. pygame.draw.line(DISPLAY_SCREEN , white, (current_x_vertical_lines-1, off_set_y), (current_x_vertical_lines-1, playing_field_height+off_set_y)) # horizontal line top
  44. pygame.display.update()
  45. def draw_small_screen(next_block):
  46. #Sets background
  47. pygame.draw.rect(DISPLAY_SCREEN , black, (playing_field_width+tile_length*2, height/2-20, 6*tile_length, 6*tile_length) )
  48. #Sets borders
  49. pygame.draw.line(DISPLAY_SCREEN , blue, (playing_field_width+tile_length*2-2, height/2-20-2), ((6*tile_length)+(playing_field_width+tile_length*2), (height/2-20-2)), 3) # horizontal line top
  50. pygame.draw.line(DISPLAY_SCREEN , blue, (playing_field_width+tile_length*2-2, height/2-20+(6*tile_length)), ((6*tile_length)+(playing_field_width+tile_length*2), height/2-20+(6*tile_length)), 3) # horizontal line bottom
  51. pygame.draw.line(DISPLAY_SCREEN , blue, (playing_field_width+tile_length*2-2, height/2-20-2), (playing_field_width+tile_length*2-2, height/2-20+(6*tile_length)), 3) # vertical line left
  52. pygame.draw.line(DISPLAY_SCREEN , blue, ((6*tile_length)+(playing_field_width+tile_length*2), height/2-20-2), ((6*tile_length)+(playing_field_width+tile_length*2), height/2-20+(6*tile_length)), 3) # vertical line right
  53. #Sets text
  54. font = pygame.font.SysFont("comicsansms", 30)
  55. rendered_text = font.render("Next Block", 1, orange)
  56. DISPLAY_SCREEN.blit(rendered_text, (playing_field_width+tile_length*2, height/2-70))
  57. #Displays next block
  58. temp_block = Block(next_block.shape, next_block.color)
  59. temp_block.tiles = [Tile(playing_field_width+tile_length*2+2*tile_length, height/2-20+4*tile_length, next_block.color), Tile(0, 0, next_block.color), Tile(0, 0, next_block.color), Tile(0, 0, next_block.color)]
  60. temp_block.complete_block()
  61. for tile in temp_block.tiles:
  62. tile.draw_tile()
  63. def is_game_over(playing_field, player):
  64. y = off_set_y
  65. for i in range(20):
  66. for tile in playing_field.tiles["row"+str(i+1)][y]:
  67. if not tile.empty and tile.y <= off_set_y:
  68. temp_y = off_set_y
  69. for j in range(20):
  70. for tile in playing_field.tiles["row"+str(j+1)][temp_y]:
  71. tile.draw_tile()
  72. temp_y += tile_length
  73. font = pygame.font.SysFont("comicsansms", 48)
  74. rendered_text = font.render("GAME OVER", 1, white)
  75. DISPLAY_SCREEN.blit(rendered_text, (off_set_x+20, playing_field_height/2))
  76. pygame.display.update()
  77. time.sleep(2)
  78. introduction(player)
  79. y += tile_length
  80. def start_game():
  81. global best_score
  82. global longest_time
  83. rand_index = random.randint(0, 6)
  84. block = Block(shapes[rand_index], block_colors[rand_index])
  85. next_rand_index = random.randint(0, 6)
  86. next_block = Block(shapes[next_rand_index], block_colors[next_rand_index])
  87. playing_field = PlayingField()
  88. start_time = pygame.time.get_ticks()
  89. player = Player(start_time)
  90. while True:
  91. update_graphics(block, next_block, playing_field, player)
  92. (block, next_block, is_new) = block.get_new_block(next_block, playing_field, player)
  93. if is_new:
  94. for event in pygame.event.get():
  95. if event.type == pygame.QUIT:
  96. pygame.quit()
  97. sys.exit()
  98. pygame.event.clear()
  99. manage_events(block, next_block, playing_field, player)
  100. update_graphics(block, next_block, playing_field, player)
  101. block.block_is_falling(next_block, playing_field, player)
  102. update_graphics(block, next_block, playing_field, player)
  103. playing_field.destory_full_row(player)
  104. update_graphics(block, next_block, playing_field, player)
  105. if player.score > best_score:
  106. best_score = player.score
  107. if player.time_since_start > longest_time:
  108. longest_time = player.time_since_start
  109. is_game_over(playing_field, player)
  110. update_graphics(block, next_block, playing_field, player)
  111. pygame.display.update()
  112. clock.tick(60)
  113. def manage_events(block, next_block, playing_field, player):
  114. for event in pygame.event.get():
  115. if event.type == pygame.QUIT:
  116. pygame.quit()
  117. sys.exit()
  118. if event.type == pygame.KEYDOWN:
  119. if event.key == pygame.K_LEFT:
  120. #move the block to the left
  121. block.move_left(playing_field)
  122. elif event.key == pygame.K_RIGHT:
  123. #move the block to the right
  124. block.move_right(playing_field)
  125. elif event.key == pygame.K_UP:
  126. # rotate block
  127. block.rotate(next_block, playing_field, player)
  128. if event.key == pygame.K_SPACE:
  129. # let the block fall completely
  130. block.fall_completely(next_block, playing_field, player)
  131. if event.key == pygame.K_DOWN:
  132. # let the block fall down faster
  133. block.block_is_falling(next_block, playing_field, player, "faster")
  134. update_graphics(block, next_block, playing_field, player)
  135. def introduction(player = None):
  136. button_width = 300
  137. button_height = 90
  138. #start_x_button = width/2-button_width/2
  139. play_button = Button(blue, orange, -400, height/2, button_width, button_height, 32, black, white, "PLAY")
  140. instructions_button = Button(blue, orange, width+150, height/2+button_height+10, button_width,button_height, 32, black, white, "INSTRUCTIONS")
  141. quit_button = Button(blue, orange, -400, height/2+button_height*2+20, button_width,button_height, 32, black, white, "QUIT")
  142. font = pygame.font.SysFont("comicsansms", 48)
  143. rendered_text = font.render("Tetris", 1, black)
  144. rendered_text_y = height
  145. #To draw the Tetris text in an animated way
  146. while rendered_text_y > 10:
  147. DISPLAY_SCREEN.blit(background_img, (0, 0))
  148. for event in pygame.event.get():
  149. if event.type == pygame.QUIT:
  150. pygame.quit()
  151. sys.exit()
  152. rendered_text_y -= 1.5
  153. DISPLAY_SCREEN.blit(rendered_text, (width/2-80, rendered_text_y))
  154. pygame.display.update()
  155. #To draw the score and time texts in an animated way
  156. if player:
  157. font_small = pygame.font.SysFont("comicsansms", 30)
  158. rendered_current_score = font_small.render("Current Score: " + str(player.score), 1, orange)
  159. rendered_best_score = font_small.render("Best Score: " + str(best_score), 1, orange)
  160. rendered_current_time = font_small.render("Current Time: " + str(player.time_since_start), 1, orange)
  161. rendered_longest_time = font_small.render("Longest Time: " + str(longest_time), 1, orange)
  162. rendered_current_score_y = height
  163. rendered_best_score_y = height+40
  164. rendered_current_time_y = height+80
  165. rendered_longest_time_y = height+120
  166. while rendered_current_score_y > 150:
  167. DISPLAY_SCREEN.blit(background_img, (0, 0))
  168. DISPLAY_SCREEN.blit(rendered_text, (width/2-80, rendered_text_y))
  169. for event in pygame.event.get():
  170. if event.type == pygame.QUIT:
  171. pygame.quit()
  172. sys.exit()
  173. rendered_current_score_y -= 1.5
  174. rendered_best_score_y -= 1.5
  175. rendered_current_time_y -= 1.5
  176. rendered_longest_time_y -= 1.5
  177. DISPLAY_SCREEN.blit(rendered_current_score, (off_set_x, rendered_current_score_y))
  178. DISPLAY_SCREEN.blit(rendered_best_score, (off_set_x+45, rendered_best_score_y))
  179. DISPLAY_SCREEN.blit(rendered_current_time, (off_set_x+15, rendered_current_time_y))
  180. DISPLAY_SCREEN.blit(rendered_longest_time, (off_set_x+15, rendered_longest_time_y))
  181. pygame.display.update()
  182. #To draw the buttons in an animated way
  183. while play_button.x < width/2-button_width/2 or instructions_button.x > width/2-button_width/2:
  184. DISPLAY_SCREEN.blit(background_img, (0, 0))
  185. DISPLAY_SCREEN.blit(rendered_text, (width/2-80, rendered_text_y))
  186. if player:
  187. DISPLAY_SCREEN.blit(rendered_current_score, (off_set_x, rendered_current_score_y))
  188. DISPLAY_SCREEN.blit(rendered_best_score, (off_set_x+45, rendered_best_score_y))
  189. DISPLAY_SCREEN.blit(rendered_current_time, (off_set_x+15, rendered_current_time_y))
  190. DISPLAY_SCREEN.blit(rendered_longest_time, (off_set_x+15, rendered_longest_time_y))
  191. for event in pygame.event.get():
  192. if event.type == pygame.QUIT:
  193. pygame.quit()
  194. sys.exit()
  195. if play_button.x < width/2-button_width/2:
  196. play_button.x += 3
  197. quit_button.x += 3
  198. if instructions_button.x > width/2-button_width/2 :
  199. instructions_button.x -= 3
  200. play_button.blit(DISPLAY_SCREEN)
  201. instructions_button.blit(DISPLAY_SCREEN)
  202. quit_button.blit(DISPLAY_SCREEN)
  203. pygame.display.update()
  204. run = True
  205. while run:
  206. DISPLAY_SCREEN.blit(background_img, (0, 0))
  207. DISPLAY_SCREEN.blit(rendered_text, (width/2-80, rendered_text_y))
  208. if player:
  209. DISPLAY_SCREEN.blit(rendered_current_score, (off_set_x, rendered_current_score_y))
  210. DISPLAY_SCREEN.blit(rendered_best_score, (off_set_x+45, rendered_best_score_y))
  211. DISPLAY_SCREEN.blit(rendered_current_time, (off_set_x+15, rendered_current_time_y))
  212. DISPLAY_SCREEN.blit(rendered_longest_time, (off_set_x+15, rendered_longest_time_y))
  213. # Get the position of the mouse
  214. mouse_position = pygame.mouse.get_pos()
  215. for event in pygame.event.get():
  216. if event.type == pygame.QUIT:
  217. pygame.quit()
  218. sys.exit()
  219. if event.type == pygame.MOUSEBUTTONDOWN:
  220. if play_button.is_clicked(mouse_position, event):
  221. start_game()
  222. run = False
  223. elif instructions_button.is_clicked(mouse_position, event):
  224. instructions(player)
  225. run = False
  226. elif quit_button.is_clicked(mouse_position, event):
  227. pygame.quit()
  228. sys.exit()
  229. if play_button.is_hovered_over(mouse_position):
  230. play_button.blit_hovered_over(DISPLAY_SCREEN)
  231. else:
  232. play_button.blit(DISPLAY_SCREEN, gray)
  233. if instructions_button.is_hovered_over(mouse_position):
  234. instructions_button.blit_hovered_over(DISPLAY_SCREEN)
  235. else:
  236. instructions_button.blit(DISPLAY_SCREEN, gray)
  237. if quit_button.is_hovered_over(mouse_position):
  238. quit_button.blit_hovered_over(DISPLAY_SCREEN)
  239. else:
  240. quit_button.blit(DISPLAY_SCREEN, gray)
  241. clock.tick(60)
  242. pygame.display.update()
  243. def instructions(player = None):
  244. button_width = 150
  245. button_height = 60
  246. play_button = Button(blue, orange, width-150-10, height-80, button_width, button_height, 32, black, white, "PLAY >>")
  247. back_button = Button(blue, orange, 10, height-80, button_width, button_height, 32, black, white, "<< BACK")
  248. run = True
  249. while run:
  250. DISPLAY_SCREEN.blit(instructions_img, (0, 0))
  251. font = pygame.font.SysFont("comicsansms", 48)
  252. rendered_text = font.render("Tetris", 1, orange)
  253. DISPLAY_SCREEN.blit(rendered_text, (width/2-80, 10))
  254. # Get the position of the mouse
  255. mouse_position = pygame.mouse.get_pos()
  256. for event in pygame.event.get():
  257. if event.type == pygame.QUIT:
  258. pygame.quit()
  259. sys.exit()
  260. if event.type == pygame.MOUSEBUTTONDOWN:
  261. if play_button.is_clicked(mouse_position, event):
  262. start_game()
  263. run = False
  264. elif back_button.is_clicked(mouse_position, event):
  265. introduction(player)
  266. run = False
  267. instructions_label = "Instructions"
  268. font = pygame.font.SysFont("comicsansms", 40)
  269. rendered_text = font.render(instructions_label, 1, orange)
  270. DISPLAY_SCREEN.blit(rendered_text, (width/2 - rendered_text.get_width()/2, 100))
  271. instructions1 = " Move Right: right arrow >"
  272. instructions2 = " Move Left: left arrow <"
  273. instructions3 = " Rotate: up arrow ^"
  274. instructions4 = " Soft Drop: down arrow"
  275. instructions5 = " Hard Drop: space"
  276. font = pygame.font.SysFont("comicsansms", 20)
  277. rendered_text1 = font.render(instructions1, 1, orange)
  278. rendered_text2 = font.render(instructions2, 1, orange)
  279. rendered_text3 = font.render(instructions3, 1, orange)
  280. rendered_text4 = font.render(instructions4, 1, orange)
  281. rendered_text5 = font.render(instructions5, 1, orange)
  282. DISPLAY_SCREEN.blit(rendered_text1, (20, 200))
  283. DISPLAY_SCREEN.blit(rendered_text2, (20, 240))
  284. DISPLAY_SCREEN.blit(rendered_text3, (20, 280))
  285. DISPLAY_SCREEN.blit(rendered_text4, (20, 320))
  286. DISPLAY_SCREEN.blit(rendered_text5, (20, 360))
  287. if play_button.is_hovered_over(mouse_position):
  288. play_button.blit_hovered_over(DISPLAY_SCREEN)
  289. else:
  290. play_button.blit(DISPLAY_SCREEN, gray)
  291. if back_button.is_hovered_over(mouse_position):
  292. back_button.blit_hovered_over(DISPLAY_SCREEN)
  293. else:
  294. back_button.blit(DISPLAY_SCREEN, gray)
  295. clock.tick(60)
  296. pygame.display.update()
  297. if __name__ == "__main__":
  298. introduction()

Python俄罗斯方块Tetris源文件本站下载:
https://download.csdn.net/download/mufenglaoshi/88612722

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/832526
推荐阅读
相关标签
  

闽ICP备14008679号