forked from jaredly/babytux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
131 lines (104 loc) · 3.93 KB
/
game.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
#!/usr/bin/env python
import string, math, time, sys, random
import os
import os.path
from simage import SImage, SImageStatic
from sprite import SpriteText
from firework import FireWork
import app
import rabbyt
import pyglet
from pyglet import clock, font
IMAGES = []
def normal(ang):
ang %= 360
if ang > 180:
ang -= 360
return ang
class Main(app.App):
def __init__(self):
super(Main, self).__init__()
clock.schedule(rabbyt.add_time)
self.ft = font.load('Helvetica', self.win.width/20)
self.pos = 0,0
self.key_buffer = []
def on_key_press(self, symbol, mods):
self.key_buffer.append(symbol)
self.key_buffer = self.key_buffer[-6:]
code = 'smash'
if len(self.key_buffer) == 6:
for key, char in zip(self.key_buffer, code):
if key != ord(char):
break
else:
if self.key_buffer[-1] == pyglet.window.key.ESCAPE:
sys.exit(0)
try:
s = chr(symbol)
except (ValueError, OverflowError):
# Deal with <ENTER>, <ESC>, <SHIFT>, <F1-F12>, <ALT>, unicode
# character, etc. For our purpose, change it to a period
s = '.'
pass
if s in string.ascii_letters:
self.make_string(s.upper())
elif s in string.digits:
self.make_string(s)
else: # work with our ValueError and if user enters space, puncutations, etc
self.make_shape_animations()
def make_string(self, string):
x, y = get_xy_positions(self.win.width, self.win.height)
s = SpriteText(self.ft, string)
s.rgba = rcolor()
s.x = rabbyt.lerp(x, random.uniform(0, self.win.width), dt=1)
s.y = rabbyt.lerp(y, random.uniform(0, self.win.height), dt=1)
s.rot = rabbyt.lerp(start=0, end=360, dt=1)
s.scale = rabbyt.lerp(.5, 1, dt=1)
self.world.objects.append(s)
def tmp(dt):
s.alpha = rabbyt.lerp(1.0, 0, dt=2)
clock.schedule_once(lambda dt:self.world.objects.remove(s),2)
clock.schedule_once(tmp, 2)
def make_shape_animations(self):
x, y = get_xy_positions(self.win.width, self.win.height)
s = SImage(get_random_image(), x, y)
s.sp.x = rabbyt.lerp(end=random.uniform(0, self.win.width), dt=1)
s.sp.y = rabbyt.lerp(end=random.uniform(0, self.win.height), dt=1)
s.sp.rot = rabbyt.lerp(start=0, end=360, dt=1)
s.sp.scale = rabbyt.lerp(.25, 1, dt=1)
self.world.objects.append(s)
clock.schedule_once(lambda dt:self.world.objects.remove(s), 1)
def on_mouse_press(self, x, y, button, mods):
self.pos = x,y
self.world.objects.append(FireWork(x,y))
def on_mouse_drag(self, x, y, dx, dy, buttons, mods):
if dst(x-self.pos[0], y-self.pos[1]) > 15:
self.add(x,y)
self.pos = x,y
def on_mouse_motion(self, x, y, buttons, mods):
if dst(x-self.pos[0], y-self.pos[1]) > 15:
self.add(x,y)
self.pos = x,y
def add(self, x, y):
s = SImageStatic('res/ring.png', x, y)
s.sp.scale = rabbyt.lerp(start=.1, end=2, dt=1)
s.sp.alpha = rabbyt.lerp(end=0, dt=1)
self.world.objects.append(s)
clock.schedule_once(lambda dt:self.world.objects.remove(s), 1)
def step(self):
pass
def rcolor():
return tuple(random.uniform(0,255) for i in (0,0,0)) + (1,)
def dst(x,y):
return math.sqrt(x**2+y**2)
def get_xy_positions(width, height):
return random.uniform(0, width), random.uniform(0, height)
def get_random_image():
return random.choice(IMAGES)
def init_image_resources():
for dirpath, dirnames, filenames in os.walk("res"):
for filename in [f for f in filenames if f.endswith(".png")]:
IMAGES.append(os.path.join(dirpath, filename))
if __name__=='__main__':
init_image_resources()
Main().mainLoop()