跳到正文

STL_qwq

403146

再见祝你能到你想去的地方,到了以后能永远快乐。

在洛谷打开
资料、奖项与版本

资料

编号
403146
抓取

奖项

2023 NOIP
三等奖

1

以下是用Python和Pygame库实现的俄罗斯方块代码:

Python
import pygame
import random

# 初始化游戏
pygame.init()

# 游戏窗口设置
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 720
GRID_SIZE = 30
GRID_WIDTH = 10
GRID_HEIGHT = 20

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)

# 方块形状
SHAPES = [
    [[1, 5, 9, 13], [4, 5, 6, 7]],  # I
    [[4, 5, 9, 10], [2, 6, 5, 9]],  # Z
    [[6, 7, 9, 10], [1, 5, 6, 10]],  # S
    [[1, 2, 5, 9], [0, 4, 5, 6], [1, 5, 9, 8], [4, 5, 6, 10]],  # L
    [[1, 2, 6, 10], [5, 6, 7, 9], [2, 6, 10, 11], [3, 5, 6, 7]],  # J
    [[1, 4, 5, 6], [1, 4, 5, 9], [4, 5, 6, 9], [1, 5, 6, 9]],  # T
    [[1, 2, 5, 6]],  # O
]

COLORS = [CYAN, GREEN, RED, BLUE, ORANGE, MAGENTA, YELLOW]

class Tetris:
    def __init__(self):
        self.width = GRID_WIDTH
        self.height = GRID_HEIGHT
        self.grid = [[0 for _ in range(self.width)] for _ in range(self.height)]
        self.score = 0
        self.game_over = False
        self.new_piece()
        
    def new_piece(self):
        self.shape = random.randint(0, 6)
        self.color = COLORS[self.shape]
        self.rotation = 0
        self.x = self.width // 2 - 2
        self.y = 0
        
        if self.check_collision():
            self.game_over = True
            
    def check_collision(self):
        for i in range(4):
            for j in range(4):
                if i * 4 + j in SHAPES[self.shape][self.rotation]:
                    x = self.x + j
                    y = self.y + i
                    if x < 0 or x >= self.width or y >= self.height:
                        return True
                    if y >= 0 and self.grid[y][x]:
                        return True
        return False
    
    def rotate(self):
        old_rotation = self.rotation
        self.rotation = (self.rotation + 1) % len(SHAPES[self.shape])
        if self.check_collision():
            self.rotation = old_rotation
            
    def move_down(self):
        self.y += 1
        if self.check_collision():
            self.y -= 1
            self.freeze()
            
    def move_left(self):
        self.x -= 1
        if self.check_collision():
            self.x += 1
            
    def move_right(self):
        self.x += 1
        if self.check_collision():
            self.x -= 1
            
    def freeze(self):
        for i in range(4):
            for j in range(4):
                if i * 4 + j in SHAPES[self.shape][self.rotation]:
                    x = self.x + j
                    y = self.y + i
                    if y >= 0:
                        self.grid[y][x] = self.color
                        
        self.clear_lines()
        self.new_piece()
        
    def clear_lines(self):
        lines = 0
        for i in range(self.height-1, -1, -1):
            if all(self.grid[i]):
                del self.grid[i]
                self.grid.insert(0, [0 for _ in range(self.width)])
                lines += 1
        self.score += lines * 100
        
    def draw(self, screen):
        for y in range(self.height):
            for x in range(self.width):
                if self.grid[y][x]:
                    pygame.draw.rect(screen, self.grid[y][x], 
                                   (x*GRID_SIZE + 1, y*GRID_SIZE + 1,
                                    GRID_SIZE - 2, GRID_SIZE - 2))
                    
        if not self.game_over:
            for i in range(4):
                for j in range(4):
                    if i * 4 + j in SHAPES[self.shape][self.rotation]:
                        pygame.draw.rect(screen, self.color,
                                       ((self.x + j)*GRID_SIZE + 1,
                                        (self.y + i)*GRID_SIZE + 1,
                                        GRID_SIZE - 2, GRID_SIZE - 2))

def main():
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption("俄罗斯方块")
    clock = pygame.time.Clock()
    game = Tetris()
    
    fall_time = 0
    fall_speed = 500
    
    while True:
        screen.fill(BLACK)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    game.move_left()
                if event.key == pygame.K_RIGHT:
                    game.move_right()
                if event.key == pygame.K_DOWN:
                    game.move_down()
                if event.key == pygame.K_UP:
                    game.rotate()
        
        keys = pygame.key.get_pressed()
        if keys[pygame.K_DOWN]:
            fall_speed = 50
            
        current_time = pygame.time.get_ticks()
        fall_time += current_time - clock.get_rawtime()
        clock.tick()
        
        if fall_time >= fall_speed:
            game.move_down()
            fall_time = 0
            fall_speed = 500
            
        game.draw(screen)
        
        # 绘制得分
        font = pygame.font.SysFont('Arial', 30)
        text = font.render(f'Score: {game.score}', True, WHITE)
        screen.blit(text, (GRID_SIZE * GRID_WIDTH + 50, 50))
        
        if game.game_over:
            font = pygame.font.SysFont('Arial', 60)
            text = font.render('GAME OVER', True, RED)
            screen.blit(text, (WINDOW_WIDTH//2 - 150, WINDOW_HEIGHT//2 - 30))
            
        pygame.display.flip()

if __name__ == '__main__':
    main()

这个俄罗斯方块游戏包含以下功能:

  1. 标准俄罗斯方块游戏机制
  2. 7种不同的方块形状
  3. 得分系统
  4. 游戏结束判定
  5. 方块旋转和移动
  6. 自动下落和手动加速下落
  7. 消除满行功能

操作方法:

  • 左右箭头:移动方块
  • 上箭头:旋转方块
  • 下箭头:加速下落

游戏特点:

  1. 使用不同颜色区分不同方块
  2. 实时显示当前得分
  3. 游戏结束提示
  4. 合理的碰撞检测
  5. 自适应游戏速度

要

共 1 篇
  1. 个人记录

    浮水法学习笔记

    本文介绍了一种解决区间染色问题(如贴海报)的算法,无需使用线段树。其核心思路是倒序处理线段,通过递归模拟颜色“上浮”:若当前区间未被后续层完全遮挡,则未遮挡部分向上一层递归,能浮至最顶层即计入答案。文中提供了相应的 C++ 代码实现及洛谷 P3740 例题参考。