Here's the code:-
Code: Select all
import math
import pygame
import os
# Define colors
black = (0, 0, 0)
white = (255, 255, 255)
colors = [pygame.Color("red"), pygame.Color("orange"),
pygame.Color("yellow"), pygame.Color("green"),
pygame.Color("blue")]
block_width = 23
block_height = 15
# Load high score from file
def load_high_score():
if os.path.exists("highscore.txt"):
with open("highscore.txt", "r") as f:
return int(f.read())
return 0
# Save high score to file
def save_high_score(score):
with open("highscore.txt", "w") as f:
f.write(str(score))
class Block(pygame.sprite.Sprite):
def __init__(self, color, x, y):
super().__init__()
self.image = pygame.Surface([block_width, block_height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Ball(pygame.sprite.Sprite):
speed = 10.0
x = 400.0
y = 300.0
direction = 200
width = 10
height = 10
def __init__(self):
super().__init__()
self.image = pygame.Surface([self.width, self.height])
self.image.fill(white)
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
def bounce(self, diff):
self.direction = (180 - self.direction) % 360
self.direction -= diff
def update(self):
direction_radians = math.radians(self.direction)
self.x += self.speed * math.sin(direction_radians)
self.y -= self.speed * math.cos(direction_radians)
self.rect.x = self.x
self.rect.y = self.y
if self.y <= 0:
self.bounce(0)
self.y = 1
if self.x <= 0:
self.direction = (360 - self.direction) % 360
self.x = 1
if self.x > self.screenwidth - self.width:
self.direction = (360 - self.direction) % 360
self.x = self.screenwidth - self.width - 1
if self.y > self.screenheight:
return True
return False
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.width = 75
self.height = 15
self.image = pygame.Surface([self.width, self.height])
self.image.fill(white)
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
self.rect.x = 0
self.rect.y = self.screenheight - self.height
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0]
if self.rect.x > self.screenwidth - self.width:
self.rect.x = self.screenwidth - self.width
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Breakout')
pygame.mouse.set_visible(0)
font = pygame.font.Font(None, 36)
background = pygame.Surface(screen.get_size())
blocks = pygame.sprite.Group()
balls = pygame.sprite.Group()
allsprites = pygame.sprite.Group()
player = Player()
allsprites.add(player)
ball = Ball()
allsprites.add(ball)
balls.add(ball)
top = 80
blockcount = 32
# Create colored blocks
for row in range(5):
for column in range(blockcount):
color = colors[row % len(colors)]
block = Block(color, column * (block_width + 2) + 1, top)
blocks.add(block)
allsprites.add(block)
top += block_height + 2
clock = pygame.time.Clock()
game_over = False
exit_program = False
score = 0
high_score = load_high_score()
lives = 3
while not exit_program:
clock.tick(30)
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_program = True
if not game_over:
player.update()
if ball.update():
lives -= 1
if lives <= 0:
game_over = True
else:
ball.x = 400
ball.y = 300
ball.direction = 200
if game_over:
text = font.render("Game Over", True, white)
textpos = text.get_rect(centerx=background.get_width()/2)
textpos.top = 220
screen.blit(text, textpos)
score_text = font.render(f"Score: {score}", True, white)
screen.blit(score_text, (300, 270))
high_score_text = font.render(f"High Score: {high_score}", True, white)
screen.blit(high_score_text, (300, 310))
lives_left = font.render(f"Lives Left: {lives}", True, white)
screen.blit(lives_left, (300, 350))
if score > high_score:
high_score = score
save_high_score(high_score)
if pygame.sprite.spritecollide(player, balls, False):
diff = (player.rect.x + player.width / 2) - (ball.rect.x + ball.width / 2)
ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
ball.bounce(diff)
deadblocks = pygame.sprite.spritecollide(ball, blocks, True)
if len(deadblocks) > 0:
ball.bounce(0)
score += len(deadblocks) * 10
if len(blocks) == 0:
game_over = True
allsprites.draw(screen)
if not game_over:
score_text = font.render(f"Score: {score}", True, white)
screen.blit(score_text, (10, 10))
high_score_text = font.render(f"High Score: {high_score}", True, white)
screen.blit(high_score_text, (10, 40))
lives_text = font.render(f"Lives: {lives}", True, white)
screen.blit(lives_text, (10, 70))
pygame.display.flip()
pygame.quit()