Python mini game

1. Program code

1. Game introduction

Happy Match is a elimination game, suitable for leisure and entertainment time. As long as we click on two blocks of the same color, the blocks will disappear. Until all the blocks of the color disappear, we will successfully pass the level and enter the next level.

Insert image description here

2. Game code

# 导入我们需要用到的库
import turtle as tt
import random as ra
import time as ti

# 设置关卡n从第一关开始
n=1
isck=0
tt.tracer(1,0)
tpencolor='deeppink,darkorange,brown,fuchsia,olive,aqua,red,green,yellow,blue,purple,salmon,sandybrown,skyblue,plum,coral,blueviolet,darkgoldenrod'
colors=tpencolor.split(',')
isonclick=True
scr=tt.Screen()
# 创建分数
fenshu=[tt.Turtle(),-1,1]
fenshu[0].ht()
fenshu[0].up()
fenshu[0].sety(-380)
fenshu[0].down()

onclicknum=1
onclickTurtle=tt.Turtle()
onclickTurtle.ht()
turtles=[]
def fenshus():
    global fenshu
    fenshu[1]=fenshu[1]+fenshu[2]
    fenshu[0].undo()
    fenshu[0].write('你的分数为:'+str(fenshu[1])+'分',align='center',font=('宋体',20))
fenshus()
def creategame(lve):
    global turtles
    global colors
    if len(turtles)!=0:
        for i in turtles:
            i['tpen'].clear()
        del turtles[0:len(turtles)]
    x=-25-55*lve
    y=25+55*lve
    tpenid=0
    colorsid=0
    for i in range(lve*2+1):
        for j in range(lve*2+1):
            tpen=tt.Turtle()
            tpen.ht()
            tpen.speed(0)
            tpencolor='silver'
            if tpenid%2==0 and tpenid!=0:
                colorsid=colorsid+1
            tpenfillcolor=colors[colorsid%len(colors)]
            tpenxy=(x+i*55,y-j*55)
            tpenisshow=True
            tpenid=tpenid+1
            # 画笔,画笔颜色,填充颜色,画笔初始坐标,画笔是否显示,画笔编号
            tpenType={
    
    'tpen':tpen,'tpencolor':tpencolor,'tpenfillcolor':tpenfillcolor,'tpenxy':tpenxy,'tpenisshow':tpenisshow,'tpenid':tpenid}
            turtles.append(tpenType)
def suiji(turtles):
    for i in turtles:
        # 让每一个颜色---begin---随机去一个地方
        colors=i['tpenfillcolor']
        suiji=ra.randint(0,len(turtles)-1)
        i['tpenfillcolor']=turtles[suiji]['tpenfillcolor']
        turtles[suiji]['tpenfillcolor']=colors
        # ---end---
def maingame(turtles):
    for i in range(len(turtles)):
        if turtles[i]['tpenisshow']==False:
            continue
        maingameid(i)
def maingameid(turtleid):
    turtles[turtleid]['tpen'].color(turtles[turtleid]['tpencolor'])
    turtles[turtleid]['tpen'].fillcolor(turtles[turtleid]['tpencolor'])
    turtles[turtleid]['tpen'].up()
    turtles[turtleid]['tpen'].goto(turtles[turtleid]['tpenxy'])
    turtles[turtleid]['tpen'].down()
    turtles[turtleid]['tpen'].begin_fill()
    for j in range(4):
        turtles[turtleid]['tpen'].fd(50)
        turtles[turtleid]['tpen'].right(90)
    turtles[turtleid]['tpen'].end_fill()

def maingameids(turtleid):
    turtles[turtleid]['tpen'].color(turtles[turtleid]['tpenfillcolor'])
    turtles[turtleid]['tpen'].fillcolor(turtles[turtleid]['tpenfillcolor'])
    turtles[turtleid]['tpen'].up()
    turtles[turtleid]['tpen'].goto(turtles[turtleid]['tpenxy'])
    turtles[turtleid]['tpen'].down()
    turtles[turtleid]['tpen'].begin_fill()
    for j in range(4):
        turtles[turtleid]['tpen'].fd(50)
        turtles[turtleid]['tpen'].right(90)
    turtles[turtleid]['tpen'].end_fill()

def showcolor(turtles):
    for i in range(len(turtles)):
        if turtles[i]['tpenisshow']==False:
            continue
        maingameids(i)


def maingameidss(turtleid):
    turtles[turtleid]['tpen'].color(turtles[turtleid]['tpenfillcolor'])
    turtles[turtleid]['tpen'].up()
    turtles[turtleid]['tpen'].goto(turtles[turtleid]['tpenxy'])
    turtles[turtleid]['tpen'].down()
    for j in range(4):
        turtles[turtleid]['tpen'].fd(50)
        turtles[turtleid]['tpen'].right(90)

def showpencolor(turtles):
    for i in range(len(turtles)):
        if turtles[i]['tpenisshow']==False:
            continue
        maingameidss(i)

def isY():
    global turtles
    global n
    count=0
    for i in turtles:
        if i['tpenisshow']==True:
            count+=1
        if count>=2:
            break
    if count<=1:
        n=n+1
        global fenshu
        fenshu[2]=fenshu[2]+1
        creategame(n)
        suiji(turtles)
        showcolor(turtles)
        ti.sleep(3)
        maingame(turtles)
    tt.ontimer(isY,1000)
def onclickTurtle(x,y):
    global isonclick
    global isck
    if isonclick==False:
        return
    isonclick=False
    global onclicknum
    global onclickTurtle
    global turtles
    global n
    for i in range(len(turtles)):
        if turtles[i]['tpenisshow']==True and turtles[i]['tpen'].pos()[0]<=x and x<=turtles[i]['tpen'].pos()[0]+50 and turtles[i]['tpen'].pos()[1]>=y and y>=turtles[i]['tpen'].pos()[1]-50:
            turtles[i]['tpen'].color(turtles[i]['tpencolor'])
            turtles[i]['tpen'].fillcolor(turtles[i]['tpenfillcolor'])
            turtles[i]['tpen'].begin_fill()
            for z in range(4):
                turtles[i]['tpen'].fd(50)
                turtles[i]['tpen'].right(90)
            turtles[i]['tpen'].end_fill()
            if onclicknum%2==0 and onclicknum != 0:
                if turtles[i]['tpen'].fillcolor()==turtles[onclickTurtle]['tpen'].fillcolor() and turtles[i]['tpenid']!=turtles[onclickTurtle]['tpenid']:
                    ti.sleep(0.4)
                    turtles[i]['tpenisshow']=False
                    turtles[i]['tpen'].clear()
                    turtles[onclickTurtle]['tpenisshow']=False
                    turtles[onclickTurtle]['tpen'].clear()
                    fenshus()
                    if isck==0:
                        maingame(turtles)
                    isck=1
                else:
                    ti.sleep(0.4)
                    maingameid(i)
                    maingameid(onclickTurtle)
                    if isck>=10:
                        showpencolor(turtles)
                        isck=0
                    elif isck==0:
                        maingame(turtles)
                        isck=1
                    else:
                        isck+=1
            else:
                onclickTurtle=i
            onclicknum=onclicknum+1
            break
    isonclick=True
creategame(n)
suiji(turtles)
showcolor(turtles)
ti.sleep(3)
maingame(turtles)

scr.onclick(onclickTurtle)
isY()
# 设置当用户按下退出才能退出
tt.mainloop()

2. Package the program into an exe program

1. Install pyinstaller

If you have pip installed, just enter: pip install pyinstaller in cmd and wait a moment for the installation to be completed.

2. Package the python file into an exe program

1. First, we enter the directory where the python file we want to package is located, and copy the path.
  2. Then open cmd and enter the directory where the python file is located.
  cd 路径
  Insert image description here

3. Package error reporting

Enter the command:pyinstaller -F 文件名.py Run. If the package is a project, then the file name must be the entry file of the project, otherwise an error may occur after the packaging is completed.

If an error is reportedThe 'enum34' package is an obsolete backport of a standard library package and is incompatible with PyInstaller. Please conda remove enum34 then try again.
Insert image description here

Solution: Open a new cmd and enterpip uninstall enum34 to uninstall the enum34 package.
Insert image description here
Repackaging was successful.
Insert image description here
Insert image description here
The program directory has two additional folders, build and dist, and 消消乐.spec files. The exe program we want to run is in the dist file. Clip inside.
Insert image description here

Double-click this program, and the following picture will appear:

Insert image description here
After waiting for a while, the colored squares will turn gray. Click on the squares of the same color and the squares will disappear. After all is completed, upgrade to the next level.
Insert image description here

Guess you like

Origin blog.csdn.net/m0_46688827/article/details/128912192