Untitled
unknown
plain_text
a month ago
1.9 kB
7
Indexable
import pygame
import random
pygame.init()
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ball Game")
clock = pygame.time.Clock()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
score = 0
time_left = 20
font = pygame.font.SysFont(None, 36)
pygame.time.set_timer(pygame.USEREVENT, 1000)
balls = []
def create_ball():
return {
"x": random.randint(50, 750),
"y": HEIGHT + 20,
"r": 20,
"speed": random.randint(3, 6)
}
# створюємо стартові кульки
for i in range(10):
balls.append(create_ball())
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.USEREVENT:
time_left -= 1
if time_left <= 0:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
mx, my = pygame.mouse.get_pos()
for b in balls:
dx = mx - b["x"]
dy = my - b["y"]
dist = (dx*dx + dy*dy) ** 0.5
if dist < b["r"]:
score += 1
balls.remove(b)
balls.append(create_ball())
break
for b in balls:
b["y"] -= b["speed"]
if b["y"] < -30:
balls.remove(b)
balls.append(create_ball())
screen.fill(WHITE)
for b in balls:
pygame.draw.circle(screen, RED, (b["x"], b["y"]), b["r"])
# текст
text1 = font.render("Score: " + str(score), True, BLACK)
text2 = font.render("Time: " + str(time_left), True, BLACK)
screen.blit(text1, (20, 20))
screen.blit(text2, (20, 60))
pygame.display.update()
pygame.quit()Editor is loading...
Leave a Comment