Pygame制作2048小游戏

import pygame
import random
BLOCK_SIZE = 100
ROWS = 4
COLS = 4
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
pygame.init()
WINDOW_SIZE = (BLOCK_SIZE * COLS, BLOCK_SIZE * ROWS)
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('2048')
font_32 = pygame.font.SysFont('Arial', 32)
font_64 = pygame.font.SysFont('Arial', 64)

# 定义方块类
class Block(pygame.sprite.Sprite):
    def __init__(self, number=0, row=0, col=0):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([BLOCK_SIZE, BLOCK_SIZE])
        self.rect = self.image.get_rect()
        self.number = number
        self.row = row
        self.col = col
        self.update()

    def update(self):
        # 绘制方块
        if self.number == 0:
            pygame.draw.rect(self.image, GRAY, [0, 0, BLOCK_SIZE, BLOCK_SIZE])
        else:
            pygame.draw.rect(self.image, YELLOW, [0, 0, BLOCK_SIZE, BLOCK_SIZE])
            text = font_32.render(str(self.number), True, BLACK)
            text_rect = text.get_rect(center=(BLOCK_SIZE / 2, BLOCK_SIZE / 2))
            self.image.blit(text, text_rect)

# 定义游戏面板类
class Board():
    def __init__(self):
        self.grid = [[0 for i in range(COLS)] for j in range(ROWS)]
        self.add_block()
        self.add_block()

    def add_block(self):
        empty_blocks = [(i, j) for i in range(ROWS) for j in range(COLS) if self.grid[i][j] == 0]
        if not empty_blocks:
            return False
        row, col = random.choice(empty_blocks)
        self.grid[row][col] = random.choice([2, 4])
        return True

    def move_left(self):
        for row in range(ROWS):
            # 合并相同的块
            for col in range(COLS - 1):
                if self.grid[row][col] != 0 and self.grid[row][col] == self.grid[row][col + 1]:
                    self.grid[row][col] *= 2
                    self.grid[row][col + 1] = 0
            # 移动块
            merge = False
            for col in range(COLS - 1):
                if self.grid[row][col] == 0 and self.grid[row][col + 1] != 0:
                    self.grid[row][col] = self.grid[row][col + 1]
                    self.grid[row][col + 1] = 0
                    merge = True
            if merge:
                self.move_left()

    def move_right(self):
        for row in range(ROWS):
            # 合并相同的块
            for col in range(COLS - 1, 0, -1):
                if self.grid[row][col] != 0 and self.grid[row][col] == self.grid[row][col - 1]:
                    self.grid[row][col] *= 2
                    self.grid[row][col - 1] = 0
            # 移动块
            merge = False
            for col in range(COLS - 1, 0, -1):
                if self.grid[row][col] == 0 and self.grid[row][col - 1] != 0:
                    self.grid[row][col] = self.grid[row][col - 1]
                    self.grid[row][col - 1] = 0
                    merge = True
            if merge:
                self.move_right()

    def move_up(self):
        for col in range(COLS):
            # 合并相同的块
            for row in range(ROWS - 1):
                if self.grid[row][col] != 0 and self.grid[row][col] == self.grid[row + 1][col]:
                    self.grid[row][col] *= 2
                    self.grid[row + 1][col] = 0
            # 移动块
            merge = False
            for row in range(ROWS - 1):
                if self.grid[row][col] == 0 and self.grid[row + 1][col] != 0:
                    self.grid[row][col] = self.grid[row + 1][col]
                    self.grid[row + 1][col] = 0
                    merge = True
            if merge:
                self.move_up()

    def move_down(self):
        for col in range(COLS):
            # 合并相同的块
            for row in range(ROWS - 1, 0, -1):
                if self.grid[row][col] != 0 and self.grid[row][col] == self.grid[row - 1][col]:
                    self.grid[row][col] *= 2
                    self.grid[row - 1][col] = 0
            # 移动块
            merge = False
            for row in range(ROWS - 1, 0, -1):
                if self.grid[row][col] == 0 and self.grid[row - 1][col] != 0:
                    self.grid[row][col] = self.grid[row - 1][col]
                    self.grid[row - 1][col] = 0
                    merge = True
            if merge:
                self.move_down()

    def draw(self):
        for row in range(ROWS):
            for col in range(COLS):
                block = Block(self.grid[row][col], row, col)
                block.rect.x = col * BLOCK_SIZE
                block.rect.y = row * BLOCK_SIZE
                screen.blit(block.image, block.rect)

# 初始化游戏面板
board = Board()

# 游戏循环
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:
                board.move_left()
                board.add_block()
            elif event.key == pygame.K_RIGHT:
                board.move_right()
                board.add_block()
            elif event.key == pygame.K_UP:
                board.move_up()
                board.add_block()
            elif event.key == pygame.K_DOWN:
                board.move_down()
                board.add_block()
            
    # 绘制背景
    screen.fill(WHITE)
    
    # 绘制游戏面板
    board.draw()

    # 绘制分数
    score = sum([sum(row) for row in board.grid])
    text = font_64.render('Score: ' + str(score), True, BLACK)
    text_rect = text.get_rect(center=(WINDOW_SIZE[0] / 2, BLOCK_SIZE * ROWS + BLOCK_SIZE / 2))
    screen.blit(text, text_rect)

    # 更新屏幕显示
    pygame.display.flip()

# 退出pygame
pygame.quit()

猜你喜欢

转载自blog.csdn.net/hzxhxyj1/article/details/132654780