Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import time
- from random import randrange as rnd, random, choice
- root = Tk()
- fr = Frame(root)
- root.geometry('800x600')
- canv = Canvas(root, bg = 'white')
- canv.pack(fill=BOTH,expand=1)
- colors = ['red', 'orange', 'yellow', 'green', 'blue']
- def click(event):
- j.vx = (event.x - j.x)/10
- j.vy = (event.y - j.y)/10
- canv.bind('<1>',click)
- class Jumper():
- def __init__(self):
- self.x = 40
- self.y = 150
- self.r = 20
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill='orange')
- self.vy = 5
- self.vx = 6
- def move(self):
- self.y += self.vy
- self.x += self.vx
- if self.x > 750:
- self.x = 750
- self.vx *= -1
- if self.x < 50:
- self.x = 50
- self.vx *= -1
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- class Platform():
- def __init__(self):
- self.x = 300
- self.y = 550
- self.ax = 0
- self.w = 120
- self.h = 20
- self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
- self.vx = 0
- self.vy = 0
- if self.x < 50:
- self.ax = 0
- self.vx = 0
- if self.x > 750:
- self.ax = 0
- self.vx = 0
- def plat(self):
- self.vx -= 30
- def auto(self,target):
- print('a')
- if target.x > self.x+self.w/2:
- self.ax = 1.2
- else:
- self.ax = -1.2
- self.move()
- def move(self):
- self.vx += self.ax
- self.x += self.vx
- self.vx *= 0.9
- if self.x+self.w > 750:
- self.x = 750-self.w
- if self.x < 50:
- self.x = 50
- if self.x <= j.x+j.r and j.x-j.r < self.x +self.w and self.y <= j.y+j.r and j.y-j.r <= self.y+self.h:
- j.vy *= -1
- j.vx += (j.x-(self.x+self.w/2))/7
- canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
- j = Jumper()
- pp1 = Platform()
- pp2 = Platform()
- pp2.y = 50
- pp1.y = 550
- def keyDown(event):
- print(event.keycode)
- keys.add(event.keycode)
- def keyUp(event):
- keys.remove(event.keycode)
- root.bind('<Key>',keyDown)
- root.bind('<KeyRelease>',keyUp)
- keys = set()
- while 1:
- pp1.ax = 0
- #pp2.ax = 0
- if 113 in keys:
- pp1.ax = -1
- if 114 in keys:
- pp1.ax = 1
- #if 38 in keys:
- # pp2.ax = -1
- #if 40 in keys:
- # pp2.ax = 1
- j.move()
- pp1.move()
- pp2.auto(j)
- canv.update()
- time.sleep(0.03)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement