python小游戏俄罗斯方块有积分和关卡代码
时间: 2023-08-01 10:07:07 浏览: 116
以下是一个简单的俄罗斯方块小游戏的代码,包含积分和关卡功能:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 游戏区域的大小
width = 300
height = 600
# 方块的大小
block_size = 30
# 游戏区域的行数和列数
rows = int(height / block_size)
cols = int(width / block_size)
# 创建游戏区域的矩阵
board = [[0 for _ in range(cols)] for _ in range(rows)]
# 定义各种方块的形状
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]]
]
# 方块的颜色
colors = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]
# 游戏区域显示的位置
board_x = int((width - cols * block_size) / 2)
board_y = int((height - rows * block_size) / 2)
# 创建游戏窗口
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("俄罗斯方块")
# 初始化游戏得分和当前关卡
score = 0
level = 1
# 定义字体
font = pygame.font.Font(None, 36)
# 定义每个方块的类
class Block:
def __init__(self):
self.shape = random.choice(shapes)
self.color = random.choice(colors)
self.row = 0
self.col = int(cols / 2) - 1
# 将方块加入游戏区域
def add_to_board(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
board[self.row + i][self.col + j] = self.color
# 从游戏区域中删除方块
def remove_from_board(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
board[self.row + i][self.col + j] = 0
# 判断方块是否可以向下移动
def can_move_down(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
if self.row + i == rows - 1 or board[self.row + i + 1][self.col + j] != 0:
return False
return True
# 将方块向下移动一格
def move_down(self):
if self.can_move_down():
self.remove_from_board()
self.row += 1
self.add_to_board()
return True
else:
return False
# 判断方块是否可以向左移动
def can_move_left(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
if self.col + j == 0 or board[self.row + i][self.col + j - 1] != 0:
return False
return True
# 将方块向左移动一格
def move_left(self):
if self.can_move_left():
self.remove_from_board()
self.col -= 1
self.add_to_board()
# 判断方块是否可以向右移动
def can_move_right(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
if self.col + j == cols - 1 or board[self.row + i][self.col + j + 1] != 0:
return False
return True
# 将方块向右移动一格
def move_right(self):
if self.can_move_right():
self.remove_from_board()
self.col += 1
self.add_to_board()
# 判断方块是否可以旋转
def can_rotate(self):
new_shape = [[0 for _ in range(len(self.shape))] for _ in range(len(self.shape[0]))]
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
new_shape[j][len(self.shape) - 1 - i] = self.shape[i][j]
for i in range(len(new_shape)):
for j in range(len(new_shape[i])):
if new_shape[i][j] != 0:
if self.row + i < 0 or self.row + i >= rows or self.col + j < 0 or self.col + j >= cols or board[self.row + i][self.col + j] != 0:
return False
return True
# 将方块旋转
def rotate(self):
if self.can_rotate():
self.remove_from_board()
new_shape = [[0 for _ in range(len(self.shape))] for _ in range(len(self.shape[0]))]
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
new_shape[j][len(self.shape) - 1 - i] = self.shape[i][j]
self.shape = new_shape
self.add_to_board()
# 创建一个方块对象
current_block = Block()
# 定义计时器
timer = pygame.time.Clock()
fall_time = 1000
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_block.move_left()
elif event.key == pygame.K_RIGHT:
current_block.move_right()
elif event.key == pygame.K_UP:
current_block.rotate()
elif event.key == pygame.K_DOWN:
fall_time = 50
# 重新设置计时器的频率
timer.tick(60)
# 计算得分和关卡
lines_cleared = 0
for i in range(rows):
if 0 not in board[i]:
lines_cleared += 1
board.pop(i)
board.insert(0, [0 for _ in range(cols)])
if lines_cleared == 1:
score += 100
elif lines_cleared == 2:
score += 300
elif lines_cleared == 3:
score += 500
elif lines_cleared == 4:
score += 800
if score >= level * 1000:
level += 1
fall_time -= 100
# 让方块自动向下移动
if pygame.time.get_ticks() - fall_time > current_block.row * 50:
if not current_block.move_down():
current_block.add_to_board()
current_block = Block()
fall_time = 1000
# 绘制游戏界面
screen.fill((255, 255, 255))
for i in range(rows):
for j in range(cols):
pygame.draw.rect(screen, colors[board[i][j]], (board_x + j * block_size, board_y + i * block_size, block_size, block_size), 0)
pygame.draw.rect(screen, (0, 0, 0), (board_x, board_y, cols * block_size, rows * block_size), 1)
score_text = font.render("Score: " + str(score), True, (0, 0, 0))
level_text = font.render("Level: " + str(level), True, (0, 0, 0))
screen.blit(score_text, (10, 10))
screen.blit(level_text, (10, 50))
pygame.display.flip()
# 退出游戏
pygame.quit()
```
在上面的代码中,每清除一行方块就可以获得一定的得分,不同数量的行清除可以获得不同的分数。当得分达到一定值时,游戏进入下一关,方块下落的速度也会加快。
阅读全文