Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_speedy_car.py
- import Tkinter
- from PIL import ImageGrab
- import random
- root=Tkinter.Tk()
- ww=root.winfo_screenwidth()
- hh=root.winfo_screenheight()
- root.geometry(str(ww+10)+"x"+str(hh+10)+"+-10+-32") # to hide the frame
- canvas=Tkinter.Canvas(width=ww, height=hh, bg='green')
- canvas.pack()
- momentum = 0
- carbox=10
- road=[ww/2-50]*(hh/carbox)
- def draw_road():
- canvas.delete('road')
- for i in range(len(road)):
- canvas.create_rectangle(road[i],i*10,road[i]+100,i*10+10, fill='white', outline='', tags='road')
- def draw_car():
- canvas.delete('car')
- c='black'
- bbox = ImageGrab.grab([carx-3,cary+7,carx+10,cary+8])
- t=[bbox.getpixel((0, 0)), bbox.getpixel((12, 0))]
- if (0,255,0) in t:
- c='red'
- canvas.create_rectangle(carx,cary,carx+10,cary+16, fill=c, tags='car')
- def veer_left(z):
- global carx
- carx-=10
- def veer_right(z):
- global carx
- carx+=10
- def animate():
- apply_road()
- draw_road()
- draw_car()
- canvas.after(3, animate)
- def apply_road():
- global momentum
- momentum=min(8,max(-8,momentum))
- if road[0]+momentum<500:
- momentum+=0.075
- roadside=road[0]+momentum
- elif road[0]+momentum>ww-600:
- momentum-=0.075
- roadside=road[0]+momentum
- else:
- momentum+=random.choice([-0.1,0.1,-0.2,0.2,-0.25,0.25])
- roadside=road[0]+momentum
- road.insert(0, roadside)
- road.pop()
- draw_road()
- carx=ww/2-5
- cary=hh-100
- draw_car()
- animate()
- canvas.bind_all('<Left>',veer_left)
- canvas.bind_all('<Right>',veer_right)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement