俄罗斯方块小游戏开发

代码图:

import pygame, random

pygame.init()

# 游戏界面参数
width = 300
height = 600
surface = pygame.display.set_mode((width, height))

# 颜色定义
black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 0, 0)
green = (0, 200, 0)
blue = (0, 0, 200)

# 俄罗斯方块参数
block_size = 20
grid_width = width // block_size
grid_height = height // block_size
grid = [[0] * grid_width for _ in range(grid_height)]

# 方块形状定义
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]]
]


# 方块随机生成函数
def new_block():
    shape = random.choice(shapes)
    block = {'shape': shape, 'x': grid_width // 2 - len(shape[0]) // 2, 'y': 0,
             'color': random.choice([red, green, blue])}
    return block


# 方块绘制函数
def draw_block(x, y, color):
    pygame.draw.rect(surface, color, (x * block_size, y * block_size, block_size, block_size))
    pygame.draw.rect(surface, black, (x * block_size, y * block_size, block_size, block_size), 1)


# 方块下落函数
def drop(block):
    block['y'] += 1
    if collision(block):
        block['y'] -= 1
        place(block)
        block = new_block()
    return block


# 方块移动函数
def move(block, dx):
    block['x'] += dx
    if collision(block):
        block['x'] -= dx
    return block


# 方块旋转函数
def rotate(block):
    shape = block['shape']
    block['shape'] = [[shape[y][x] for y in range(len(shape))] for x in range(len(shape[0]) - 1, -1, -1)]
    if collision(block):
        block['shape'] = shape
    return block


# 方块碰撞检测函数
def collision(block):
    shape = block['shape']
    for y, row in enumerate(shape):
        for x, cell in enumerate(row):
            if cell and (block['y'] + y >= grid_height or block['x'] + x < 0 or block['x'] + x >= grid_width or
                         grid[block['y'] + y][block['x'] + x]):
                return True
    return False


# 方块存放函数
def place(block):
    shape = block['shape']
    for y, row in enumerate(shape):
        for x, cell in enumerate(row):
            if cell:
                grid[block['y'] + y][block['x'] + x] = block['color']


# 行满检测函数
def full_rows():
    rows = []
    for y, row in enumerate(grid):
        if 0 not in row:
            rows.append(y)
    return rows


# 行删除函数
def remove_rows(rows):
    rows.sort(reverse=True)
    for row in rows:
        del grid[row]
        grid.insert(0, [0] * grid_width)


# 游戏循环
block = new_block()
clock = pygame.time.Clock()
while True:
    surface.fill(white)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                block = move(block, -1)
            elif event.key == pygame.K_RIGHT:
                block = move(block, 1)
            elif event.key == pygame.K_UP:
                block = rotate(block)
            elif event.key == pygame.K_DOWN:
                block = drop(block)

    block = drop(block)

    for y, row in enumerate(grid):
        for x, cell in enumerate(row):
            if cell:
                draw_block(x, y, cell)

    for y, row in enumerate(block['shape']):
        for x, cell in enumerate(row):
            if cell:
                draw_block(block['x'] + x, block['y'] + y, block['color'])

    rows = full_rows()
    if rows:
        remove_rows(rows)

    pygame.display.update()
    clock.tick(10)

结果图:

俄罗斯方块小游戏开发_第1张图片

你可能感兴趣的:(pygame,python,开发语言)