Advertisement
boris-vlasenko

breakout3

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