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']
- class Block():
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.h = 20
- self.w = 60
- self.k = rnd(5)
- self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill=colors[self.k])
- def kick(self):
- if self.k > 0:
- self.k -= 1
- else:
- self.kill()
- def move(self):
- ball = j
- x = self.x + self.w/2
- y = self.y + self.h/2
- d = ''
- if self.x < ball.x < self.x+self.w and self.y-ball.r < ball.y < self.y + self.h + ball.r:
- d = 'vy'
- if ball.x-x != 0:
- h = ((ball.x - x)**2 + (ball.y - y)**2)**0.5
- a = abs((ball.y - y)/(ball.x-x))
- if a < 0.25 :
- if self.w/3 < h < self.w/1.5:
- d = 'vx'
- if d == 'vy':
- ball.vy *= -1
- self.kick()
- elif d == 'vx':
- ball.vx *= -1
- self.kick()
- canv.itemconfig(self.id,fill=colors[self.k])
- def kill(self):
- blocks.remove(self)
- canv.delete(self.id)
- class Jumper():
- def __init__(self):
- self.x = 40
- self.y = 150
- self.r = 8
- 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 = 6
- self.vx = 9
- def move(self):
- self.x += self.vx
- self.y += self.vy
- if self.y < 50:
- self.y = 50
- self.vy *= -1
- if self.x < 50:
- self.x = 50
- self.vx *= -1
- if self.x > 750:
- self.x = 750
- self.vx *= -1
- if self.y > 600:
- self.y = 600
- self.vy *= -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 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
- canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
- j = Jumper()
- x = 100
- y = 100
- blocks = []
- for i in range(10):
- for ii in range(10):
- blocks.append(Block(x,y))
- x += 62
- y += 22
- x = 100
- h = 40
- #Block(400,300)
- p = Platform()
- def keyDown(event):
- keys.add(event.keycode)
- def keyUp(event):
- keys.remove(event.keycode)
- def move(event):
- j.x = event.x
- j.y = event.y
- j.move()
- root.bind('<Key>',keyDown)
- root.bind('<KeyRelease>',keyUp)
- #canv.bind('<Motion>',move)
- keys = set()
- while 1:
- p.ax = 0
- if 113 in keys:
- p.ax = -2
- if 114 in keys:
- p.ax = 2
- j.move()
- p.move()
- for block in blocks:
- block.move()
- if p.x <= j.x+j.r and j.x-j.r < p.x +p.w and p.y <= j.y+j.r and j.y-j.r <= p.y+p.h:
- j.vy *= -1
- j.vx = (j.x - (p.x+p.w/2))/7
- canv.update()
- time.sleep(0.03)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement