俄罗斯方块完整Python代码
时间: 2023-06-14 07:03:20 浏览: 92
以下是一个基于Python的俄罗斯方块游戏的完整代码:
```python
import pygame
import random
# 定义一些常量
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BLOCK_SIZE = 20
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
FPS = 30
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)
]
# 定义方块类
class Block:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
# 定义游戏类
class Tetris:
def __init__(self):
pygame.init()
pygame.display.set_caption("俄罗斯方块")
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
self.clock = pygame.time.Clock()
self.font = pygame.font.SysFont(None, 30)
self.board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)]
self.current_block = self.get_new_block()
self.next_block = self.get_new_block()
self.score = 0
self.game_over = False
# 获取一个新的方块
def get_new_block(self):
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]]
]
shape = random.choice(shapes)
x = BOARD_WIDTH // 2 - len(shape[0]) // 2
y = 0
return Block(x, y, shape)
# 判断方块是否与边界或已有方块重叠
def is_valid_position(self, block):
for i in range(len(block.shape)):
for j in range(len(block.shape[i])):
if block.shape[i][j] != 0:
x = block.x + j
y = block.y + i
if x < 0 or x >= BOARD_WIDTH or y >= BOARD_HEIGHT or self.board[y][x] != 0:
return False
return True
# 旋转方块
def rotate_block(self, block):
new_shape = []
for i in range(len(block.shape[0])):
new_row = []
for j in range(len(block.shape)):
new_row.append(block.shape[len(block.shape) - j - 1][i])
new_shape.append(new_row)
return Block(block.x, block.y, new_shape)
# 消除满行
def clear_full_rows(self):
rows_cleared = 0
for i in range(len(self.board)):
if all(self.board[i]):
del self.board[i]
self.board.insert(0, [0] * BOARD_WIDTH)
rows_cleared += 1
self.score += rows_cleared * 100
# 绘制游戏界面
def draw(self):
self.screen.fill(COLORS[0])
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if self.board[i][j] != 0:
pygame.draw.rect(self.screen, COLORS[self.board[i][j]], pygame.Rect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
for i in range(len(self.current_block.shape)):
for j in range(len(self.current_block.shape[i])):
if self.current_block.shape[i][j] != 0:
pygame.draw.rect(self.screen, COLORS[self.current_block.shape[i][j]], pygame.Rect((self.current_block.x + j) * BLOCK_SIZE, (self.current_block.y + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
next_block_text = self.font.render("NEXT BLOCK:", True, COLORS[7])
score_text = self.font.render("SCORE: {}".format(self.score), True, COLORS[7])
self.screen.blit(next_block_text, (SCREEN_WIDTH - 180, 50))
self.screen.blit(score_text, (SCREEN_WIDTH - 180, 150))
for i in range(len(self.next_block.shape)):
for j in range(len(self.next_block.shape[i])):
if self.next_block.shape[i][j] != 0:
pygame.draw.rect(self.screen, COLORS[self.next_block.shape[i][j]], pygame.Rect((SCREEN_WIDTH - 150) + j * BLOCK_SIZE, 200 + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
pygame.display.update()
# 运行游戏
def run(self):
while not self.game_over:
self.clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.current_block.x -= 1
if not self.is_valid_position(self.current_block):
self.current_block.x += 1
elif event.key == pygame.K_RIGHT:
self.current_block.x += 1
if not self.is_valid_position(self.current_block):
self.current_block.x -= 1
elif event.key == pygame.K_UP:
rotated_block = self.rotate_block(self.current_block)
if self.is_valid_position(rotated_block):
self.current_block = rotated_block
elif event.key == pygame.K_DOWN:
self.current_block.y += 1
if not self.is_valid_position(self.current_block):
self.current_block.y -= 1
if not self.is_valid_position(self.current_block):
for i in range(len(self.current_block.shape)):
for j in range(len(self.current_block.shape[i])):
if self.current_block.shape[i][j] != 0:
x = self.current_block.x + j
y = self.current_block.y + i
self.board[y][x] = self.current_block.shape[i][j]
self.clear_full_rows()
self.current_block = self.next_block
self.next_block = self.get_new_block()
if not self.is_valid_position(self.current_block):
self.game_over = True
self.current_block.y += 1
self.draw()
game_over_text = self.font.render("GAME OVER", True, COLORS[1])
self.screen.blit(game_over_text, (SCREEN_WIDTH // 2 - 75, SCREEN_HEIGHT // 2))
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()
# 启动游戏
if __name__ == "__main__":
game = Tetris()
game.run()
```
该代码使用了 Pygame 库来实现游戏界面和交互,核心逻辑包括:
- Block 类:用来表示方块的位置和形状。
- Tetris 类:用来表示整个游戏,包括界面绘制、方块的移动和旋转、消除满行等逻辑。
- 一些常量:包括窗口大小、方块大小、游戏区域大小、颜色等。
- 主程序:创建 Tetris 对象并启动游戏。
该代码实现的俄罗斯方块游戏具有基本的功能,但仍有许多改进的空间,比如可以增加更多种类的方块、音效、计时器等。
阅读全文