俄罗斯方块游戏python代码
时间: 2023-07-23 17:18:29 浏览: 71
好的,以下是一个用 Python 和 Pygame 实现的俄罗斯方块游戏的代码示例。该代码实现了基本的游戏逻辑和图形界面。您可以根据自己的需要进行修改和优化。
```python
import pygame
import random
# 游戏区域宽度和高度
WIDTH = 300
HEIGHT = 600
# 方块大小
BLOCK_SIZE = 30
# 方块种类和颜色
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]]
]
COLORS = [(0, 0, 0),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255),
(128, 128, 128)]
# 初始化 Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tetris')
# 绘制方块
def draw_block(x, y, color):
pygame.draw.rect(screen,
COLORS[color],
[BLOCK_SIZE * x, BLOCK_SIZE * y, BLOCK_SIZE, BLOCK_SIZE])
# 绘制游戏区域
def draw_board(board):
for y, row in enumerate(board):
for x, color in enumerate(row):
if color:
draw_block(x, y, color)
# 创建新的方块
def new_block():
shape = random.choice(SHAPES)
block = {'shape': shape,
'x': int(WIDTH / BLOCK_SIZE / 2) - int(len(shape[0]) / 2),
'y': 0,
'color': random.randint(1, len(COLORS) - 1)}
return block
# 检查方块是否碰撞
def check_collision(board, block, offset):
off_x, off_y = offset
for y, row in enumerate(block['shape']):
for x, val in enumerate(row):
if val and board[y + block['y'] + off_y][x + block['x'] + off_x]:
return True
return False
# 将方块放到游戏区域中
def place_block(board, block):
for y, row in enumerate(block['shape']):
for x, val in enumerate(row):
if val:
board[y + block['y']][x + block['x']] = block['color']
# 移除已满的行
def remove_rows(board):
num_removed = 0
y = len(board) - 1
while y >= 0:
if all(board[y]):
del board[y]
num_removed += 1
else:
y -= 1
for i in range(num_removed):
board.insert(0, [0] * (WIDTH // BLOCK_SIZE))
return num_removed
# 游戏主循环
def main():
# 初始化游戏区域
board = [[0] * (WIDTH // BLOCK_SIZE) for _ in range(HEIGHT // BLOCK_SIZE)]
# 初始化分数和速度
score = 0
speed = 1
# 创建第一个方块
block = new_block()
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
# 处理方块移动和旋转
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if not check_collision(board, block, (-1, 0)):
block['x'] -= 1
if keys[pygame.K_RIGHT]:
if not check_collision(board, block, (1, 0)):
block['x'] += 1
if keys[pygame.K_DOWN]:
if not check_collision(board, block, (0, 1)):
block['y'] += 1
if keys[pygame.K_UP]:
old_shape = block['shape']
block['shape'] = [list(reversed(x)) for x in zip(*block['shape'])]
if check_collision(board, block, (0, 0)):
block['shape'] = old_shape
if keys[pygame.K_SPACE]:
while not check_collision(board, block, (0, 1)):
block['y'] += 1
# 处理方块下落
if check_collision(board, block, (0, 1)):
place_block(board, block)
num_rows = remove_rows(board)
score += 10 * num_rows
speed = 1 + score // 1000
block = new_block()
if check_collision(board, block, (0, 0)):
pygame.quit()
return
# 绘制游戏界面
screen.fill(COLORS[0])
draw_board(board)
for y, row in enumerate(block['shape']):
for x, val in enumerate(row):
if val:
draw_block(block['x'] + x, block['y'] + y, block['color'])
pygame.display.update()
# 控制游戏速度
pygame.time.wait(1000 // speed)
if __name__ == '__main__':
main()
```
希望这段代码能够对您有所帮助!
阅读全文