-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_template.py
146 lines (115 loc) · 4.26 KB
/
pygame_template.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
import pygame
import random
import math
import matplotlib.pyplot as plt
pi = math.pi
sin = lambda x: math.sin(x)
step = pi/1200
X = [step*i for i in range(2400)]
Y = [255*(sin(x)+1)/2 for x in X]
# Initialize pygame and create a window
pygame.init()
info = pygame.display.Info()
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.FULLSCREEN) #, screen_num=1
# Create a list to store the circles
objects = []
# Create a class for the circles
class Object:
def __init__(self, x, y, size):
self.x = x
self.y = y
self.size = size
self.color_r = random.randint(1,119)
self.color_g = random.randint(1,119)
self.color_b = random.randint(1,119)
self.color = (Y[self.color_r],Y[self.color_g],Y[self.color_b])
self.speed_x = random.randint(-5, 5)
self.speed_y = random.randint(-5, 5)
self.growth_rate = random.uniform(0.1, 0.5)
def move(self):
# Move the circle and rebound off the edges of the screen
self.x += self.speed_x
self.y += self.speed_y
if self.x < 0 or self.x > info.current_w:
self.speed_x = -self.speed_x
if self.y < 0 or self.y > info.current_h:
self.speed_y = -self.speed_y
# self.speed_x = self.speed_x * random.uniform(0.8, 1.2)
# self.speed_y = self.speed_y * random.uniform(0.8, 1.2)
def grow(self):
# Increase the size of the circle
self.size += self.growth_rate
if self.size > 100:
self.growth_rate = -self.growth_rate
elif self.size < 20:
self.growth_rate = abs(self.growth_rate)
def change_color(self):
# Change the color of the circle
self.color_r = (self.color_r+1)%119
self.color_g = (self.color_g+1)%119
self.color_b = (self.color_b+1)%119
self.color = (Y[self.color_r],Y[self.color_g],Y[self.color_b])
class Circle(Object):
def draw(self):
# Draw the circle on the screen
pygame.draw.circle(screen, (self.color_r,self.color_g,self.color_b),(int(self.x), int(self.y)),int(self.size))
class Rectangle(Object):
def draw(self):
# Draw rectangle on the screen
pygame.draw.rect(screen, (int(self.x), int(self.y), int(self.size), int(self.size)))
class Ellipse(Object):
def draw(self):
# Draw the ellipse on the screen
pygame.draw.ellipse(screen,(int(self.x), int(self.y), int(self.size), int(self.size)))
def main():
# Create some initial circles
for i in range(50):
x = random.randint(0, info.current_w)
y = random.randint(0, info.current_h)
size = random.randint(10, 50)
objects.append(Circle(x, y, size))
# color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
'''
# Create some initial rectangles
for i in range(20):
x = random.randint(0, info.current_w)
y = random.randint(0, info.current_h)
size = random.randint(10, 50)
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
objects.append(Rectangle(x, y, size, color))
# Create some initial ellipses
for i in range(20):
x = random.randint(0, info.current_w)
y = random.randint(0, info.current_h)
size = random.randint(10, 50)
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
objects.append(Ellipse(x, y, size, color))
'''
life = 0
# Main game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame. K_ESCAPE:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Move and grow the circles, then draw them
for object in objects:
object.move()
object.grow()
object.draw()
object.change_color()
life = (life+1)%120
# Update the display
pygame.display.flip()
# Wait for a bit
pygame.time.wait(10)
# Clean up
pygame.quit()
if __name__ == "__main__":
main()