forked from gunny26/python-demoscene
/
CoffeeBean.py
97 lines (84 loc) · 3.01 KB
/
CoffeeBean.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
#!/usr/bin/python3
import sys
import pygame
from pygame import gfxdraw
import noise
class CoffeeDraw(object):
"""draws nice colored lines on surface"""
class Bean(object):
"""represents one colored line"""
def __init__(self, surface, beans, parameter_dict):
"""
(pygame.Surface) surface - surface to draw on
(list) beans - list of beans to remove self when velocity is 0
(dict) parameter_dict - dictionary of parameters
"""
self.surface = surface
self.beans = beans
self.x = None
self.y = None
self.x_off = None
self.y_off = None
self.__dict__.update(parameter_dict)
self.vel = 3 # or option vel
self.accel = -0.003 # or option accel
self.width = self.surface.get_width()
self.height = self.surface.get_height()
self.color = pygame.Color(0, 0, 0, 0)
def draw(self):
"""draw line"""
if self.vel < 0 :
# remove self
self.beans.remove(self)
# original 0.0007
self.x_off += 0.0007
self.y_off += 0.0007
self.vel += self.accel
self.x += noise.pnoise1(self.x_off, octaves=8) * self.vel - self.vel / 2
self.y += noise.pnoise1(self.y_off, octaves=8) * self.vel - self.vel / 2
# set color
h = abs(noise.pnoise1((self.x_off + self.y_off) / 2)) * 360
self.color.hsva = (h, 100, 100, 4)
pygame.gfxdraw.pixel(self.surface, int(self.x) % self.width, int(self.y) % self.height, self.color)
def __init__(self, surface):
"""
(pygame.Surface) surface - surface to draw on
"""
self.surface = surface
# initialize
self.beans = []
self.framecount = 1
def update(self):
"""update every frame"""
self.framecount += 1
x_off = self.framecount * 0.0003
y_off = x_off + 20
x = noise.pnoise1(x_off, octaves=8) * self.surface.get_width()
y = noise.pnoise1(y_off, octaves=8) * self.surface.get_height()
# every 8th frame a new bean
if self.framecount % 8 == 0:
self.beans.append(self.Bean(self.surface, self.beans, {
"x" : x,
"y" : y,
"x_off" : x_off,
"y_off" : y_off}))
for bean in self.beans:
bean.draw()
def test():
""" test """
fps = 50
surface = pygame.display.set_mode((600, 400))
pygame.init()
clock = pygame.time.Clock()
coffee_draw = CoffeeDraw(surface)
while True:
clock.tick(fps)
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit(0)
pygame.display.set_caption("frame rate: %.2f frames per second" % clock.get_fps())
coffee_draw.update()
pygame.display.update()
if __name__ == "__main__" :
test()