Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用TKINTER在python内置的游戏中添加关卡?_Python_Python 3.x_Tkinter_Tkinter Canvas - Fatal编程技术网

如何使用TKINTER在python内置的游戏中添加关卡?

如何使用TKINTER在python内置的游戏中添加关卡?,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,我正在做一个记忆游戏,你必须找到红球的位置,一旦你移动你的球,红球就会消失,所以为了得到更多的分数,你必须尽可能快地移动,我的问题是如何添加更多的级别,例如级别1:找到5个红球的位置,级别2找到10个位置。 下面是代码: ''' 从tkinter导入* 从随机导入* 导入时间 全球玩家;全球目标;全球n;全球c;全球得分 全球目标;全球游戏时间 得分=0;posPlayer=[0,0];n=15;c=50 ################轮班管理#########################

我正在做一个记忆游戏,你必须找到红球的位置,一旦你移动你的球,红球就会消失,所以为了得到更多的分数,你必须尽可能快地移动,我的问题是如何添加更多的级别,例如级别1:找到5个红球的位置,级别2找到10个位置。 下面是代码: '''

从tkinter导入*
从随机导入*
导入时间
全球玩家;全球目标;全球n;全球c;全球得分
全球目标;全球游戏时间
得分=0;posPlayer=[0,0];n=15;c=50
################轮班管理#################################
def权限(事件=无):
posPlayer[0]+=50
如果posPlayer[0]>700:
posPlayer[0]=700
副主席(右)
def dep_right():
c1.删除(全部)
rond=c1.创建椭圆(posPlayer[0],posPlayer[1],posPlayer[0]+c,posPlayer[1]+c,fill='#fff')
play()
def left(事件=无):
posPlayer[0]-=50
如果posPlayer[0]700:
posPlayer[1]=700
副署长
def dep_down():
c1.删除(全部)
rond=c1.创建椭圆(posPlayer[0],posPlayer[1],posPlayer[0]+c,posPlayer[1]+c,fill='#fff')
play()
def up(事件=无):
posPlayer[1]=50

如果posPlayer[1]在我了解了这个游戏的意义之后,我想我找到了解决方案:

if nbBut==5:
是决定游戏是否结束的实例。因此,第一步是使comperator变量并将其设置得更高,如:

goal = IntVar()

def jouer():
    global score
    if posJoueur[0]==posBut[0] and posJoueur[1]==posBut[1]:
        if nbBut==goal:
            ...
            goal.set(goal.get()+1)
完整代码,如果此处缺少某些内容,请通知我:

from tkinter import *
from random import *
import time

global posJoueur ; global posBut ; global n ; global c ; global score
global nbBut ; global gameTime

score=0 ; posJoueur=[0,0] ; n=15 ; c=50

'########### #### Gestion des déplacements #################################'

def droite(event = None):
    posJoueur[0]+=50
    if posJoueur[0]>700:
        posJoueur[0]=700
    dep_droite()

def dep_droite():
    c1.delete(ALL)
    rond= c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()

def gauche(event = None):
    posJoueur[0]-=50
    if posJoueur[0]<0:
        posJoueur[0]=0
    dep_gauche()

def dep_gauche():
    c1.delete(ALL)
    rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()

def bas(event = None):
    posJoueur[1]+=50
    if posJoueur[1]>700:
        posJoueur[1]=700
    dep_bas()

def dep_bas():
    c1.delete(ALL)
    rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()

def haut(event = None):
    posJoueur[1]-=50
    if posJoueur[1]<0:
        posJoueur[1]=0
    dep_haut()

def dep_haut():
    c1.delete(ALL)
    rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()


'################# Gestion du jeu #########################################'

def commencerJeu():
    global score ; global gameTime ; global nbBut ; global posJoueur
    global posBut
    nbBut=0
    gameTime=[time.time(),0]
    score=0
    posJoueur = [0, 0]
    posBut = [0, 0]
    But()

def But():
    global nbBut
    nbBut =nbBut+ 1
    posBut[0]=randint(0,14)*50
    posBut[1]=randint(0,14)*50
    but=c1.create_oval(posBut[0],posBut[1],posBut[0]+c,posBut[1]+c, fill="red")
    jouer()

def jouer():
    global score
    if posJoueur[0]==posBut[0] and posJoueur[1]==posBut[1]:
        if nbBut==goal.get():
            gameTime[1]=time.time()
            score=round(1/(gameTime[1]-gameTime[0])*1000,2)
            affichageScore.set("Score =" +str(score))
            c1.create_text(c*n/2, c*n/2, font="Purisa",
                               text="Jeu terminé !",fill="red")
            c1.pack()
            goal.set(goal.get()+1)
        else:
            But()


'################# programme principale ####################################'

f1=Tk()
f1.title("Python Game Challenge")
f1.bind("<Right>", droite) ; f1.bind("<Left>", gauche)
f1.bind("<Down>", bas) ; f1.bind("<Up>", haut)

l1 = Label(f1, text="Bienvenu dans mon jeu !")
l1.pack(side="top",ipadx=30,ipady=30)

affichageScore=StringVar()
affichageScore.set("Score =00.00")
l2 = Label(f1, textvariable=affichageScore)
l2.pack(side="right",ipadx=10,ipady=10)

b1=Button(f1,text="Jouer",fg='yellow',bg="black", command=commencerJeu)
b1.pack(side="left",ipadx=10,ipady=10)

b2=Button(f1,text="Fermer",fg='yellow',bg="black",command=f1.destroy)
b2.pack(side="left",ipadx=10,ipady=10)

c1=Canvas(f1,width=n*c,height=n*c, bg="black")
rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
c1.pack()

goal = IntVar()
goal.set(1)

f1.mainloop()
从tkinter导入*
从随机导入*
导入时间
全球装腔作势;全球posBut;全球n;全球c;全球得分
全球nbBut;全球游戏时间
得分=0;Posjouer=[0,0];n=15;c=50
本月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日##############
def droite(事件=无):
Posjouer[0]+=50
如果Posjouer[0]>700:
Posjouer[0]=700
副署长()
def dep_droite():
c1.删除(全部)
rond=c1.创建椭圆(posjouer[0],posjouer[1],posjouer[0]+c,posjouer[1]+c,fill='#fff')
jouer()
def gauche(事件=无):
Posjouer[0]=50
如果Posjouer[0]700:
Posjouer[1]=700
副秘书
def dep_bas():
c1.删除(全部)
rond=c1.创建椭圆(posjouer[0],posjouer[1],posjouer[0]+c,posjouer[1]+c,fill='#fff')
jouer()
def haut(事件=无):
Posjouer[1]=50

如果Posjouer[1]我知道这需要大量的工作,但是你能把代码翻译成英语吗,因为我不知道任何函数都能做什么。好的,没问题,这里是英文的:它可以工作,兄弟,谢谢你,但是你能告诉我如何在画布上显示每一个新的级别吗?
from tkinter import *
from random import *
import time

global posJoueur ; global posBut ; global n ; global c ; global score
global nbBut ; global gameTime

score=0 ; posJoueur=[0,0] ; n=15 ; c=50

'########### #### Gestion des déplacements #################################'

def droite(event = None):
    posJoueur[0]+=50
    if posJoueur[0]>700:
        posJoueur[0]=700
    dep_droite()

def dep_droite():
    c1.delete(ALL)
    rond= c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()

def gauche(event = None):
    posJoueur[0]-=50
    if posJoueur[0]<0:
        posJoueur[0]=0
    dep_gauche()

def dep_gauche():
    c1.delete(ALL)
    rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()

def bas(event = None):
    posJoueur[1]+=50
    if posJoueur[1]>700:
        posJoueur[1]=700
    dep_bas()

def dep_bas():
    c1.delete(ALL)
    rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()

def haut(event = None):
    posJoueur[1]-=50
    if posJoueur[1]<0:
        posJoueur[1]=0
    dep_haut()

def dep_haut():
    c1.delete(ALL)
    rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
    jouer()


'################# Gestion du jeu #########################################'

def commencerJeu():
    global score ; global gameTime ; global nbBut ; global posJoueur
    global posBut
    nbBut=0
    gameTime=[time.time(),0]
    score=0
    posJoueur = [0, 0]
    posBut = [0, 0]
    But()

def But():
    global nbBut
    nbBut =nbBut+ 1
    posBut[0]=randint(0,14)*50
    posBut[1]=randint(0,14)*50
    but=c1.create_oval(posBut[0],posBut[1],posBut[0]+c,posBut[1]+c, fill="red")
    jouer()

def jouer():
    global score
    if posJoueur[0]==posBut[0] and posJoueur[1]==posBut[1]:
        if nbBut==goal.get():
            gameTime[1]=time.time()
            score=round(1/(gameTime[1]-gameTime[0])*1000,2)
            affichageScore.set("Score =" +str(score))
            c1.create_text(c*n/2, c*n/2, font="Purisa",
                               text="Jeu terminé !",fill="red")
            c1.pack()
            goal.set(goal.get()+1)
        else:
            But()


'################# programme principale ####################################'

f1=Tk()
f1.title("Python Game Challenge")
f1.bind("<Right>", droite) ; f1.bind("<Left>", gauche)
f1.bind("<Down>", bas) ; f1.bind("<Up>", haut)

l1 = Label(f1, text="Bienvenu dans mon jeu !")
l1.pack(side="top",ipadx=30,ipady=30)

affichageScore=StringVar()
affichageScore.set("Score =00.00")
l2 = Label(f1, textvariable=affichageScore)
l2.pack(side="right",ipadx=10,ipady=10)

b1=Button(f1,text="Jouer",fg='yellow',bg="black", command=commencerJeu)
b1.pack(side="left",ipadx=10,ipady=10)

b2=Button(f1,text="Fermer",fg='yellow',bg="black",command=f1.destroy)
b2.pack(side="left",ipadx=10,ipady=10)

c1=Canvas(f1,width=n*c,height=n*c, bg="black")
rond = c1.create_oval(posJoueur[0],posJoueur[1],posJoueur[0]+c, posJoueur[1]+c,fill='#fff')
c1.pack()

goal = IntVar()
goal.set(1)

f1.mainloop()