Python写2048小游戏全部代码及所用图片

时间:2024-10-20 19:07:17

鉴于有很多朋友要图片,把整个项目上传了,自行去取吧~
资源地址:/download/qq_44651842/20009590

游戏效果

效果
效果

游戏用到的图片

背景
数字图片都是90像素的正方形
数字图片

游戏代码

(游戏主模块)

import pygame
from settings import Settings
import game_body as gb
import 算法 as sf

nums = [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]
def run_game():
    set = Settings()
    ()
    screen = .set_mode((set.screen_width, set.screen_height))
    .set_caption("2048")
    (nums)
    (nums)
    while True:
        gb.check_events(nums)
        gb.update_screen(nums, set, screen)
        ()
run_game()

算法.py(对应按键的数据处理,判断游戏是否结束,添加数字)

对数据的处理没有好好想更好的处理方法,我觉着肯定有简单的方法,但是我想不到。。。

import random
import sys
def right(nums):
    for i in [0,1,2,3]:
        j = 3
        while j >= 0:
            if nums[i][j] == 0:
                j -= 1
            elif j > 0 and nums[i][j] == nums[i][j-1]:
                nums[i][j] *= 2
                nums[i][j-1] = 0
                j -= 2
            elif j > 1 and nums[i][j-1] == 0 and nums[i][j] == nums[i][j-2]:
                nums[i][j] *= 2
                nums[i][j - 2] = 0
                j = -1
            elif j == 3 and nums[i][j] == nums[i][0] and nums[i][j-1] == 0 \
                    and nums[i][j-2] == 0:
                nums[i][j] *= 2
                nums[i][0] = 0
                j = -1
            else:
                j -= 1
        j = 0
        n = []
        while j <= 3:
            if nums[i][j] != 0:
                (nums[i][j])
                nums[i][j] = 0
            j += 1
        m = len(n)
        for i1 in n:
            nums[i][3-m+1] = i1
            m -= 1
def left(nums):
    for i in [0, 1, 2, 3]:
        j = 0
        while j <= 3:
            if nums[i][j] == 0:
                j += 1
            elif j < 3 and nums[i][j] == nums[i][j+1]:
                nums[i][j] *= 2
                nums[i][j+1] = 0
                j += 2
            elif j < 2 and nums[i][j+1] == 0 and nums[i][j] ==nums[i][j+2]:
                nums[i][j] *= 2
                nums[i][j + 2] = 0
                j = 4
            elif j == 0 and nums[i][j] == nums[i][3] and nums[i][j+1] == 0\
                and nums[i][j+2] == 0:
                nums[i][j] *= 2
                nums[i][3] =0
                j = 4
            else:
                j += 1
        j = 0
        n = []
        while j <= 3:
            if nums[i][j] != 0:
                (nums[i][j])
                nums[i][j] = 0
            j += 1
        m = 0
        for i1 in n:
            nums[i][m] = i1
            m += 1
def down(nums):
    for j in [0,1,2,3]:
        i = 3
        while i>=0:
            if nums[i][j] == 0:
                i -= 1
            elif i> 0 and nums[i][j] == nums[i-1][j]:
                nums[i][j] *= 2
                nums[i-1][j] = 0
                i -= 2
            elif i>1 and nums[i-1][j] == 0 and nums[i][j] == nums[i-2][j]:
                nums[i][j] *= 2
                nums[i-2][j] = 0
                i -= 1
            elif i == 3 and nums[i][j] == nums[0][j] and nums[i-1][j] == 0\
                and nums[i-2][j] == 0:
                nums[i][j] *= 2
                nums[0][j] = 0
                i = -1
            else:
                i -= 1
        i = 0
        n = []
        while i <= 3:
            if nums[i][j] != 0:
                (nums[i][j])
                nums[i][j] = 0
            i += 1
        m = len(n)
        for i1 in n:
            nums[3-m+1][j] = i1
            m -= 1
def up(nums):
    for j in [0,1,2,3]:
        i = 0
        while i <= 3:
            if nums[i][j] == 0:
                i += 1
            elif i<3 and nums[i][j] == nums[i+1][j]:
                nums[i][j] *= 2
                nums[i + 1][j] = 0
                i += 2
            elif i < 2 and nums[i+1][j] == 0 and nums[i][j] == nums[i+2][j]:
                nums[i][j] *= 2
                nums[i + 2][j] = 0
                i += 1
            elif i ==0 and nums[i][j] ==nums[3][j] and nums[i+1][j] == 0\
                and nums[i+2][j] == 0:
                nums[i][j] *= 2
                nums[3][j] = 0
                i = 4
            else:
                i += 1
        i = 0
        n = []
        while i <= 3:
            if nums[i][j] != 0:
                (nums[i][j])
                nums[i][j] = 0
            i += 1
        m = 0
        for i1 in n:
            nums[m][j] = i1
            m += 1
def addnum(nums):
    s = []
    for i in range(4):
        for j in range(4):
            if nums[i][j] == 0:
                ((i, j))
            if nums[i][j] == 2048:
                print("游戏胜利!")
                ()
    if len(s) == 0:
        print("游戏结束!")
        ()
    x = len(s)
    n = (0, x-1)
    ij = s[n]
    i = int(ij[0])
    j = int(ij[1])
    nums[i][j] = ([2, 4])

game_body.py (响应按键,屏幕更新)

import sys
import pygame
import 算法 as sf
import number as nu
def check_events(nums):
    for event in ():
        if  == :
            ()
        elif  == :
            if  == pygame.K_RIGHT:
                (nums)
            elif  == pygame.K_LEFT:
                (nums)
            elif  == pygame.K_DOWN:
                (nums)
            elif  == pygame.K_UP:
                (nums)
            (nums)
def update_screen(nums, set, screen):
    (set.screen_bg, set.screen_rect)
    y =  * 2.5
    for i in range(4):
        x =  * 0.5
        for j in range(4):
            if nums[i][j] != 0:
                (nums[i][j], screen, x, y)
            x += 
        y += 

(加载数字图片)

import pygame


def drawnum(num, screen, x, y):
    if num == 2:
        n = ('images/')
    elif num == 4:
        n = ('images/')
    elif num == 8:
        n = ('images/')
    elif num == 16:
        n = ('images/')
    elif num == 32:
        n = ('images/')
    elif num == 64:
        n = ('images/')
    elif num == 128:
        n = ('images/')
    elif num == 256:
        n = ('images/')
    elif num == 512:
        n = ('images/')
    elif num == 1024:
        n = ('images/')
    else:
        n = ('images/')
    r = n.get_rect()
     = x
     = y
    (n, r)

(游戏窗口属性)

import pygame
class Settings():
    def __init__(self):
        # 屏幕设置
        self.screen_width = 400
        self.screen_height = 600
        self.screen_bg = ('images/背景')
        self.screen_rect = self.screen_bg.get_rect()
         = 100