-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
383 lines (294 loc) · 12.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import pygame
import argparse
import io
import os
import random
import math
from bitmapfont import BitmapFont
from globalconst import *
from gameobjects import *
from playerobject import *
from gamestate import *
from particles import *
from graphics import *
import network
import sound
actions = []
gamestate = None
playerColor = 0
particleColors = [(62,154,193),(221,61,0),(49,221,0),(188,62,193),(193,182,62),(120,120,120)]
parser = argparse.ArgumentParser()
parser.add_argument('--connect')
parser.add_argument('--port', type=int, default=2000)
parser.add_argument('--host', action='store_true')
args = parser.parse_args()
net = None
clients = {}
ownId = int(random.random() * 1000000)
if args.connect is not None:
net = network.connect(args.connect, args.port)
actions.append(('create-player', ownId))
print('i am player with id=', ownId)
elif args.host:
net = network.serve(args.port)
pygame.display.init()
if FULLSCREEN:
window = pygame.display.set_mode(pygame.display.list_modes()[0], pygame.FULLSCREEN)
else:
window = pygame.display.set_mode((WIN_W, WIN_H), 0)
screen = pygame.Surface((SCR_W, SCR_H))
clock = pygame.time.Clock()
pygame.mixer.init(44100)
pygame.joystick.init()
for i in range(pygame.joystick.get_count()):
pygame.joystick.Joystick(i).init()
pygame.mouse.set_visible(False)
level = ['###############################################################################',
'#4 3#',
'# #',
'# #',
'# #',
'# #',
'# 1#######2 #',
'# 1#########2 #',
'# 1#####4 3#####2 #',
'# 1########## ####4 1###',
'# 1##########4 1#### 1####',
'# 1#4 3## 3##4 1#####',
'#2 1## 3# #4 3#####',
'#######F___T##########4 #####',
'#################4 1######',
'##########4 1#######',
'####4 1#########',
'###4 1###########',
'##4 1############',
'#4 1###################################################',
'# ####################################################',
'# 1#######4 3###4 #',
'# 1##4 ### #',
'# 1####4 ### #',
'# 1##### ### #',
'# 1######4 ### #',
'# 1####4 ### #',
'# 1####4 3#4 #',
'# 3###4 #',
'# 3#4 #',
'# 1#2 #',
'# 1###2 #',
'# 1###### #',
'# ##########2 #',
'#2 1##############2 1#######F___T####2 1#',
'###############################################################################',
]
gamestate = GameState()
def render_object(obj,camera_pos):
tileId = obj.getSprite()
if not tileId in getTiles():
return
tile = getTiles()[tileId]
rotated_sprite, rotated_rect = rotateSprite(obj)
screen.blit(rotated_sprite, (rotated_rect.x - camera_pos[0], rotated_rect.y - camera_pos[1]))
def toggleFullscreen():
global FULLSCREEN, window
FULLSCREEN = not FULLSCREEN
if FULLSCREEN:
window = pygame.display.set_mode(pygame.display.list_modes()[0], pygame.FULLSCREEN)
else:
window = pygame.display.set_mode((WIN_W, WIN_H), 0)
def createPlayer(objId):
global playerColor, gamestate
# create ordinary player
x, y = (0,0)
newPlayer = PlayerObject(SPAWN_X, SPAWN_Y, tile='player'+str(playerColor), particleColor=particleColors[playerColor])
playerColor += 1
playerColor %= 6
gamestate.objects[objId] = newPlayer
print('created player with id=', objId)
def removePlayer(objId):
del gamestate.objects[objId]
def controls():
global ownId, actions, gamestate
for e in pygame.event.get():
if e.type == pygame.QUIT:
return False
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
return False
if e.key in KEYS_THRUST:
actions.append(('move-up', ownId))
if e.key in KEYS_ROTATE_LEFT:
actions.append(('rotate-left', ownId))
if e.key in KEYS_ROTATE_RIGHT:
actions.append(('rotate-right', ownId))
if e.key in KEYS_FIRE:
actions.append(('fire-press', ownId))
if e.key == pygame.K_RETURN:
mods = pygame.key.get_mods()
if mods & pygame.KMOD_LALT or mods & pygame.KMOD_RALT:
toggleFullscreen()
if e.type == pygame.KEYUP:
if e.key in KEYS_THRUST:
actions.append(('stop-up', ownId))
if e.key in KEYS_ROTATE_LEFT:
actions.append(('stop-rotate-left', ownId))
if e.key in KEYS_ROTATE_RIGHT:
actions.append(('stop-rotate-right', ownId))
if e.key in KEYS_FIRE:
actions.append(('fire-release', ownId))
if e.key in KEYS_RESET:
actions.append(('reset', ownId))
if e.key == pygame.K_F11:
global FPS
if FPS == 20:
FPS = 60
else:
FPS = 20
if e.key == pygame.K_F12:
global DEBUG_MODE
DEBUG_MODE = not DEBUG_MODE
if e.type == pygame.JOYAXISMOTION:
if e.axis == 0:
if e.value < -JOY_DEADZONE:
actions.append(('rotate-left', ownId))
elif e.value > JOY_DEADZONE:
actions.append(('rotate-right', ownId))
else:
actions.append(('stop-rotate-left', ownId))
actions.append(('stop-rotate-right', ownId))
if e.type == pygame.JOYBUTTONDOWN:
actions.append(('move-up', ownId))
if e.type == pygame.JOYBUTTONUP:
actions.append(('stop-up', ownId))
return True
def render():
screen.fill((0, 0, 0))
if tick < 180:
getFont().drawText(screen, 'GRAVWERK', 2, 2, fgcolor=(255,255,255))#, bgcolor=(0,0,0))
#get own position
camera_pos = (0,0)
for obj_id ,obj in gamestate.objects.items():
if obj_id == ownId:
camera_pos = (math.floor(obj.x - SCR_W/2 + TILE_W/2), math.floor(obj.y - SCR_H/2 + TILE_H/2))
break
#blit all the tiles onto the screen
for y in range(len(level)):
for x in range(len(level[y])):
tileId = level[y][x]
if tileId in getTiles():
screen.blit(getTiles()[tileId], (x * TILE_W - camera_pos[0], y * TILE_H - camera_pos[1]))
#blit objects on the screen
for wobjId, obj in gamestate.objects.items():
render_object(obj,camera_pos)
if DEBUG_MODE:
for cx, cy in debugTiles:
screen.blit(getTiles()['debug'], (cx * TILE_W - camera_pos[0], cy * TILE_H - camera_pos[1]))
debugTiles.clear()
particlesRender(screen,camera_pos)
def update():
global actions, gamestate
if net is None or net.isHost():
for obj in gamestate.objects.values():
obj.update(gamestate)
for obj in gamestate.objects.values():
obj.updateLocal(gamestate)
if net is not None:
# as a host, put all played sounds into the queue
if net.isHost():
for soundname in sound.popHistory():
gamestate.soundQueue.add(soundname)
# sync gamestate over network
gamestate, actions = net.update(gamestate, actions)
ownPlayer = gamestate.objects.get(ownId)
# retrieve sounds to be played as a client
if not net.isHost():
for soundname in gamestate.soundQueue:
sound.playSound(soundname)
gamestate.soundQueue = set()
particlesUpdate()
clientId = None
for action, objId in actions:
if action == 'client-actions':
clientId = objId
continue
if action == 'client-disconnect':
if objId in clients:
removePlayer(clients[objId])
continue
if action == 'create-player':
clients[clientId] = objId
createPlayer(objId)
continue
obj = gamestate.objects.get(objId)
print('action:', action, 'objId:', objId)
if not obj:
continue
if action == 'move-left':
obj.move(-1, None)
elif action == 'move-right':
obj.move(1, None)
elif action == 'move-up':
playSound("boost-start")
obj.move(None, -1)
elif action == 'move-down':
obj.move(None, 1)
elif action == 'rotate-left':
playSound("turn")
obj.rotate(1)
elif action == 'rotate-right':
playSound("turn")
obj.rotate(-1)
elif action == 'stop-left':
obj.stop(True,False,False,False)
elif action == 'stop-right':
obj.stop(False,True,False,False)
elif action == 'stop-up':
obj.stop(False,False,True,False)
elif action == 'stop-down':
obj.stop(False,False,False,True)
elif action == 'stop-rotate-left':
obj.rotate(0)
elif action == 'stop-rotate-right':
obj.rotate(0)
elif action == 'reset':
playSound("spawn")
obj.reset()
elif action == 'fire-press':
obj.interact(gamestate)
elif action == 'fire-release':
obj.interact(gamestate, release=True)
actions = []
def init():
global gamestate,playerColor,particleColors
loadSound("boost-loop","snd/boost-loop.wav")
loadSound("boost-start","snd/boost-start.wav")
loadSound("collect","snd/collect.wav")
loadSound("collision-25","snd/collision-25.wav")
loadSound("collision-50","snd/collision-50.wav")
loadSound("collision-75","snd/collision-75.wav")
loadSound("spawn","snd/spawn.wav")
loadSound("turn","snd/turn.wav")
player = PlayerObject(SPAWN_X, SPAWN_Y, tile='player'+str(playerColor),particleColor = particleColors[playerColor])
playerColor +=1
gamestate = GameState()
gamestate.objects[ownId] = player
gamestate.level = level
particlesInit()
loadGraphics()
init()
tick = 0
running = True
try:
while running:
tick += 1
render()
pygame.transform.scale(screen, window.get_size(), window)
pygame.display.flip()
cont = controls()
if not cont:
running = False
update()
clock.tick(FPS)
finally:
if net is not None:
net.stop()
pygame.quit()