-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame-gemini-2.0.py
109 lines (84 loc) · 3.66 KB
/
pygame-gemini-2.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import pygame
import math
# Initialize Pygame
pygame.init()
# Screen dimensions
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Concentric Circles Animation")
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
# Outer circle properties
outer_radius = 200
outer_center = [width // 2, height // 2] # Changed to list for mutability
outer_rotation_speed = 0.01 # Radians per frame
outer_angle = 0
outer_velocity = [1, 0.5] # Initial velocity for outer circle movement
outer_speed = 0.5
# Inner circle properties
inner_radius = 20
inner_position = [outer_center[0] + outer_radius / 2, outer_center[1]] # Initial position
inner_velocity = [2, 3] # Initial velocity
inner_speed = 5
# Function to handle collision and bounce (inner circle with outer circle)
def handle_inner_collision(inner_position, inner_velocity, outer_center, outer_radius):
# Calculate the vector from the outer center to the inner circle's position
distance_x = inner_position[0] - outer_center[0]
distance_y = inner_position[1] - outer_center[1]
# Calculate the distance between the centers
distance = math.sqrt(distance_x**2 + distance_y**2)
# Check for collision
if distance + inner_radius > outer_radius:
# Calculate the collision normal
normal_x = distance_x / distance
normal_y = distance_y / distance
# Reflect the velocity vector
dot_product = inner_velocity[0] * normal_x + inner_velocity[1] * normal_y
inner_velocity[0] -= 2 * dot_product * normal_x
inner_velocity[1] -= 2 * dot_product * normal_y
# Move the inner circle out of the collision
overlap = distance + inner_radius - outer_radius
inner_position[0] -= overlap * normal_x
inner_position[1] -= overlap * normal_y
return inner_position, inner_velocity
# Function to handle outer circle collision with screen edges
def handle_outer_collision(outer_center, outer_velocity, width, height, outer_radius):
if outer_center[0] + outer_radius > width or outer_center[0] - outer_radius < 0:
outer_velocity[0] *= -1 # Reverse x velocity
if outer_center[1] + outer_radius > height or outer_center[1] - outer_radius < 0:
outer_velocity[1] *= -1 # Reverse y velocity
return outer_center, outer_velocity
# Game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill(black)
# Update outer circle rotation
outer_angle += outer_rotation_speed
# Update outer circle position
outer_center[0] += outer_velocity[0] * outer_speed
outer_center[1] += outer_velocity[1] * outer_speed
# Handle outer circle collision with screen edges
outer_center, outer_velocity = handle_outer_collision(outer_center, outer_velocity, width, height, outer_radius)
# Update inner circle position
inner_position[0] += inner_velocity[0]
inner_position[1] += inner_velocity[1]
# Handle collision (inner circle with outer circle)
inner_position, inner_velocity = handle_inner_collision(inner_position, inner_velocity, outer_center, outer_radius)
# Draw outer circle
pygame.draw.circle(screen, white, (int(outer_center[0]), int(outer_center[1])), outer_radius, 1)
# Draw inner circle
pygame.draw.circle(screen, red, (int(inner_position[0]), int(inner_position[1])), inner_radius)
# Update the display
pygame.display.flip()
# Control the frame rate
clock.tick(60)
# Quit Pygame
pygame.quit()