-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (105 loc) · 3.98 KB
/
main.py
File metadata and controls
129 lines (105 loc) · 3.98 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
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
import pygame
import random
import pygame.freetype
import math
from pygame.locals import (
K_ESCAPE,
KEYDOWN,
)
# # float-mod
# # fmod(24.2, 11), returns 2.2
# # fmod(24.345, 11), returns 2.345
# def fmod(val: float, limit: int) -> float:
# str_val = str(val)
# dec_left = int(str_val[:str_val.find('.')])
# dec_right = str_val[str_val.find('.') + 1:]
# return (dec_left % limit) + float('.' + dec_right)
pygame.init()
# settings
FPS = 60
SCREEN_WIDTH = SCREEN_HEIGHT = 800
RESOLUTION = 500
FACTOR_UPDATE = .1
SLEEP_TIME = 100 # milli secs
CUSTOM_FACTOR = float(input("Any custom factor (enter 0 for if you aren't sure): "))
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
clock = pygame.time.Clock()
font = pygame.freetype.Font("assets/Open_Sans/OpenSans-Regular.ttf", 25)
mid_pt = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
circle_radius = SCREEN_WIDTH / 2
line_color = [0, 0, 255]
num_points = RESOLUTION
update = 2 * math.pi / num_points
# # the points around the circle
# points = []
# angle = 0
# while angle <= 2 * math.pi:
# points.append((mid_pt[0] + circle_radius * math.cos(angle),
# mid_pt[1] + circle_radius * math.sin(angle)))
# angle += update
accumumlate = 0
factor = 2 if not CUSTOM_FACTOR else CUSTOM_FACTOR
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
# draw the circle
# pygame.draw.circle(screen, (0, 0, 255), (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), circle_radius, width=5)
# join the points
# for i in range(len(points)):
# pt0 = points[i]
# pt1 = points[(i * factor) % len(points)]
# # line_color = ((i % 2 == 0) * 255,
# # (i % 2 != 0) * 255,
# # (factor * 255) % 255)
# # line_color = (random.random() * 255, random.random() * 255, random.random() * 255)
# # line_color = (0, 255, 0)
# # aaline = anti-aliased line
# pygame.draw.aaline(screen, line_color, pt0, pt1)
# draw the lines
angle = 0.1
while (angle <= (2.0 * math.pi)):
pt0 = (mid_pt[0] + circle_radius * math.cos(angle),
mid_pt[1] + circle_radius * math.sin(angle))
pt1 = (mid_pt[0] + circle_radius * math.cos(angle * factor),
mid_pt[1] + circle_radius * math.sin(angle * factor))
# aaline = anti-aliased line
pygame.draw.aaline(screen, line_color, pt0, pt1)
angle += update
# draw the factor
font.render_to(screen, (10, 10), f"Factor: {round(factor, 3)}", (255, 50, 100))
pygame.display.flip()
# cannot use pygame.time.wait(SLEEP_TIME),
# as it blocks event handling for sometime...
t = clock.tick(FPS)
accumumlate += t
if accumumlate >= SLEEP_TIME:
if not CUSTOM_FACTOR:
factor += FACTOR_UPDATE
# sine range is [-1, +1], so capping it to [0, 2]
# by adding 1, then divide by (2 - 0), i.e., 2
# then multiply 255, to get within range [0, 255]
r = ((math.sin(factor) + 1) / 2) * 255
# shifting sine curve with phase angle pi
g = ((math.sin(factor + math.pi) + 1) / 2) * 255
# shifting sine curve with phase angle pi/2
b = ((math.sin(factor + math.pi / 2) + 1) / 2) * 255
line_color = (r, g, b)
else:
line_color = (0, 0, 255)
accumumlate = 0
# line_color = [(random.random() > 0.5) * 255,
# (random.random() > 0.5) * 255,
# (random.random() > 0.5) * 255]
# # when all random nos comes to be 0
# if sum(line_color) == 0:
# line_color[2] = 255
# line_color = (random.randint(150, 250),
# random.randint(150, 250),
# random.randint(150, 250))
pygame.quit()