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']
- bonuses = ('long','short','fire','slow','fast','ball','ball','ball','ball','ball','ball','ball','ball')
- class Bonus():
- def __init__(self,x,y):
- self.x = x + 30
- self.y = y
- self.vy = 3
- self.vx = 0
- self.bonus = choice(bonuses)
- self.id = canv.create_text(self.x,self.y,text = self.bonus)
- self.move()
- def move(self):
- self.x += self.vx
- self.y += self.vy
- if self.y > 600:
- canv.delete(self.id)
- else:
- if p.x < self.x < p.x+p.w and p.y < self.y < p.y + p.h:
- canv.delete(self.id)
- p.bonus(self.bonus)
- else:
- root.after(30,self.move)
- canv.coords(self.id,self.x,self.y)
- class Block():
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.h = 20
- self.w = 60
- self.k = 0
- 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):
- x = self.x + self.w/2
- y = self.y + self.h/2
- for ball in balls:
- 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):
- Bonus(self.x,self.y)
- if self in blocks:
- blocks.remove(self)
- canv.delete(self.id)
- class Ball():
- def __init__(self,x=400,y=500,vx=0,vy=6):
- self.x = x
- self.y = y
- 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 = vy
- self.vx = vx
- 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 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 == 'ball':
- balls.append(Ball(p.x+p.w/2,p.y-10,0,-6))
- elif bonus == 'fast':
- for ball in balls:
- ball.vx *= 1.2
- ball.vy *= 1.2
- elif bonus == 'slow':
- for ball in balls:
- ball.vx *= 0.8
- ball.vy *= 0.8
- 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
- for ball in balls:
- if p.x <= ball.x+ball.r and ball.x-ball.r < p.x +p.w and p.y <= ball.y+ball.r and ball.y-ball.r <= p.y+p.h:
- ball.vy *= -1
- ball.vx = (ball.x - (p.x+p.w/2))/7
- canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
- balls=[Ball(),Ball(350)]
- 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
- p = Platform()
- def keyDown(event):
- keys.add(event.keycode)
- def keyUp(event):
- keys.remove(event.keycode)
- def move(event):
- p.x = event.x
- p.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
- for ball in balls:
- ball.move()
- p.move()
- for block in blocks:
- block.move()
- canv.update()
- time.sleep(0.03)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement