你可以写一个俄罗斯方块吗?
当然可以!以下是一个简单的俄罗斯方块游戏的示例代码:
import pygame
import random
# 游戏界面大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 方块大小
BLOCK_SIZE = 30
# 游戏界面大小(按方块数量计算)
GRID_WIDTH = WINDOW_WIDTH // BLOCK_SIZE
GRID_HEIGHT = WINDOW_HEIGHT // BLOCK_SIZE
# 方块类型及其形状
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
]
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
ORANGE = (255, 165, 0)
COLORS = [WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, ORANGE]
# 初始化Pygame
pygame.init()
# 创建游戏界面
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 设置游戏标题
pygame.display.set_caption("俄罗斯方块")
# 创建方块类
class Block:
def __init__(self, shape, x, y):
self.shape = shape
self.x = x
self.y = y
self.color = random.choice(COLORS)
# 绘制方块
def draw(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] == 1:
pygame.draw.rect(window, self.color, (self.x * BLOCK_SIZE + j * BLOCK_SIZE, self.y * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 方块向下移动
def move_down(self):
self.y += 1
# 方块向左移动
def move_left(self):
self.x -= 1
# 方块向右移动
def move_right(self):
self.x += 1
# 方块旋转
def rotate(self):
self.shape = list(zip(*self.shape[::-1]))
# 创建游戏类
class Game:
def __init__(self):
self.grid = [[None] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
self.current_block = self.create_block()
self.is_game_over = False
# 创建方块
def create_block(self):
shape = random.choice(SHAPES)
x = (GRID_WIDTH - len(shape[0])) // 2
y = 0
return Block(shape, x, y)
# 判断方块是否可以移动
def can_move(self, block):
for i in range(len(block.shape)):
for j in range(len(block.shape[i])):
if block.shape[i][j] == 1:
if block.x + j < 0 or block.x + j >= GRID_WIDTH or block.y + i >= GRID_HEIGHT or self.grid[block.y + i][block.x + j] is not None:
return False
return True
# 将方块加入到网格中
def add_block_to_grid(self, block):
for i in range(len(block.shape)):
for j in range(len(block.shape[i])):
if block.shape[i][j] == 1:
self.grid[block.y + i][block.x + j] = block.color
# 消除满行
def clear_lines(self):
lines_cleared = 0
for i in range(GRID_HEIGHT):
if None not in self.grid[i]:
self.grid.pop(i)
self.grid.insert(0, [None] * GRID_WIDTH)
lines_cleared += 1
return lines_cleared
# 绘制游戏界面
def draw(self):
window.fill(BLACK)
for i in range(GRID_HEIGHT):
for j in range(GRID_WIDTH):
if self.grid[i][j] is not None:
pygame.draw.rect(window, self.grid[i][j], (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
self.current_block.draw()
# 游戏主循环
def run(self):
clock = pygame.time.Clock()
while not self.is_game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.is_game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if self.can_move(self.current_block):
self.current_block.move_left()
elif event.key == pygame.K_RIGHT:
if self.can_move(self.current_block):
self.current_block.move_right()
elif event.key == pygame.K_DOWN:
if self.can_move(self.current_block):
self.current_block.move_down()
elif event.key == pygame.K_UP:
rotated_block = Block(self.current_block.shape, self.current_block.x, self.current_block.y)
rotated_block.rotate()
if self.can_move(rotated_block):
self.current_block.rotate()
if self.can_move(self.current_block):
self.current_block.move_down()
else:
self.add_block_to_grid(self.current_block)
lines_cleared = self.clear_lines()
if lines_cleared > 0:
print(f"Lines cleared: {lines_cleared}")
self.current_block = self.create_block()
if not self.can_move(self.current_block):
self.is_game_over = True
self.draw()
pygame.display.update()
clock.tick(5)
# 创建游戏对象并运行游戏
game = Game()
game.run()
# 退出Pygame
pygame.quit()
请确保你已经安装了Pygame库,可以通过pip install pygame命令进行安装。运行上述代码后,你将看到一个俄罗斯方块游戏界面,可以使用左右箭头键控制方块的左右移动,上箭头键控制方块旋转,下箭头键控制方块的加速下降。玩家需要尽可能地将方块填满一行或多行并消除,当方块堆到达界面顶部时,游戏结束
原文地址: http://www.cveoy.top/t/topic/h6Db 著作权归作者所有。请勿转载和采集!