用Python写一个俄罗斯方块
时间: 2023-06-06 13:06:20 浏览: 212
python实现俄罗斯方块
好的,我可以帮助您用Python写一个俄罗斯方块。以下是示例代码:
```python
import pygame
import random
# 定义常量
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 640
BLOCK_SIZE = 20
BLOCK_COLORS = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0), (0, 255, 255), (255, 0, 255), (255, 255, 255)]
# 初始化 pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
# 定义方块类
class Block():
def __init__(self):
self.x = SCREEN_WIDTH // 2
self.y = 0
self.color = random.choice(BLOCK_COLORS)
self.shape = random.choice(["I", "O", "T", "S", "Z", "J", "L"])
if self.shape == "I":
self.cells = [(self.x, self.y), (self.x, self.y + BLOCK_SIZE), (self.x, self.y + BLOCK_SIZE * 2), (self.x, self.y + BLOCK_SIZE * 3)]
elif self.shape == "O":
self.cells = [(self.x, self.y), (self.x + BLOCK_SIZE, self.y), (self.x, self.y + BLOCK_SIZE), (self.x + BLOCK_SIZE, self.y + BLOCK_SIZE)]
elif self.shape == "T":
self.cells = [(self.x, self.y), (self.x - BLOCK_SIZE, self.y + BLOCK_SIZE), (self.x, self.y + BLOCK_SIZE), (self.x + BLOCK_SIZE, self.y + BLOCK_SIZE)]
elif self.shape == "S":
self.cells = [(self.x + BLOCK_SIZE, self.y), (self.x, self.y), (self.x, self.y + BLOCK_SIZE), (self.x - BLOCK_SIZE, self.y + BLOCK_SIZE)]
elif self.shape == "Z":
self.cells = [(self.x - BLOCK_SIZE, self.y), (self.x, self.y), (self.x, self.y + BLOCK_SIZE), (self.x + BLOCK_SIZE, self.y + BLOCK_SIZE)]
elif self.shape == "J":
self.cells = [(self.x - BLOCK_SIZE, self.y), (self.x - BLOCK_SIZE, self.y + BLOCK_SIZE), (self.x, self.y + BLOCK_SIZE), (self.x + BLOCK_SIZE, self.y + BLOCK_SIZE)]
else:
self.cells = [(self.x + BLOCK_SIZE, self.y), (self.x + BLOCK_SIZE, self.y + BLOCK_SIZE), (self.x, self.y + BLOCK_SIZE), (self.x - BLOCK_SIZE, self.y + BLOCK_SIZE)]
def move(self, dx, dy):
self.x += dx
self.y += dy
for i in range(len(self.cells)):
self.cells[i] = (self.cells[i][0] + dx, self.cells[i][1] + dy)
def rotate(self):
if self.shape == "O":
return
center = self.cells[1]
for i in range(len(self.cells)):
x, y = self.cells[i]
x -= center[0]
y -= center[1]
x, y = -y, x
x += center[0]
y += center[1]
self.cells[i] = (x, y)
def draw(self):
for cell in self.cells:
pygame.draw.rect(screen, self.color, (cell[0], cell[1], BLOCK_SIZE, BLOCK_SIZE))
def check_collisions(self):
for cell in self.cells:
x, y = cell
if x < 0 or x >= SCREEN_WIDTH or y >= SCREEN_HEIGHT:
return True
return False
def check_overlap(self, other_cells):
for cell in self.cells:
if cell in other_cells:
return True
return False
def fix(self, fixed_cells):
for cell in self.cells:
fixed_cells.append(cell)
# 在游戏循环中,调用方块类的方法
def game_loop():
clock = pygame.time.Clock()
game_over = False
fixed_cells = []
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
block.move(-BLOCK_SIZE, 0)
if block.check_collisions() or block.check_overlap(fixed_cells):
block.move(BLOCK_SIZE, 0)
elif event.key == pygame.K_RIGHT:
block.move(BLOCK_SIZE, 0)
if block.check_collisions() or block.check_overlap(fixed_cells):
block.move(-BLOCK_SIZE, 0)
elif event.key == pygame.K_DOWN:
block.move(0, BLOCK_SIZE)
if block.check_collisions() or block.check_overlap(fixed_cells):
block.move(0, -BLOCK_SIZE)
block.fix(fixed_cells)
block = Block()
elif event.key == pygame.K_UP:
block.rotate()
# 绘制背景
screen.fill((0, 0, 0))
# 绘制固定方块
for cell in fixed_cells:
pygame.draw.rect(screen, (128, 128, 128), (cell[0], cell[1], BLOCK_SIZE, BLOCK_SIZE))
# 绘制当前方块
block.draw()
# 刷新屏幕
pygame.display.flip()
# 控制帧数
clock.tick(10)
# 启动游戏
block = Block()
game_loop()
# 退出游戏
pygame.quit()
```
希望代码可以帮助到您!
阅读全文