Untitled
Anonymous
plain_text
01/27/2026 8:31 PM
3.2 KB
8
Indexable
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 600, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Block Blaster (Python)")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Paddle
paddle_width = 100
paddle_height = 10
paddle_x = (WIDTH - paddle_width) // 2
paddle_speed = 8
# Ball
ball_radius = 8
ball_x = WIDTH // 2
ball_y = HEIGHT - 30
ball_speed_x = 4 * random.choice((1, -1))
ball_speed_y = -4
# Blocks
block_width = 50
block_height = 20
blocks = []
for i in range(0, WIDTH - block_width, block_width + 10):
for j in range(50, 200, block_height + 10):
blocks.append(pygame.Rect(i, j, block_width, block_height))
clock = pygame.time.Clock()
running = True
score = 0
font = pygame.font.SysFont(None, 30)
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x > 0:
paddle_x -= paddle_speed
if keys[pygame.K_RIGHT] and paddle_x < WIDTH - paddle_width:
paddle_x += paddle_speed
# Move ball
ball_x += ball_speed_x
ball_y += ball_speed_y
# Wall collisions
if ball_x <= ball_radius or ball_x >= WIDTH - ball_radius:
ball_speed_x *= -1
if ball_y <= ball_radius:
ball_speed_y *= -1
# Paddle collision
paddle_rect = pygame.Rect(paddle_x, HEIGHT - 40, paddle_width, paddle_height)
if paddle_rect.collidepoint(ball_x, ball_y + ball_radius):
ball_speed_y *= -1
# Block collisions
for block in blocks[:]:
if block.collidepoint(ball_x, ball_y):
blocks.remove(block)
ball_speed_y *= -1
score += 10
# Bottom (missed)
if ball_y >= HEIGHT:
running = False
screen.fill(BLACK)
# Draw paddle
pygame.draw.rect(screen, WHITE, paddle_rect)
# Draw ball
pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)
# Draw blocks
for block in blocks:
pygame.draw.rect(screen, GREEN, block)
# Draw score
score_display = font.render("Score: " + str(score), True, WHITE)
screen.blit(score_display, (10, HEIGHT - 30))
pygame.display.flip()
pygame.quit()
Editor is loading...
Leave a Comment