每日一个小题
import pygame
import random
# 初始化 Pygame
pygame.init()
# 屏幕大小
screen_width = 300
screen_height = 600
block_size = 30
# 颜色定义
colors = [
(0, 0, 0),
(255, 0, 0),
(0, 150, 0),
(0, 0, 255),
(255, 120, 0),
(255, 255, 0),
(180, 0, 255),
(0, 220, 220)
]
# 形状定义
shapes = [
[[1, 1, 1],
[0, 1, 0]],
[[0, 2, 2],
[2, 2, 0]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 4],
[4, 4]],
[[0, 5, 0],
[5, 5, 5]],
[[6, 6, 6, 6]],
[[7, 7],
[7, 7]]
]
class Tetris:
def __init__(self):
self.width = screen_width // block_size
self.height = screen_height // block_size
self.board = [[0 for x in range(self.width)] for y in range(self.height)]
self.score = 0
self.gameover = False
self.current_piece = self.new_piece()
self.next_piece = self.new_piece()
self.clock = pygame.time.Clock()
self.fall_time = 0
self.level = 1
self.lines = 0
def new_piece(self):
shape = random.choice(shapes)
color = colors[shapes.index(shape) + 1]
return {'shape': shape, 'color': color, 'x': self.width // 2 - len(shape[0]) // 2, 'y': 0}
def rotate_piece(self):
piece = self.current_piece['shape']
new_piece = list(zip(*piece[::-1]))
self.current_piece['shape'] = new_piece
def valid_space(self, piece, offset):
off_x, off_y = offset
for y, row in enumerate(piece):
for x, cell in enumerate(row):
if cell and (x + off_x < 0 or x + off_x >= self.width or y + off_y >= self.height or self.board[y + off_y][x + off_x]):
return False
return True
def freeze(self):
piece = self.current_piece['shape']
for y, row in enumerate(piece):
for x, cell in enumerate(row):
if cell:
self.board[y + self.current_piece['y']][x + self.current_piece['x']] = self.current_piece['color']
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
if not self.valid_space(self.current_piece['shape'], (self.current_piece['x'], self.current_piece['y'])):
self.gameover = True
def clear_lines(self):
lines = 0
for i, row in enumerate(self.board):
if all(row):
del self.board[i]
self.board.insert(0, [0 for _ in range(self.width)])
lines += 1
self.score += lines ** 2 * 100
self.lines += lines
if self.lines >= 10:
self.level += 1
self.lines -= 10
pygame.time.set_timer(pygame.USEREVENT+1, max(100, 1000 - (self.level-1)*100))
def drop(self):
self.current_piece['y'] += 1
if not self.valid_space(self.current_piece['shape'], (self.current_piece['x'], self.current_piece['y'])):
self.current_piece['y'] -= 1
self.freeze()
def move(self, dx):
self.current_piece['x'] += dx
if not self.valid_space(self.current_piece['shape'], (self.current_piece['x'], self.current_piece['y'])):
self.current_piece['x'] -= dx
def draw_grid(self, surface):
for y in range(self.height):
for x in range(self.width):
pygame.draw.rect(surface, colors[self.board[y][x]], (x*block_size, y*block_size, block_size, block_size), 0)
pygame.draw.rect(surface, (255, 255, 255), (x*block_size, y*block_size, block_size, block_size), 1)
def draw_piece(self, surface):
shape = self.current_piece['shape']
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(surface, self.current_piece['color'], ((self.current_piece['x'] + x) * block_size, (self.current_piece['y'] + y) * block_size, block_size, block_size), 0)
pygame.draw.rect(surface, (255, 255, 255), ((self.current_piece['x'] + x) * block_size, (self.current_piece['y'] + y) * block_size, block_size, block_size), 1)
def run(self):
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Tetris')
pygame.time.set_timer(pygame.USEREVENT+1, 1000)
while not self.gameover:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move(-1)
elif event.key == pygame.K_RIGHT:
self.move(1)
elif event.key == pygame.K_DOWN:
self.drop()
elif event.key == pygame.K_UP:
self.rotate_piece()
elif event.type == pygame.USEREVENT+1:
self.drop()
self.fall_time += self.clock.get_rawtime()
self.clock.tick()
if self.fall_time / 1000 > 1:
self.drop()
self.fall_time = 0
screen.fill((0, 0, 0))
self.draw_grid(screen)
self.draw_piece(screen)
pygame.display.flip()
print("Game Over! Score:", self.score)
pygame.quit()
if __name__ == '__main__':
game = Tetris()
game.run()
实现了一个基本的俄罗斯方块游戏,包括方块的移动、旋转、下落以及行的消除等功能,你可以直接运行这个脚本来玩游戏