Advertisement
Guest User

kek

a guest
Nov 21st, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.13 KB | None | 0 0
  1. # --- Import libraries used for this program
  2.  
  3. import math
  4. import pygame
  5.  
  6. # Define some colors
  7. black = (0, 0, 0)
  8. white = (255, 255, 255)
  9. blue = (0, 0, 255)
  10.  
  11. # Size of break-out blocks
  12. block_width = 23
  13. block_height = 15
  14.  
  15. class Block(pygame.sprite.Sprite):
  16.     """This class represents each block that will get knocked out by the ball
  17.    It derives from the "Sprite" class in Pygame """
  18.  
  19.     def __init__(self, color, x, y):
  20.         """ Constructor. Pass in the color of the block,
  21.            and its x and y position. """
  22.          
  23.         # Call the parent class (Sprite) constructor
  24.         super().__init__()
  25.          
  26.         # Create the image of the block of appropriate size
  27.         # The width and height are sent as a list for the first parameter.
  28.         self.image = pygame.Surface([block_width, block_height])
  29.          
  30.         # Fill the image with the appropriate color
  31.         self.image.fill(color)
  32.          
  33.         # Fetch the rectangle object that has the dimensions of the image
  34.         self.rect = self.image.get_rect()
  35.          
  36.         # Move the top left of the rectangle to x,y.
  37.         # This is where our block will appear..
  38.         self.rect.x = x
  39.         self.rect.y = y
  40.  
  41.  
  42. class Ball(pygame.sprite.Sprite):
  43.     """ This class represents the ball        
  44.        It derives from the "Sprite" class in Pygame """
  45.      
  46.     # Speed in pixels per cycle
  47.     speed = 10.0
  48.      
  49.     # Floating point representation of where the ball is
  50.     x = 0.0
  51.     y = 180.0
  52.      
  53.     # Direction of ball (in degrees)
  54.     direction = 200
  55.  
  56.     width = 10
  57.     height = 10
  58.      
  59.     # Constructor. Pass in the color of the block, and its x and y position
  60.     def __init__(self):
  61.         # Call the parent class (Sprite) constructor
  62.         super().__init__()
  63.          
  64.         # Create the image of the ball
  65.         self.image = pygame.Surface([self.width, self.height])
  66.          
  67.         # Color the ball
  68.         self.image.fill(white)
  69.          
  70.         # Get a rectangle object that shows where our image is
  71.         self.rect = self.image.get_rect()
  72.          
  73.         # Get attributes for the height/width of the screen
  74.         self.screenheight = pygame.display.get_surface().get_height()
  75.         self.screenwidth = pygame.display.get_surface().get_width()
  76.      
  77.     def bounce(self, diff):
  78.         """ This function will bounce the ball
  79.            off a horizontal surface (not a vertical one) """
  80.          
  81.         self.direction = (180 - self.direction) % 360
  82.         self.direction -= diff
  83.      
  84.     def update(self):
  85.         """ Update the position of the ball. """
  86.         # Sine and Cosine work in degrees, so we have to convert them
  87.         direction_radians = math.radians(self.direction)
  88.          
  89.         # Change the position (x and y) according to the speed and direction
  90.         self.x += self.speed * math.sin(direction_radians)
  91.         self.y -= self.speed * math.cos(direction_radians)
  92.          
  93.         # Move the image to where our x and y are
  94.         self.rect.x = self.x
  95.         self.rect.y = self.y
  96.          
  97.         # Do we bounce off the top of the screen?
  98.         if self.y <= 0:
  99.             self.bounce(0)
  100.             self.y = 1
  101.              
  102.         # Do we bounce off the left of the screen?
  103.         if self.x <= 0:
  104.             self.direction = (360 - self.direction) % 360
  105.             self.x = 1
  106.              
  107.         # Do we bounce of the right side of the screen?
  108.         if self.x > self.screenwidth - self.width:
  109.             self.direction = (360 - self.direction) % 360
  110.             self.x = self.screenwidth - self.width - 1
  111.          
  112.         # Did we fall off the bottom edge of the screen?
  113.         if self.y > 600:
  114.             return True
  115.         else:
  116.             return False
  117.  
  118. class Player(pygame.sprite.Sprite):
  119.     """ This class represents the bar at the bottom that the player controls. """
  120.      
  121.     def __init__(self):
  122.         """ Constructor for Player. """
  123.         # Call the parent's constructor
  124.         super().__init__()
  125.          
  126.         self.width = 75
  127.         self.height = 15
  128.         self.image = pygame.Surface([self.width, self.height])
  129.         self.image.fill((white))
  130.          
  131.         # Make our top-left corner the passed-in location.
  132.         self.rect = self.image.get_rect()
  133.         self.screenheight = pygame.display.get_surface().get_height()
  134.         self.screenwidth = pygame.display.get_surface().get_width()
  135.  
  136.         self.rect.x = 0
  137.         self.rect.y = self.screenheight-self.height
  138.      
  139.     def update(self):
  140.         """ Update the player position. """
  141.         # Get where the mouse is
  142.         pos = pygame.mouse.get_pos()
  143.         # Set the left side of the player bar to the mouse position
  144.         self.rect.x = pos[0]
  145.         # Make sure we don't push the player paddle
  146.         # off the right side of the screen
  147.         if self.rect.x > self.screenwidth - self.width:
  148.             self.rect.x = self.screenwidth - self.width
  149.  
  150. # Call this function so the Pygame library can initialize itself
  151. pygame.init()
  152.  
  153. # Create an 800x600 sized screen
  154. screen = pygame.display.set_mode([800, 600])
  155.  
  156. # Set the title of the window
  157. pygame.display.set_caption('Breakout')
  158.  
  159. # Enable this to make the mouse disappear when over our window
  160. pygame.mouse.set_visible(0)
  161.  
  162. # This is a font we use to draw text on the screen (size 36)
  163. font = pygame.font.Font(None, 36)
  164.  
  165. # Create a surface we can draw on
  166. background = pygame.Surface(screen.get_size())
  167.  
  168. # Create sprite lists
  169. blocks = pygame.sprite.Group()
  170. balls = pygame.sprite.Group()
  171. allsprites = pygame.sprite.Group()
  172.  
  173. # Create the player paddle object
  174. player = Player()
  175. allsprites.add(player)
  176.  
  177. # Create the ball
  178. ball = Ball()
  179. allsprites.add(ball)
  180. balls.add(ball)
  181.  
  182. # The top of the block (y position)
  183. top = 80
  184.  
  185. # Number of blocks to create
  186. blockcount = 32
  187.  
  188. # --- Create blocks
  189.  
  190. # Five rows of blocks
  191. for row in range(5):
  192.     # 32 columns of blocks
  193.     for column in range(0, blockcount):
  194.         # Create a block (color,x,y)
  195.         block = Block(blue, column * (block_width + 2) + 1, top)
  196.         blocks.add(block)
  197.         allsprites.add(block)
  198.     # Move the top of the next row down
  199.     top += block_height + 2
  200.  
  201. # Clock to limit speed
  202. clock = pygame.time.Clock()
  203.  
  204. # Is the game over?
  205. game_over = False
  206.  
  207. # Exit the program?
  208. exit_program = False
  209.  
  210. # Main program loop
  211. while exit_program != True:
  212.  
  213.     # Limit to 30 fps
  214.     clock.tick(30)
  215.  
  216.     # Clear the screen
  217.     screen.fill(black)
  218.      
  219.     # Process the events in the game
  220.     for event in pygame.event.get():
  221.         if event.type == pygame.QUIT:
  222.             exit_program = True
  223.      
  224.     # Update the ball and player position as long
  225.     # as the game is not over.
  226.     if not game_over:
  227.         # Update the player and ball positions
  228.         player.update()
  229.         game_over = ball.update()
  230.      
  231.     # If we are done, print game over
  232.     if game_over:
  233.         text = font.render("Game Over", True, white)
  234.         textpos = text.get_rect(centerx=background.get_width()/2)
  235.         textpos.top = 300
  236.         screen.blit(text, textpos)
  237.      
  238.     # See if the ball hits the player paddle
  239.     if pygame.sprite.spritecollide(player, balls, False):
  240.         # The 'diff' lets you try to bounce the ball left or right
  241.         # depending where on the paddle you hit it
  242.         diff = (player.rect.x + player.width/2) - (ball.rect.x+ball.width/2)
  243.          
  244.         # Set the ball's y position in case
  245.         # we hit the ball on the edge of the paddle
  246.         ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
  247.         ball.bounce(diff)
  248.      
  249.     # Check for collisions between the ball and the blocks
  250.     deadblocks = pygame.sprite.spritecollide(ball, blocks, True)
  251.      
  252.     # If we actually hit a block, bounce the ball
  253.     if len(deadblocks) > 0:
  254.         ball.bounce(0)
  255.          
  256.         # Game ends if all the blocks are gone
  257.         if len(blocks) == 0:
  258.             game_over = True
  259.      
  260.     # Draw Everything
  261.     allsprites.draw(screen)
  262.  
  263.     # Flip the screen and show what we've drawn
  264.     pygame.display.flip()
  265.  
  266. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement