Shcoder001

Python Santa Game

Dec 16th, 2022
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | Gaming | 0 0
  1. import random
  2. from tkinter import *
  3.  
  4.  
  5. def move_santa(event):
  6.     if event.keysym == 'a':
  7.         canvas.move(santa, -10, 0)
  8.     if event.keysym == 'd':
  9.         canvas.move(santa, 10, 0)
  10.     if event.keysym == 'w':
  11.         canvas.move(santa, 0, -10)
  12.     if event.keysym == 's':
  13.         canvas.move(santa, 0, 10)
  14.     check()
  15.  
  16.  
  17. def check():
  18.     santa_cords = canvas.coords(santa)
  19.     for present in presents:
  20.         pr_cords = canvas.coords(present)
  21.         if pr_cords[0] - 40 < santa_cords[0] and pr_cords[0] + 40 > santa_cords[0] and pr_cords[1] - 40 < santa_cords[
  22.             1] and pr_cords[1] + 40 > santa_cords[1]:
  23.             canvas.delete(present)
  24.             presents.remove(present)
  25.  
  26.  
  27. presents = []
  28.  
  29. window = Tk()
  30. window.title('New Year App')
  31.  
  32. santa_image = PhotoImage(file='santa.png').subsample(15, 15)
  33.  
  34. present_image = PhotoImage(file='present.png').subsample(15, 15)
  35.  
  36. canvas = Canvas(window, width=700, height=700, bg='wheat2')
  37. canvas.pack()
  38.  
  39. santa = canvas.create_image(100, 100, image=santa_image)
  40. for i in range(10):
  41.     presents.append(canvas.create_image(random.randint(50, 650), random.randint(50, 650), image=present_image))
  42.  
  43. canvas.bind_all('<Key>', move_santa)
  44.  
  45. window.mainloop()
  46.  
Advertisement
Add Comment
Please, Sign In to add comment