# !/usr/bin/env python3 import pygame, random, math from ball import Ball from brick import Brick from paddle import Paddle pygame.init() WHITE = (255, 255, 255) DARKBLUE = (0, 0, 139) Screen_Width = 800 Screen_Height = 600 screen = pygame.display.set_mode((Screen_Width, Screen_Height)) pygame.display.set_caption("Breakout") pygame.mixer.init() pygame.mixer.music.load('images/Ketsa - Holding The Line.mp3') pygame.mixer.music.set_volume(0.7) pygame.mixer.music.play(-1) def main(): try: paddle = Paddle(screen.get_rect()) paddlegroup = pygame.sprite.Group() paddlegroup.add(paddle) ball = Ball() ballgroup = pygame.sprite.Group() ballgroup.add(ball) brickgroup = pygame.sprite.Group() score = 0 lives = 5 brick_coord_list = [ 32, 64, 32, 96, 32, 128, 32, 160 ] i = 0 while i < len(brick_coord_list): brick = Brick(brick_coord_list[i], brick_coord_list[i + 1]) brickgroup.add(brick) i = i + 2 # to control how fast the screen updates clock = pygame.time.Clock() while True: # Limit to 60 frames per second clock.tick(60) # update object lives = ball.move(lives) if lives == 0: font = pygame.font.Font(None, 74) text = font.render("Game over", 1, WHITE) screen.blit(text, (250, 300)) pygame.display.flip() pygame.time.wait(3000) # stop the game return # check for collisions # ball / paddle if ball.collpaddle(paddlegroup): ball.dy *= -1 # ball / brick for brick in brickgroup: if ball.collbrick(brickgroup): ball.dy *= -1 score = brick.takehit(score) if len(brickgroup) == 0: font = pygame.font.Font(None, 74) text = font.render("Level complete", 1, WHITE) screen.blit(text, (200, 300)) pygame.display.flip() pygame.time.wait(3000) # stop the game return screen.fill(DARKBLUE) font = pygame.font.Font(None, 34) text = font.render("Score: " + str(score), 1, WHITE) screen.blit(text, (20, 10)) text = font.render("Lives: " + str(lives), 1, WHITE) screen.blit(text, (650, 10)) for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.MOUSEMOTION: paddle.update2(event.pos) pygame.display.flip() finally: pygame.quit() if __name__ == "__main__": main()