Python编写的俄罗斯方块小游戏

游戏页面

左右键移动方块位置,上键切换方块形态。
在这里插入图片描述

实现代码

import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义颜色
colors = [
    (0, 0, 0),  # 黑色
    (255, 0, 0),  # 红色
    (0, 255, 0),  # 绿色
    (0, 0, 255),  # 蓝色
    (255, 255, 0),  # 黄色
    (255, 0, 255),  # 紫色
    (0, 255, 255)  # 青色
]

# 俄罗斯方块形状
shapes = [
    [[1, 1, 1, 1]],
    [[1, 1],
     [1, 1]],
    [[0, 1, 1],
     [1, 1, 0]],
    [[1, 1, 0],
     [0, 1, 1]],
    [[1, 1, 1],
     [0, 1, 0]],
    [[1, 1, 1],
     [1, 0, 0]],
    [[1, 1, 1],
     [0, 0, 1]]
]

# 设置游戏屏幕
screen_width = 300
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('俄罗斯方块')

# 游戏网格
grid = [[0 for _ in range(10)] for _ in range(20)]

# 初始化时钟
clock = pygame.time.Clock()

# 定义方块类
class Shape:
    def __init__(self):
        self.shape = random.choice(shapes)
        self.color = random.randint(1, len(colors) - 1)
        self.x = 3
        self.y = 0

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

    def draw(self):
        for i, row in enumerate(self.shape):
            for j, val in enumerate(row):
                if val:
                    pygame.draw.rect(screen, colors[self.color], (self.x * 30 + j * 30, self.y * 30 + i * 30, 30, 30))

def check_collision(shape):
    for i, row in enumerate(shape.shape):
        for j, val in enumerate(row):
            if val:
                if shape.x + j < 0 or shape.x + j >= 10 or shape.y + i >= 20 or grid[shape.y + i][shape.x + j]:
                    return True
    return False

def merge_shape(shape):
    for i, row in enumerate(shape.shape):
        for j, val in enumerate(row):
            if val:
                grid[shape.y + i][shape.x + j] = shape.color

def remove_full_lines():
    global grid
    grid = [row for row in grid if not all(row)]
    while len(grid) < 20:
        grid.insert(0, [0 for _ in range(10)])

def draw_grid():
    for y in range(20):
        for x in range(10):
            pygame.draw.rect(screen, colors[grid[y][x]], (x * 30, y * 30, 30, 30))

def main():
    running = True
    current_shape = Shape()
    fall_time = 0

    while running:
        screen.fill((0, 0, 0))
        draw_grid()
        current_shape.draw()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    current_shape.x -= 1
                    if check_collision(current_shape):
                        current_shape.x += 1
                if event.key == pygame.K_RIGHT:
                    current_shape.x += 1
                    if check_collision(current_shape):
                        current_shape.x -= 1
                if event.key == pygame.K_DOWN:
                    current_shape.y += 1
                    if check_collision(current_shape):
                        current_shape.y -= 1
                if event.key == pygame.K_UP:
                    current_shape.rotate()
                    if check_collision(current_shape):
                        current_shape.rotate()
                        current_shape.rotate()
                        current_shape.rotate()

        fall_time += clock.get_rawtime()
        clock.tick()
        if fall_time / 1000 >= 0.5:
            fall_time = 0
            current_shape.y += 1
            if check_collision(current_shape):
                current_shape.y -= 1
                merge_shape(current_shape)
                remove_full_lines()
                current_shape = Shape()
                if check_collision(current_shape):
                    running = False

        pygame.display.update()

    pygame.quit()

if __name__ == "__main__":
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的Python俄罗斯方块小游戏的实现,可以使用Python标准库中的`pygame`模块进行图形化界面的开发: ```python import pygame import random # 定义方块的大小和游戏区域的大小 block_size = 30 game_width = 300 game_height = 600 # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) yellow = (255, 255, 0) # 定义各种形状的方块,每个方块由四个小方块组成 shapes = [ [[1, 1, 1, 1]], [[1, 1], [1, 1]], [[1, 1, 1], [0, 1, 0]], [[1, 1, 0], [0, 1, 1]], [[0, 1, 1], [1, 1, 0]], [[1, 0, 0], [1, 1, 1]], [[0, 0, 1], [1, 1, 1]], ] class Block: def __init__(self, shape): self.shape = shape self.color = random.choice([red, green, blue, yellow]) self.x = 0 self.y = 0 def rotate(self): self.shape = list(zip(*self.shape[::-1])) def move_down(self): self.y += 1 def move_left(self): self.x -= 1 def move_right(self): self.x += 1 class Game: def __init__(self): self.screen = pygame.display.set_mode((game_width, game_height)) self.clock = pygame.time.Clock() self.score = 0 self.game_over = False self.board = [[0] * (game_width // block_size) for _ in range(game_height // block_size)] self.current_block = Block(random.choice(shapes)) def draw_block(self, block): for i, row in enumerate(block.shape): for j, cell in enumerate(row): if cell == 1: pygame.draw.rect(self.screen, block.color, (block.x + j, block.y + i, 1, 1)) def draw_board(self): for i, row in enumerate(self.board): for j, cell in enumerate(row): if cell != 0: pygame.draw.rect(self.screen, white, (j, i, 1, 1)) def check_collision(self, block): for i, row in enumerate(block.shape): for j, cell in enumerate(row): if cell == 1: if i + block.y >= game_height // block_size or j + block.x < 0 or j + block.x >= game_width // block_size or self.board[i + block.y][j + block.x] != 0: return True return False def add_block_to_board(self, block): for i, row in enumerate(block.shape): for j, cell in enumerate(row): if cell == 1: self.board[i + block.y][j + block.x] = block.color def remove_full_rows(self): new_board = [[0] * (game_width // block_size) for _ in range(game_height // block_size)] new_row = game_height // block_size - 1 for i in range(game_height // block_size - 1, -1, -1): if sum(self.board[i]) != game_width // block_size: new_board[new_row] = self.board[i] new_row -= 1 else: self.score += 1 self.board = new_board def run(self): while not self.game_over: self.clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: self.game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.current_block.move_left() if self.check_collision(self.current_block): self.current_block.move_right() elif event.key == pygame.K_RIGHT: self.current_block.move_right() if self.check_collision(self.current_block): self.current_block.move_left() elif event.key == pygame.K_DOWN: self.current_block.move_down() if self.check_collision(self.current_block): self.current_block.move_up() elif event.key == pygame.K_UP: self.current_block.rotate() if self.check_collision(self.current_block): self.current_block.rotate() self.screen.fill(black) self.draw_board() self.draw_block(self.current_block) if self.check_collision(self.current_block): self.add_block_to_board(self.current_block) self.current_block = Block(random.choice(shapes)) if self.check_collision(self.current_block): self.game_over = True else: self.current_block.move_down() self.remove_full_rows() pygame.display.update() pygame.quit() if __name__ == '__main__': pygame.init() game = Game() game.run() ``` 这个游戏包括一个游戏类`Game`和一个方块类`Block`,其中`Game`类负责游戏逻辑和图形化界面的绘制,`Block`类负责方块的移动和变形操作。游戏的主循环中,不断更新屏幕并处理用户输入,实现了俄罗斯方块的基本功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叁拾舞

你的鼓励将是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值