GabeLinux

Python Gaming - 6 - 1

Feb 14th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. WIDTH = 1280
  2. HEIGHT = 720
  3.  
  4. TITLE = 'BREAKOUT'
  5.  
  6. CANVAS = Rect((0, 0), (WIDTH, HEIGHT))
  7.  
  8. class Player(Actor):
  9.     def __init__(self, image, pos, speed):
  10.         Actor.__init__(self, image, pos)
  11.  
  12.         self.speed = speed
  13.         self.vel = 0
  14.  
  15.     def update(self):
  16.         self.vel = 0
  17.  
  18.         if keyboard.right:
  19.             self.vel += self.speed
  20.  
  21.         if keyboard.left:
  22.             self.vel -= self.speed
  23.  
  24. class Ball(Actor):
  25.     def __init__(self, image, pos, speed):
  26.         Actor.__init__(self, image, pos)
  27.  
  28.         self.xspeed = speed
  29.         self.yspeed = speed
  30.  
  31.     def update(self):
  32.         self.x += self.xspeed
  33.         self.y += self.yspeed
  34.  
  35.         if self.right >= WIDTH or self.left <= 0:
  36.             self.xspeed = -self.xspeed
  37.         if self.top <= 0:
  38.             self.yspeed = -self.yspeed
  39.  
  40.         if self.bottom >= HEIGHT:
  41.             print('GAME OVER')
  42.             quit()
  43.  
  44.         if self.colliderect(paddle):
  45.             self.yspeed = -self.yspeed
  46.  
  47.         for block in blocks:
  48.             if self.colliderect(block):
  49.                 blocks.remove(block)
  50.                 self.yspeed = -self.yspeed
  51.                 if not len(blocks):
  52.                     print('YOU WIN')
  53.                     quit()
  54.  
  55. def spawnBlocks(image, amount, spacing = 5):
  56.     balls = []
  57.  
  58.     actor = Actor(image)
  59.     ballsPerRow = int(WIDTH / (actor.width + spacing)) - 1
  60.     rows = int(amount / ballsPerRow) + 1
  61.     offset = (WIDTH - ballsPerRow * (actor.width + spacing)) / 2
  62.  
  63.     spawned_balls = 0
  64.  
  65.     for y in range(rows):
  66.         for x in range(ballsPerRow):
  67.             if spawned_balls == amount:
  68.                 break
  69.  
  70.             spawned_balls += 1
  71.             pos = x * (actor.width + spacing) + offset, y * (actor.height + spacing) + spacing
  72.             balls.append(Actor(image, topleft = pos))
  73.  
  74.     return balls
  75.  
  76. paddle = Player('paddle', CANVAS.midbottom, 7)
  77. ball = Ball('masterball', CANVAS.center, 7)
  78.  
  79. blocks = spawnBlocks('brick', 20, 5)
  80.  
  81. def update():
  82.     paddle.update()
  83.     ball.update()
  84.  
  85. def draw():
  86.     screen.blit('background', (0, 0))
  87.  
  88.     paddle.draw()
  89.     ball.draw()
  90.  
  91.     for block in blocks:
  92.         block.draw()
Advertisement
Add Comment
Please, Sign In to add comment