-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_state.py
78 lines (63 loc) · 2.48 KB
/
init_state.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
from math import sin
import pygame
from noise import snoise2
from utils import COLORS
octaves = 4
freq = 32.0 * octaves
def _get_noise_val(x, y, t):
return snoise2((x + t) / freq, (y + t / 5) / freq, octaves)
class InitState(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.start = False
self.t = 0
self.title_font = pygame.font.Font('fonts/upheavtt.ttf', 20)
txt = "PYCON SNAKE"
self.title_text_surfaces = []
for c in txt:
t_w, t_h = self.title_font.size(c)
surf = pygame.Surface((t_w + 1, t_h + 1))
surf.blit(self.title_font.render(c, False, COLORS[3]), (1, 1))
surf.blit(self.title_font.render(c, False, COLORS[0]), (0, 0))
surf.set_colorkey(0)
self.title_text_surfaces.append(surf)
self.title_width = sum(s.get_width() for s in self.title_text_surfaces)
self.title_height = max(s.get_height() for s in self.title_text_surfaces)
def get_next_state(self):
if self.start:
from game_state import GameState
return GameState(self.width, self.height)
return self
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.start = True
def draw(self, buffer):
t = pygame.time.get_ticks() / 100
for x in range(self.width):
for y in range(self.height):
color = _get_noise_val(x, y, self.t)
if color < -.4:
buffer.set_at((x, y), COLORS[1])
elif color < 0:
if (x + y) % 2 == 0:
buffer.set_at((x, y), COLORS[1])
else:
buffer.set_at((x, y), COLORS[2])
elif color < .3:
if (x + y) % 3 == 0:
buffer.set_at((x, y), COLORS[1])
else:
buffer.set_at((x, y), COLORS[2])
else:
if (x + y) % 5 == 0:
buffer.set_at((x, y), COLORS[1])
else:
buffer.set_at((x, y), COLORS[2])
self.t += .2
base_x = (self.width - self.title_width) / 2
base_y = (self.height - self.title_height) / 2
for n, s in enumerate(self.title_text_surfaces):
buffer.blit(s, (base_x, base_y + 3 * sin(t + n)))
base_x += s.get_width()