-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheel_rotation.py
149 lines (116 loc) · 4.85 KB
/
wheel_rotation.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pygame
import math
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Cyclopic Balance')
# Colors
BLACK = (0, 0, 0)
GREY = (192, 192, 192)
RED = (155, 100, 50)
YELLOW = (100, 100, 100)
# Tire properties
tire_pos_1 = (200, 400)
tire_pos_2 = (600, 400)
tire_radius = 100
# Rim properties
rim_radius = 85
rim_thickness = 15
rim_angle_1 = 0 # Starting angle
rim_angle_2 = 20 # Starting angle
rotation_speed_1 = 1 # Degrees per frame
rotation_speed_2 = 0.5 # Degrees per frame
# Load the rim image
rim_image_path = 'rim_1.png' # Replace with your image file path
rim_image = pygame.image.load(rim_image_path)
rim_image = pygame.transform.scale(rim_image, (2 * rim_radius, 2 * rim_radius))
# Load the rim image
tire_image_path = 'tire_1.png' # Replace with your image file path
tire_image = pygame.image.load(tire_image_path)
tire_image = pygame.transform.scale(tire_image, (2 * tire_radius, 2 * tire_radius))
# Shaft properties
shaft_length = 120
shaft_width = 25
#base properties
base_length = 400
base_width = 25
# Rotation state
rotate_clockwise = True
max_rotation = 90
# Game loop flag
running = True
# Clock to control frame rate
clock = pygame.time.Clock()
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the screen
screen.fill((255, 255, 255))
shaft_base_pos_1 = (tire_pos_1[0] - (math.cos(math.radians((rim_angle_1))) * rim_radius)+10, tire_pos_1[1] + (math.sin(math.radians((rim_angle_1))) * rim_radius))
shaft_base_pos_2 = (tire_pos_2[0] - (math.cos(math.radians((rim_angle_2))) * rim_radius)+10, tire_pos_2[1] + (math.sin(math.radians((rim_angle_2))) * rim_radius))
shaft_end_pos_1 = (shaft_base_pos_1[0],shaft_base_pos_1[1]- shaft_length)
shaft_end_pos_2 = (shaft_base_pos_2[0],shaft_base_pos_2[1]- shaft_length)
# Draw the shaft
shaft_rect_1 = pygame.Rect(shaft_base_pos_1[0] - shaft_width // 2,
shaft_base_pos_1[1] - shaft_length,
shaft_width,
shaft_length)
shaft_rect_2 = pygame.Rect(shaft_base_pos_2[0] - shaft_width // 2,
shaft_base_pos_2[1] - shaft_length,
shaft_width,
shaft_length)
pygame.draw.rect(screen, RED, shaft_rect_1)
pygame.draw.rect(screen, RED, shaft_rect_2)
# Blit the tire image onto the screen at the tire positions
tire_rect_1 = tire_image.get_rect(center=tire_pos_1)
tire_rect_2 = tire_image.get_rect(center=tire_pos_2)
screen.blit(tire_image, tire_rect_1.topleft)
screen.blit(tire_image, tire_rect_2.topleft)
# Rotate the rim
if rotate_clockwise:
rim_angle_1 += rotation_speed_1
if rim_angle_1 >= max_rotation:
rotate_clockwise = False
else:
rim_angle_1 -= rotation_speed_1
if rim_angle_1 <= -max_rotation:
rotate_clockwise = True
# Rotate the rim
if rotate_clockwise:
rim_angle_2 += rotation_speed_2
if rim_angle_2 >= max_rotation:
rotate_clockwise = False
else:
rim_angle_2 -= rotation_speed_2
if rim_angle_2 <= -max_rotation:
rotate_clockwise = True
# Create a surface for the rim to rotate
rim_surface_1 = pygame.Surface((2 * rim_radius, 2 * rim_radius), pygame.SRCALPHA)
pygame.draw.circle(rim_surface_1, GREY, (rim_radius, rim_radius), rim_radius, rim_thickness)
# Rotate the rim
rotated_rim_1 = pygame.transform.rotate(rim_image, rim_angle_1)
rotated_rim_2 = pygame.transform.rotate(rim_image, rim_angle_2)
# Get the new rect for the rotated rim to blit at the correct position
rotated_rect_1 = rotated_rim_1.get_rect(center=tire_pos_1)
# Get the new rect for the rotated rim to blit at the correct position
rotated_rect_2 = rotated_rim_2.get_rect(center=tire_pos_2)
base_rect = pygame.Rect(shaft_end_pos_1[0],
shaft_end_pos_1[1] - base_width,
base_length,
base_width )
pygame.draw.line(screen,YELLOW, (shaft_end_pos_1[0]- shaft_width/2,
shaft_end_pos_1[1]-base_width/2 ), (shaft_base_pos_2[0]+shaft_width/2,shaft_base_pos_2[1]- shaft_length-base_width/2),20)
# Blit the rotated rim onto the screen
screen.blit(rotated_rim_1, rotated_rect_1.topleft)
screen.blit(rotated_rim_2, rotated_rect_2.topleft)
#pygame.draw.rect(screen, YELLOW, base_rect)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(10)
# Quit Pygame
pygame.quit()