-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
162 lines (113 loc) · 4.5 KB
/
play.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
import sys
import glob
import random
import pygame
import pygame.locals
from PIL import Image, ImageFilter
class View() :
def __init__(self):
# Initialize PyGame
pygame.init()
self.screen = pygame.display.set_mode( (700,700), 0, 32)
pygame.display.set_caption("ElectricDreamz")
# Set up FPS clock
self.fps = 60
self.fpsClock = pygame.time.Clock()
self.speed = 0.1
def loadImages(self):
self.images_path = []
self.images = []
for path in glob.glob("./"+word+"/*.jpeg"):
self.images_path.append(path)
self.images.append(pygame.image.load(path))
self.coordinates = [ random.randint(0, 100) for i in self.images ]
self.coordinates[random.randint(0,len(self.images)-1)] = 100
self.renorm_coordinates()
self.direction = random.randint(0,len(self.images)-1)
self.font = pygame.font.Font("./Lack-Regular.otf",50)
self.text1 = self.makeText("What is")
self.text2 = self.makeText("'"+word+"' ?")
def change_direction(self):
self.direction = random.randint(0,len(self.images)-1)
def renorm_coordinates(self):
s = sum(self.coordinates)
self.coordinates = [ c/s for c in self.coordinates ]
def makeText(self, text, insideColor = (250,250,250), outsideColor=(150,150,150)) :
return self.font.render(text, 1, insideColor )
textIn = self.font.render(text, 1, insideColor )
#textOut = self.font.render(text, 1, outsideColor)
#size = textIn.get_width() + 2, textIn.get_height() + 2
#s = pygame.Surface(size, pygame.SRCALPHA, 32)
#s.blit(textOut,(0,0))
#s.blit(textOut,(2,2))
#s.blit(textOut,(2,0))
#s.blit(textOut,(0,2))
#s.blit(textIn, (1,1))
#return s
def mainLoop(self) :
# Handle events
self.eventHandler()
# Handle keys
self.keysHandler()
# Render elements
self.update()
self.render()
# Update screen
pygame.display.update()
self.fpsClock.tick(self.fps)
def eventHandler(self) :
for event in pygame.event.get():
if (event.type == pygame.QUIT) :
pygame.quit()
sys.exit()
def keysHandler(self) :
pass
#keyPressed = pygame.key.get_pressed()
#print(keyPressed)
def blur(self,image, radius):
# Convert the surface to PIL image
surfSize = image.get_size()
surfInString = pygame.image.tostring(image, "RGBA", False)
surfPIL = Image.frombytes("RGBA", surfSize, surfInString)
# Blur image using PIL
surfPILblurred = surfPIL.filter(ImageFilter.GaussianBlur(radius=radius))
return pygame.image.fromstring(surfPILblurred.tobytes("raw", "RGBA"), surfSize, "RGBA")
def update(self):
current_max = max(self.coordinates)
current_max_coordinate = [i for i,j in enumerate(self.coordinates) if j == current_max][0]
if current_max >= 0.99 and current_max_coordinate == self.direction:
self.change_direction()
elif current_max <= 0.80 and random.randint(0,100)<6:
self.change_direction()
self.speed = 0.9 * self.speed + 0.1 * max((1-current_max)/7.5,0.02)
self.coordinates[self.direction] += self.speed
self.renorm_coordinates()
def render(self):
self.screen.fill( (0,0,0) )
coordinates_and_images = sorted(zip(self.coordinates, self.images), key=lambda ci:ci[0], reverse=True)
i = 0
for coordinate, image in coordinates_and_images:
render_image = self.blur(image, (25*(1-coordinate))**1.5)
render_image.set_alpha(int((1-coordinate)*255))
if i == 0:
self.screen.blit(render_image, (0,0))
else:
self.screen.blit(render_image, (0,0), special_flags=pygame.BLEND_RGBA_MIN)
i+=1
if i >= 5:
break
#size = textIn.get_width() + 2, textIn.get_height() + 2
self.screen.blit(pygame.transform.scale(self.screen, (750*2,750*2)), (0,0))
w1 = self.text1.get_width()
h1 = self.text1.get_height()
w2 = self.text2.get_width()
h2 = self.text2.get_height()
self.screen.blit(self.text1, (750/2-w1/2, 750/2-h1-10))
self.screen.blit(self.text2, (750/2-w2/2, 750/2+10))
def main():
v = View()
v.loadImages()
while True:
v.mainLoop()
word = sys.argv[1]
main()