Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3.  
  4. vx=10
  5. vy=0
  6.  
  7. def move():
  8. canvas.move(head, vx, vy)
  9. window.after(50,move)
  10. cc = canvas.coords(coin)
  11. if canvas.coords(head) == cc:
  12. tx = random.randrange(0,50)*10
  13. ty = random.randrange(0,50)*10
  14. canvas.move(coin, tx-cc[0], ty-cc[1])
  15.  
  16.  
  17. def go_left(e):
  18. global vx
  19. global vy
  20. vx=-10
  21. vy=0
  22.  
  23. def go_right(e):
  24. global vx
  25. global vy
  26. vx=10
  27. vy=0
  28.  
  29. def go_up(e):
  30. global vx
  31. global vy
  32. vx=0
  33. vy=-10
  34.  
  35. def go_down(e):
  36. global vx
  37. global vy
  38. vx=0
  39. vy=10
  40.  
  41. window= Tk()
  42. canvas = Canvas(window, width=500,height=500,background="white")
  43. canvas.pack()
  44. #for i in range(50):
  45. # canvas.create_line(i*10,0,i*10,500,fill ="#777777")
  46. # canvas.create_line(0,i*10,500,i*10)
  47. head = canvas.create_rectangle(250,250,260,260,fill='magenta')
  48.  
  49. window.after(100,move)
  50.  
  51. window.bind('<Left>', go_left)
  52. window.bind('<Right>', go_right)
  53. window.bind('<Up>', go_up)
  54. window.bind('<Down>', go_down)
  55. tx = random.randrange(0,50)*10
  56. ty = random.randrange(0,50)*10
  57. coin=canvas.create_rectangle(tx,ty,tx+10,ty+10,fill='lightblue')
  58.  
  59. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement