图片,文件和运行产生的文件夹我都上传到百度网盘上了,大家可以自行去取~
链接: https://pan.baidu.com/s/1F-1pI0wcu1iUmDdPCvA9NQ
提取码: nbyg
游戏代码:
文件名:2048.py(游戏主模块)
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()
pygame.init()
screen = pygame.display.set_mode((set.screen_width, set.screen_height))
pygame.display.set_caption("2048")
sf.addnum(nums)
sf.addnum(nums)
while True:
gb.check_events(nums)
gb.update_screen(nums, set, screen)
pygame.display.flip()
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:
n.append(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:
n.append(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:
n.append(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:
n.append(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:
s.append((i, j))
if nums[i][j] == 2048:
print("游戏胜利!")
sys.exit()
if len(s) == 0:
print("游戏结束!")
sys.exit()
x = len(s)
n = random.randint(0, x-1)
ij = s[n]
i = int(ij[0])
j = int(ij[1])
nums[i][j] = random.choice([2, 4])
文件名:game_body.py
import sys
import pygame
import 算法 as sf
import number as nu
def check_events(nums):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
sf.right(nums)
elif event.key == pygame.K_LEFT:
sf.left(nums)
elif event.key == pygame.K_DOWN:
sf.down(nums)
elif event.key == pygame.K_UP:
sf.up(nums)
sf.addnum(nums)
def update_screen(nums, set, screen):
screen.blit(set.screen_bg, set.screen_rect)
y = set.numlen * 2.5
for i in range(4):
x = set.numlen * 0.5
for j in range(4):
if nums[i][j] != 0:
nu.drawnum(nums[i][j], screen, x, y)
x += set.numlen
y += set.numlen
文件名:number.py
import pygame
def drawnum(num, screen, x, y):
if num == 2:
n = pygame.image.load('2.gif')
elif num == 4:
n = pygame.image.load('4.gif')
elif num == 8:
n = pygame.image.load('8.gif')
elif num == 16:
n = pygame.image.load('16.gif')
elif num == 32:
n = pygame.image.load('32.gif')
elif num == 64:
n = pygame.image.load('64.gif')
elif num == 128:
n = pygame.image.load('128.gif')
elif num == 256:
n = pygame.image.load('256.gif')
elif num == 512:
n = pygame.image.load('512.gif')
elif num == 1024:
n = pygame.image.load('1024.gif')
else:
n = pygame.image.load('2048.gif')
r = n.get_rect()
r.centerx = x
r.centery = y
screen.blit(n, r)
文件名:settings.py
import pygame
class Settings():
def __init__(self):
# 屏幕设置
self.screen_width = 400
self.screen_height = 600
self.screen_bg = pygame.image.load('背景2.gif')
self.screen_rect = self.screen_bg.get_rect()
self.numlen = 100