Untitled

 avatar
user_9733620
plain_text
a year ago
2.1 kB
6
Indexable
from tkinter import*

bomb = 100
score = 0
press_return = True

def start(event):
    global press_return
    global bomb
    global score
    if not press_return:
        pass
    else:
        bomb = 100
        score =0
        label.config(text="")
        update_bomb()
        update_score()
        update_display()
        press_return = False

def update_display():
    global bomb
    global score
    if bomb > 50:
        bomb_label.config(image=normal_photo)
    elif 0<bomb<=50:
        bomb_label.config(image=no_photo)
    else:
        bomb_label.config(image=bang_photo)
    fuse_label.config(text="Fuse: "+str(bomb))
    score_label.config(text="Score: "+str(score))
    fuse_label.after(100,update_display)

def update_bomb():
    global bomb
    bomb-=3
    if is_alive():
        fuse_label.after(400,update_bomb)

def update_score():
    global score
    score += 1
    if is_alive():
        score_label.after(3000, update_score)

def click():
    global bomb
    if is_alive():
        bomb+=1

def is_alive():
    global bomb
    global press_return
    if bomb <=0:
        label.config(text="Game Over!")
        press_return = True
        return False
    else:
        return True

root = Tk()
root.title("Bang Bang")
root.geometry("500x550")

label = Label(root,text="Press [Enter] to start the game",
        font=("Comic Sans MS",12,"italic","bold"))
label.pack()

fuse_label = Label(root,text="Fuse: "+str(bomb),
            font=("Comic Sans MS",14))
fuse_label.pack()

score_label = Label(root,text="Score: "+str(score),
            font=("Comic Sans MS",14))
score_label.pack()

no_photo = PhotoImage(file = "img/bomb_no.gif")
normal_photo = PhotoImage(file="img/bomb_normal.gif")
bang_photo = PhotoImage(file="img/pow.gif")

bomb_label = Label(root,image=normal_photo)
bomb_label.pack()

click_button = Button(root,text="Click me",bg="#000000",fg="#ffffff",
            font=("Comic Sans MS",14),width=15,command=click)
click_button.pack()

root.bind('<Return>',start)
root.mainloop()
Editor is loading...
Leave a Comment