Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from tkinter import *
- def move_santa(event):
- if event.keysym == 'a':
- canvas.move(santa, -10, 0)
- if event.keysym == 'd':
- canvas.move(santa, 10, 0)
- if event.keysym == 'w':
- canvas.move(santa, 0, -10)
- if event.keysym == 's':
- canvas.move(santa, 0, 10)
- check()
- def check():
- santa_cords = canvas.coords(santa)
- for present in presents:
- pr_cords = canvas.coords(present)
- if pr_cords[0] - 40 < santa_cords[0] and pr_cords[0] + 40 > santa_cords[0] and pr_cords[1] - 40 < santa_cords[
- 1] and pr_cords[1] + 40 > santa_cords[1]:
- canvas.delete(present)
- presents.remove(present)
- presents = []
- window = Tk()
- window.title('New Year App')
- santa_image = PhotoImage(file='santa.png').subsample(15, 15)
- present_image = PhotoImage(file='present.png').subsample(15, 15)
- canvas = Canvas(window, width=700, height=700, bg='wheat2')
- canvas.pack()
- santa = canvas.create_image(100, 100, image=santa_image)
- for i in range(10):
- presents.append(canvas.create_image(random.randint(50, 650), random.randint(50, 650), image=present_image))
- canvas.bind_all('<Key>', move_santa)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment