boris-vlasenko

BreakOut

Nov 3rd, 2016
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice
  3. import time
  4.  
  5. root = Tk()
  6. root.geometry('800x600')
  7.  
  8. canv = Canvas(root,bg='white')
  9. canv.pack(fill=BOTH,expand=1)
  10.  
  11.  
  12.  
  13. colors = ['violet', 'brown', 'red', 'orange', 'yellow', 'green','blue']
  14. #1
  15.  
  16. class Ball():
  17.    
  18.     def __init__(self):
  19.         self.x = 400
  20.         self.y = 300
  21.         self.r = 6
  22.         self.vx = rnd(-5,6)
  23.         self.vy = 6
  24.         self.k =0
  25.         self.id = canv.create_oval(self.x - self.r, self.y - self.r, self.x + self.r, self.y + self.r, fill = 'red', width = 0)
  26.    
  27.     def move(self):
  28.         self.x += self.vx
  29.         self.y += self.vy
  30.        
  31.        
  32.        
  33.         if self.y > 650:
  34.             self.vy = -self.vy
  35.         if self.x > 750:
  36.              self.vx =- self.vx
  37.         if self.y < 50:
  38.             self.vy = -self.vy
  39.         if self.x < 50:
  40.             self.vx =- self.vx
  41.            
  42.         canv.coords(self.id,self.x - self.r, self.y - self.r, self.x + self.r, self.y + self.r)
  43.        
  44. ball = Ball()
  45.  
  46.  
  47.  
  48. class Platform():
  49.     def __init__(self,x,y):
  50.         self.w = 100
  51.         self.x = x - self.w/2
  52.         self.y = y
  53.         self.h = 20
  54.         self.vx = 0
  55.         self.ax = 0
  56.         self.id = canv.create_rectangle(self.x,self.y,self.x + self.w,self.y + self.h, fill = 'green')
  57.  
  58.     def move(self):
  59.         if self.x > 750 - self.w:
  60.             self.vx = 0
  61.             self.ax = 0
  62.             self.x = 750 - self.w
  63.         if self.x < 50:
  64.             self.vx = 0
  65.             self.ax = 0
  66.             self.x = 50
  67.         self.vx *= 0.8
  68.         self.vx += self.ax
  69.         self.x += self.vx
  70.        
  71.         if self.x < ball.x < self.x + self.w and self.y < ball.y < self.y + self.h:
  72.             ball.vy = -ball.vy
  73.             ball.vx = (ball.x - (self.x+self.w/2))/5
  74.         canv.coords(self.id,self.x,self.y,self.x + self.w,self.y + self.h)
  75.        
  76.        
  77. plat1 = Platform(400,550)
  78.  
  79. def keyDown(event):
  80.     print(event.keycode)
  81.     keys.add(event.keycode)
  82.    
  83. def keyUp(event):
  84.     keys.remove(event.keycode)
  85.    
  86. keys = set()
  87. root.bind('<Key>',keyDown)
  88. root.bind('<KeyRelease>',keyUp)
  89.  
  90.  
  91. colors = ['yellow','orange','red','green','blue']
  92.  
  93. class Block():
  94.     def __init__(self,x,y,hp=0):
  95.         self.w = 60
  96.         self.x = x
  97.         self.y = y
  98.         self.h = 20
  99.         self.vx = 0
  100.         self.ax = 0
  101.         self.hp = hp
  102.         self.id = canv.create_rectangle(self.x,self.y,self.x + self.w,self.y + self.h, fill = colors[self.hp])
  103.    
  104.     def check(self):
  105.         if self.x < ball.x < self.x + self.w and self.y < ball.y < self.y+self.h:
  106.             x = ball.x - ball.vx
  107.             y = ball.y - ball.vy
  108.             if x < self.x or x > self.x+self.w:
  109.                 ball.vx = - ball.vx
  110.             if y < self.y or y > self.y + self.h:
  111.                 ball.vy = -ball.vy
  112.            
  113.             self.hp -= 1
  114.             canv.itemconfig(self.id, fill = colors[self.hp])
  115.             if self.hp < 0:
  116.                 self.kill()
  117.            
  118.             return True
  119.         return False
  120.    
  121.     def kill(self):
  122.         canv.delete(self.id)
  123.         blocks.remove(self)
  124.    
  125. def change_colors():
  126.     for bl in blocks:
  127.         canv.itemconfig(bl.id,fill=choice(colors))
  128.     root.after(1000,change_colors)
  129.        
  130.    
  131.    
  132. blocks = []
  133. #change_colors()
  134. f = open('level.txt','r')
  135. y = 50
  136. for line in f.readlines():
  137.     x = 50
  138.     for s in line.strip():
  139.         if s != '-':
  140.             blocks.append(Block(x,y,int(s)))
  141.         x += 65
  142.     y += 35
  143. f.close()
  144.  
  145.  
  146. while 1:
  147.     ball.move()
  148.     for block in blocks:
  149.         if block.check():
  150.             break
  151.     plat1.ax = 0
  152.     if 113 in keys:
  153.         plat1.ax = -3
  154.     if 114 in keys:
  155.         plat1.ax = +3
  156.        
  157.        
  158.     plat1.move()
  159.     canv.update()
  160.     time.sleep(0.03)
  161.    
  162.    
  163.  
  164. mainloop()
  165.  
  166.  
  167. --- level.txt ----
  168. -
  169. -
  170. ---000000---
  171. --00033000--
  172. ----0000----
  173. -----00-----
Advertisement
Add Comment
Please, Sign In to add comment