Advertisement
GabeLinux

Python Gaming - 6 - 2

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