python实现俄罗斯方块
时间: 2023-05-22 20:04:30 浏览: 67
可以使用 Pygame 库来实现俄罗斯方块,以下是示例代码:
```python
import pygame
import random
# 定义方块大小
BLOCK_SIZE = 30
# 定义方块类型及其对应颜色
SHAPE_TYPES = {
"I": (cyan, [(0, -1), (0, 0), (0, 1), (0, 2)]),
"J": (blue, [(0, -1), (0, 0), (0, 1), (-1, 1)]),
"L": (orange, [(0, -1), (0, 0), (0, 1), (1, 1)]),
"O": (yellow, [(0, 0), (0, 1), (1, 0), (1, 1)]),
"S": (green, [(0, 0), (0, 1), (-1, 0), (1, 1)]),
"T": (purple, [(0, -1), (0, 0), (0, 1), (1, 0)]),
"Z": (red, [(0, 0), (0, 1), (1, 0), (-1, 1)])
}
# 定义方块类
class Block:
def __init__(self, shape, x, y):
self.shape = shape
self.color, self.coords = SHAPE_TYPES[shape]
self.x = x
self.y = y
def move(self, dx, dy):
self.x += dx
self.y += dy
def rotate(self):
self.coords = [(-y, x) for x, y in self.coords]
# 判断当前方块是否合法
def is_valid(self, board):
for x, y in self.coords:
if (self.x + x < 0 or self.x + x >= len(board[0])
or self.y + y < 0 or self.y + y >= len(board)):
return False
if board[self.y + y][self.x + x] != " ":
return False
return True
# 将当前方块放入板子中
def addToBoard(self, board):
for x, y in self.coords:
board[self.y + y][self.x + x] = self.color
# 定义游戏类
class Tetris:
def __init__(self, width, height):
self.width = width
self.height = height
self.screen = pygame.display.set_mode((width * BLOCK_SIZE, height * BLOCK_SIZE))
self.clock = pygame.time.Clock()
self.board = [[" " for _ in range(width)] for _ in range(height)]
self.current_block = None
self.next_block = None
self.score = 0
# 生成新方块
def newBlock(self):
self.current_block = self.next_block or Block(random.choice(list(SHAPE_TYPES.keys())), len(self.board[0]) // 2 - 2, 0)
self.next_block = Block(random.choice(list(SHAPE_TYPES.keys())), len(self.board[0]) // 2 - 2, 0)
# 清除已满行
def clearFullRows(self):
rows = [i for i, row in enumerate(self.board) if " " not in row]
for row in sorted(rows, reverse=True):
self.board.pop(row)
self.board.insert(0, [" " for _ in range(len(self.board[0]))])
self.score += len(rows) ** 2
# 更新游戏状态
def update(self):
# 方块已经落下,将其加入到板子中
if not self.current_block.is_valid(self.board):
self.current_block.move(0, -1)
self.current_block.addToBoard(self.board)
self.clearFullRows()
self.newBlock()
# 画板子和方块
self.screen.fill((0, 0, 0))
for y, row in enumerate(self.board):
for x, color in enumerate(row):
pygame.draw.rect(self.screen, color, (x * BLOCK_SIZE, (self.height - y - 1) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
if self.current_block:
for x, y in self.current_block.coords:
pygame.draw.rect(self.screen, self.current_block.color, ((self.current_block.x + x) * BLOCK_SIZE, (self.height - self.current_block.y - y - 1) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 更新屏幕
pygame.display.flip()
# 运行游戏
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.current_block.move(-1, 0)
if not self.current_block.is_valid(self.board):
self.current_block.move(1, 0)
elif event.key == pygame.K_RIGHT:
self.current_block.move(1, 0)
if not self.current_block.is_valid(self.board):
self.current_block.move(-1, 0)
elif event.key == pygame.K_DOWN:
self.current_block.move(0, -1)
if not self.current_block.is_valid(self.board):
self.current_block.move(0, 1)
elif event.key == pygame.K_UP:
self.current_block.rotate()
self.clock.tick(10)
self.current_block.move(0, -1)
self.update()
if __name__ == "__main__":
pygame.init()
pygame.display.set_caption("Tetris")
tetris = Tetris(10, 20)
tetris.newBlock()
tetris.run()
```
请注意,此处使用了 Pygame 库,如果您没有安装该库需要先进行安装。