Python俄罗斯方块游戏的代码
时间: 2023-05-29 16:04:59 浏览: 45
以下是一个基本的Python俄罗斯方块游戏的代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
window_width = 300
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
# 设置游戏标题
pygame.display.set_caption("俄罗斯方块")
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 定义方块的形状和颜色
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 = [
white,
red,
green,
blue,
(255, 128, 0),
(255, 0, 255),
(128, 128, 128)
]
# 定义方块类
class Block():
def __init__(self, shape, color, x, y):
self.shape = shape
self.color = color
self.x = x
self.y = y
def rotate(self):
self.shape = [[self.shape[j][i] for j in range(len(self.shape))] for i in range(len(self.shape[0])-1, -1, -1)]
def move_down(self):
self.y += 1
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def draw(self, surface):
for i in range(len(self.shape)):
for j in range(len(self.shape[0])):
if self.shape[i][j] != 0:
pygame.draw.rect(surface, self.color, (self.x+j*20, self.y+i*20, 20, 20))
# 定义游戏主循环
def main():
# 定义变量
score = 0
fps = 30
clock = pygame.time.Clock()
game_over = False
blocks = []
current_block = Block(random.choice(shapes), random.choice(colors), 130, 0)
next_block = Block(random.choice(shapes), random.choice(colors), 350, 50)
# 开始游戏循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if 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:
current_block.move_down()
# 移动当前方块
current_block.move_down()
# 判断当前方块是否与其他方块重叠
overlap = False
for block in blocks:
if current_block.y + len(current_block.shape) * 20 >= block.y and current_block.x < block.x + len(block.shape[0]) * 20 and current_block.x + len(current_block.shape[0]) * 20 > block.x:
overlap = True
break
# 如果当前方块与其他方块重叠,将当前方块加入方块列表,并生成新的方块
if overlap:
blocks.append(current_block)
current_block = next_block
next_block = Block(random.choice(shapes), random.choice(colors), 350, 50)
# 绘制背景和分数
window.fill(black)
font = pygame.font.SysFont(None, 30)
text = font.render("Score: "+str(score), True, white)
window.blit(text, (10, 10))
# 绘制当前方块和方块列表中的方块
current_block.draw(window)
for block in blocks:
block.draw(window)
# 更新屏幕
pygame.display.update()
# 判断方块是否到达底部
if current_block.y + len(current_block.shape) * 20 >= window_height:
blocks.append(current_block)
current_block = next_block
next_block = Block(random.choice(shapes), random.choice(colors), 350, 50)
# 判断是否有一行方块被填满
for i in range(window_height//20):
row_blocks = []
for block in blocks:
if block.y == i*20:
row_blocks.append(block)
if len(row_blocks) == window_width//20:
for block in row_blocks:
blocks.remove(block)
for block in blocks:
if block.y < i*20:
block.move_down()
score += 10
# 控制游戏帧率
clock.tick(fps)
# 退出游戏
pygame.quit()
if __name__ == "__main__":
main()
```
这个游戏有以下功能:
- 方向键左右移动方块
- 方向键上旋转方块
- 方向键下加速方块下落
- 当一行方块被填满时得分
- 当方块堆满整个窗口时游戏结束