以下是一个简单的俄罗斯方块程序代码,使用Python语言编写:

import pygame
import random

# 初始化
pygame.init()

# 定义常量
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BLOCK_SIZE = 20
FONT_SIZE = 30
FPS = 30

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

# 初始化窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("俄罗斯方块")

# 加载字体
font = pygame.font.SysFont("SimHei", FONT_SIZE)

# 加载音效
move_sound = pygame.mixer.Sound("move.wav")
rotate_sound = pygame.mixer.Sound("rotate.wav")
drop_sound = pygame.mixer.Sound("drop.wav")
clear_sound = pygame.mixer.Sound("clear.wav")
gameover_sound = pygame.mixer.Sound("gameover.wav")

# 定义方块形状
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 = [
    BLACK,
    RED,
    GREEN,
    BLUE,
    YELLOW,
    CYAN,
    MAGENTA,
]

# 定义方块类
class Block:
    def __init__(self, shape, color):
        self.shape = shape
        self.color = color
        self.row = 0
        self.col = SCREEN_WIDTH // BLOCK_SIZE // 2 - len(shape[0]) // 2

    def move_down(self):
        self.row += 1

    def move_left(self):
        self.col -= 1

    def move_right(self):
        self.col += 1

    def rotate(self):
        self.shape = list(zip(*self.shape[::-1]))

    def draw(self):
        for i in range(len(self.shape)):
            for j in range(len(self.shape[0])):
                if self.shape[i][j] != 0:
                    pygame.draw.rect(screen, self.color, (self.col * BLOCK_SIZE + j * BLOCK_SIZE, self.row * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

# 定义游戏类
class Game:
    def __init__(self):
        self.grid = [[0] * (SCREEN_WIDTH // BLOCK_SIZE) for _ in range(SCREEN_HEIGHT // BLOCK_SIZE)]
        self.score = 0
        self.level = 1
        self.block = Block(random.choice(SHAPES), random.choice(COLORS))

    def check_collision(self):
        for i in range(len(self.block.shape)):
            for j in range(len(self.block.shape[0])):
                if self.block.shape[i][j] != 0:
                    if self.block.row + i >= SCREEN_HEIGHT // BLOCK_SIZE or self.block.col + j < 0 or self.block.col + j >= SCREEN_WIDTH // BLOCK_SIZE or self.grid[self.block.row + i][self.block.col + j] != 0:
                        return True
        return False

    def add_to_grid(self):
        for i in range(len(self.block.shape)):
            for j in range(len(self.block.shape[0])):
                if self.block.shape[i][j] != 0:
                    self.grid[self.block.row + i][self.block.col + j] = self.block.color

    def remove_complete_rows(self):
        num_rows_removed = 0
        for i in range(len(self.grid)):
            if all(self.grid[i]):
                del self.grid[i]
                self.grid.insert(0, [0] * (SCREEN_WIDTH // BLOCK_SIZE))
                num_rows_removed += 1
        if num_rows_removed > 0:
            self.score += num_rows_removed ** 2
            clear_sound.play()

    def draw_grid(self):
        for i in range(len(self.grid)):
            for j in range(len(self.grid[0])):
                if self.grid[i][j] != 0:
                    pygame.draw.rect(screen, self.grid[i][j], (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
                pygame.draw.rect(screen, WHITE, (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)

    def draw_score(self):
        score_text = font.render("Score: {}".format(self.score), True, WHITE)
        screen.blit(score_text, (SCREEN_WIDTH // 2 - score_text.get_width() // 2, SCREEN_HEIGHT - FONT_SIZE - 10))

    def draw_level(self):
        level_text = font.render("Level: {}".format(self.level), True, WHITE)
        screen.blit(level_text, (10, SCREEN_HEIGHT - FONT_SIZE - 10))

    def draw_gameover(self):
        gameover_text = font.render("Game Over", True, RED)
        screen.blit(gameover_text, (SCREEN_WIDTH // 2 - gameover_text.get_width() // 2, SCREEN_HEIGHT // 2 - gameover_text.get_height() // 2))
        pygame.display.update()
        pygame.time.wait(2000)

    def update(self):
        self.block.move_down()
        if self.check_collision():
            self.block.move_up()
            self.add_to_grid()
            self.remove_complete_rows()
            self.block = Block(random.choice(SHAPES), random.choice(COLORS))
            if self.check_collision():
                gameover_sound.play()
                self.draw_gameover()
                return False
        return True

# 创建游戏对象
game = Game()

# 主循环
clock = pygame.time.Clock()
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:
                game.block.move_left()
                if game.check_collision():
                    game.block.move_right()
                else:
                    move_sound.play()
            elif event.key == pygame.K_RIGHT:
                game.block.move_right()
                if game.check_collision():
                    game.block.move_left()
                else:
                    move_sound.play()
            elif event.key == pygame.K_UP:
                game.block.rotate()
                if game.check_collision():
                    game.block.rotate()
                else:
                    rotate_sound.play()
            elif event.key == pygame.K_DOWN:
                game.block.move_down()
                if game.check_collision():
                    game.block.move_up()
                    game.add_to_grid()
                    game.remove_complete_rows()
                    game.block = Block(random.choice(SHAPES), random.choice(COLORS))
                    if game.check_collision():
                        gameover_sound.play()
                        game.draw_gameover()
                        running = False
                else:
                    drop_sound.play()

    # 更新游戏状态
    if game.update():
        # 绘制游戏界面
        screen.fill(BLACK)
        game.draw_grid()
        game.draw_score()
        game.draw_level()
        game.block.draw()
        pygame.display.update()

    # 控制帧率
    clock.tick(FPS)

# 退出程序
pygame.quit()

注意:这只是一个简单的示例代码,还有许多细节和优化可以进行。

俄罗斯方块程序代码

原文地址: https://www.cveoy.top/t/topic/bYMp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录