Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- vx=10
- vy=0
- def move():
- canvas.move(head, vx, vy)
- window.after(50,move)
- cc = canvas.coords(coin)
- if canvas.coords(head) == cc:
- tx = random.randrange(0,50)*10
- ty = random.randrange(0,50)*10
- canvas.move(coin, tx-cc[0], ty-cc[1])
- def go_left(e):
- global vx
- global vy
- vx=-10
- vy=0
- def go_right(e):
- global vx
- global vy
- vx=10
- vy=0
- def go_up(e):
- global vx
- global vy
- vx=0
- vy=-10
- def go_down(e):
- global vx
- global vy
- vx=0
- vy=10
- window= Tk()
- canvas = Canvas(window, width=500,height=500,background="white")
- canvas.pack()
- #for i in range(50):
- # canvas.create_line(i*10,0,i*10,500,fill ="#777777")
- # canvas.create_line(0,i*10,500,i*10)
- head = canvas.create_rectangle(250,250,260,260,fill='magenta')
- window.after(100,move)
- window.bind('<Left>', go_left)
- window.bind('<Right>', go_right)
- window.bind('<Up>', go_up)
- window.bind('<Down>', go_down)
- tx = random.randrange(0,50)*10
- ty = random.randrange(0,50)*10
- coin=canvas.create_rectangle(tx,ty,tx+10,ty+10,fill='lightblue')
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement