Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WIDTH = 1280
- HEIGHT = 720
- TITLE = 'BREAKOUT'
- CANVAS = Rect((0, 0), (WIDTH, HEIGHT))
- class Player(Actor):
- def __init__(self, image, pos, speed):
- Actor.__init__(self, image, pos)
- self.speed = speed
- self.vel = 0
- def update(self):
- self.vel = 0
- if keyboard.right:
- self.vel += self.speed
- if keyboard.left:
- self.vel -= self.speed
- self.x += self.vel
- class Ball(Actor):
- def __init__(self, image, pos, speed):
- Actor.__init__(self, image, pos)
- self.xspeed = speed
- self.yspeed = speed
- def update(self):
- self.x += self.xspeed
- self.y += self.yspeed
- if self.right >= WIDTH or self.left <= 0:
- self.xspeed = -self.xspeed
- if self.top <= 0:
- self.yspeed = -self.yspeed
- if self.bottom >= HEIGHT:
- print('GAME OVER')
- quit()
- if self.colliderect(paddle):
- if not paddle.vel == 0:
- self.xspeed = (self.xspeed) * (paddle.vel / (paddle.vel))
- self.xspeed = self.xspeed * 1.02
- self.yspeed = -self.yspeed
- for block in blocks:
- if self.colliderect(block):
- blocks.remove(block)
- self.yspeed = -self.yspeed
- if not len(blocks):
- print('YOU WIN')
- quit()
- def spawnBlocks(image, amount, spacing = 5):
- balls = []
- actor = Actor(image)
- ballsPerRow = int(WIDTH / (actor.width + spacing)) - 1
- rows = int(amount / ballsPerRow) + 1
- offset = (WIDTH - ballsPerRow * (actor.width + spacing)) / 2
- spawned_balls = 0
- for y in range(rows):
- for x in range(ballsPerRow):
- if spawned_balls == amount:
- break
- spawned_balls += 1
- pos = x * (actor.width + spacing) + offset, y * (actor.height + spacing) + spacing
- balls.append(Actor(image, topleft = pos))
- return balls
- paddle = Player('paddle', CANVAS.midbottom, 7)
- ball = Ball('masterball', CANVAS.center, 7)
- blocks = spawnBlocks('brick', 20, 5)
- def update():
- paddle.update()
- ball.update()
- def draw():
- screen.blit('background', (0, 0))
- paddle.draw()
- ball.draw()
- for block in blocks:
- block.draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement