当前位置:   article > 正文

俄罗斯方块游戏的Python代码示例_俄罗斯方块python代码

俄罗斯方块python代码
  1. import pygame
  2. from pygame.locals import *
  3. import random
  4. # 定义方块的形状
  5. shapes = [
  6. [[1, 1, 1, 1]],
  7. [[1, 1],
  8. [1, 1]],
  9. [[1, 1, 0],
  10. [0, 1, 1]],
  11. [[0, 1, 1],
  12. [1, 1, 0]],
  13. [[1, 1, 1],
  14. [0, 1, 0]],
  15. [[1, 1, 1],
  16. [1, 0, 0]],
  17. [[1, 1, 1],
  18. [0, 0, 1]]
  19. ]
  20. # 初始化游戏
  21. pygame.init()
  22. WIDTH, HEIGHT = 300, 600
  23. WIN = pygame.display.set_mode((WIDTH, HEIGHT))
  24. pygame.display.set_caption("俄罗斯方块")
  25. # 定义颜色
  26. WHITE = (255, 255, 255)
  27. BLACK = (0, 0, 0)
  28. RED = (255, 0, 0)
  29. GREEN = (0, 255, 0)
  30. BLUE = (0, 0, 255)
  31. YELLOW = (255, 255, 0)
  32. CYAN = (0, 255, 255)
  33. ORANGE = (255, 165, 0)
  34. PURPLE = (128, 0, 128)
  35. # 定义方块的大小和位置
  36. BLOCK_SIZE = 30
  37. GRID_WIDTH, GRID_HEIGHT = WIDTH // BLOCK_SIZE, HEIGHT // BLOCK_SIZE
  38. # 游戏循环
  39. def game_loop():
  40. grid = [[BLACK] * GRID_WIDTH for _ in range(GRID_HEIGHT)] # 初始化游戏网格
  41. # 创建一个随机方块
  42. shape = random.choice(shapes)
  43. current_x = GRID_WIDTH // 2 - len(shape[0]) // 2
  44. current_y = 0
  45. clock = pygame.time.Clock()
  46. game_over = False
  47. while not game_over:
  48. for event in pygame.event.get():
  49. if event.type == QUIT:
  50. pygame.quit()
  51. return
  52. if event.type == KEYDOWN:
  53. if event.key == K_LEFT:
  54. current_x -= 1
  55. if not check_collision(shape, current_x, current_y, grid):
  56. current_x += 1
  57. elif event.key == K_RIGHT:
  58. current_x += 1
  59. if not check_collision(shape, current_x, current_y, grid):
  60. current_x -= 1
  61. elif event.key == K_DOWN:
  62. current_y += 1
  63. if not check_collision(shape, current_x, current_y, grid):
  64. current_y -= 1
  65. elif event.key == K_SPACE:
  66. shape = rotate(shape)
  67. if not check_collision(shape, current_x, current_y, grid):
  68. shape = rotate(shape, rotate_counterclockwise=True)
  69. if not check_collision(shape, current_x, current_y, grid):
  70. current_y += 1
  71. if check_game_over(shape, current_x, current_y, grid):
  72. game_over = True
  73. # 更新游戏网格
  74. update_grid(shape, current_x, current_y, grid)
  75. # 消除满行
  76. clear_lines(grid)
  77. # 画出游戏界面
  78. draw_grid(grid)
  79. draw_shape(shape, current_x, current_y)
  80. pygame.display.flip()
  81. clock.tick(5)
  82. # 检查方块是否与游戏网格发生碰撞
  83. def check_collision(shape, x, y, grid):
  84. for row in range(len(shape)):
  85. for col in range(len(shape[row])):
  86. if shape[row][col] == 1:
  87. if (y + row >= GRID_HEIGHT or x + col < 0 or x + col >= GRID_WIDTH or
  88. grid[y + row][x + col] != BLACK):
  89. return True
  90. return False
  91. # 检查游戏是否结束
  92. def check_game_over(shape, x, y, grid):
  93. for row in range(len(shape)):
  94. for col in range(len(shape[row])):
  95. if shape[row][col] == 1 and y + row < 0:
  96. return True
  97. return False
  98. # 更新游戏网格
  99. def update_grid(shape, x, y, grid):
  100. for row in range(len(shape)):
  101. for col in range(len(shape[row])):
  102. if shape[row][col] == 1:
  103. grid[y + row][x + col] = current_color
  104. # 消除满行
  105. def clear_lines(grid):
  106. for row in range(GRID_HEIGHT):
  107. if all(cell != BLACK for cell in grid[row]):
  108. del grid[row]
  109. grid.insert(0, [BLACK] * GRID_WIDTH)
  110. # 旋转方块
  111. def rotate(shape, rotate_counterclockwise=False):
  112. if rotate_counterclockwise:
  113. return [[shape[col][len(shape) - 1 - row] for col in range(len(shape))]
  114. for row in range(len(shape[0]))]
  115. else:
  116. return [[shape[len(shape) - 1 - col][row] for col in range(len(shape))]
  117. for row in range(len(shape[0]))]
  118. # 画出游戏网格
  119. def draw_grid(grid):
  120. for row in range(GRID_HEIGHT):
  121. for col in range(GRID_WIDTH):
  122. pygame.draw.rect(WIN, grid[row][col],
  123. (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
  124. pygame.draw.rect(WIN, WHITE,
  125. (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
  126. # 画出方块
  127. def draw_shape(shape, x, y):
  128. for row in range(len(shape)):
  129. for col in range(len(shape[row])):
  130. if shape[row][col] == 1:
  131. pygame.draw.rect(WIN, current_color,
  132. ((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
  133. pygame.draw.rect(WIN, WHITE,
  134. ((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
  135. # 游戏开始
  136. game_loop()

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

闽ICP备14008679号