I’m creating a game with python/pygame and I’m pretty new into this. Using a lot of ChatGPT-4o to help me.
The game should work like this: There is a jumping ball inside this circle and if the ball falls out of the circle through the gap, basically disappears, then new 2 balls will spawn and at the end it the circle will overflow with balls.
But I’m not getting it done creating the circle with a moving gap in it. The gap is just not being seen but is is there and it’s definitely not rotating. Every time I tell ChatGPT to solve the problem it will do the same code again and again.
Does anybody has a way to create the circle with the rotating gap?
Currently the gap is getting created somewhere, but I have no clue where. The first ball just falls down into nowhere and this triggers new 2 balls but 1 is falling out aswell and this continues, but the big problem is that the balls jump against the created gap but they wont fall through it because its not there. Probably not rotating with the circle.
It should work like this: the circle should rotate all the time with a gap in it. Now if the first ball gets through the hole/gap in the circle, it should trigger that 2 new balls who are a bit bigger are spawning in the middle of the circle. This should continue until the whole circle is full of balls.
`# Initial ball position, velocity, and acceleration due to gravity
ball_x, ball_y = WIDTH // 2, HEIGHT // 2 - 150
velocity_x, velocity_y = 1, 0
ball_radius = RADIUS # Current radius of the ball
collision_count = 0 # Initialize collision counter
# Font for the counter
font = pygame.font.Font(None, 36)
running = True
color_index = 0
color_progress = 0
prev_color = colors[color_index]
inner_gap_start_angle = 0 # Initial angle of the gap
def draw_circle_with_gap(surface, color, center, radius, width, gap_angle, start_angle):
end_angle = start_angle + (360 - gap_angle)
start_rad = math.radians(start_angle)
end_rad = math.radians(end_angle)
rect = pygame.Rect(center[0] - radius, center[1] - radius, radius * 2, radius * 2)
pygame.draw.arc(surface, color, rect, start_rad, end_rad, width)
def spawn_new_balls():
global ball_radius
new_radius = ball_radius + GROWTH_RATE
return [
{"x": WIDTH // 2, "y": HEIGHT // 2, "vx": 1, "vy": -1, "radius": new_radius},
{"x": WIDTH // 2, "y": HEIGHT // 2, "vx": -1, "vy": -1, "radius": new_radius}
]
new_balls = []
time.sleep(0.5)
while running:
screen.fill(BLACK) # Set the background color to black
# Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
prev_x = ball_x
prev_y = ball_y
prev_radius = ball_radius
# Apply gravity
velocity_y += GRAVITY
# Update ball position
ball_x += velocity_x
ball_y += velocity_y
# Check for boundary collision
distance = math.sqrt((ball_x - WIDTH // 2) ** 2 + (ball_y - HEIGHT // 2) ** 2)
angle_to_ball = math.degrees(math.atan2(ball_y - HEIGHT // 2, ball_x - WIDTH // 2)) % 360
gap_end_angle = (inner_gap_start_angle + GAP_SIZE) % 360
if distance + ball_radius > BOUNDARY_RADIUS:
if inner_gap_start_angle <= angle_to_ball <= gap_end_angle or (inner_gap_start_angle <= (angle_to_ball + 360) % 360 <= gap_end_angle):
new_balls = spawn_new_balls()
ball_radius = 0
ball_x, ball_y = WIDTH // 2, HEIGHT // 2 - 150
velocity_x, velocity_y = 1, 0
else:
SOUNDS[SONG[index]].play()
if index == len(SONG) - 1:
index = 0
else:
index += 1
# Adjust ball position and simulate bounce
normal_x = ball_x - WIDTH // 2
normal_y = ball_y - HEIGHT // 2
normal_length = math.sqrt(normal_x ** 2 + normal_y ** 2)
normal_x /= normal_length
normal_y /= normal_length
# Calculate dot product for computing new velocities
dot_product = velocity_x * normal_x + velocity_y * normal_y
# Update velocities for reflection
velocity_x -= 2 * dot_product * normal_x
velocity_y -= 2 * dot_product * normal_y
# Calculate exact collision point
collision_x = int(WIDTH // 2 + (BOUNDARY_RADIUS - BORDER_WIDTH) * normal_x)
collision_y = int(HEIGHT // 2 + (BOUNDARY_RADIUS - BORDER_WIDTH) * normal_y)
collision_points.append((collision_x, collision_y))
ball_x = int(WIDTH // 2 + (BOUNDARY_RADIUS - ball_radius - 9) * normal_x)
ball_y = int(HEIGHT // 2 + (BOUNDARY_RADIUS - ball_radius - 9) * normal_y)
ball_radius += GROWTH_RATE
# Increment collision counter
collision_count += 1
# Update new balls positions and check collisions for new balls
for new_ball in new_balls:
new_ball["vy"] += GRAVITY
new_ball["x"] += new_ball["vx"]
new_ball["y"] += new_ball["vy"]
# Check for boundary collision for new balls
distance = math.sqrt((new_ball["x"] - WIDTH // 2) ** 2 + (new_ball["y"] - HEIGHT // 2) ** 2)
angle_to_ball = math.degrees(math.atan2(new_ball["y"] - HEIGHT // 2, new_ball["x"] - WIDTH // 2)) % 360
gap_end_angle = (inner_gap_start_angle + GAP_SIZE) % 360
if distance + new_ball["radius"] > BOUNDARY_RADIUS:
if inner_gap_start_angle <= angle_to_ball <= gap_end_angle or (inner_gap_start_angle <= (angle_to_ball + 360) % 360 <= gap_end_angle):
new_balls.remove(new_ball) # Ball flies out, remove it
else:
normal_x = new_ball["x"] - WIDTH // 2
normal_y = new_ball["y"] - HEIGHT // 2
normal_length = math.sqrt(normal_x ** 2 + normal_y ** 2)
normal_x /= normal_length
normal_y /= normal_length
dot_product = new_ball["vx"] * normal_x + new_ball["vy"] * normal_y
new_ball["vx"] -= 2 * dot_product * normal_x
new_ball["vy"] -= 2 * dot_product * normal_y
new_ball["x"] = int(WIDTH // 2 + (BOUNDARY_RADIUS - new_ball["radius"] - 9) * normal_x)
new_ball["y"] = int(HEIGHT // 2 + (BOUNDARY_RADIUS - new_ball["radius"] - 9) * normal_y)
# Draw the ball with a white border
if ball_radius > 0:
pygame.draw.circle(screen, WHITE, (ball_x, ball_y), ball_radius + 1)
pygame.draw.circle(screen, (r, g, b), (ball_x, ball_y), ball_radius)
# Draw the new balls with a white border
for new_ball in new_balls:
pygame.draw.circle(screen, WHITE, (new_ball["x"], new_ball["y"]), new_ball["radius"] + 1)
pygame.draw.circle(screen, (r, g, b), (new_ball["x"], new_ball["y"]), new_ball["radius"])
# Render the collision counter text
counter_text = font.render(str(collision_count), True, BLACK)
counter_rect = counter_text.get_rect(center=(ball_x, ball_y))
screen.blit(counter_text, counter_rect)
# Draw the boundary with the gap
draw_circle_with_gap(screen, (r, g, b), (WIDTH // 2, HEIGHT // 2), BOUNDARY_RADIUS, BORDER_WIDTH, GAP_SIZE, inner_gap_start_angle)
# Update gap start angle for rotation
inner_gap_start_angle = (inner_gap_start_angle + 1) % 360 # Rotate circle clockwise
pygame.display.flip()
clock.tick(120)
pygame.quit()`
TimRo21 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.