You are on page 1of 4

import pygame

import sys

# Cores
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
PURPLE = (128, 0, 128)
CYAN = (0, 255, 255)

# Cores para os blocos


BLOCK_COLORS = [RED, GREEN, YELLOW, PURPLE, CYAN]

# Configurações do jogo
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BALL_RADIUS = 10
PADDLE_WIDTH = 60
PADDLE_HEIGHT = 15
PADDLE_SPEED = 2
BALL_SPEED = 2
BLOCK_WIDTH = 60
BLOCK_HEIGHT = 20
BLOCK_ROWS = 5
BLOCK_COLS = 10
LIVES = 3

# Inicializando o Pygame e criando uma janela


pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
font = pygame.font.Font(None, 36)
clock = pygame.time.Clock()

# Níveis
levels = [
[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
],
[
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
],
#... add more levels if desired
]

# Classe para a raquete


class Paddle:
def __init__(self):
self.rect = pygame.Rect((SCREEN_WIDTH - PADDLE_WIDTH) // 2, SCREEN_HEIGHT -
PADDLE_HEIGHT - 10, PADDLE_WIDTH, PADDLE_HEIGHT)
self.speed = PADDLE_SPEED
self.direction = 0

def update(self):
self.rect.x += self.direction * self.speed
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH

def draw(self):
pygame.draw.rect(screen, BLUE, self.rect)

# Classe para a bola


class Ball:
def __init__(self, x, y, dx, dy, speed):
self.rect = pygame.Rect(x, y, BALL_RADIUS * 2, BALL_RADIUS * 2)
self.dx = dx
self.dy = dy
self.speed = speed

def update(self):
self.rect.x += self.dx * self.speed
self.rect.y += self.dy * self.speed
if self.rect.left < 0 or self.rect.right > SCREEN_WIDTH:
self.dx *= -1
if self.rect.top < 0:
self.dy *= -1
elif self.rect.bottom > SCREEN_HEIGHT:
self.dy *= -1

def draw(self):
pygame.draw.circle(screen, WHITE, self.rect.center, BALL_RADIUS)

# Classe para os blocos


class Block:
def __init__(self, x, y, color):
self.rect = pygame.Rect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT)
self.color = color

def draw(self):
pygame.draw.rect(screen, self.color, self.rect)

# Criando objetos do jogo


paddle = Paddle()
balls = [Ball(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, 1, -1, BALL_SPEED)]
blocks = []
for i, row in enumerate(levels[0]):
color = BLOCK_COLORS[i % len(BLOCK_COLORS)]
for j, cell in enumerate(row):
if cell != 0:
blocks.append(Block(j * (BLOCK_WIDTH + 3), i * (BLOCK_HEIGHT + 3),
color))

lives = LIVES
score = 0
current_level = 0

# Loop principal do jogo


running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
paddle.direction = -1
elif event.key == pygame.K_RIGHT:
paddle.direction = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and paddle.direction == -1:
paddle.direction = 0
elif event.key == pygame.K_RIGHT and paddle.direction == 1:
paddle.direction = 0

screen.fill((0, 0, 0))

paddle.update()
paddle.draw()

for ball in balls:


ball.update()
ball.draw()

if paddle.rect.colliderect(ball.rect):
ball.dy *= -1

for block in blocks[:]:


if ball.rect.colliderect(block.rect):
ball.dy *= -1
blocks.remove(block)
score += 1

if ball.rect.bottom > SCREEN_HEIGHT:


balls.remove(ball)
lives -= 1
if lives > 0:
balls.append(Ball(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, 1, -1,
BALL_SPEED + current_level))

if not blocks:
current_level += 1
if current_level >= len(levels):
print("You won!")
running = False
else:
balls = [Ball(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, 1, -1, BALL_SPEED
+ current_level)]
blocks = []
for i, row in enumerate(levels[current_level]):
color = BLOCK_COLORS[i % len(BLOCK_COLORS)]
for j, cell in enumerate(row):
if cell != 0:
blocks.append(Block(j * (BLOCK_WIDTH + 3), i *
(BLOCK_HEIGHT + 3), color))

for block in blocks:


block.draw()
text = font.render(f'Score: {score} Lives: {lives}', True, WHITE)
screen.blit(text, (20, 20))

pygame.display.flip()
clock.tick(60)

if lives <= 0:
print("Game Over!")
running = False

pygame.quit()
sys.exit()

You might also like