[Python] How can I make it so the create_oval changes color when Teleport is in cooldown?

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm been trying to make a simple game of tag on python, for balance I've decided that tagger (Purple) should be bigger and the other one (yellow) smaller, but faster, and I added an ability for yellow to tp to a random space.

    The game works fine, but I've been trying to make it so when Tp is on cooldown, yellow should turn Red, and when cooldown is over, to turn back to yellow.

    from tkinter import *
    import sys
    import math
    import time
    import random
    import math
    from tkinter import messagebox
    from datetime import datetime

    cooldown={}
    Tp_Usage=0
    def move_up(event):
    if canva.coords(j1)[1]>5:
    canva.move(j1,0,-20)
    check_collision()
    def move_down(event):
    if canva.coords(j1)[3]<canva.winfo_height()-5:
    canva.move(j1,0,20)
    check_collision()
    def move_right(event):
    if canva.coords(j1)[2]<canva.winfo_width()-5:
    canva.move(j1,20,0)
    check_collision()
    def move_left(event):
    if canva.coords(j1)[0]>5:
    canva.move(j1,-20,0)
    check_collision()
    def update(action, cool):
    cooldown.update({action: time.time() + cool})
    def validate(action):
    return cooldown.get(action, 0) - time.time() <= 0
    def Teleport(event):
    global Tp_Usage
    if validate("Teleport") and Tp_Usage < 5:
    Tp1=round(random.randint(0,490),-1)
    Tp2=round(random.randint(0,490),-1)
    canva.coords(j2,Tp1,Tp2,Tp1+10,Tp2+10)
    update("Teleport", 2.5)
    Tp_Usage += 1
    L1.config(text=f"Tp: {Tp_Usage}/5")
    if Tp_Usage == 5:
    L1.config(text=f"Tp: {Tp_Usage}/5", fg="red")
    check_collision()

    def move_up2(event):
    if canva.coords(j2)[1]>5:
    canva.move(j2,0,-10)
    check_collision()
    def move_down2(event):
    if canva.coords(j2)[3]<canva.winfo_height()-5:
    canva.move(j2,0,10)
    check_collision()
    def move_right2(event):
    if canva.coords(j2)[2]<canva.winfo_width()-5:
    canva.move(j2,10,0)
    check_collision()
    def move_left2(event):
    if canva.coords(j2)[0]>5:
    canva.move(j2,-10,0)
    check_collision()

    def salir(event):
    sys.exit()

    def distance(coord1, coord2):
    x1, y1 = coord1
    x2, y2 = coord2
    return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

    def check_collision():
    global inicio

    coord1 = canva.coords(j1)
    coord2 = canva.coords(j2)
    center1 = (coord1[0] + coord1[2]) / 2, (coord1[1] + coord1[3]) / 2
    center2 = (coord2[0] + coord2[2]) / 2, (coord2[1] + coord2[3]) / 2
    if distance(center1, center2) < 15:
    tiempo=datetime.now()-inicio
    messagebox.showinfo("Pillado!", "Has tardado "+str(tiempo))
    sys.exit()

    v=Tk()
    v.resizable(width=False, height=False)

    v.bind("<Up>",move_up)
    v.bind("<Down>",move_down)
    v.bind("<Left>",move_left)
    v.bind("<Right>",move_right)
    v.bind("<w>",move_up2)
    v.bind("<s>",move_down2)
    v.bind("<a>",move_left2)
    v.bind("<d>",move_right2)
    v.bind("<Shift_L>",Teleport)
    v.bind("<Escape>",salir)

    L1=Label(v,text=f"Tp: {Tp_Usage}/5")
    L1.pack()
    canva= Canvas(v,width=500, height=500,bg="grey")
    canva.pack()
    j1 = canva.create_oval(40,40,60,60,fill="purple")
    j2 = canva.create_oval(440,440,450,450,fill="Yellow")
    inicio=datetime.now()
    v.mainloop()


    I've tried to change it's color inside the validate and update, thinking that it might work, (didn't) tried to put an if inside j2 and inside of Teleport, tried to make a loop where the program constantly checks the cooldown timer, and nothing.

    Continue reading...

Compartilhe esta Página