Python 实现2048

2048游戏是一个经典的数字益智游戏,使用Python语言可以很容易地实现它。以下是一个简单的代码示例:

import pygame
import random

pygame.init()

# 设置颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 设置屏幕大小
size = (400, 500)
screen = pygame.display.set_mode(size)

# 设置标题
pygame.display.set_caption("2048")

# 设置字体
font = pygame.font.SysFont('SimHei', 36)

# 初始化游戏数据
data = [[0 for i in range(4)] for j in range(4)]
score = 0

def init():
    # 随机生成两个2或4
    for i in range(2):
        while True:
            row = random.randint(0, 3)
            col = random.randint(0, 3)
            if data[row][col] == 0:
                data[row][col] = random.choice([2, 4])
                break

def draw():
    # 绘制背景
    screen.fill(GRAY)

    # 绘制分数
    score_text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))

    # 绘制方格
    for row in range(4):
        for col in range(4):
            x = col * 100
            y = row * 100 + 100
            pygame.draw.rect(screen, WHITE, [x, y, 100, 100], 1)
            if data[row][col] != 0:
                num_text = font.render(str(data[row][col]), True, WHITE)
                text_rect = num_text.get_rect()
                text_rect.centerx = x + 50
                text_rect.centery = y + 50
                screen.blit(num_text, text_rect)

def move_up():
    global score
    for col in range(4):
        # 合并相同的数字
        for row in range(3):
            if data[row][col] == data[row+1][col]:
                data[row][col] *= 2
                data[row+1][col] = 0
                score += data[row][col]
        # 移动数字
        for row in range(3):
            if data[row][col] == 0:
                for i in range(row+1, 4):
                    if data[i][col] != 0:
                        data[row][col] = data[i][col]
                        data[i][col] = 0
                        break

def move_down():
    global score
    for col in range(4):
        # 合并相同的数字
        for row in range(3, 0, -1):
            if data[row][col] == data[row-1][col]:
                data[row][col] *= 2
                data[row-1][col] = 0
                score += data[row][col]
        # 移动数字
        for row in range(3, -1,-1):
            if data[row][col] == 0:
                for i in range(row-1,-1,-1):
                    if data[i][col] != 0:
                        data[row][col] = data[i][col]
                        data[i][col] = 0
                        break

def move_left():
    global score
    for row in range(4):
        # 合并相同的数字
        for col in range(3):
            if data[row][col] == data[row][col+1]:
                data[row][col] *=2
                data[row][col+1]=0
                score +=data[row][col]
        # 移动数字
        for col in range(3):
            if data[row][col]==0:
                for i in range(col+1,4):
                    if data[row][i]!=0:
                        data[row][col]=data[row][i]
                        data[row][i]=0
                        break

def move_right():
    global score
    for row in range(4):
        # 合并相同的数字
        for col in range(3, 0, -1):
            if data[row][col] == data[row][col-1]:
                data[row][col] *= 2
                data[row][col-1] = 0
                score += data[row][col]
        # 移动数字
        for col in range(3, 0, -1):
            if data[row][col] == 0:
                for i in range(col-1, -1, -1):
                    if data[row][i] != 0:
                        data[row][col] = data[row][i]
                        data[row][i] = 0
                        break

def generate_new_num():
    while True:
        row = random.randint(0, 3)
        col = random.randint(0, 3)
        if data[row][col] == 0:
            data[row][col] = random.choice([2, 4])
            break

def is_game_over():
    for row in range(4):
        for col in range(4):
            if data[row][col] == 0:
                return False
            if row < 3 and data[row][col] == data[row+1][col]:
                return False
            if col < 3 and data[row][col] == data[row][col+1]:
                return False
    return True

init()

done = False

while not done:
    draw()
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                move_up()
                generate_new_num()
            elif event.key == pygame.K_DOWN:
                move_down()
                generate_new_num()
            elif event.key == pygame.K_LEFT:
                move_left()
                generate_new_num()
            elif event.key == pygame.K_RIGHT:
                move_right()
                generate_new_num()

    if is_game_over():
        print("Game Over!")
        done = True

pygame.quit()

效果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值