-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_pygame_elliptical_orbit.py
More file actions
49 lines (43 loc) · 1.69 KB
/
12_pygame_elliptical_orbit.py
File metadata and controls
49 lines (43 loc) · 1.69 KB
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
import pygame, math
#calucation of angle with respect to midpoint and mouse co-ordinates.
def getAngle(x1, y1, x2, y2):
angle = math.atan2(x1 - x2, y1 - y2) # get the angle in radians
angle = angle * (180 / math.pi) # convert to degrees
angle = (angle + 90) % 360# adjusting for 90 degree difference.
return angle
#color definition //
#in (R,G,B) format
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
#pygame window initialization
pygame.init()
bg_color = white
window_width = 640 # width in pixels
window_height = 480 # height in pixels
windows_resolution = (window_width,window_height)
window_x_center = int(window_width / 2)
window_y_center = int(window_height / 2)
frameRate = 60 #fps
gameClock = pygame.time.Clock()
display_surface = pygame.display.set_mode(windows_resolution)
pygame.display.set_caption('Elliptical Orbit')
#GameLOOP
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == K_ESCAPE):
pygame.quit()
exit()
#Rendering
display_surface.fill(bg_color)
mousex, mousey = pygame.mouse.get_pos()
degrees = getAngle(window_x_center, window_y_center, mousex, mousey)
pygame.draw.circle(display_surface, green, (window_x_center, window_y_center), 100, 4)
pygame.draw.circle(display_surface, blue, (window_x_center, window_y_center), 20, 0)
xPos = math.cos(degrees * (math.pi / 180)) * 100
yPos = math.sin(degrees * (math.pi / 180)) * 100
pygame.draw.circle(display_surface, red, (int(xPos) + window_x_center, -1 * int(yPos) + window_y_center), 40)
pygame.display.update()
gameClock.tick(frameRate)