Untitled
unknown
plain_text
2 years ago
2.8 kB
15
Indexable
import pygame
import sys
import math
import os
import random
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bouncing Neon Ball with Sound")
boundary_radius = min(width, height) // 2 - 50
ball_radius = 20
initial_ball_radius = ball_radius
ball_speed = 10
ball_speed_increment = 1
gravity = 0.8
ball_pos = [width // 2, height // 2]
ball_direction = 45
vertical_speed = 0
mp3_files = [f for f in os.listdir() if f.endswith('.mp3')]
if mp3_files:
sound_file = random.choice(mp3_files)
pygame.mixer.init()
pygame.mixer.music.load(sound_file)
else:
print("No MP3 files found in directory.")
sys.exit()
bounce_count = 0
font = pygame.font.Font(None, 36)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
distance_to_center = math.sqrt((ball_pos[0] - width // 2)**2 + (ball_pos[1] - height // 2)**2)
if distance_to_center + ball_radius > boundary_radius:
ball_direction = math.degrees(math.atan2(height // 2 - ball_pos[1], width // 2 - ball_pos[0]))
velocity = math.sqrt(ball_speed ** 2 + vertical_speed ** 2)
angle = math.atan2(vertical_speed, ball_speed)
angle = -angle
ball_speed = velocity * math.cos(angle)
vertical_speed = velocity * math.sin(angle)
bounce_count += 1
ball_radius = max(initial_ball_radius, int(ball_radius * 0.9))
neon_color_boundary = [int(127 * math.sin(pygame.time.get_ticks() / 500) + 128),
int(127 * math.sin(pygame.time.get_ticks() / 700) + 128),
int(127 * math.sin(pygame.time.get_ticks() / 900) + 128)]
neon_color_ball = [int(127 * math.sin(pygame.time.get_ticks() / 500) + 128),
int(127 * math.sin(pygame.time.get_ticks() / 700) + 128),
int(127 * math.sin(pygame.time.get_ticks() / 900) + 128)]
ball_pos[0] += int(ball_speed * math.cos(math.radians(ball_direction)))
ball_pos[1] += int(vertical_speed)
vertical_speed += gravity
screen.fill((0, 0, 0)) #black background
pygame.draw.circle(screen, neon_color_boundary, (width // 2, height // 2), boundary_radius, 2)
pygame.draw.circle(screen, neon_color_ball, (ball_pos[0], ball_pos[1]), ball_radius)
text = font.render(f"Bounces: {bounce_count}", True, (255, 255, 255))
text_rect = text.get_rect(center=(width // 2, height // 2 + boundary_radius + 30))
screen.blit(text, text_rect)
if not pygame.mixer.music.get_busy():
pygame.mixer.music.load(random.choice(mp3_files))
pygame.mixer.music.play()
pygame.display.flip()
pygame.time.Clock().tick(60)
Editor is loading...
Leave a Comment