天天看点

Python-pygame模块实现2048小游戏-游戏源码Python-pygame模块实现2048小游戏

python-pygame模块实现2048小游戏-游戏源码

  • Python-pygame模块实现2048小游戏
    • 代码展示
      • 主程序,程序入口
      • 定义函数-绘制第x行第y列的显示单元的显示情况
      • 定义函数-设置操作音
      • 算法-实际操作-向上操作示例
    • 页面展示
    • 总结

Python-pygame模块实现2048小游戏

本来是想做个离线2048小游戏APP(网上下载的总是有广告,很烦人),但是android studio使用还不熟练,先来用python捋一遍逻辑。

代码展示

主程序,程序入口

import sys
import os
width=4 #设置行数
height=6 #设置列出
background=(250,250,250) #背景色
r=80 #设置每一个显示小方块的长宽
arr=[[0]*width for i in range(height)] #初始化二维数组

#定义一个pygame
pygame.init()
screen=pygame.display.set_mode((width*r,height*r),0,32)
screen.fill(background)
pygame.display.set_caption("2048")
#循环显示页面,并添加监控事件
while 1:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            exit()
        if event.type==pygame.KEYDOWN:
            try:
                if event.key==pygame.K_UP:
                    print("up") #这句可以不要,不影响执行
                    arr=getnewassign_Up(arr,width,height)
                    playmusic("../source/sound.mp3")
                if event.key==pygame.K_DOWN:
                    print("DOWN")
                    arr=getnewassign_Down(arr,width,height)
                    playmusic("../source/sound.mp3")
                if event.key==                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   pygame.K_LEFT:
                    print("LEFT")
                    arr=getnewassign_Left(arr,width,height)
                    playmusic("../source/sound.mp3")
                if event.key==pygame.K_RIGHT:
                    print("RIGHT")
                    arr=getnewassign_Right(arr,width,height)
                    playmusic("../source/sound.mp3")
            except:
                print("游戏结束!Fail!")
                exit()
    for i in range(0,width):
        for j in range(0,height):
            if arr[j][i]==0:
                color = background
                position = (i * r, j * r, i * r + r, j * r + r)
                pygame.draw.rect(screen, color, position, 0)
                pygame.draw.rect(screen, (0,0,0), position, 1)
            else:
                color = tupiancolor(arr[j][i])
                position = (i * r, j * r, i * r + r, j * r + r)
                pygame.draw.rect(screen, color, position, 0)
                pygame.draw.rect(screen, (0, 0, 0), position, 1)
                pic=addpic(screen, arr, j, i)
                screen.blit(pic,(i*r+0.1*r,j*r+0.1*r))
    pygame.display.update()
           

定义函数-绘制第x行第y列的显示单元的显示情况

#screen代指一个已经定义好的显示
def addpic(screen,arr,x,y):
    color=tupiancolor(arr[x][y])
    font=pygame.font.Font("../font/msyh.ttc",32)
    content=str(arr[x][y])
    text_sf=font.render(content,True,(0,0,0),color)
    return text_sf
           

定义函数-设置操作音

def playmusic(musicpath):
    pygame.mixer.music.load(musicpath)
    pygame.mixer.music.play()
           

算法-实际操作-向上操作示例

import pygame
import random
#随机生成一个值,相当于为一个二维数组中的非0的值,随机挑选一个设置为2或者4
def getrandominfo(str,width,height):
    newstr=[]
    for i in range(0,height):
        for j in range(0,width):
            if str[i][j]==0:
                newstr.append((i,j))
    temp1=random.randint(0,len(newstr)-1)
    temp2=random.randint(1,2)
    loc=newstr[temp1]
    str[loc[0]][loc[1]]=2*temp2
    return str
#定义向上操作事件
def getnewassign_Up(str,width,height):
    for i in range (0,width):
        newstr = []
        newstr1 = []
        #将该列的有数的集合在最上面,0值在最下面
        for j in range(0,height):
            if str[j][i]!=0:
                newstr.append(str[j][i])
        for temp1 in range(len(newstr),height):
            newstr.append(0)
        #执行算法,开始计算,把可以加的都加好
        for a in range(0,len(newstr)-1):
            if newstr[a]==newstr[a+1]:
                newstr[a]=2*newstr[a]
                newstr[a+1]=0
        #重新整理结果,移除零至末尾
        for temp2 in range(0, height):
            if newstr[temp2] != 0:
                newstr1.append(newstr[temp2])
        for temp3 in range(len(newstr1), len(newstr)):
            newstr1.append(0)
        #将我们的缓存数组newstr1中的值,替换数组中,生成最终数组
        for m in range(0,len(newstr)):
            str[m][i]=newstr1[m]
    str=getrandominfo(str,width,height)
    return str
           

页面展示

Python-pygame模块实现2048小游戏-游戏源码Python-pygame模块实现2048小游戏

总结

还存在以下问题,懒得搞了,以后有机会再搞吧~

问题1:初始页面的时候,没有任何欢迎词。

问题2:游戏结束页面中,没有提示信息。

问题3:按键有音量,操作快的时候提示音很烦人,没有制作静音设置。

问题4:没有通关设置,所以这是个无法通关的游戏,也没有记录分数,所以有点没意思。

点击此处下载源码包

最近听了一句话,感觉很好,与大家共勉:

成功不必在我 而功力必不唐捐!