Advertisement
Guest User

balsdf

a guest
Jul 23rd, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.97 KB | None | 0 0
  1. # Pygame imports
  2. import pygame, pygame.mixer
  3. from pygame import Surface
  4. from pygame.image import load
  5. from pygame.locals import *
  6. from pygame.mixer import music
  7. from pygame.rect import Rect
  8. from pygame.sprite import Group, Sprite
  9.  
  10. # Path imports
  11. from os.path import join
  12.  
  13. # Random imports
  14. from random import randint, choice
  15.  
  16. # Microgame-specific imports
  17. import locals
  18. from microgame import Microgame
  19. ################################################################################
  20. # Extra Functions #
  21.  
  22. # Determines what type of collision occurs *only called when one does occur
  23. REFLECT_X = 1
  24. REFLECT_Y = 2
  25. REFLECT_BOTH = 3
  26.  
  27. def determine_collision(rect1, rect2):
  28.     tl = not rect2.collidepoint(rect1.topleft)
  29.     bl = not rect2.collidepoint(rect1.bottomleft)
  30.     tr = not rect2.collidepoint(rect1.topright)
  31.     br = not rect2.collidepoint(rect1.bottomright)
  32.     if (tl and bl and tr) or (tr and br and tl) or (tl and bl and br) or (tr and br and bl):
  33.         return REFLECT_BOTH
  34.     if (tl and bl) or (tr and br):
  35.         return REFLECT_X
  36.     else:
  37.         return REFLECT_Y
  38.  
  39. ##### LOADER-REQUIRED FUNCTIONS ################################################
  40.  
  41. def make_game():
  42.     # TODO: Return a new instance of your Microgame class.
  43.     return BrickBreaker()
  44.  
  45. def title():
  46.     # TODO: Return the title of the game.
  47.     return "#BrickBreaker"
  48.  
  49. def thumbnail():
  50.     # TODO: Return a (relative path) to the thumbnail image file for your game.
  51.     return join("games", "brick_breaker", "thumbnail.png")
  52.  
  53. def hint():
  54.     # TODO: Return the hint string for your game.
  55.     return "#Collect"
  56.  
  57. ################################################################################
  58.  
  59. def _load_image(name, x, y):
  60.     '''
  61.    Loads an image file, returning the surface and rectangle corresponding to
  62.    that image at the given location.
  63.    '''
  64.     try:
  65.         image = load(name)
  66.         if image.get_alpha is None:
  67.             image = image.convert()
  68.         else:
  69.             image = image.convert_alpha()
  70.     except pygame.error, msg:
  71.         print 'Cannot load image: {}'.format(name)
  72.         raise SystemExit, msg
  73.     rect = image.get_rect().move(x, y)
  74.     return image, rect
  75.  
  76. ##### MODEL CLASSES ############################################################
  77.  
  78. # Global Vars
  79.  
  80. #Movement of Bar
  81. MOVE_SPEED = 20
  82.  
  83. #Values for blocks
  84. BLOCK_WIDTH = 100
  85. BLOCK_HEIGHT = 40
  86. INITIAL_BLOCK_X = 0
  87. INITIAL_BLOCK_Y = 0
  88. INITIAL_POS = randint(100, locals.WIDTH-100)
  89.  
  90. #Start Ball Speed
  91. INIT_VELOCITY_X = choice([10, -10])
  92. INIT_VELOCITY_Y = -10
  93.  
  94. #############################################################################
  95.  
  96. class BlockSprite(Sprite):
  97.     def __init__(self, x, y):
  98.         Sprite.__init__(self)
  99.         self.block_options = ["blue.png", "red.png", "yellow.png", "orange.png", "green.png"]
  100.         imgpath = join("games", "brick_breaker", choice(self.block_options))
  101.         self.image, self.rect = _load_image(imgpath, x, y)
  102.  
  103. class BallSprite(Sprite):
  104.     def __init__(self):
  105.         Sprite.__init__(self)
  106.         imgpath = join("games", "brick_breaker", "ball.png")
  107.         self.image, self.rect = _load_image(imgpath, INITIAL_POS - 25, locals.HEIGHT - 45)
  108.         self.velocity_x = INIT_VELOCITY_X
  109.         self.velocity_y = INIT_VELOCITY_Y
  110.  
  111.     def update(self):
  112.         self.rect = self.rect.move(self.velocity_x, self.velocity_y)
  113.  
  114. class BarSprite(Sprite):
  115.     def __init__(self):
  116.         Sprite.__init__(self)
  117.         imgpath = join("games", "brick_breaker", "bar.png")
  118.         self.image, self.rect = _load_image(imgpath, INITIAL_POS - 100, locals.HEIGHT - 20)
  119.         self.velocity = 0
  120.  
  121.     def update(self):
  122.         #Update Position
  123.         self.rect = self.rect.move(self.velocity, 0)
  124.  
  125. ##### MICROGAME CLASS ##########################################################
  126.  
  127. # TODO: rename this class to your game's name...
  128.  
  129. ################################################################################
  130. class BrickBreaker(Microgame):
  131.     def __init__(self):
  132.         Microgame.__init__(self)
  133.         self.bar = BarSprite()
  134.         self.ball = BallSprite()
  135.  
  136.         # Sound Effects
  137.         self.break_sound = pygame.mixer.Sound(join("games", "brick_breaker", "break.wav"))
  138.         self.bounce = pygame.mixer.Sound(join("games", "brick_breaker", "bounce.wav"))
  139.  
  140.         #Groups
  141.         self.bar_group = Group(self.bar)
  142.         self.ball_group = Group(self.ball)
  143.         self.blocks = Group()
  144.         self.build_board()
  145.  
  146.         #Constructs the layout of the blocks
  147.     def build_board(self):
  148.         position_x = INITIAL_BLOCK_X
  149.         position_y = INITIAL_BLOCK_Y
  150.  
  151.         for block in range(0, 59):
  152.             if position_x < locals.WIDTH/2 - 100 or (position_x >= locals.WIDTH/2 + 100 and position_x <= locals.WIDTH - 100):
  153.                 self.blocks.add(BlockSprite(position_x, position_y))
  154.                 position_x += BLOCK_WIDTH
  155.             elif position_x == locals.WIDTH/2 - 100:
  156.                 position_x = locals.WIDTH/2 + 100
  157.             else:
  158.                 position_y += 40
  159.                 position_x = INITIAL_BLOCK_X
  160.  
  161.     #Detect and determine types of Collisions
  162.     def detect_collision(self):
  163.         for block in pygame.sprite.spritecollide(self.ball, self.blocks, True):
  164.             self.break_sound.play()
  165.             return determine_collision(self.ball.rect, block.rect)
  166.  
  167.     #Check for Bar Deflection
  168.     def deflection(self):
  169.         for i in pygame.sprite.spritecollide(self.ball, self.bar_group, False):
  170.             self.bounce.play()
  171.             return True
  172.  
  173.     def start(self):
  174.         # TODO: Startup code here
  175.         pass
  176.  
  177.     def stop(self):
  178.         # TODO: Clean-up code here
  179.         pass
  180.  
  181.     def update(self, events):
  182.         # Regular Updates
  183.         self.bar_group.update()
  184.         self.ball_group.update()
  185.         self.blocks.update()
  186.  
  187.         # Set Bar Boundaries
  188.         x, _ = self.bar.rect.topleft
  189.         a, _ = self.bar.rect.topright
  190.         if x <= 0 or a >= locals.WIDTH:
  191.             self.bar.velocity = 0
  192.  
  193.         #Set Ball Boundaries
  194.         e, f = self.ball.rect.topleft
  195.         g, h = self.ball.rect.bottomright
  196.         if e <= 0 or g >= locals.WIDTH:
  197.             self.bounce.play()
  198.             self.ball.velocity_x *= -1
  199.         elif f <= 0:
  200.             self.ball.velocity_y *= -1
  201.             self.bounce.play()
  202.         elif h >= locals.HEIGHT:
  203.             self.lose()
  204.  
  205.         #Process Key Presses
  206.         for event in events:
  207.             if event.type == KEYUP:
  208.                 self.bar.velocity = 0
  209.             elif event.type == KEYDOWN and event.key == K_LEFT and x >= 0:
  210.                 self.bar.velocity = -1*MOVE_SPEED
  211.             elif event.type == KEYDOWN and event.key == K_RIGHT and a <= locals.WIDTH:
  212.                 self.bar.velocity = MOVE_SPEED
  213.  
  214.         #Process Collisions Between Ball and Bricks
  215.         reflection = self.detect_collision()
  216.         if  reflection == REFLECT_BOTH:
  217.             self.ball.velocity_y *=-1
  218.             self.ball.velocity_x *=-1
  219.         elif reflection == REFLECT_Y:
  220.             self.ball.velocity_y *= -1
  221.         elif reflection == REFLECT_X:
  222.             self.ball.velocity_x *= -1
  223.  
  224.         # Process collisions between bar and ball
  225.         if self.deflection():
  226.             self.ball.velocity_y *= -1
  227.  
  228.             # Random Change in X Velocity When Deflected
  229.             self.ball.velocity_x += randint(0, 10) * choice([-1, 1])
  230.             if self.ball.velocity_x < -7:
  231.                 self.ball.velocity_x = -1
  232.             elif self.ball.velocity_x > 7:
  233.                 self.ball.velocity_x = 7
  234.  
  235.         #Check for Win
  236.         if len(self.blocks) == 0:
  237.             self.win()
  238.  
  239.     def render(self, surface):
  240.         # TODO: Rendering code here
  241.         surface.fill(Color(0, 0, 0))
  242.         self.bar_group.draw(surface)
  243.         self.ball_group.draw(surface)
  244.         self.blocks.draw(surface)
  245.  
  246.     def get_timelimit(self):
  247.         # TODO: Return the time limit of this game (in seconds, 0 <= s <= 15)
  248.         return 120
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement