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
- from math import sin, cos, asin
- root = Tk()
- fr = Frame(root)
- root.geometry('800x600')
- canv = Canvas(root, bg = 'white')
- canv.pack(fill=BOTH,expand=1)
- colors = ['yellow', 'orange', 'red', 'green', 'blue']
- bonuses = ('fire','long','short','fast','slow','haos')
- bonuses = ('haos',)
- class Bonus():
- def __init__(self,x,y):
- self.x = x+30
- self.y = y
- self.bonus = choice(bonuses)
- self.vx = 0
- self.vy = 3
- self.id = canv.create_text(self.x,self.y,text = self.bonus)
- self.move()
- def move(self):
- self.y += self.vy
- self.x += self.vx
- canv.coords(self.id,self.x,self.y)
- if p.x < self.x < p.x + p.w and p.y < self.y < p.y+p.h:
- p.bonus(self.bonus)
- self.kill()
- else:
- if y < 600:
- root.after(30,self.move)
- else:
- self.kill()
- def kill(self):
- canv.delete(self.id)
- class Block():
- def __init__(self,x,y,n):
- self.x = x
- self.y = y
- self.h = 20
- self.w = 60
- self.k = n
- 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 check(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.2:
- d = 'vx'
- if d == 'vy':
- if not ball.isFire:
- if not ball.isHaos:
- ball.vy *= -1
- else:
- v = (ball.vx**2+ball.vy**2)**0.5
- a = asin(ball.vy/v)
- a = 1+random()
- ball.vy = sin(a)*v
- ball.vx = cos(a)*v
- self.kick()
- return True
- elif d == 'vx':
- if not ball.isFire:
- if not ball.isHaos:
- ball.vx *= -1
- else:
- v = (ball.vx**2+ball.vy**2)**0.5
- a = asin(ball.vy/v)
- a = 1+random()
- ball.vy = sin(a)*v
- ball.vx = cos(a)*v
- self.kick()
- return True
- canv.itemconfig(self.id,fill=colors[self.k])
- def kill(self):
- Bonus(self.x,self.y)
- blocks.remove(self)
- canv.delete(self.id)
- class Jumper():
- def __init__(self):
- self.x = 40
- self.y = 150
- self.r = 8
- self.isFire = False
- self.isHaos = False
- self.color = 'black'
- self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
- self.vy = 9
- self.vx = 2
- def unfire(self):
- self.isFire = False
- self.color = 'black'
- def fire(self):
- self.isFire = True
- root.after(6000,self.unfire)
- def haos(self):
- self.isHaos = True
- root.after(6000,self.unhaos)
- def unhaos(self):
- self.isHaos = False
- def move(self):
- if self.isHaos:
- self.color = 'blue'
- if self.isFire:
- self.color = 'red'
- canv.itemconfig(self.id,fill=self.color)
- 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 = 80
- 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 bonus(self,bonus):
- if bonus == 'long':
- if self.w < 140:
- self.w += 20
- self.x -= 10
- elif bonus == 'short':
- if self.w > 40:
- self.w -= 20
- self.x += 10
- elif bonus == 'fast':
- j.vy *= 1.2
- j.vx *= 1.2
- elif bonus == 'slow':
- j.vy *= 0.8
- j.vx *= 0.8
- elif bonus == 'fire':
- ball.fire()
- elif bonus == 'haos':
- ball.haos()
- 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)
- ball = j = Jumper()
- x = 100
- y = 100
- blocks = []
- for ii in range(8):
- blocks.append(Block(x,y,1))
- x += 62
- y += 22
- x = 100
- for i in range(8):
- blocks.append(Block(x,y,0))
- for ii in range(6):
- x += 62
- blocks.append(Block(x,y,0))
- x += 62
- blocks.append(Block(x,y,1))
- y += 22
- x = 100
- h = 40
- x = 100
- for ii in range(8):
- blocks.append(Block(x,y,1))
- x += 62
- #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:
- if block.check():
- break
- 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