好的,我可以为您提供一个简单的俄罗斯方块小游戏的示例代码。这个游戏使用 Python 的 Pygame 库实现,需要先安装 Pygame 库。
```python
import pygame
import random
# 初始化 Pygame 库
pygame.init()
# 定义游戏区域的大小和方格的大小
screen_width = 300
screen_height = 600
block_size = 30
# 定义颜色常量
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Tetris")
# 定义方块的形状和颜色
shapes = [
[[1, 1, 1], [0, 1, 0]],
[[2, 2], [2, 2]],
[[3, 3, 0], [0, 3, 3]],
[[0, 4, 4], [4, 4, 0]],
[[5, 5, 5, 5]],
[[6, 6, 6], [0, 0, 6]],
[[7, 7, 7], [7, 0, 0]]
]
colors = [red, green, blue, white, black, blue, green]
# 定义方块类
class Block:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = colors[shapes.index(shape)]
def draw(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
pygame.draw.rect(screen, self.color, (self.x + j * block_size, self.y + i * block_size, block_size, block_size))
def move_down(self):
self.y += block_size