Python 俄罗斯方块游戏代码示例
下面是用 Python 实现俄罗斯方块游戏的基本代码示例:
import pygame
import random
# 游戏区域大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
GRID_SIZE = 30
GRID_WIDTH = WINDOW_WIDTH // GRID_SIZE
GRID_HEIGHT = WINDOW_HEIGHT // GRID_SIZE
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
# 方块类型及颜色定义
SHAPES = [
[[1, 1, 1, 1]], # I形状
[[1, 1], [1, 1]], # O形状
[[1, 1, 1], [0, 1, 0]], # T形状
[[1, 1, 0], [0, 1, 1]], # Z形状
[[0, 1, 1], [1, 1, 0]], # S形状
[[0, 1, 0], [1, 1, 1]], # J形状
[[0, 1, 0], [0, 1, 0], [1, 1, 1]] # L形状
]
COLORS = [CYAN, YELLOW, MAGENTA, GREEN, RED, BLUE, ORANGE]
class TetrisGame:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Tetris')
self.clock = pygame.time.Clock()
self.grid = [[BLACK] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
self.current_shape = None
self.current_shape_pos = (0, 0)
self.score = 0
self.game_over = False
def new_shape(self):
shape_index = random.randint(0, len(SHAPES) - 1)
self.current_shape = SHAPES[shape_index]
self.current_shape_pos = (GRID_WIDTH // 2 - len(self.current_shape[0]) // 2, 0)
def collide(self):
for row in range(len(self.current_shape)):
for col in range(len(self.current_shape[row])):
if (self.current_shape[row][col] == 1 and
(self.current_shape_pos[0] + col < 0 or
self.current_shape_pos[0] + col >= GRID_WIDTH or
self.current_shape_pos[1] + row >= GRID_HEIGHT or
self.grid[self.current_shape_pos[1] + row][self.current_shape_pos[0] + col] != BLACK)):
return True
return False
def lock_shape(self):
for row in range(len(self.current_shape)):
for col in range(len(self.current_shape[row])):
if self.current_shape[row][col] == 1:
self.grid[self.current_shape_pos[1] + row][self.current_shape_pos[0] + col] = COLORS.index(COLOR)
def clear_rows(self):
full_rows = [row for row in range(GRID_HEIGHT) if self.grid[row] == [COLOR] * GRID_WIDTH]
for row in full_rows:
del self.grid[row]
self.grid.insert(0, [BLACK] * GRID_WIDTH)
self.score += 1
def draw_grid(self):
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
pygame.draw.rect(self.screen, self.grid[row][col], (col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE))
def draw_shape(self):
for row in range(len(self.current_shape)):
for col in range(len(self.current_shape[row])):
if self.current_shape[row][col] == 1:
pygame.draw.rect(self.screen, COLORS.index(COLOR),
((self.current_shape_pos[0] + col) * GRID_SIZE,
(self.current_shape_pos[1] + row) * GRID_SIZE,
GRID_SIZE, GRID_SIZE))
def draw_score(self):
font = pygame.font.Font(None, 36)
text = font.render(f'Score: {self.score}', True, WHITE)
self.screen.blit(text, (10, 10))
def game_over_screen(self):
font = pygame.font.Font(None, 72)
text = font.render('Game Over', True, WHITE)
self.screen.blit(text, (WINDOW_WIDTH // 2 - text.get_width() // 2, WINDOW_HEIGHT // 2 - text.get_height() // 2))
pygame.display.flip()
pygame.time.wait(3000)
def run(self):
self.new_shape()
while not self.game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.current_shape_pos = (self.current_shape_pos[0] - 1, self.current_shape_pos[1])
if self.collide():
self.current_shape_pos = (self.current_shape_pos[0] + 1, self.current_shape_pos[1])
elif event.key == pygame.K_RIGHT:
self.current_shape_pos = (self.current_shape_pos[0] + 1, self.current_shape_pos[1])
if self.collide():
self.current_shape_pos = (self.current_shape_pos[0] - 1, self.current_shape_pos[1])
elif event.key == pygame.K_DOWN:
self.current_shape_pos = (self.current_shape_pos[0], self.current_shape_pos[1] + 1)
if self.collide():
self.current_shape_pos = (self.current_shape_pos[0], self.current_shape_pos[1] - 1)
elif event.key == pygame.K_UP:
rotated_shape = list(zip(*reversed(self.current_shape)))
if not self.collide():
self.current_shape = rotated_shape
elif self.current_shape_pos[0] + len(rotated_shape[0]) > GRID_WIDTH:
self.current_shape_pos = (self.current_shape_pos[0] - 1, self.current_shape_pos[1])
if not self.collide():
self.current_shape = rotated_shape
else:
self.current_shape_pos = (self.current_shape_pos[0] + 1, self.current_shape_pos[1])
elif self.current_shape_pos[0] < 0:
self.current_shape_pos = (self.current_shape_pos[0] + 1, self.current_shape_pos[1])
if not self.collide():
self.current_shape = rotated_shape
else:
self.current_shape_pos = (self.current_shape_pos[0] - 1, self.current_shape_pos[1])
self.current_shape_pos = (self.current_shape_pos[0], self.current_shape_pos[1] + 1)
if self.collide():
self.current_shape_pos = (self.current_shape_pos[0], self.current_shape_pos[1] - 1)
self.lock_shape()
self.clear_rows()
if self.current_shape_pos[1] == 0:
self.game_over = True
else:
self.new_shape()
self.screen.fill(BLACK)
self.draw_grid()
self.draw_shape()
self.draw_score()
pygame.display.flip()
self.clock.tick(10)
self.game_over_screen()
pygame.quit()
if __name__ == '__main__':
game = TetrisGame()
game.run()
这段代码使用了 pygame 库来实现游戏窗口、事件处理和绘制功能。在游戏中,方块会从顶部下落,玩家可以使用键盘的左右箭头键来移动方块,向下箭头键加速下落,向上箭头键旋转方块。当一行方块填满时,该行会被清除,并且玩家得分增加。游戏结束条件为方块堆到游戏区域的顶部。
你可以根据需要调整窗口大小、方块大小、颜色等参数来定制游戏的外观和行为。希望这段代码能帮助你开始实现一个俄罗斯方块游戏!
原文地址: https://www.cveoy.top/t/topic/rBg 著作权归作者所有。请勿转载和采集!