一、背景介绍与引用 感谢@彡楠风丶吹 二、源代码 然后是图形文件GameImages.py,包含了背景图片、数字对应的图片和胜负的图片。这就是有些借鉴 @彡楠风丶吹 的部分。借鉴是采用了原来的分辨率设定与数字块图片位置与刷新;区别是所有的图片都是我自己重新用PS做的,并且做了圆角处理,还有就是添加了胜负图片。下面是源代码: 最后是运行文件GameRun.py,包括键盘操作和游戏开始。键盘操作部分包括判定移动是否有效部分。下面是源代码: 三、游戏图形 四、运行演示
最近在重温python,加上一点私心,想要编一个有图像界面的2048程序。
我曾经想过在论坛里面找一个类似的,但是搜索了很久都找不到合适的,有的是只有控制台界面,有的实在是不好自制。最终,我找到了一个非常合适的解决方案(下面会放出链接)。但是我只是采用了其中图形界面的设定,具体的算法还有图形我是自己设计的,原因是原作者在规则上与2048有些不符(比如当移动并不改变现在的顺序时,不产生新的随机块)。
下面放出引用作者的原贴链接
Python写2048小游戏全部代码及所用图片
虽然算法是自己写的,但我采用了 @彡楠风丶吹 的源代码方案,把源代码分了三部分:游戏算法、游戏图形和游戏运行。
首先是算法部分:GameCalculation.py,这部分包含了主要的内部算法过程,主要是上下左右移动、添加随机数、游戏规则。其中,上下左右移动的过程是先移动后合并然后再次移动。下面是这部分的代码:import numpy as np#为了方便,用了矩阵 import random as rd import sys def Up(GameList):#向上移动 NewList1=np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]])#用两个零矩阵过渡 NewList2=np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) for j in range(4):#先储存非零量在赋值给新列表 NoneZeroList = [] for i in range(4): if GameList[i,j]!=0: NoneZeroList.append(GameList[i,j]) for k in range(len(NoneZeroList)): NewList1[k,j]=NoneZeroList[k] for j in range(4):#相同值合并 for i in range(3): if NewList1[i,j]==NewList1[i+1,j]: NewList1[i,j]=NewList1[i,j]*2 NewList1[i+1,j]=0 for j in range(4):#合并后再移动 NoneZeroList=[] for i in range(4): if NewList1[i,j]!=0: NoneZeroList.append(NewList1[i,j]) for k in range(len(NoneZeroList)): NewList2[k,j]=NoneZeroList[k] NoneSame=0 for i in range(4): for j in range(4): if GameList[i,j]!=NewList2[i,j]: NoneSame=1 break if NoneSame==1: for i in range(4): for j in range(4): GameList[i,j]=NewList2[i,j] return 1 else: return 0 def Down(GameList): NewList1=np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) NewList2=np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) for j in range(4): NoneZeroList = [] for i in range(-1,-5,-1): if GameList[i,j]!=0: NoneZeroList.append(GameList[i,j]) for k in range(len(NoneZeroList)): NewList1[3-k,j]=NoneZeroList[k] for j in range(4): for i in range(-1,-4,-1): if NewList1[i,j]==NewList1[i-1,j]: NewList1[i,j]=NewList1[i,j]*2 NewList1[i-1,j]=0 for j in range(4): NoneZeroList=[] for i in range(-1,-5,-1): if NewList1[i,j]!=0: NoneZeroList.append(NewList1[i,j]) for k in range(len(NoneZeroList)): NewList2[3-k,j]=NoneZeroList[k] NoneSame=0 for i in range(4): for j in range(4): if GameList[i,j]!=NewList2[i,j]: NoneSame=1 break if NoneSame==1: for i in range(4): for j in range(4): GameList[i,j]=NewList2[i,j] return 1 else: return 0 def Left(GameList): NewList1 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) NewList2 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) for i in range(4): NoneZeroList=[] for j in range(4): if GameList[i,j]!=0: NoneZeroList.append(GameList[i,j]) for k in range(len(NoneZeroList)): NewList1[i,k]=NoneZeroList[k] for i in range(4): for j in range(3): if NewList1[i,j]==NewList1[i,j+1]: NewList1[i,j]*=2 NewList1[i,j+1]=0 for i in range(4): NoneZeroList=[] for j in range(4): if NewList1[i,j]!=0: NoneZeroList.append(NewList1[i,j]) for k in range(len(NoneZeroList)): NewList2[i,k]=NoneZeroList[k] NoneSame=0 for i in range(4): for j in range(4): if GameList[i,j]!=NewList2[i,j]: NoneSame=1 break if NoneSame==1: for i in range(4): for j in range(4): GameList[i,j]=NewList2[i,j] return 1 else: return 0 def Right(GameList): NewList1 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) NewList2 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) for i in range(4): NoneZeroList=[] for j in range(-1,-5,-1): if GameList[i,j]!=0: NoneZeroList.append(GameList[i,j]) for k in range(len(NoneZeroList)): NewList1[i,3-k]=NoneZeroList[k] for i in range(4): for j in range(-1,-4,-1): if NewList1[i,j]==NewList1[i,j-1]: NewList1[i,j]*=2 NewList1[i,j-1]=0 for i in range(4): NoneZeroList=[] for j in range(-1,-5,-1): if NewList1[i,j]!=0: NoneZeroList.append(NewList1[i,j]) for k in range(len(NoneZeroList)): NewList2[i,3-k]=NoneZeroList[k] NoneSame=0 for i in range(4): for j in range(4): if GameList[i,j]!=NewList2[i,j]: NoneSame=1 break if NoneSame==1: for i in range(4): for j in range(4): GameList[i,j]=NewList2[i,j] return 1 else: return 0 def Random2or4(GameList): ZeroList = [] Count4 = 0 for i in range(4): for j in range(4): if GameList[i, j] == 0: ZeroList.append([i, j]) for i in range(4): for j in range(4): if GameList[i, j] == 4: Count4 = 1 if Count4 == 0: RandomNum = rd.choice(ZeroList) GameList[RandomNum[0], RandomNum[1]] = 2 else: RandomNum = rd.choice(ZeroList) GameList[RandomNum[0], RandomNum[1]] = rd.choice([2, 2, 2, 2, 2, 2, 2, 2, 2, 4]) def Rule(GameList): Temp2048 = np.sum(GameList == 2048) Temp0 = np.sum(GameList == 0) if Temp2048 != 0: return 1 if Temp0==0: TempSame=0 for i in range(4): for j in range(3): if GameList[i, j] == GameList[i,j+1]: TempSame = 1 break for j in range(4): for i in range(3): if GameList[i,j]==GameList[i+1,j]: TempSame = 1 break if TempSame==0: return 0 else: return 2
import pygame import time import sys class Settings(): def __init__(self): # 屏幕设置 self.screen_width = 400 self.screen_height = 600 self.screen_bg = pygame.image.load('images/bg_1.png') self.screen_rect = self.screen_bg.get_rect() self.numlen = 100 def NumImage(num, screen, x, y): if num == 2: n = pygame.image.load('images/2.png') elif num == 4: n = pygame.image.load('images/4.png') elif num == 8: n = pygame.image.load('images/8.png') elif num == 16: n = pygame.image.load('images/16.png') elif num == 32: n = pygame.image.load('images/32.png') elif num == 64: n = pygame.image.load('images/64.png') elif num == 128: n = pygame.image.load('images/128.png') elif num == 256: n = pygame.image.load('images/256.png') elif num == 512: n = pygame.image.load('images/512.png') elif num == 1024: n = pygame.image.load('images/1024.png') else: n = pygame.image.load('images/2048.png') r = n.get_rect() r.centerx = x r.centery = y screen.blit(n, r) def SoFImage(x,screen): if x==0: n=pygame.image.load('images/2048_Failed.png') screen.blit(n, (20,175)) pygame.display.update() time.sleep(1) elif x==1: n = pygame.image.load('images/2048_Succeed.png') screen.blit(n, (20, 175)) pygame.display.update() time.sleep(1) def UpdateScreen(GameList, 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 GameList[i,j] != 0: NumImage(GameList[i,j], screen, x, y) x += set.numlen y += set.numlen
import numpy as np import pygame import GameCalculation as GC import GameImages as GI import sys def Operation(GameList,screen): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: if GC.Right(GameList)==1: GC.Rule(GameList) if GC.Rule(GameList)==2: GC.Random2or4(GameList) GI.SoFImage(GC.Rule(GameList),screen) elif event.key == pygame.K_LEFT: if GC.Left(GameList)==1: GC.Rule(GameList) if GC.Rule(GameList)==2: GC.Random2or4(GameList) GI.SoFImage(GC.Rule(GameList),screen) elif event.key == pygame.K_DOWN: if GC.Down(GameList)==1: GC.Rule(GameList) if GC.Rule(GameList)==2: GC.Random2or4(GameList) GI.SoFImage(GC.Rule(GameList),screen) elif event.key == pygame.K_UP: if GC.Up(GameList)==1: GC.Rule(GameList) if GC.Rule(GameList)==2: GC.Random2or4(GameList) GI.SoFImage(GC.Rule(GameList),screen) def RunGame(): GameList = np.zeros((4, 4), dtype=int) set = GI.Settings() pygame.init() screen = pygame.display.set_mode((set.screen_width, set.screen_height)) pygame.display.set_caption("2048 @K1snan") GC.Random2or4(GameList) GC.Random2or4(GameList) while True: Operation(GameList,screen) GI.UpdateScreen(GameList, set, screen) pygame.display.update() RunGame()
其实上面已经把代码写完了,自己找一些图片就能运行了,背景图是400×600像素,数字块是90×90像素。我写这一部分就是来把自己的图片展示一下。开头的时候我已经说了,是有一些私心哒,做这个除了重新熟悉一下python外,也是想自己做一个喜欢的偶像的游戏,下面放图。
下面两张图是运行截图,由于我实在是凑不出2048来,只能给出失败的图了。
THE END
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算