当然可以,以下是一个简单的俄罗斯方块的 Python 代码示例:
``` python
import pygame
import random
# 初始化Pygame
pygame.init()
# 创建游戏窗口
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("俄罗斯方块")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 定义方块大小和格子数
block_size = 20
grid_width = screen_width // block_size
grid_height = screen_height // block_size
# 定义方块形状
shapes = [
[[1, 1],
[1, 1]],
[[0, 2, 0],
[2, 2, 2]],
[[0, 3, 3],
[3, 3, 0]],
[[4, 4, 0],
[0, 4, 4]],
[[5, 5, 5, 5]],
[[6, 6, 6],
[0, 6, 0],
[0, 6, 0]],
[[0, 7, 0],
[7, 7, 7],
[0, 7, 0]]
]
# 定义方块颜色
colors = [
BLACK,
RED,
GREEN,
BLUE,
YELLOW
]
# 定义游戏变量
score = 0
grid = [[0] * grid_width for _ in range(grid_height)]
cur_shape = random.choice(shapes)
cur_color = random.choice(colors)
cur_x = grid_width // 2 - len(cur_shape[0]) // 2
cur_y = 0
# 定义游戏函数
def draw_block(x, y, color):
pygame.draw.rect(screen, color, (x * block_size, y * block_size, block_size, block_size))
def draw_shape(shape, x, y, color):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] != 0:
draw_block(x + j, y + i, color)
def check_collision(shape, x, y):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] != 0 and (y + i >= grid_height or x + j < 0 or x + j >= grid_width or grid[y + i][x + j] != 0):
return True
return False
def place_shape(shape, x, y, color):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] != 0:
grid[y + i][x + j] = color
def remove_lines():
global score
lines_removed = 0
for i in range(grid_height):
if all(grid[i]):
grid.pop(i)
grid.insert(0, [0] * grid_width)
lines_removed += 1
score += lines_removed ** 2
# 游戏循环
clock = pygame.time.Clock()
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
cur_x -= 1
if check_collision(cur_shape, cur_x, cur_y):
cur_x += 1
elif event.key == pygame.K_RIGHT:
cur_x += 1
if check_collision(cur_shape, cur_x, cur_y):
cur_x -= 1
elif event.key == pygame.K_UP:
old_shape = cur_shape
cur_shape = [[cur_shape[j][i] for j in range(len(cur_shape))] for i in range(len(cur_shape[0]) - 1, -1, -1)]
if check_collision(cur_shape, cur_x, cur_y):
cur_shape = old_shape
elif event.key == pygame.K_DOWN:
cur_y += 1
if check_collision(cur_shape, cur_x, cur_y):
cur_y -= 1
# 更新游戏状态
cur_y += 1
if check_collision(cur_shape, cur_x, cur_y):
cur_y -= 1
place_shape(cur_shape, cur_x, cur_y, cur_color)
remove_lines()
cur_shape = random.choice(shapes)
cur_color = random.choice(colors)
cur_x = grid_width // 2 - len(cur_shape[0]) // 2
cur_y = 0
if check_collision(cur_shape, cur_x, cur_y):
pygame.quit()
exit()
# 绘制游戏界面
screen.fill(WHITE)
for i in range(grid_height):
for j in range(grid_width):
if grid[i][j] != 0:
draw_block(j, i, colors[grid[i][j]])
draw_shape(cur_shape, cur_x, cur_y, cur_color)
pygame.display.set_caption("俄罗斯方块 - 得分: {}".format(score))
pygame.display.flip()
# 控制游戏速度
clock.tick(10)
```
这是一个非常简单的俄罗斯方块示例,只有基本的游戏逻辑和图形界面。你可以根据自己的需要进行修改和扩展。