写一个俄罗斯方块的python代码
时间: 2023-04-08 18:05:06 浏览: 63
好的,我可以回答这个问题。以下是一个简单的俄罗斯方块的Python代码:
```python
import pygame
import random
# 游戏界面的大小
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 600
# 方块的大小
BLOCK_SIZE = 20
# 方块的颜色
COLORS = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 255, 255)]
# 俄罗斯方块的形状
SHAPES = [
[[1, 1, 1],
[0, 1, 0]],
[[0, 2, 2],
[2, 2, 0]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 0, 0],
[4, 4, 4]],
[[0, 0, 5],
[5, 5, 5]],
[[6, 6],
[6, 6]],
[[7, 7, 7, 7]]
]
# 初始化pygame
pygame.init()
# 创建游戏界面
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 设置游戏标题
pygame.display.set_caption("俄罗斯方块")
# 创建字体对象
font = pygame.font.SysFont(None, 36)
# 创建方块类
class Block:
def __init__(self, shape, color):
self.shape = shape
self.color = color
self.x = 4
self.y = 0
# 旋转方块
def rotate(self):
self.shape = [[self.shape[j][i] for j in range(len(self.shape))] for i in range(len(self.shape[0])-1, -1, -1)]
# 判断方块是否可以移动
def can_move(self, dx, dy, board):
for i in range(len(self.shape)):
for j in range(len(self.shape[0])):
if self.shape[i][j] != 0:
x = self.x + j + dx
y = self.y + i + dy
if x < 0 or x >= 10 or y < 0 or y >= 20 or board[y][x] != 0:
return False
return True
# 移动方块
def move(self, dx, dy, board):
if self.can_move(dx, dy, board):
self.x += dx
self.y += dy
return True
else:
return False
# 绘制方块
def draw(self, screen):
for i in range(len(self.shape)):
for j in range(len(self.shape[0])):
if self.shape[i][j] != 0:
pygame.draw.rect(screen, COLORS[self.color], (self.x*BLOCK_SIZE+j*BLOCK_SIZE, self.y*BLOCK_SIZE+i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 创建游戏类
class Game:
def __init__(self):
self.board = [[0 for x in range(10)] for y in range(20)]
self.block = Block(random.choice(SHAPES), random.randint(1, 7))
self.score = 0
# 判断是否可以消除一行
def can_clear(self, y):
for x in range(10):
if self.board[y][x] == 0:
return False
return True
# 消除一行
def clear(self, y):
for i in range(y, 0, -1):
for j in range(10):
self.board[i][j] = self.board[i-1][j]
# 绘制游戏界面
def draw(self, screen):
screen.fill(COLORS[0])
for i in range(20):
for j in range(10):
if self.board[i][j] != 0:
pygame.draw.rect(screen, COLORS[self.board[i][j]], (j*BLOCK_SIZE, i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
self.block.draw(screen)
score_text = font.render("Score: "+str(self.score), True, COLORS[7])
screen.blit(score_text, (10, 10))
# 运行游戏
def run(self):
clock = pygame.time.Clock()
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.block.move(-1, 0, self.board)
elif event.key == pygame.K_RIGHT:
self.block.move(1, 0, self.board)
elif event.key == pygame.K_DOWN:
self.block.move(0, 1, self.board)
elif event.key == pygame.K_UP:
self.block.rotate()
# 移动方块
if not self.block.move(0, 1, self.board):
for i in range(len(self.block.shape)):
for j in range(len(self.block.shape[0])):
if self.block.shape[i][j] != 0:
x = self.block.x + j
y = self.block.y + i
self.board[y][x] = self.block.color
for i in range(20):
if self.can_clear(i):
self.clear(i)
self.score += 10
self.block = Block(random.choice(SHAPES), random.randint(1, 7))
if not self.block.can_move(0, 0, self.board):
pygame.quit()
exit()
# 绘制游戏界面
self.draw(screen)
# 刷新屏幕
pygame.display.update()
# 控制游戏帧率
clock.tick(10)
# 创建游戏对象并运行游戏
game = Game()
game.run()
```
希望这个代码对你有所帮助!
阅读全文