相关文章推荐

在python tkinter中制作游戏计时器的问题

0 人关注

我用Python做了一个小游戏,随机地有一个图片改变位置,玩家应该点击图片来获得积分。然而,我想在游戏中设置一个计时器,这样用户就只有例如10秒的时间。

我真的无法让它与mainloop一起工作。

在这段代码中, checker 方法只运行一次, rpl 方法是游戏方法。

import tkinter as tk
import random
import time
point=0
root=tk.Tk()
root.title("Game 2")
root.geometry("450x400")
root.resizable(0,0)
img = tk.PhotoImage(file="dice\\1.png")
canvas = tk.Canvas(root, width=100, height=100)
# canvas.place(x=400,y=100,anchor='center')
canvas.create_image(50,50,image=img,anchor='center')
bt1=tk.Button(text="Start",width=10)
bt1.place(x=0,y=0,anchor=tk.NW)
lbl1=tk.Label(root,text="Score: "+str( point),font=('robot',20))
lbl1.place(x=10,y=370,anchor=tk.W)
lbl2=tk.Label(root,text="Time: "+str(st-en),font=('robot',20))
lbl2.place(x=150,y=370,anchor=tk.W)
def rpl(event):
    global st
    global point
    point+=1
    lbl1.configure(text="Score: "+str( point))
    canvas.place(x=random.randint(75,350),y=random.randint(75,300),anchor='center')
def start():
    global st
    st=time.perf_counter()
    print("START !")
    canvas.place(x=random.randint(75, 350), y=random.randint(75, 300), anchor='center')
def checker():
    total=time.perf_counter()-st
    lbl2.configure(text="Time: " +str(int(total)))
    if(total>=10):
        print ("Game over")
        canvas.configure(width=0,height=0)
        lblend=tk.Label(root,text="Game over\nYou got "+str(point)+" Points")
        lblend.configure(font=('robot',20),fg='Red')
        lblend.place(x=225,y=200,anchor='center')
checker()
canvas.bind('<Button-1>',rpl)
bt1.configure(command=start)
tk.mainloop()
    
python
tkinter
Neverpain
Neverpain
发布于 2021-02-19
2 个回答
norie
norie
发布于 2021-02-19
已采纳
0 人赞同

你可以使用after方法,在指定的毫秒数之后调用一个函数。

要把它作为一个定时器使用,从它调用的函数中再次调用它,直到定时器为0时,你可以调用代码的 "游戏结束 "部分。

你需要取消定时器,并在用户点击骰子时重新启动它。

下面的代码应该可以工作,但可能有一个关于取消定时器的小错误。

Fixed bug with timer speeding up.

import tkinter as tk
import random
import time
countdown = None
en = 0
time = 10
point = 0
root=tk.Tk()
root.title("Game 2")
root.geometry("450x400")
root.resizable(0,0)
img = tk.PhotoImage(file="dice1.png")
canvas = tk.Canvas(root, width=100, height=100)
# canvas.place(x=400,y=100,anchor='center')
canvas.create_image(50,50,image=img,anchor='center')
bt1=tk.Button(text="Start",width=10)
bt1.place(x=0,y=0,anchor=tk.NW)
lbl1=tk.Label(root,text="Score: "+str( point),font=('robot',20))
lbl1.place(x=10,y=370,anchor=tk.W)
lbl2=tk.Label(root,text=f"Time: {time}",font=('robot',20))
lbl2.place(x=150,y=370,anchor=tk.W)
def rpl(event):
    global point, countdown, time
    root.after_cancel(countdown)
    root.after(1000, timer)
    time = 10
    lbl2.configure(text=f'Time: {time}')
    point+=1
    lbl1.configure(text="Score: "+str( point))
    canvas.place(x=random.randint(75,350),y=random.randint(75,300),anchor='center')
def start():
    global countdown
    countdown = root.after(1000, timer)
    print("START !")
    canvas.place(x=random.randint(75, 350), y=random.randint(75, 300), anchor='center')
def timer():
  global time
  if time >0:
    time -= 1
    lbl2.configure(text=f'Time: {time}')
    countdown = root.after(1000, timer)
  else:
    checker()
def checker():   
    canvas.configure(width=0,height=0)
    lblend=tk.Label(root,text="Game over\nYou got "+str(point)+" Points")
    lblend.configure(font=('robot',20),fg='Red')
    lblend.place(x=225,y=200,anchor='center')
canvas.bind('<Button-1>',rpl)
bt1.configure(command=start)
tk.mainloop()
    
非常感谢你。通过改变你的代码,我的问题解决了。我只是在 rpl 中删除了这些。 root.after(1000, timer) time = 10` lbl2.configure(text=f'Time: {time}') 。 我现在可以工作了!非常感谢你
Neverpain
Neverpain
发布于 2021-02-19
0 人赞同

感谢@norie,问题解决了。我只是从他的代码中删除了 rpl 中的3行。

def rpl(event):
    global point, countdown, time
    root.after_cancel(countdown)
    root.after(1000, timer)
    time = 10
    lbl2.configure(text=f'Time: {time}')
 
推荐文章