-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
322 lines (260 loc) · 10.3 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Try to import pygame, if pygame fails to import, raise an error.
try:
import pygame
except ImportError:
raise ImportError("ERR: Failed to import pygame. Please install pygame.")
import math
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
YELLOW = (255,255,0)
GREEN = (0,255,0)
# Define sprite classes
# Define class for circle sprite
class Circle(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
# set up sprite surface
self.image = pygame.Surface([500, 500])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# draw the circle
pygame.draw.circle(self.image, BLUE, (pos_x // 2, pos_y // 2), 200, 5)
self.rect = self.image.get_rect()
# define class for the line across the x axis
class XLine(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
# set up sprite surface
self.image = pygame.Surface([500, 700])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# draw the line
pygame.draw.rect(self.image, RED, (pos_x, pos_y, 5, 400))
self.rect = self.image.get_rect()
# define class for the line across the y axis
class YLine(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
# set up sprite surface
self.image = pygame.Surface([500, 700])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# draw the line
pygame.draw.rect(self.image, RED, (pos_x, pos_y, 400, 5))
self.rect = self.image.get_rect()
# define class for the marker that moves across the x marker
class XMarker(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
# set up variables
self.angle = 0
self.angle_speed = 0.05
self.pos_x = pos_x
self.pos_y = pos_y
# set up sprite surface
self.image = pygame.Surface([600, 600])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# draw the marker
pygame.draw.circle(self.image, YELLOW, (self.pos_x // 2, self.pos_y // 2), 15)
self.rect = self.image.get_rect()
# define method to move the x marker
def move(self):
# set up variables
radius = 400
center = 600
# increase angle
self.angle += self.angle_speed
# do math
# This represents the following equation:
# x = c + cos(a) * r
# x = 600 + cos(angle) * 400
self.new_x = math.floor(center + math.cos(self.angle) * radius)
# apply new x value to marker
# clear the image
self.image.fill(BLACK)
# redraw the marker
pygame.draw.circle(self.image, YELLOW, (self.new_x // 2, self.pos_y // 2), 15)
# define class for the marker that will move along the y axis
class YMarker(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
# set up variables
self.angle = 0
self.angle_speed = 0.05
self.pos_x = pos_x
self.pos_y = pos_y
# set up sprite surface
self.image = pygame.Surface([600, 600])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# draw the marker
pygame.draw.circle(self.image, YELLOW, (self.pos_x // 2, self.pos_y // 2), 15)
self.rect = self.image.get_rect()
# define a method to move the y marker
def move(self):
# set variables
radius = 400
center = 600
self.angle += self.angle_speed
# do math
# this represents the equation:
# y = c + sin(a) * r
# y = 600 + sin(angle) * 400
self.new_y = math.floor(center + math.sin(self.angle) * radius)
# redraw the marker
self.image.fill(BLACK) # clear the sprite
pygame.draw.circle(self.image, YELLOW, (self.pos_x // 2, self.new_y // 2), 15) # redraw
# define class for the marker that will move along the circle
class CircleMarker(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__()
# set up variables
self.angle = 0
self.angle_speed = 0.05
# set up sprite surface
self.image = pygame.Surface([1000, 1000])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# draw marker
pygame.draw.circle(self.image, GREEN, (pos_x // 2, pos_y // 2), 15)
self.rect = self.image.get_rect()
# define method to move the circle marker
def move(self):
# define variables
radius = 400
self.center = 600
# increase angle
self.angle += self.angle_speed
# do math
# this represents the equation:
# x = c + cos(a) * r
# y = c + sin(a) * r
self.new_x = math.floor(self.center + math.cos(self.angle) * radius)
self.new_y = math.floor(self.center + math.sin(self.angle) * radius)
# redraw the marker w/ new coordinates
self.image.fill(BLACK) # clear the surface
pygame.draw.circle(self.image, GREEN, (self.new_x // 2, self.new_y // 2), 15) # redraw the marker
# define class for the text
class Text(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# define the font; arial at size 24
self.font = pygame.font.SysFont('Arial', 24)
self.crfont = pygame.font.SysFont('Arial', 14)
# define the surface
self.image = pygame.Surface([1000, 1000])
# fill with black and make transparent
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# define all the different text
# define the text
self.angle_text = self.font.render('Angle θ: ', False, WHITE)
# render it to the surface
self.image.blit(self.angle_text, (25, 25))
self.speed_text = self.font.render('Speed: ', False, WHITE)
self.image.blit(self.speed_text, (25, 50))
self.sin_text = self.font.render('SIN: ', False, WHITE)
self.image.blit(self.sin_text, (25, 75))
self.cos_text = self.font.render('COSINE: ', False, WHITE)
self.image.blit(self.cos_text, (25, 100))
self.cr_text = self.crfont.render('github.com/tommiecc', False, WHITE)
self.image.blit(self.cr_text, (600, 100))
self.rect = self.image.get_rect()
# method to update the text
def update_text(self, angle, speed, sin, cos):
# clear surface
self.image.fill(BLACK)
# update the text with new values
self.angle_text = self.font.render(f'Angle θ: {angle}', False, WHITE)
self.speed_text = self.font.render(f'Speed: {speed}', False, WHITE)
self.sin_text = self.font.render(f'SIN: {sin}', False, WHITE)
self.cos_text = self.font.render(f'COS: {cos}', False, WHITE)
self.cr_text = self.crfont.render('github.com/tommiecc', False, WHITE)
# render the text to the surface
self.image.blit(self.angle_text, (25, 25))
self.image.blit(self.speed_text, (25, 50))
self.image.blit(self.sin_text, (25, 75))
self.image.blit(self.cos_text, (25, 100))
self.image.blit(self.cr_text, (250, 550))
# function to determine the current angle of the location of the circle marker in respect to it's place around the circle.
def determine_angle(y, x, c):
# determine the current angle of the circle
angle = math.floor((math.atan2(( y - c ), ( x - c ) )) * (180 / math.pi))
# if the angle is a negative then add 360 (otherwise angle goes from 180 to -180 not 0 - 360)
if angle < 0:
angle += 360
return angle
def determine_sin(sine):
# define max and min variables
min = 200
max = 999
# normalize the y location of the marker to be scaled to sine
# this represents the equation:
# x = (s - min / max - min) * 2 - 1
# x = (s - 200 / 999 - 200) * 2 - 1
normalized_sin = round(((sine - min) / (max - min)) * 2 - 1, 3)
# return the inverse of the normalized sine
return -normalized_sin
def determine_cos(cosine):
# define max and min variables
min = 200
max = 999
# normalize the x location of the marker to be scaled correctly to cosine
normalized_cosine = round(((cosine - min) / (max - min)) * 2 - 1, 3)
# return the normalized cosine
return normalized_cosine
# define basic variables
WIDTH = 600
HEIGHT = 600
FPS = 30
# initalise pygame
pygame.init()
# define pygame variables
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Trig functions in pygame")
clock = pygame.time.Clock()
# Create sprite group and sprites
sprites = pygame.sprite.Group()
circle = Circle(600, 600)
x_line = XLine(297, 100)
y_line = YLine(100, 297)
x_marker = XMarker(200, 600)
y_marker = YMarker(600, 200)
c_marker = CircleMarker(1000, 600)
text = Text()
sprites.add(circle)
sprites.add(x_line)
sprites.add(y_line)
sprites.add(x_marker)
sprites.add(y_marker)
sprites.add(c_marker)
sprites.add(text)
# Game loop
running = True
while running:
# Event handling loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# update the sprites group
sprites.update()
# fill the background with black
screen.fill(BLACK)
# draw the sprites on the screen
sprites.draw(screen)
# move each of the marker sprites
c_marker.move()
x_marker.move()
y_marker.move()
# update the text
text.update_text(determine_angle(c_marker.new_y, c_marker.new_x, c_marker.center), c_marker.angle_speed, determine_sin(y_marker.new_y), determine_cos(x_marker.new_x))
# update the display
pygame.display.update()
# sync the clock
clock.tick(FPS)
# when the loop exits, quit pygame
pygame.quit()