-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
310 lines (283 loc) · 11.4 KB
/
main.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import sys
import math
from random import randint
from collections import defaultdict
from direct.showbase.ShowBase import ShowBase
import panda3d
import pman.shim
from keybindings.device_listener import add_device_listener
from keybindings.device_listener import SinglePlayerAssigner
from panda3d.core import TextNode
from panda3d.core import WindowProperties
from sounds import load_sounds
from lines import *
from objects import *
panda3d.core.load_prc_file(
panda3d.core.Filename.expand_from('$MAIN_DIR/settings.prc')
)
class GameApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
pman.shim.init(self)
info = self.pipe.getDisplayInformation()
for idx in range(info.getTotalDisplayModes()):
width = info.getDisplayModeWidth(idx)
height = info.getDisplayModeHeight(idx)
bits = info.getDisplayModeBitsPerPixel(idx)
wp = WindowProperties()
wp.set_size(width, height)
base.win.requestProperties(wp)
base.win.set_clear_color((0,0,0,1))
self.accept('escape', sys.exit)
add_device_listener(
config_file=panda3d.core.Filename.expand_from('$MAIN_DIR/keybindings.toml'),
assigner=SinglePlayerAssigner(),
)
self.map_size = [25,50]
self.segment_time = [0, 0.06]
self.flower_time = [0, 4]
self.extra_life = 0
self.camera = NodePath("camera")
#base.cam.set_pos(0,-25,50)
base.cam.set_pos(0, -30, 40)
base.cam.set_p(-50)
base.cam.reparent_to(self.camera)
self.camera.reparent_to(render)
self.sounds = load_sounds()
self.music = loader.load_sfx("music/song1.ogg")
self.music.set_loop(True)
self.music.play()
self.load_models()
self.bg = self.bg_model = None
self.make_background()
self.fonts = {}
self.fonts["dot"] = loader.loadFont("fonts/dotrice.otf")
self.fonts["pixel"] = loader.loadFont("fonts/pressstart2p.ttf")
## FLOATING TEXT GARBAGE
self.textimation = Actor("models/textimation.bam")
self.textimation.loop("animation")
self.announcement = TextNode("announcement")
self.announcement.font = self.fonts["dot"]
self.announcement.text = ">>GRIDFLY<<\n\nPRESS SPACE TO START\n\n\nMADE BY HENDRIK-JAN\n\nFOR PYWEEK29\n\nPANDA3D FOR THE WIN"
self.announcement.align = 2
self.announcement.set_text_color((1,0,1,1))
self.a_root = render.attach_new_node("announcement")
self.a_node = NodePath("announcement")
self.announcement_node = self.a_node.attach_new_node(self.announcement)
self.announcement_node.set_scale(5)
self.announcement_node.set_p(-130)
for i in range(2):
n = self.a_root.attach_new_node(str("t"+str(i)))
self.a_node.instance_to(n)
n.set_z(i)
self.textimation.expose_joint(self.a_node, jointName="text", partName="modelRoot")
self.a_root.reparent_to(self.camera)
self.a_root.set_pos((0,70,-50))
self.a_root.set_transparency(True)
self.a_root.set_alpha_scale(0.5)
self.text_timer = 0
## END FLOATING TEXT GARBAGE
self.highscore = 0
self.score = 0
self.lives = 0
self.infotext = TextNode("info")
self.infotext.font = self.fonts["pixel"]
self.infotext.text = "HIGHSCORE:{}\n\nSCORE:{}\n\nLIVES:{}".format(self.highscore, self.score, self.lives)
self.infotext_node = render2d.attach_new_node(self.infotext)
self.infotext_node.set_scale(0.02)
self.infotext_node.set_pos(-0.95,0,0.9)
self.infotext_node.hide()
self.numbers = {}
numbers = ["10"]
for i in range(1, 25):
numbers.append(str((i)*50))
numbers.append("1000")
for n, number in enumerate(numbers):
self.numbers[number] = NodePath(number), TextNode(number)
self.numbers[number][1].text = number
self.numbers[number][1].align = 2
self.numbers[number][1].font = self.fonts["pixel"]
self.numbers[number][1].set_text_color((0,1,1,1))
self.numbers[number][0].attach_new_node(self.numbers[number][1])
self.numbers[number][0].set_scale(0.5+(n/10))
self.first = True
self.segments = []
self.chasers = []
self.flowers = []
self.bullets = []
self.mines = []
self.explosions = []
self.zaplines = []
self.scores = []
self.player = Player()
self.task_mgr.add(self.update_objects)
def destroy(self):
while len(self.segments) > 0: self.segments[0].destroy()
while len(self.chasers) > 0: self.chasers[0].destroy()
while len(self.flowers) > 0: self.flowers[0].destroy()
while len(self.bullets) > 0: self.bullets[0].destroy()
while len(self.mines) > 0: self.mines[0].destroy()
while len(self.explosions) > 0: self.explosions[0].destroy()
while len(self.zaplines) > 0: self.zaplines[0].destroy()
def start(self, spawn=False):
if self.first:
draw_lines(self)
self.first = False
self.a_root.set_pos((0,50,-50))
self.announcement_node.set_scale(6)
self.flower_time[0] = 0
self.destroy()
if not spawn:
base.music.set_volume(1)
self.announce("starting_game", "LEVEL 1 \n\n WAVE 1")
self.extra_life = 0
self.player.highscore = False
self.level = 1
self.wave = 1
self.lives = 3
self.score = 0
self.infotext_node.show()
self.player.spawn((0,20,0))
self.chasers.append(Chaser(self.models["chasers"]["spider"], (0,60,0)))
self.make_enemies()
def zapline(self, a, b):
color = choice(((1,0,1,1), (1,0,0,1), (0,1,0,1), (0,1,1,1), (0,0,1,1)))
base.linesegs.set_thickness(randint(3,5))
base.linesegs.set_color(color)
base.linesegs.move_to(a.get_pos())
base.linesegs.draw_to(b.get_pos())
line = render.attach_new_node(base.linesegs.create())
base.zaplines.append(line)
def make_background(self, n=0):
if self.bg_model:
self.bg_model.remove_node()
if self.bg:
self.bg.remove_node()
self.bg_model = Actor("models/bg_"+str(n)+".bam")
self.bg_model.loop("animation")
self.bg = NodePath("bg")
for i in range(3):
bg = self.bg.attach_new_node("bg-"+str(i))
self.bg_model.instance_to(bg)
bg.set_pos(0,0,-20-(i*20))
self.bg.set_scale(self.map_size[0]*4, self.map_size[1]*2, 1)
self.bg.set_transparency(True)
self.bg.set_alpha_scale(0.1)
self.bg.reparent_to(render)
def load_models(self):
models = ["enemies", "misc"]
self.models = {}
for model in models:
self.models[model] = {}
for child in loader.loadModel("models/{}.bam".format(model)).get_children():
for child_child in child.get_children():
child_child.set_pos(child, (0,0,0))
child.set_pos((0,0,0))
child.detach_node()
self.models[model][child.name] = child
self.models["chasers"] = {}
self.models["chasers"]["spider"] = Actor("models/spider.bam")
self.models["chasers"]["spider"].loop("walk")
self.models["chasers"]["spider"].set_play_rate(2, "walk")
def make_enemies(self):
#self.segment_time = [0, 0.06]
self.segment_time = [0, 0.1-(0.005*self.level)]
self.chasers[0].speed = self.level
if self.chasers[0].speed > 6:
self.chasers[0].speed = 6
amount = self.wave+1
self.player.max_combo = 4+(self.level*2)
gap = (self.map_size[0]*2)/amount
for i in range(amount):
self.segments.append(EnemySegment(
self.models["enemies"]["cent"+str(((self.level-1)%7)+1)],
length=4+(self.level*2), x=-self.map_size[0]+((gap/2)+gap*i)))
def update_objects(self, task):
dt = globalClock.get_dt()
if self.text_timer > 0 and self.player.alive:
self.text_timer -= dt
if self.text_timer < 0:
self.text_timer = 0
self.announcement.text = ""
for zapline in self.zaplines:
zapline.remove_node()
self.zaplines.remove(zapline)
if self.player.alive:
self.flower_time[0] += dt
self.player.update()
if self.player.zapping > 0:
self.bg.set_alpha_scale(0.2)
else:
self.bg.set_alpha_scale(0.06)
for explosion in self.explosions:
explosion.update()
for bullet in self.bullets:
bullet.update()
for mine in self.mines:
mine.update()
if len(self.mines) == 0:
self.sounds["2d"]["lines"].stop()
for chaser in self.chasers:
chaser.update()
for flower in self.flowers:
flower.update()
for score in self.scores:
score.update()
for number in self.numbers:
self.numbers[number][1].set_text_color(choice(((0,1,1,1),(0,1,0,1),(0,0,1,1))))
# segments
self.segment_time[0] += dt
if self.segment_time[0] > self.segment_time[1]:
self.segment_time[0] -= self.segment_time[1]
for s, segment in enumerate(self.segments):
try:
segment.update()
except:
pass
# end wave
if len(self.segments) == 0 and base.player.alive:
self.wave += 1
if self.wave > 4:
self.wave = 1
self.level += 1
self.player.zapping = -1
self.player.flowerpower = 0
self.flower_time[0] = 0
self.announce(choice(("give_it_to_me", "oh_baby", "sexy", "thats_the_stuff", "sure_why_not")),
"LEVEL " + str(self.level)+"\n\nWAVE " + str(self.wave))
self.make_enemies()
# camera
vector = base.player.node.getPos() - self.camera.getPos()
self.camera.set_pos(self.camera.get_pos()+(vector*(4*dt)))
if not self.player.alive:
if self.device_listener.read_context('game')["spawn"]:
if self.lives == 0:
self.start()
else:
self.start(True)
self.announcement.text = ""
self.infotext.text = "HIGHSCORE:{}\n\nSCORE:{}\n\nLIVES:{}".format(self.highscore, self.score, self.lives)
if self.score > self.highscore:
self.highscore = self.score
self.player.highscore = True
if self.score > 25000*(self.extra_life+1):
self.announcement.text = str(25000*(self.extra_life+1)) + str("POINTS!!!\n\nEXTRA LIFE!!!")
self.extra_life += 1
self.lives += 1
base.sounds["2d"]["extralife"].play()
self.text_timer = 2
return task.cont
def announce(self, say, extra=""):
for sound in base.sounds["announce"]:
base.sounds["announce"][sound].stop()
base.sounds["announce"][say].play()
s = say.split("_")
s = " ".join(s)
self.textimation.loop("animation")
self.announcement.text = s.upper() + "!!!"+"\n\n"+extra
self.text_timer = 2
def main():
app = GameApp()
app.run()
if __name__ == '__main__':
main()