Advertisement
boris-vlasenko

breakout4

May 8th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.20 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3. from random import randrange as rnd, random, choice
  4. from math import atan
  5.  
  6.  
  7. #~ Бонусы:
  8.     #~ 1. быстрый мяч +
  9.     #~ 2. медленный мяч +
  10.     #~ 3. случайный отскок
  11.     #~ 4. короткая платформа +
  12.     #~ 5. длинная платформа +
  13.     #~ 6. три мяча +
  14.     #~ 7. три черных мяча (нельзя терять ни один)
  15.     #~ 8. пол на время +
  16.     #~ 9. пол - страховка (на удар)
  17.     #~ 10. огненный мяч (сбивает все подряд, бонусов нет) +
  18.     #~ 11. сильный мяч (сбивает сбиваемые, отскакивает) +
  19.     #~ 12. выбиватель бонусов (выбивает чаще)
  20.     #~ 13. жизнь +
  21.     #~ 14. уничтожение всех блоков
  22.     #~ 15. липучка (на время)
  23.     #~ 16. целеуказатель (линия)
  24.     #~ 17. мяч ловит бонусы
  25.     #~ 18. пушка
  26.     #~ 19. неизвестный бонус
  27.    
  28.  
  29. root = Tk()
  30. fr = Frame(root)
  31. root.geometry('850x600')
  32. canv = Canvas(root, bg = 'white')
  33. canv.pack(fill=BOTH,expand=1)
  34. colors = ['green','yellow','orange', 'red']
  35. bonuses = ('long','short','fire','slow','fast','balls','live','floor','energy_ball','gun')
  36.  
  37. class Bullet():
  38.     def __init__(self,x,y):
  39.         self.x = x
  40.         self.y = y
  41.         self.r = r = 3
  42.         self.vy = -15
  43.         self.vx = 0
  44.         self.id = canv.create_oval(x-r,y-r,x+r,y+r,fill='black')
  45.         self.kill = False
  46.         self.move()
  47.        
  48.     def move(self):
  49.         if mode == 'game' and self.y > 0 and not self.kill:
  50.             self.y += self.vy
  51.             for block in blocks:
  52.                 if block.x < self.x < block.x + block.w and block.y < self.y < block.y+block.h:
  53.                     block.kill()
  54.                     self.kill = True
  55.                     break
  56.  
  57.             canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  58.             root.after(30,self.move)
  59.         else:
  60.             canv.delete(self.id)
  61.        
  62.        
  63.  
  64. class Bonus():
  65.     def __init__(self,x,y):
  66.         self.x = x + 30
  67.         self.y = y
  68.         self.vy = 3
  69.         self.vx = 0
  70.         self.bonus = choice(bonuses)
  71.         self.id = canv.create_text(self.x,self.y,text = self.bonus)
  72.         self.move()
  73.        
  74.     def move(self):
  75.         self.x += self.vx
  76.         self.y += self.vy
  77.         if self.y > 600:
  78.             canv.delete(self.id)
  79.         else:
  80.             if p.x < self.x < p.x+p.w and p.y < self.y < p.y + p.h:
  81.                 canv.delete(self.id)
  82.                 p.bonus(self.bonus)
  83.             else:
  84.                 if mode == 'game':
  85.                     root.after(30,self.move)
  86.                     canv.coords(self.id,self.x,self.y)
  87.                 else:
  88.                     canv.delete(self.id)
  89.        
  90.  
  91. class Block():
  92.     def __init__(self,x,y,k):
  93.         self.x = x
  94.         self.y =
  95.         self.h = 20
  96.         self.w = 60
  97.         self.k = k
  98.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill=colors[self.k],width=2)
  99.    
  100.     def kick(self):
  101.         self.k -= 1
  102.         if p.isFire or p.isEnergy_ball:
  103.             self.k = -1
  104.         if self.k < 0:
  105.             self.kill()
  106.            
  107.     def check(self):
  108.         for ball in balls:
  109.             if not ball.kick:
  110.                 if self.x-ball.r < ball.x < self.x + self.w + ball.r and self.y-ball.r < ball.y < self.y + self.h+ball.r:
  111.                     if not p.isFire:
  112.                         prev_ball_x = ball.x -ball.vx
  113.                         prev_ball_y = ball.y -ball.vy
  114.                         if abs(ball.vy) > 1 and prev_ball_y < self.y or prev_ball_y > self.y+self.h:
  115.                             ball.vy *= -1
  116.                             ball.kick = True
  117.                         if prev_ball_x < self.x or prev_ball_x > self.x+self.w:
  118.                             ball.kick = True
  119.                             ball.vx *= -1
  120.                             if abs(ball.vx) < 1:
  121.                                 ball.vx *= 3
  122.                     self.kick()
  123.                    
  124.         if self.k >= 0:
  125.             canv.itemconfig(self.id,fill=colors[self.k])       
  126.    
  127.     def kill(self):
  128.         if not p.isFire:
  129.             Bonus(self.x,self.y)
  130.            
  131.         if self in blocks:
  132.             blocks.remove(self)
  133.             canv.delete(self.id)
  134.             if not blocks:
  135.                 next_level()
  136.  
  137.        
  138. class Ball():
  139.     def __init__(self,x=400,y=500,vx=0,vy=6):
  140.         self.x = x
  141.         self.y = y
  142.         self.r = 8
  143.         self.kick = False
  144.        
  145.         self.color = 'orange'
  146.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color,tag='balls')
  147.         self.vy = vy
  148.         self.vx = vx
  149.  
  150.     def move(self):
  151.         if mode == 'init':
  152.             self.x = p.x+p.w/2
  153.             self.y = p.y-self.r
  154.         else:
  155.             self.kick = False
  156.             self.x += self.vx
  157.             self.y += self.vy
  158.            
  159.             if self.y < 50:
  160.                 self.y = 50
  161.                 self.vy *= -1
  162.             if self.x < 50:
  163.                 self.x = 50
  164.                 self.vx *= -1
  165.             if self.x > 750:
  166.                 self.x = 750
  167.                 self.vx *= -1
  168.             if self.y > 580 and p.isFloor > 0:
  169.                 self.vy *= -1
  170.             if self.y > 600:
  171.                 self.kill()
  172.                
  173.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  174.         canv.itemconfig(self.id,fill=self.color)
  175.  
  176.     def kill(self):
  177.         canv.delete(self.id)
  178.         balls.remove(self)
  179.         if not balls:
  180.             p.lives -= 1
  181.             init()
  182.  
  183.          
  184. class Platform():
  185.     def __init__(self):
  186.         self.x = 300
  187.         self.y = 550
  188.         self.ax = 0
  189.         self.w = 60
  190.         self.h = 20
  191.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
  192.         self.vx = 0
  193.         self.vy = 0
  194.         self.lives = 2
  195.         self.line = False
  196.         self.isFloor = 0
  197.         self.isFire = 0
  198.         self.isEnergy_ball = 0
  199.         self.isGun = 300
  200.         self.gun_cool_time = 10
  201.         self.fire_id = canv.create_text(790,400,text = self.isFire)
  202.         self.enery_ball_id = canv.create_text(790,425,text = self.isEnergy_ball)
  203.         self.floor_id = canv.create_text(790,450,text = self.isFloor)
  204.         self.gun_id = canv.create_text(790,475,text = self.isGun)
  205.        
  206.         if self.x < 50:
  207.             self.ax = 0
  208.             self.vx = 0
  209.         if self.x > 750:
  210.             self.ax = 0
  211.             self.vx = 0
  212.        
  213.     def gun(self):
  214.         self.isGun += 300
  215.        
  216.     def energy_ball(self):
  217.         self.isEnergy_ball += 300
  218.         for ball in balls:
  219.             ball.color = 'blue'
  220.         self.isFire = 0
  221.        
  222.  
  223.     def unenergy_ball(self):
  224.         for ball in balls:
  225.             ball.color = 'orange'
  226.        
  227.  
  228.     def fire(self):
  229.         self.isFire += 500
  230.         for ball in balls:
  231.             ball.color = 'red'
  232.         self.isEnergy_ball = 0
  233.  
  234.     def unfire(self):
  235.         for ball in balls:
  236.             ball.color = 'orange'
  237.                    
  238.     def floor(self):
  239.         self.isFloor += 300
  240.         canv.create_line(50,580,750,580,width=3,tag='floor')
  241.        
  242.     def unfloor(self):
  243.         canv.delete('floor')
  244.  
  245.     def bonus(self,bonus):
  246.         if bonus == 'long':
  247.             if self.w < 140:
  248.                 self.w += 20
  249.                 self.x -= 10
  250.                
  251.         elif bonus == 'short':
  252.             if self.w > 40:
  253.                 self.w -= 20
  254.                 self.x += 10
  255.                
  256.         elif bonus == 'balls':
  257.             balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-5))
  258.             balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-5))
  259.             balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-5))
  260.             if self.isFire > 0:
  261.                 for ball in balls:
  262.                     ball.color = 'red'
  263.                    
  264.         elif bonus == 'three_ball':
  265.            
  266.             balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-3))
  267.             balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-3))
  268.             balls.append(Ball(p.x+p.w/2,p.y-10,random()*2-1,-3))
  269.             self.three_ball += 300
  270.             for ball in balls:
  271.                 ball.color = 'black'
  272.            
  273.                
  274.            
  275.         elif bonus == 'fast':
  276.             for ball in balls:
  277.                 ball.vx *= 1.2
  278.                 ball.vy *= 1.2
  279.  
  280.         elif bonus == 'slow':
  281.             for ball in balls:
  282.                 ball.vx *= 0.8
  283.                 ball.vy *= 0.8
  284.  
  285.         elif bonus == 'fire':
  286.             self.fire()
  287.                
  288.         elif bonus == 'energy_ball':
  289.             self.energy_ball()
  290.                
  291.         elif bonus == 'live':
  292.             p.lives += 1
  293.             show_lives()
  294.        
  295.         elif bonus == 'line':
  296.             p.line = True
  297.            
  298.         elif bonus == 'floor':
  299.             p.floor()
  300.  
  301.         elif bonus == 'gun':
  302.             print(1)
  303.             p.gun()
  304.        
  305.     def move(self):
  306.         self.vx += self.ax
  307.         self.x += self.vx
  308.         self.vx *= 0.9
  309.         if self.x+self.w > 750:
  310.             self.x = 750-self.w
  311.         if self.x < 50:
  312.             self.x = 50
  313.            
  314.         for ball in balls:
  315.             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:    
  316.                 ball.vy *= -1
  317.                 ball.vx = (ball.x - (p.x+p.w/2))/7
  318.                 ball.y = p.y - ball.r
  319.        
  320.         if self.isFloor > 0:
  321.             self.isFloor -= 1
  322.             if self.isFloor <= 0:
  323.                 self.unfloor()
  324.  
  325.         if self.isFire > 0:
  326.             self.isFire -= 1
  327.             if self.isFire <= 0:
  328.                 self.unfire()
  329.  
  330.         if self.isEnergy_ball > 0:
  331.             self.isEnergy_ball -= 1
  332.             if self.isEnergy_ball <= 0:
  333.                 self.unenergy_ball()
  334.        
  335.         if self.isGun > 0:
  336.             self.isGun -= 1
  337.             self.gun_cool_time -= 1
  338.            
  339.         canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  340.         canv.itemconfig(self.fire_id,text = self.isFire)
  341.         canv.itemconfig(self.floor_id,text = self.isFloor)
  342.         canv.itemconfig(self.enery_ball_id,text = self.isEnergy_ball)
  343.         canv.itemconfig(self.gun_id,text = self.isGun)
  344.        
  345.    
  346.  
  347. def new_game(event=0):
  348.     global balls, p, keys, level
  349.     level = 1
  350.     canv.create_rectangle(0,0,900,600,fill='gray')
  351.     canv.create_rectangle(50,50,750,700,fill='white')
  352.    
  353.     load_level()
  354.  
  355.     root.bind('<Key>',keyDown)
  356.     root.bind('<KeyRelease>',keyUp)
  357.     keys = set()
  358.     balls = []
  359.     p = Platform()
  360.     init()
  361.  
  362. def load_level():
  363.     global blocks, level
  364.     blocks = []
  365.     try:
  366.         f = open('level' + str(level) + '.txt','r')
  367.     except:
  368.         level = 1
  369.         f = open('level' + str(level) + '.txt','r')
  370.    
  371.     x = 100
  372.     y = 100
  373.  
  374.     for line in f.readlines():
  375.         for hp in line.strip():
  376.             if hp != '-':
  377.                 blocks.append(Block(x,y,int(hp)))
  378.             x += 65
  379.         y += 25
  380.         x = 100
  381.         h = 40
  382.        
  383.     f.close()
  384.    
  385. def init():
  386.     global mode, balls
  387.     if p.lives < 0:
  388.         game_over()
  389.     p.x = 400 - p.w/2
  390.     p.vx = 0
  391.     p.ax = 0
  392.     mode = 'init'
  393.     canv.delete('balls')
  394.     balls = [Ball()]
  395.     # standart settings, remove bonuses...
  396.     p.w = 60
  397.     p.isFire = 0
  398.     p.isFloor = 0
  399.     p.isEnergy_ball = 0
  400.    
  401.     show_lives()
  402.  
  403. def show_lives():
  404.     canv.delete('lives')
  405.     y = 550
  406.     for i in range(p.lives):
  407.         canv.create_oval(790,y,800,y+10,fill='blue', tag = 'lives')
  408.         y -= 18
  409.  
  410.    
  411. def next_level():
  412.     global level
  413.     level += 1
  414.     load_level()
  415.     init()
  416.    
  417. def game_over():
  418.     root.unbind('<Key>')
  419.     root.unbind('<KeyRelease>')
  420.     canv.create_text(400,300,text='Game over', font = 'Arial 100')
  421.     canv.bind('<1>',new_game)
  422.  
  423. def keyDown(event):
  424.     global mode
  425.     if mode == 'init' and event.keycode == 65:
  426.         mode = 'game'
  427.         balls[-1].vy = -6
  428.     elif event.keycode == 65 and p.isGun > 0 and p.gun_cool_time < 0:
  429.         Bullet(p.x+p.w/2,p.y)
  430.         p.gun_cool_time = 30
  431.     else:
  432.         keys.add(event.keycode)
  433.  
  434. def keyUp(event):
  435.     if event.keycode in keys:
  436.         keys.remove(event.keycode)
  437.  
  438. def move(event):
  439.     p.x = event.x
  440.     p.move()
  441.    
  442.  
  443. new_game()
  444.  
  445.  
  446.    
  447. while 1:
  448.     p.ax = 0
  449.     if 113 in keys:
  450.         p.ax = -2
  451.     if 114 in keys:
  452.         p.ax = 2
  453.  
  454.     for ball in balls:
  455.         ball.move()
  456.        
  457.     p.move()
  458.     for block in blocks:
  459.         block.check()
  460.  
  461.    
  462.     canv.update()
  463.     time.sleep(0.03)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement