用python代码实现俄罗斯方块
时间: 2023-11-19 13:56:00 浏览: 87
以下是一个简单的 Python 代码实现俄罗斯方块游戏的示例。该代码使用了 pygame 模块,因此需要安装pygame。
```python
import pygame
import random
pygame.init()
# 游戏窗口大小
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 600
# 方块大小
BLOCK_SIZE = 20
# 游戏区域大小
GAME_WIDTH = 10
GAME_HEIGHT = 20
# 方块形状
SHAPES = [
[[1, 1],
[1, 1]],
[[0, 1, 0],
[1, 1, 1]],
[[1, 1, 0],
[0, 1, 1]],
[[0, 1, 1],
[1, 1, 0]],
[[1, 0, 0],
[1, 1, 1]],
[[0, 0, 1],
[1, 1, 1]],
[[1, 1, 1, 1]]
]
# 方块颜色
COLORS = [
(255, 255, 255),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255)
]
# 方块类
class Block:
def __init__(self):
self.shape = random.choice(SHAPES)
self.color = random.choice(COLORS)
self.x = GAME_WIDTH // 2 - len(self.shape[0]) // 2
self.y = 0
# 方块移动
def move(self, dx, dy):
self.x += dx
self.y += dy
# 获取方块的坐标列表
def get_coords(self):
coords = []
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] == 1:
x = self.x + j
y = self.y + i
coords.append((x, y))
return coords
# 旋转方块
def rotate(self):
self.shape = [list(row)[::-1] for row in zip(*self.shape)]
if self.x + len(self.shape[0]) > GAME_WIDTH:
self.x = GAME_WIDTH - len(self.shape[0])
if self.y + len(self.shape) > GAME_HEIGHT:
self.y = GAME_HEIGHT - len(self.shape)
# 判断方块是否碰到边界或其他方块
def is_valid(self, dx, dy, game_board):
for x, y in self.get_coords():
if x + dx < 0 or x + dx >= GAME_WIDTH:
return False
if y + dy < 0 or y + dy >= GAME_HEIGHT:
return False
if game_board[y + dy][x + dx] != (0, 0, 0):
return False
return True
# 绘制方块
def draw(self, surface):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] == 1:
x = self.x + j
y = self.y + i
pygame.draw.rect(surface, self.color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 游戏主循环
def main():
# 创建窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Tetris')
# 创建游戏板
game_board = [[(0, 0, 0) for x in range(GAME_WIDTH)] for y in range(GAME_HEIGHT)]
# 创建方块
block = Block()
# 游戏是否结束
game_over = False
# 游戏分数
score = 0
clock = pygame.time.Clock()
# 主循环
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 and block.is_valid(-1, 0, game_board):
block.move(-1, 0)
elif event.key == pygame.K_RIGHT and block.is_valid(1, 0, game_board):
block.move(1, 0)
elif event.key == pygame.K_UP:
block.rotate()
if not block.is_valid(0, 0, game_board):
block.rotate()
block.rotate()
block.rotate()
elif event.key == pygame.K_DOWN:
if block.is_valid(0, 1, game_board):
block.move(0, 1)
# 移动方块
if block.is_valid(0, 1, game_board):
block.move(0, 1)
else:
# 方块不能再移动,放到游戏板上
for x, y in block.get_coords():
game_board[y][x] = block.color
# 消除满行
for y in range(GAME_HEIGHT):
if all(game_board[y][x] != (0, 0, 0) for x in range(GAME_WIDTH)):
game_board.pop(y)
game_board.insert(0, [(0, 0, 0) for x in range(GAME_WIDTH)])
score += 1
# 创建新的方块
block = Block()
# 判断游戏是否结束
if not block.is_valid(0, 0, game_board):
game_over = True
# 绘制游戏界面
screen.fill((0, 0, 0))
for x in range(GAME_WIDTH):
for y in range(GAME_HEIGHT):
pygame.draw.rect(screen, game_board[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
block.draw(screen)
pygame.display.update()
# 控制游戏速度
clock.tick(10)
# 游戏结束
font = pygame.font.Font(None, 36)
text = font.render('Game Over', True, (255, 255, 255))
screen.blit(text, (WINDOW_WIDTH // 2 - text.get_width() // 2, WINDOW_HEIGHT // 2 - text.get_height() // 2))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
```
运行该代码即可开始玩俄罗斯方块游戏。
阅读全文