| import pygame |
| import random |
| import sys |
|
|
| |
| pygame.mixer.quit() |
|
|
| |
| pygame.init() |
|
|
| |
| screen_width, screen_height = 800, 600 |
| screen = pygame.display.set_mode((screen_width, screen_height)) |
| pygame.display.set_caption("Cat Ball Game") |
|
|
| |
| colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] |
|
|
| |
| balls = [] |
| for _ in range(3): |
| x = random.randint(20, screen_width - 20) |
| y = random.randint(20, screen_height - 20) |
| vx, vy = random.choice([-5, -4, -3, 3, 4, 5]), random.choice([-5, -4, -3, 3, 4, 5]) |
| color = random.choice(colors) |
| balls.append([x, y, vx, vy, color]) |
|
|
| clock = pygame.time.Clock() |
|
|
| def draw_balls(): |
| for ball in balls: |
| pygame.draw.circle(screen, ball[4], (ball[0], ball[1]), 20) |
|
|
| def move_balls(): |
| for ball in balls: |
| ball[0] += ball[2] |
| ball[1] += ball[3] |
| if ball[0] <= 20 or ball[0] >= screen_width - 20: |
| ball[2] = -ball[2] |
| if ball[1] <= 20 or ball[1] >= screen_height - 20: |
| ball[3] = -ball[3] |
|
|
| def remove_ball(pos): |
| for ball in balls: |
| distance = ((ball[0] - pos[0]) ** 2 + (ball[1] - pos[1]) ** 2) ** 0.5 |
| if distance < 20: |
| balls.remove(ball) |
| break |
|
|
| running = True |
| while running: |
| for event in pygame.event.get(): |
| if event.type == pygame.QUIT: |
| running = False |
|
|
| if event.type == pygame.MOUSEBUTTONDOWN: |
| remove_ball(pygame.mouse.get_pos()) |
|
|
| screen.fill((255,255,255)) |
| draw_balls() |
| move_balls() |
| pygame.display.flip() |
| clock.tick(60) |
|
|
| pygame.quit() |
| sys.exit() |
|
|