
from tkinter import *
bomb = 100
score = 0
best_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 -= 5
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 update_best_score():
global best_score
best_score += 1
if is_alive():
best_score_label.after(3000, update_best_score)
def click():
global bomb
if is_alive():
bomb += 1
def is_alive():
global bomb
global press_return
if bomb <= 0:
label.config(text='Ban! Ban! Ban')
press_return = True
return False
else:
return True
root = Tk()
root.title('Ban Ban')
root.geometry('500x550')
label = Label(root, text='Press [enter] to start the game', font=('Georgia', 4))
label.pack()
fuse_label = Label(root, text='Fuse: ' + str(bomb), font=('Georgia', 6))
fuse_label.pack()
score_label = Label(root, text='Score: ' + str(score), font=('Georgia', 6))
score_label.pack()
best_score_label = Label(root, text='Best Score:' + str(best_score), font=('Georgia',6))
best_score_label.pack()
if best_score < score:
best_score = score
no_photo = PhotoImage(file='img/bomb_no.png')
normal_photo = PhotoImage(file='img/bomb_normal.png')
bang_photo = PhotoImage(file='img/pow.png')
bomb_label = Label(root, image=normal_photo)
bomb_label.pack()
click_button = Button(root, text='Click me', bg='#000000', fg='#ffffff', width=15,
font=('Georgia', 6), command=click)
click_button.pack()
root.bind('<Return>', start)
root.mainloop()