Advertisement
xgeovanni

Brick Breaker

Jan 27th, 2012
1,850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.50 KB | None | 0 0
  1. # Brick Breaker
  2. # Bobby Clarke
  3. # version 1.2
  4.  
  5.  
  6. #Imports
  7. import pygame, easygui, pickle, random, sys, os
  8. from time import sleep
  9. from pygame.locals import *
  10. pygame.init()
  11.  
  12. #   #   #   #   #
  13.  
  14. screen = pygame.display.set_mode((500, 600))
  15.  
  16. clock = pygame.time.Clock()
  17.  
  18. black = (0, 0, 0)
  19.  
  20. class paddle():
  21.     """The paddle, player controlled"""
  22.     def __init__(self):
  23.         self.sprite = pygame.image.load("Images/paddle.png")
  24.         self.x = 200
  25.         self.left = False
  26.         self.right = False
  27.        
  28.     def move(self):
  29.         """move the paddle"""
  30.         if self.left and self.x >= 0:
  31.             self.x -= 2
  32.         elif self.right and self.x <= 400:
  33.             self.x += 2
  34.  
  35. class ball():
  36.     """The ball"""
  37.     def __init__(self, x = 242, y = 440):
  38.         self.sprite = pygame.image.load("Images/ball.png")
  39.         self.x = x
  40.         self.y = y
  41.  
  42.         self.up = False
  43.         self.down = True
  44.         self.left = False
  45.         self.right = False
  46.  
  47.         self.lives = 3
  48.  
  49.     def move(self, paddle, start_x, start_y):
  50.        
  51.         """Move the ball according to it's direction and surroundings"""
  52.         if self.up:
  53.             self.y -= 5
  54.         elif self.down:
  55.             self.y += 5
  56.         if self.right:
  57.             self.x += 2
  58.         elif self.left:
  59.             self.x -= 2
  60.  
  61.         if self.y < 0:
  62.             self.down = True
  63.             self.up = False
  64.  
  65.         elif self.x < 0:
  66.             self.right = True
  67.             self.left = False
  68.  
  69.         elif self.x > 484:
  70.             self.left = True
  71.             self.right = False
  72.  
  73.         elif self.y > 616:
  74.             self.x = start_x
  75.             self.y = start_y
  76.  
  77.             self.up = True
  78.             self.down = False
  79.             self.left = False
  80.             self.right = False
  81.  
  82.             self.lives -= 1
  83.  
  84.         if self.x in range(paddle.x - 16, paddle.x + 100):
  85.             if self.y in range(534, 580):
  86.                 self.up = True
  87.                 self.down = False
  88.  
  89.                 if self.x in range(paddle.x - 16, paddle.x + 29):
  90.                     self.left = True
  91.                     self.right = False
  92.                 elif self.x in range(paddle.x + 65, paddle.x + 100):
  93.                     self.left = False
  94.                     self.right = True
  95.                 else:
  96.                     self.left = False
  97.                     self.right = False
  98.  
  99. class brick():
  100.     """The bricks, perameters are "broken", indicating how broken the brick is
  101.       from 1 to 5, x and y axis and unbreaking (bool) (broken, x, y, unbreaking = False),
  102.       broken must be 0 for unbreaking bricks"""
  103.     def __init__(self, broken, x, y, unbreaking = False):
  104.         self.broken = broken
  105.         self.x = x
  106.         self.y = y
  107.         self.unbreaking = unbreaking
  108.  
  109.         self.sprite = pygame.image.load("Images/brick{0}.png".format(self.broken))
  110.  
  111.     def update(self, ball, paddle):
  112.         broken_once = False
  113.         heal = False
  114.         heal_x = None
  115.         heal_y = None
  116.         """Check for changes with the brick, and act accordingly"""
  117.         if ball.x in range(self.x - 16, self.x + 60):
  118.             if ball.y in range(self.y - 16, self.y + 40):
  119.                 if not broken_once:
  120.                     if not self.unbreaking:
  121.                         self.broken += 1
  122.                         broken_once = True
  123.  
  124.                     try:
  125.                         self.sprite = pygame.image.load("Images/brick{0}.png".format(self.broken))
  126.                     except:
  127.                         healchance = random.randint(1, 10)
  128.  
  129.                         if healchance == 10:
  130.                             heal = True
  131.  
  132.                             heal_x = self.x + 22
  133.                             heal_y = self.y + 12
  134.                        
  135.                         self.x = -60
  136.                         self.y = -40
  137.  
  138.                     if ball.left and (ball.x in range(self.x - 16, self.x) or ball.x in range(self.x + 55, self.x + 60)):
  139.                         ball.left = False
  140.                         ball.right = True
  141.                        
  142.                     elif ball.right and (ball.x in range(self.x - 16, self.x) or ball.x in range(self.x + 55, self.x + 60)):
  143.                         ball.right = False
  144.                         ball.left = True
  145.  
  146.                     if ball.up:
  147.                         ball.up = False
  148.                         ball.down = True                        
  149.                    
  150.                     elif ball.down:
  151.                         ball.down = False
  152.                         ball.up = True
  153.  
  154.                     ball.move(paddle, ball.start_x, ball.start_y)
  155.  
  156.  
  157.         return heal, heal_x, heal_y
  158.  
  159.                    
  160.  
  161. class menu():
  162.     """The menu / title screen"""
  163.  
  164.     def run():
  165.         """Run the menu and return a string"""
  166.         end = False
  167.  
  168.         screen.blit(pygame.image.load("Images/menu.png"), (0, 0))
  169.         pygame.display.update()
  170.        
  171.         while not end:
  172.             for event in pygame.event.get():
  173.                 if event.type == pygame.QUIT:
  174.                     sys.exit()
  175.                
  176.                 elif event.type == MOUSEBUTTONDOWN:
  177.                     x, y = pygame.mouse.get_pos()
  178.  
  179.                     if x in range(200, 300) and y in range(200, 250):
  180.                         output = "play"
  181.                         end = True
  182.                     elif x in range(150, 370) and y in range(320, 360):
  183.                         output = "newlevel"
  184.                         end = True
  185.                     elif x in range(135, 390) and y in range(440, 480):
  186.                         output = "editlevel"
  187.                         end = True
  188.  
  189.         return output
  190.  
  191. class level():
  192.     def build(brickclass, ball, bricks = [], levelname = ""):
  193.         """User-Friendly level builder"""
  194.  
  195.         click = False
  196.  
  197.         broken = 1
  198.         x = 0
  199.         y = 0
  200.         unbreaking = False
  201.  
  202.         end = False
  203.         drag = False
  204.         delete = False
  205.  
  206.         new = True
  207.        
  208.         while not end:
  209.             clock.tick(60)
  210.  
  211.             screen.fill(black)
  212.  
  213.             if click:
  214.                 for brick in bricks:
  215.                     if (x in range(brick.x, brick.x + 60) and y in range(brick.y, brick.y + 40)) or (x in range(ball.x - 8, ball.x + 24) and y in range(ball.y - 8, ball.y + 24)):
  216.                         if delete:
  217.                             bricks.remove(brick)
  218.                             new = False
  219.  
  220.                         else:
  221.                             drag = True
  222.                             new = False
  223.                        
  224.                 if new:
  225.                     if broken == 0:
  226.                         unbreaking = True
  227.                    
  228.                     bricks.append(brickclass(broken, x - 30, y - 20, unbreaking))
  229.  
  230.                     x = 0
  231.                     y = 0
  232.                     unbreaking = False
  233.  
  234.                 click = False
  235.  
  236.             if drag:
  237.                 for brick in bricks:
  238.                     x, y = pygame.mouse.get_pos()
  239.                     if x in range(brick.x, brick.x + 60) and y in range(brick.y, brick.y + 40):
  240.                         brick.x = x - 30
  241.                         brick.y = y - 20
  242.  
  243.                     elif x in range(ball.x - 8, ball.x + 24) and y in range(ball.y - 8, ball.y + 24):
  244.                         ball.x = x - 8
  245.                         ball.y = y - 8
  246.  
  247.             for brick in bricks:
  248.                 screen.blit(brick.sprite, (brick.x, brick.y))
  249.  
  250.             screen.blit(ball.sprite, (ball.x, ball.y))
  251.  
  252.             pygame.display.update()
  253.  
  254.             for event in pygame.event.get():
  255.                 if event.type == pygame.QUIT:
  256.                     sys.exit()
  257.  
  258.                 elif event.type == MOUSEBUTTONDOWN:
  259.                     click = True
  260.                     x, y = pygame.mouse.get_pos()
  261.  
  262.                 elif event.type == MOUSEBUTTONUP:
  263.                     drag = False
  264.                     new = True
  265.  
  266.                 elif event.type == KEYDOWN:
  267.                     if event.key == K_0:
  268.                         broken = 0
  269.                     elif event.key == K_1:
  270.                         broken = 1
  271.                     elif event.key == K_2:
  272.                         broken = 2
  273.                     elif event.key == K_3:
  274.                         broken = 3
  275.                     elif event.key == K_4:
  276.                         broken = 4
  277.                     elif event.key == K_5:
  278.                         broken = 5
  279.  
  280.                     elif event.key == K_SPACE:
  281.                         if delete:
  282.                             delete = False
  283.                         else:
  284.                             delete = True
  285.  
  286.                     elif event.key == K_RETURN:
  287.                         save = easygui.boolbox("Do you want to save?", "Level Builder", ["Yes", "no"])
  288.                         if save:
  289.                             levelname = easygui.enterbox("What is this level's name?", "Level Builder", default = levelname)
  290.                             if levelname:
  291.                                 levelfile = open("Levels/{0}.lvl".format(levelname), "wb")
  292.  
  293.                                 for brick in bricks:
  294.                                     del(brick.sprite)
  295.  
  296.                                 del(ball.sprite)
  297.                                    
  298.                                 pickle.dump([bricks, ball], levelfile)
  299.                                 end = True
  300.  
  301.     def load(levelfile, brickclass, ballclass):
  302.         levelfile = open(levelfile, "rb")
  303.  
  304.         level = pickle.load(levelfile)
  305.  
  306.         bricks = level[0]
  307.         ball = level[1]
  308.  
  309.         ball = ballclass(ball.x, ball.y)
  310.  
  311.         newbricks = []
  312.  
  313.         for brick in bricks:
  314.             newbricks.append(brickclass(brick.broken, brick.x, brick.y, brick.unbreaking))
  315.  
  316.         levelfile.close()
  317.  
  318.         return newbricks, ball
  319.  
  320.     def browse():
  321.         selfdir = os.path.dirname(sys.argv[0])
  322.         levels = os.listdir("{0}{1}".format(selfdir, "/Levels"))
  323.  
  324.         newlevels = []
  325.  
  326.         for level in levels:
  327.             if ".lvl" in level:
  328.                 level = level[0 : len(level) - 4]
  329.                 newlevels.append(level)
  330.  
  331.         level = easygui.choicebox("Which level do you want to load?", "Brick Breaker", newlevels)
  332.  
  333.         return level
  334.  
  335.     def randgen(brickclass):
  336.         bricks = []
  337.         numbricks = random.randint(5, 20)
  338.  
  339.         for i in range(0, numbricks):
  340.             x = random.randint(0, 440)
  341.             y = random.randint(60, 500)
  342.  
  343.             broken = random.randint(0, 5)
  344.  
  345.             if broken == 0:
  346.                 unbreaking = True
  347.             else:
  348.                 unbreaking = False
  349.  
  350.             for brick in bricks:
  351.                 while x in range (brick.x - 60, brick.x + 60) and y in range(brick.y- 40, brick.y + 40):
  352.                     x = random.randint(0, 500)
  353.                     y = random.randint(0, 500)
  354.  
  355.             bricks.append(brickclass(broken, x, y, unbreaking))
  356.  
  357.         return bricks
  358.        
  359.                      
  360.  
  361.  
  362. def play(paddle, ball, bricks):
  363.     ball.start_x = ball.x
  364.     ball.start_y = ball.y
  365.  
  366.     heals = []
  367.    
  368.     livesimg = pygame.image.load("Images/lives.png")
  369.     gameover = pygame.image.load("Images/gameover.png")
  370.     youwin = pygame.image.load("Images/youwin.png")
  371.     healimg = pygame.image.load("Images/heal.png")
  372.  
  373.     unbreaking_bricks = 0
  374.     for brick in bricks:
  375.         if brick.unbreaking:
  376.             unbreaking_bricks += 1
  377.    
  378.     """Play the game"""
  379.     while ball.lives != 0 and len(bricks) != unbreaking_bricks:
  380.         clock.tick(60)
  381.  
  382.         screen.fill(black)
  383.  
  384.         paddle.move()
  385.         ball.move(paddle, ball.start_x, ball.start_y)
  386.  
  387.         for brick in bricks:
  388.             heal, heal_x, heal_y = brick.update(ball, paddle)
  389.  
  390.             if brick.x == -60 and brick.y == -40:
  391.                 bricks.remove(brick)
  392.  
  393.             if heal:
  394.                 heals.append([heal_x, heal_y, random.randint(1, 4)])
  395.  
  396.         for heal in heals:
  397.             screen.blit(healimg, (heal[0], heal[1]))
  398.  
  399.             heal[1] += heal[2]
  400.  
  401.             if heal[0] in range(paddle.x - 16, paddle.x + 100) and heal[1] in range(534, 580):
  402.                 heals.remove(heal)
  403.                 if ball.lives < 9:
  404.                     ball.lives += 1
  405.    
  406.         # Blits
  407.    
  408.         screen.blit(paddle.sprite, (paddle.x, 550))
  409.         screen.blit(ball.sprite, (ball.x, ball.y))
  410.  
  411.         for brick in bricks:
  412.             screen.blit(brick.sprite, (brick.x, brick.y))
  413.  
  414.         screen.blit(livesimg, (0, 0))
  415.  
  416.         try:
  417.             screen.blit(pygame.image.load("Images/{0}.png".format(ball.lives)), (100, 0))
  418.         except:
  419.             screen.blit(gameover, (0, 0))
  420.  
  421.         if len(bricks) == unbreaking_bricks:
  422.             screen.blit(youwin, (0, 0))
  423.         #   #   #   #   #
  424.  
  425.         pygame.display.update()
  426.  
  427.         if ball.lives == 0 or len(bricks) == unbreaking_bricks:
  428.             sleep(3)
  429.    
  430.         # Event Loop
  431.         for event in pygame.event.get():
  432.             if event.type == pygame.QUIT:
  433.                 sys.exit()
  434.  
  435.             elif event.type == KEYDOWN:
  436.                 #If left or right is pressed, move the paddle
  437.                 if event.key == K_RIGHT:
  438.                     paddle.right = True
  439.                 elif event.key == K_LEFT:
  440.                     paddle.left = True
  441.  
  442.             elif event.type == KEYUP:
  443.                 #If left or right is depressed, stop moving
  444.                 if event.key == K_RIGHT:
  445.                     paddle.right = False
  446.                 elif event.key == K_LEFT:
  447.                     paddle.left = False
  448.     ball.lives = 3
  449.  
  450.    
  451. def main(paddle, ballclass, brick, menu, levelclass):
  452.     pygame.display.set_caption("Brick Breaker")
  453.     while True:
  454.         choice = menu.run()
  455.  
  456.         if choice == "play":
  457.             levelname = levelclass.browse()
  458.             level = "Levels/{0}.lvl".format(levelname)
  459.  
  460.             if levelname:
  461.                 bricks, ball = levelclass.load(level, brick, ballclass)
  462.                 play(paddle(), ball, bricks)
  463.  
  464.         elif choice == "newlevel":
  465.             levelclass.build(brick, ballclass(),  bricks = [])
  466.  
  467.         elif choice == "editlevel":
  468.             levelname = levelclass.browse()
  469.             level = "Levels/{0}.lvl".format(levelname)
  470.            
  471.             if levelname:
  472.                 bricks, ball = levelclass.load(level, brick, ballclass)
  473.                 levelclass.build(brick, ball, bricks, levelname = levelname)
  474.  
  475. main(paddle, ball, brick, menu, level)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement