import math
import randomimport pygame
import sys
class Ball:def __init__(self, radius):self.radius = radiusself.pos = [screen_width // 2, screen_height - 20 - radius]self.velocity = [2, -2]def draw(self, surface):pygame.draw.circle(surface, WHITE, self.pos, self.radius)def update(self):self.pos[0] += self.velocity[0]self.pos[1] += self.velocity[1]if self.pos[0] < self.radius or self.pos[0] > (screen_width - self.radius):self.velocity[0] *= -1if self.pos[1] <= self.radius or self.pos[1] > (screen_height - self.radius):self.velocity[1] *= -1
class Paddle:def __init__(self, width, height):self.width = widthself.height = heightself.pos = [screen_width // 2 - width // 2, screen_height - 20]self.velocity = [-5, 0]def draw(self, surface):pygame.draw.rect(surface, WHITE, (self.pos[0], self.pos[1], self.width, self.height), 0, 0)def update(self):keys = pygame.key.get_pressed()if keys[pygame.K_LEFT] and self.pos[0] > 0:self.pos[0] += self.velocity[0]if keys[pygame.K_RIGHT] and self.pos[0] < screen_width - self.width:self.pos[0] -= self.velocity[0]class Brick:def __init__(self, x, y, width, height, color):self.rect = pygame.Rect(x, y, width, height)self.color = colordef draw(self, surface):pygame.draw.rect(surface, self.color, self.rect)def check_collision(ball, brick):if (brick.rect.x - ball.radius <= ball.pos[0] <= brick.rect.x + brick.rect.width + ball.radius) and (brick.rect.y <= ball.pos[1] <= brick.rect.y + brick.rect.height):return 1 if (brick.rect.y - ball.radius <= ball.pos[1] <= brick.rect.y + brick.rect.height + ball.radius) and (brick.rect.x <= ball.pos[0] <= brick.rect.x + brick.rect.width):return 2 return 0def update_bricks(ball, bricks):score = 0for brick in bricks[:]:if check_collision(ball, brick) == 1:bricks.remove(brick)ball.velocity[0] *= -1score += 10breakelif check_collision(ball, brick) == 2:bricks.remove(brick)ball.velocity[1] *= -1score += 10breakreturn scoredef create_explosion(brick):passdef update_explosions(explosions, bricks):for explosion in explosions[:]:if explosion.is_finished():explosions.remove(explosion)if explosion.intersects(brick):bricks.remove(brick)if __name__ == '__main__':pygame.init()screen_width, screen_height = 640, 480screen = pygame.display.set_mode((screen_width, screen_height))pygame.display.set_caption('Bounce Game')clock = pygame.time.Clock()WHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)ball = Ball(10)paddle = Paddle(80, 10)bricks = []for x in range(0, screen_width, 80): for y in range(0, screen_height // 4, 20): brick = Brick(x + 2, y + 2, 80 - 2, 20 - 2, (255, 255, 255)) bricks.append(brick)score = 0running = Truewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseball.update()paddle.update()if ball.pos[1] + ball.radius > paddle.pos[1]:if ball.pos[0] < paddle.pos[0] or ball.pos[0] > paddle.pos[0] + paddle.width:score -= 1else:ss = abs(ball.pos[0] - (paddle.pos[0]+paddle.width//2)) / 20ball.velocity[0] = 2+ss*2ball.velocity[1] *= -1screen.fill(BLACK)ball.draw(screen)paddle.draw(screen)xx = random.randint(0, 255)for brick in bricks:brick.draw(screen)if ball.pos[1] <= screen_height//2:score += update_bricks(ball, bricks)font = pygame.font.Font(None, 36)score_text = font.render('Score: ' + str(score), True, RED)screen.blit(score_text, (10, 10))pygame.display.flip()clock.tick(60)pygame.quit()sys.exit()
