-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotoobjects.py
199 lines (156 loc) · 4.73 KB
/
protoobjects.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# coding: utf-8
import pygame, time, math, random, os
from pipe import SubProces
def link_to_resource(p):
return os.path.join('data', p)
def tuc(s):
try:
return unicode(s, "UTF-8")
except:
try:
return unicode(str(s), "UTF-8")
except:
return s
class Config(object):
def __init__(self, path = None):
self.data = {}
self.path = path
if path:
self.load(path)
def load(self, path):
self.data = {}
for l in open(path, "r"):
try:
t = l.split(":", 1)
if t[0]:
self.data[t[0]] = tuc(t[1][:-1]) # t[1][:-1].decode('UTF-8')
except Exception:
pass
def save(self, path = None):
keylist = self.data.keys()
keylist.sort()
if path:
self.path = path
f = open(self.path, "w+")
f.write("Achtung config\n".encode('UTF-8'))
for k in keylist:
f.write(tuc(k).encode('UTF-8'))
f.write(":".encode('UTF-8'))
f.write(tuc(self.data[k]).encode('UTF-8'))
f.write("\n".encode('UTF-8'))
f.write("\n".encode('UTF-8'))
def add(self, k, d):
self.data[k] = d
def has(self, k):
return self.data.has_key(k)
def get(self, k, defv = None, typ = None):
try:
if not self.data.has_key(k):
raise
r = self.data[k]
if typ:
r = typ(r)
return r
except:
if defv == None:
raise
return defv
class Bonus(object):
r = 20
img_red = pygame.image.load(link_to_resource('red.png'))
img_green = pygame.image.load(link_to_resource('green.png'))
img = None
chance = 0.0
duration = 0.0
def __init__(self, maxpx, maxpy, game):
self.game = game
self.px = random.randint(self.r, maxpx - self.r)
self.py = random.randint(self.r, maxpy - self.r)
self.typ = random.choice([0, 1]) # 0 - ja; 1 - inni
self.to = 0
def active(self):
if self.to == 0:
self.to = time.clock() + self.duration
#print self.to, time.clock()
if self.to > time.clock():
return 1
return 0
def colide(self, p):
if p.alive and self.r + p.size > math.hypot(self.px - p.px, self.py - p.py):
return 1
return 0
def draw(self, bit):
#pygame.gfxdraw.filled_circle(bit, self.px, self.py, self.r, (255,255,255))
if self.typ:
bit.blit(self.img_red, (self.px - self.r, self.py - self.r))
else:
bit.blit(self.img_green, (self.px - self.r, self.py - self.r))
bit.blit(self.img, (self.px - self.r, self.py - self.r))
def modify(self, p): # override
pass
def unmodify(self, p): # override
pass
#def modify_game(self, game): # override
# pass
#
#def unmodify_game(self, game): # override
# pass
def Define(chance, duration, src):
def f(o):
o.chance = chance
o.duration = duration
o.img = pygame.image.load(link_to_resource(src))
return o
return f
class Return(object):
def __init__(self):
self.data = {}
def add(self, k, d):
self.data[k] = d
def has(self, k):
return self.data.has_key(k)
def get(self):
return self.data
def __str__(self):
return self.__repr__()
def __repr__(self):
r = "<Return object with parameters:"
for k, v in self.data.iteritems():
r += " " + tuc(k) + ":" + tuc(v) + ";"
r += ">"
return r
class GamePart(object):
CHSTATE = 27
def __init__(self, topgame):
self.tg = topgame
self.paused = 0
def start(self, data):
pass
def stop(self):
return Return()
def pause(self):
self.paused = 1
def unpause(self):
self.paused = 0
def frame(self):
pass
def event(self, e):
if e.type == self.CHSTATE:
e.fn()
def call_after(self, afterms, fn):
def f():
e = pygame.event.Event(self.CHSTATE, {"fn": fn})
#pygame.time.set_timer(eventid, milliseconds)
r = range(64)
while pygame.event.peek(r):
time.sleep(0.00001)
pygame.event.pump()
pygame.event.post(e)
SubProces(f, afterms)
class CObj(object):
def __init__(self):
pass
def blit(self, bit):
pass
def event(self, event):
pass