Advertisement
boris-vlasenko

breakout2

Apr 24th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3. from random import randrange as rnd, random, choice
  4. from math import sin, cos, asin
  5.  
  6. root = Tk()
  7. fr = Frame(root)
  8. root.geometry('800x600')
  9. canv = Canvas(root, bg = 'white')
  10. canv.pack(fill=BOTH,expand=1)
  11. colors = ['yellow', 'orange', 'red', 'green', 'blue']
  12.  
  13. bonuses = ('fire','long','short','fast','slow','haos')
  14. bonuses = ('haos',)
  15.  
  16. class Bonus():
  17.     def __init__(self,x,y):
  18.         self.x = x+30
  19.         self.y = y
  20.         self.bonus = choice(bonuses)
  21.         self.vx = 0
  22.         self.vy = 3
  23.         self.id = canv.create_text(self.x,self.y,text = self.bonus)
  24.         self.move()
  25.    
  26.     def move(self):
  27.         self.y += self.vy
  28.         self.x += self.vx
  29.         canv.coords(self.id,self.x,self.y)
  30.         if p.x < self.x < p.x + p.w and p.y < self.y < p.y+p.h:
  31.             p.bonus(self.bonus)
  32.             self.kill()
  33.         else:
  34.             if y < 600:
  35.                 root.after(30,self.move)
  36.             else:
  37.                 self.kill()
  38.            
  39.     def kill(self):
  40.         canv.delete(self.id)
  41.        
  42.  
  43. class Block():
  44.     def __init__(self,x,y,n):
  45.         self.x = x
  46.         self.y =
  47.         self.h = 20
  48.         self.w = 60
  49.         self.k = n
  50.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill=colors[self.k])
  51.    
  52.     def kick(self):
  53.         if self.k > 0:
  54.             self.k -= 1
  55.         else:
  56.             self.kill()
  57.  
  58.     def check(self):
  59.         ball = j
  60.         x = self.x + self.w/2
  61.         y = self.y + self.h/2
  62.         d = ''
  63.         if self.x < ball.x < self.x+self.w and self.y-ball.r < ball.y < self.y + self.h + ball.r:
  64.             d = 'vy'
  65.         if ball.x-x != 0:
  66.             h = ((ball.x - x)**2 + (ball.y - y)**2)**0.5
  67.             a = abs((ball.y - y)/(ball.x-x))
  68.  
  69.             if  a < 0.25 :
  70.                 if self.w/3  < h < self.w/1.2:
  71.                     d = 'vx'
  72.                
  73.         if d == 'vy':
  74.             if not ball.isFire:
  75.                 if not ball.isHaos:
  76.                     ball.vy *= -1
  77.                 else:
  78.                     v = (ball.vx**2+ball.vy**2)**0.5
  79.                     a = asin(ball.vy/v)
  80.                     a = 1+random()
  81.                     ball.vy = sin(a)*v
  82.                     ball.vx = cos(a)*v
  83.                    
  84.             self.kick()
  85.             return True
  86.         elif d == 'vx':
  87.             if not ball.isFire:
  88.                 if not ball.isHaos:
  89.                     ball.vx *= -1
  90.                 else:
  91.                     v = (ball.vx**2+ball.vy**2)**0.5
  92.                     a = asin(ball.vy/v)
  93.                     a = 1+random()
  94.                     ball.vy = sin(a)*v
  95.                     ball.vx = cos(a)*v
  96.                    
  97.             self.kick()
  98.             return True
  99.    
  100.         canv.itemconfig(self.id,fill=colors[self.k])
  101.    
  102.     def kill(self):
  103.         Bonus(self.x,self.y)
  104.         blocks.remove(self)
  105.         canv.delete(self.id)
  106.        
  107.  
  108. class Jumper():
  109.     def __init__(self):
  110.         self.x = 40
  111.         self.y = 150
  112.         self.r = 8
  113.         self.isFire = False
  114.         self.isHaos = False
  115.         self.color = 'black'
  116.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color)
  117.         self.vy = 9
  118.         self.vx = 2
  119.  
  120.     def unfire(self):
  121.         self.isFire = False
  122.         self.color = 'black'
  123.  
  124.     def fire(self):
  125.         self.isFire = True
  126.         root.after(6000,self.unfire)
  127.  
  128.     def haos(self):
  129.         self.isHaos = True
  130.         root.after(6000,self.unhaos)
  131.        
  132.     def unhaos(self):
  133.         self.isHaos = False
  134.        
  135.     def move(self):
  136.         if self.isHaos:
  137.             self.color = 'blue'
  138.         if self.isFire:
  139.             self.color = 'red'
  140.        
  141.         canv.itemconfig(self.id,fill=self.color)
  142.         self.x += self.vx
  143.         self.y += self.vy
  144.        
  145.         if self.y < 50:
  146.             self.y = 50
  147.             self.vy *= -1
  148.         if self.x < 50:
  149.             self.x = 50
  150.             self.vx *= -1
  151.         if self.x > 750:
  152.             self.x = 750
  153.             self.vx *= -1
  154.         if self.y > 600:
  155.             self.y = 600
  156.             self.vy *= -1
  157.            
  158.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  159.          
  160. class Platform():
  161.     def __init__(self):
  162.         self.x = 300
  163.         self.y = 550
  164.         self.ax = 0
  165.         self.w = 80
  166.         self.h = 20
  167.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
  168.         self.vx = 0
  169.         self.vy = 0
  170.         if self.x < 50:#
  171.             self.ax = 0#
  172.             self.vx = 0#
  173.         if self.x > 750:#
  174.             self.ax = 0#
  175.             self.vx = 0#
  176.  
  177.     def bonus(self,bonus):
  178.         if bonus == 'long':
  179.             if self.w < 140:
  180.                 self.w += 20
  181.                 self.x -= 10
  182.         elif bonus == 'short':
  183.             if self.w > 40:
  184.                 self.w -= 20
  185.                 self.x += 10
  186.         elif bonus == 'fast':
  187.             j.vy *= 1.2
  188.             j.vx *= 1.2
  189.         elif bonus == 'slow':
  190.             j.vy *= 0.8
  191.             j.vx *= 0.8
  192.         elif bonus == 'fire':
  193.             ball.fire()
  194.         elif bonus == 'haos':
  195.             ball.haos()
  196.        
  197.        
  198.     def move(self):
  199.         self.vx += self.ax
  200.         self.x += self.vx
  201.         self.vx *= 0.9
  202.         if self.x+self.w > 750:
  203.             self.x = 750-self.w
  204.         if self.x < 50:
  205.             self.x = 50
  206.            
  207.         canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  208.  
  209. ball = j = Jumper()
  210. x = 100
  211. y = 100
  212. blocks = []
  213. for ii in range(8):
  214.     blocks.append(Block(x,y,1))
  215.     x += 62
  216. y += 22
  217. x = 100
  218. for i in range(8):
  219.     blocks.append(Block(x,y,0))
  220.     for ii in range(6):
  221.         x += 62
  222.         blocks.append(Block(x,y,0))
  223.     x += 62
  224.     blocks.append(Block(x,y,1))
  225.    
  226.     y += 22
  227.     x = 100
  228.     h = 40
  229. x = 100
  230. for ii in range(8):
  231.     blocks.append(Block(x,y,1))
  232.     x += 62
  233. #Block(400,300)
  234. p = Platform()
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. def keyDown(event):
  243.    
  244.     keys.add(event.keycode)
  245.  
  246. def keyUp(event):
  247.     keys.remove(event.keycode)
  248.  
  249. def move(event):
  250.     j.x = event.x
  251.     j.y = event.y
  252.     j.move()
  253.    
  254. root.bind('<Key>',keyDown)
  255. root.bind('<KeyRelease>',keyUp)
  256. #canv.bind('<Motion>',move)
  257.  
  258. keys = set()
  259.  
  260.  
  261. while 1:
  262.     p.ax = 0
  263.     if 113 in keys:
  264.         p.ax = -2
  265.     if 114 in keys:
  266.         p.ax = 2
  267.    
  268.     j.move()
  269.     p.move()
  270.     for block in blocks:
  271.         if block.check():
  272.             break
  273.  
  274.     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:    
  275.         j.vy *= -1
  276.         j.vx = (j.x - (p.x+p.w/2))/7
  277.    
  278.     canv.update()
  279.     time.sleep(0.03)
  280. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement