Advertisement
boris-vlasenko

breakout

Apr 24th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 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.  
  12. class Block():
  13.     def __init__(self,x,y):
  14.         self.x = x
  15.         self.y =
  16.         self.h = 20
  17.         self.w = 60
  18.         self.k = rnd(5)
  19.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill=colors[self.k])
  20.    
  21.     def kick(self):
  22.         if self.k > 0:
  23.             self.k -= 1
  24.         else:
  25.             self.kill()
  26.  
  27.     def move(self):
  28.         ball = j
  29.         x = self.x + self.w/2
  30.         y = self.y + self.h/2
  31.         d = ''
  32.         if self.x < ball.x < self.x+self.w and self.y-ball.r < ball.y < self.y + self.h + ball.r:
  33.             d = 'vy'
  34.         if ball.x-x != 0:
  35.             h = ((ball.x - x)**2 + (ball.y - y)**2)**0.5
  36.             a = abs((ball.y - y)/(ball.x-x))
  37.  
  38.             if  a < 0.25 :
  39.                 if self.w/3  < h < self.w/1.5:
  40.                     d = 'vx'
  41.                
  42.         if d == 'vy':
  43.             ball.vy *= -1
  44.             self.kick()
  45.         elif d == 'vx':
  46.             ball.vx *= -1
  47.             self.kick()
  48.    
  49.         canv.itemconfig(self.id,fill=colors[self.k])
  50.    
  51.     def kill(self):
  52.         blocks.remove(self)
  53.         canv.delete(self.id)
  54.        
  55.  
  56. class Jumper():
  57.     def __init__(self):
  58.         self.x = 40
  59.         self.y = 150
  60.         self.r = 8
  61.        
  62.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill='orange')
  63.         self.vy = 6
  64.         self.vx = 9
  65.  
  66.  
  67.     def move(self):
  68.         self.x += self.vx
  69.         self.y += self.vy
  70.        
  71.         if self.y < 50:
  72.             self.y = 50
  73.             self.vy *= -1
  74.         if self.x < 50:
  75.             self.x = 50
  76.             self.vx *= -1
  77.         if self.x > 750:
  78.             self.x = 750
  79.             self.vx *= -1
  80.         if self.y > 600:
  81.             self.y = 600
  82.             self.vy *= -1
  83.            
  84.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  85.          
  86. class Platform():
  87.     def __init__(self):
  88.         self.x = 300
  89.         self.y = 550
  90.         self.ax = 0
  91.         self.w = 120
  92.         self.h = 20
  93.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
  94.         self.vx = 0
  95.         self.vy = 0
  96.         if self.x < 50:#
  97.             self.ax = 0#
  98.             self.vx = 0#
  99.         if self.x > 750:#
  100.             self.ax = 0#
  101.             self.vx = 0#
  102.  
  103.    
  104.        
  105.     def move(self):
  106.         self.vx += self.ax
  107.         self.x += self.vx
  108.         self.vx *= 0.9
  109.         if self.x+self.w > 750:
  110.             self.x = 750-self.w
  111.         if self.x < 50:
  112.             self.x = 50
  113.            
  114.         canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  115.  
  116. j = Jumper()
  117. x = 100
  118. y = 100
  119. blocks = []
  120. for i in range(10):
  121.     for ii in range(10):
  122.         blocks.append(Block(x,y))
  123.         x += 62
  124.    
  125.     y += 22
  126.     x = 100
  127.     h = 40
  128. #Block(400,300)
  129. p = Platform()
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. def keyDown(event):
  138.    
  139.     keys.add(event.keycode)
  140.  
  141. def keyUp(event):
  142.     keys.remove(event.keycode)
  143.  
  144. def move(event):
  145.     j.x = event.x
  146.     j.y = event.y
  147.     j.move()
  148.    
  149. root.bind('<Key>',keyDown)
  150. root.bind('<KeyRelease>',keyUp)
  151. #canv.bind('<Motion>',move)
  152.  
  153. keys = set()
  154.  
  155.  
  156. while 1:
  157.     p.ax = 0
  158.     if 113 in keys:
  159.         p.ax = -2
  160.     if 114 in keys:
  161.         p.ax = 2
  162.    
  163.     j.move()
  164.     p.move()
  165.     for block in blocks:
  166.         block.move()
  167.  
  168.     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:    
  169.         j.vy *= -1
  170.         j.vx = (j.x - (p.x+p.w/2))/7
  171.    
  172.     canv.update()
  173.     time.sleep(0.03)
  174. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement