pygame写一个俄罗斯方块的代码
时间: 2023-04-06 21:01:39 浏览: 108
你好,以下是俄罗斯方块的代码:
```
import pygame
import random
# 初始化pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 设置屏幕大小
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 500
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置字体
font = pygame.font.SysFont('arial', 25)
# 定义方块大小
BLOCK_SIZE = 20
# 定义方块类型
SHAPE_TYPES = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']
SHAPE_COLORS = {
'I': BLUE,
'J': YELLOW,
'L': GREEN,
'O': RED,
'S': GRAY,
'T': WHITE,
'Z': BLACK
}
SHAPE_COORDS = {
'I': [(0, -1), (0, 0), (0, 1), (0, 2)],
'J': [(0, -1), (0, 0), (0, 1), (-1, 1)],
'L': [(0, -1), (0, 0), (0, 1), (1, 1)],
'O': [(0, 0), (0, 1), (1, 0), (1, 1)],
'S': [(0, 0), (0, 1), (1, -1), (1, 0)],
'T': [(0, -1), (0, 0), (0, 1), (1, 0)],
'Z': [(0, -1), (0, 0), (-1, 0), (1, -1)]
}
# 定义方块类
class Block:
def __init__(self, shape, x, y):
self.shape = shape
self.color = SHAPE_COLORS[shape]
self.coords = SHAPE_COORDS[shape]
self.x = x
self.y = y
def rotate(self):
new_coords = []
for coord in self.coords:
new_coords.append((-coord[1], coord[0]))
self.coords = new_coords
def move_down(self):
self.y += 1
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def draw(self):
for coord in self.coords:
x = self.x + coord[1]
y = self.y + coord[0]
pygame.draw.rect(screen, self.color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 定义游戏类
class Game:
def __init__(self):
self.score = 0
self.level = 1
self.blocks = []
self.current_block = None
self.next_block = None
self.game_over = False
def new_block(self):
shape = random.choice(SHAPE_TYPES)
x = SCREEN_WIDTH // BLOCK_SIZE // 2
y = 0
self.current_block = Block(shape, x, y)
shape = random.choice(SHAPE_TYPES)
self.next_block = Block(shape, SCREEN_WIDTH // BLOCK_SIZE + 3, 3)
def move_current_block_down(self):
if self.current_block_can_move_down():
self.current_block.move_down()
else:
self.add_current_block_to_blocks()
self.check_for_completed_rows()
self.new_block()
if not self.current_block_can_move_down():
self.game_over = True
def current_block_can_move_down(self):
for coord in self.current_block.coords:
x = self.current_block.x + coord[1]
y = self.current_block.y + coord[0] + 1
if y >= SCREEN_HEIGHT // BLOCK_SIZE or (x, y) in [(block.x, block.y) for block in self.blocks]:
return False
return True
def move_current_block_left(self):
if self.current_block_can_move_left():
self.current_block.move_left()
def current_block_can_move_left(self):
for coord in self.current_block.coords:
x = self.current_block.x + coord[1] - 1
y = self.current_block.y + coord[0]
if x < 0 or (x, y) in [(block.x, block.y) for block in self.blocks]:
return False
return True
def move_current_block_right(self):
if self.current_block_can_move_right():
self.current_block.move_right()
def current_block_can_move_right(self):
for coord in self.current_block.coords:
x = self.current_block.x + coord[1] + 1
y = self.current_block.y + coord[0]
if x >= SCREEN_WIDTH // BLOCK_SIZE or (x, y) in [(block.x, block.y) for block in self.blocks]:
return False
return True
def rotate_current_block(self):
self.current_block.rotate()
if not self.current_block_can_rotate():
self.current_block.rotate()
self.current_block.rotate()
self.current_block.rotate()
def current_block_can_rotate(self):
for coord in self.current_block.coords:
x = self.current_block.x + coord[1]
y = self.current_block.y + coord[0]
if x < 0 or x >= SCREEN_WIDTH // BLOCK_SIZE or y >= SCREEN_HEIGHT // BLOCK_SIZE or (x, y) in [(block.x, block.y) for block in self.blocks]:
return False
return True
def add_current_block_to_blocks(self):
for coord in self.current_block.coords:
x = self.current_block.x + coord[1]
y = self.current_block.y + coord[0]
self.blocks.append(Block(self.current_block.shape, x, y))
def check_for_completed_rows(self):
rows = {}
for block in self.blocks:
if block.y not in rows:
rows[block.y] = []
rows[block.y].append(block)
for row, blocks in rows.items():
if len(blocks) == SCREEN_WIDTH // BLOCK_SIZE:
self.score += 10
self.blocks = [block for block in self.blocks if block not in blocks]
for block in self.blocks:
if block.y < row:
block.move_down()
def draw(self):
screen.fill(BLACK)
for block in self.blocks:
block.draw()
if self.current_block:
self.current_block.draw()
if self.next_block:
next_block_text = font.render('Next Block:', True, WHITE)
screen.blit(next_block_text, (SCREEN_WIDTH // BLOCK_SIZE + 2, 0))
for coord in self.next_block.coords:
x = self.next_block.x + coord[1]
y = self.next_block.y + coord[0]
pygame.draw.rect(screen, self.next_block.color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
score_text = font.render('Score: ' + str(self.score), True, WHITE)
screen.blit(score_text, (0, SCREEN_HEIGHT - 25))
level_text = font.render('Level: ' + str(self.level), True, WHITE)
screen.blit(level_text, (SCREEN_WIDTH - 100, SCREEN_HEIGHT - 25))
# 创建游戏对象
game = Game()
# 开始游戏循环
clock = pygame.time.Clock()
while not game.game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
game.move_current_block_left()
elif event.key == pygame.K_RIGHT:
game.move_current_block_right()
elif event.key == pygame.K_DOWN:
game.move_current_block_down()
elif event.key == pygame.K_UP:
game.rotate_current_block()
# 移动方块
if pygame.time.get_ticks() - game.level * 100 >= 1000:
game.move_current_block_down()
# 绘制游戏
game.draw()
pygame.display.update()
# 控制帧率
clock.tick(60)
# 退出pygame
pygame.quit()
```
阅读全文