python俄罗斯方块小游戏
时间: 2023-06-10 07:07:47 浏览: 79
下面是一个简单的 Python 俄罗斯方块小游戏的代码,你可以自己运行试一试:
```python
import random
import pygame
# 初始化 pygame
pygame.init()
# 游戏区域大小
WIDTH = 600
HEIGHT = 800
# 方块大小
BLOCK_SIZE = 30
# 方块颜色
COLORS = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(0, 255, 255),
(255, 0, 255),
(255, 255, 255)
]
# 游戏区域
game_screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 游戏区域的行和列
ROWS = HEIGHT // BLOCK_SIZE
COLS = WIDTH // BLOCK_SIZE
# 游戏区域的背景颜色
BG_COLOR = (0, 0, 0)
# 方块类
class Block:
def __init__(self, row, col, color):
self.row = row
self.col = col
self.color = color
def move(self, drow, dcol):
self.row += drow
self.col += dcol
def draw(self):
pygame.draw.rect(game_screen, self.color, (self.col * BLOCK_SIZE, self.row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 方块组类
class BlockGroup:
def __init__(self, blocks):
self.blocks = blocks
def move(self, drow, dcol):
for block in self.blocks:
block.move(drow, dcol)
def draw(self):
for block in self.blocks:
block.draw()
def get_row_col(self):
row_col_list = [(block.row, block.col) for block in self.blocks]
return row_col_list
def rotate(self):
pivot = self.blocks[0]
new_blocks = []
for block in self.blocks:
drow = block.row - pivot.row
dcol = block.col - pivot.col
new_row = pivot.row - dcol
new_col = pivot.col + drow
new_blocks.append(Block(new_row, new_col, pivot.color))
return BlockGroup(new_blocks)
# 方块组工厂类
class BlockGroupFactory:
def __init__(self):
self.block_groups = [
BlockGroup([Block(0, 3, COLORS[0]), Block(1, 3, COLORS[0]), Block(2, 3, COLORS[0]), Block(3, 3, COLORS[0])]),
BlockGroup([Block(0, 3, COLORS[1]), Block(1, 3, COLORS[1]), Block(1, 4, COLORS[1]), Block(2, 4, COLORS[1])]),
BlockGroup([Block(0, 4, COLORS[2]), Block(1, 4, COLORS[2]), Block(2, 4, COLORS[2]), Block(2, 3, COLORS[2])]),
BlockGroup([Block(0, 4, COLORS[3]), Block(1, 4, COLORS[3]), Block(1, 3, COLORS[3]), Block(2, 3, COLORS[3])]),
BlockGroup([Block(0, 4, COLORS[4]), Block(1, 4, COLORS[4]), Block(2, 4, COLORS[4]), Block(2, 5, COLORS[4])]),
BlockGroup([Block(0, 5, COLORS[5]), Block(1, 5, COLORS[5]), Block(2, 5, COLORS[5]), Block(2, 4, COLORS[5])]),
BlockGroup([Block(0, 3, COLORS[6]), Block(0, 4, COLORS[6]), Block(1, 4, COLORS[6]), Block(1, 5, COLORS[6])])
]
def create_block_group(self):
return random.choice(self.block_groups)
# 游戏类
class Game:
def __init__(self):
self.running = False
self.fps = 30
self.clock = pygame.time.Clock()
self.bg_color = BG_COLOR
self.block_factory = BlockGroupFactory()
self.current_block = None
self.next_block = None
self.game_board = []
self.score = 0
self.font = pygame.font.Font(None, 36)
def start(self):
self.running = True
self.init_game_board()
self.current_block = self.block_factory.create_block_group()
self.next_block = self.block_factory.create_block_group()
while self.running:
self.handle_events()
self.update()
self.draw()
self.clock.tick(self.fps)
def init_game_board(self):
for i in range(ROWS):
self.game_board.append([None] * COLS)
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move_current_block(-1, 0)
elif event.key == pygame.K_RIGHT:
self.move_current_block(1, 0)
elif event.key == pygame.K_DOWN:
self.move_current_block(0, 1)
elif event.key == pygame.K_SPACE:
self.rotate_current_block()
def update(self):
if not self.move_current_block(0, 1):
self.add_current_block_to_game_board()
self.remove_complete_rows()
self.current_block = self.next_block
self.next_block = self.block_factory.create_block_group()
if self.is_game_over():
self.running = False
def draw(self):
game_screen.fill(self.bg_color)
self.draw_game_board()
self.current_block.draw()
self.draw_next_block()
self.draw_score()
pygame.display.update()
def draw_game_board(self):
for i in range(ROWS):
for j in range(COLS):
if self.game_board[i][j] is not None:
pygame.draw.rect(game_screen, self.game_board[i][j], (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
def draw_next_block(self):
font = pygame.font.Font(None, 24)
text = font.render("Next Block:", True, (255, 255, 255))
game_screen.blit(text, (400, 50))
self.next_block.move(6, 2)
self.next_block.draw()
def draw_score(self):
text = self.font.render("Score: {}".format(self.score), True, (255, 255, 255))
game_screen.blit(text, (400, 200))
def move_current_block(self, drow, dcol):
new_block_group = BlockGroup([Block(block.row + drow, block.col + dcol, block.color) for block in self.current_block.blocks])
if self.is_valid_block_group(new_block_group):
self.current_block = new_block_group
return True
else:
return False
def rotate_current_block(self):
new_block_group = self.current_block.rotate()
if self.is_valid_block_group(new_block_group):
self.current_block = new_block_group
def is_valid_block_group(self, block_group):
for row, col in block_group.get_row_col():
if row < 0 or row >= ROWS or col < 0 or col >= COLS or self.game_board[row][col] is not None:
return False
return True
def add_current_block_to_game_board(self):
for block in self.current_block.blocks:
self.game_board[block.row][block.col] = block.color
def remove_complete_rows(self):
num_complete_rows = 0
for i in range(ROWS):
if None not in self.game_board[i]:
del self.game_board[i]
self.game_board.insert(0, [None] * COLS)
num_complete_rows += 1
self.score += num_complete_rows * 10
def is_game_over(self):
for block in self.current_block.blocks:
if self.game_board[block.row][block.col] is not None:
return True
return False
# 运行游戏
if __name__ == "__main__":
game = Game()
game.start()
# 退出 pygame
pygame.quit()
```
这个代码使用了 Pygame 模块来实现游戏界面和交互。在运行代码之前,需要先安装 Pygame 模块。可以使用以下命令安装:
```
pip install pygame
```
运行代码后,可以使用方向键左右移动方块组,向下键加速下落,空格键旋转方块组。游戏目标是在游戏区域底部堆积方块之前尽可能地清除完整的行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)