-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
370 lines (291 loc) · 10.9 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
import pygame
import argparse
import io
import os
import random
import math
from globalconst import *
from gameobjects import *
from bitmapfont import BitmapFont
from worm import Worm
from ball import Ball
from bird import Bird
from playerobject import *
from gamestate import GameState
import sound
import network
ownId = -1 # -1 = worm, -2 = ball, rest = players
playerColor = 0
actions = []
objects = {}
spawnpoints = [(3, 3), (3, 18), (3, 6), (3, 15), (3, 9), (3, 12)]
parser = argparse.ArgumentParser()
parser.add_argument('--connect')
parser.add_argument('--port', type=int, default=2000)
parser.add_argument('--host', action='store_true')
parser.add_argument('--nosnake', action='store_true')
parser.add_argument('--level', type=str, default='LEV1')
args = parser.parse_args()
net = None
clients = {}
if args.connect is not None:
net = network.connect(args.connect, args.port)
ownId = int(random.random() * 1000000)
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)
font = BitmapFont('gfx/heimatfont.png', scr_w=SCR_W, scr_h=SCR_H, colors=[(255,255,255), (240,0,240)])
tiles = {'#': pygame.image.load('gfx/wall.png'),
'.': pygame.image.load('gfx/goal.png'),
'H': pygame.image.load("gfx/worm_head.png"),
'B': pygame.image.load("gfx/worm_body.png"),
'o': pygame.image.load('gfx/ball.png'),
'v': pygame.image.load('gfx/bird.png'),
'10': pygame.image.load('gfx/player1.png'),
'11': pygame.image.load('gfx/player1-walk1.png'),
'12': pygame.image.load('gfx/player1-walk2.png'),
'20': pygame.image.load('gfx/player2.png'),
'21': pygame.image.load('gfx/player2-walk1.png'),
'22': pygame.image.load('gfx/player2-walk2.png'),
'30': pygame.image.load('gfx/player3.png'),
'31': pygame.image.load('gfx/player3-walk1.png'),
'32': pygame.image.load('gfx/player3-walk2.png'),
'40': pygame.image.load('gfx/player4.png'),
'41': pygame.image.load('gfx/player4-walk1.png'),
'42': pygame.image.load('gfx/player4-walk2.png'),
'50': pygame.image.load('gfx/player5.png'),
'51': pygame.image.load('gfx/player5-walk1.png'),
'52': pygame.image.load('gfx/player5-walk2.png'),
'ea': pygame.image.load('gfx/player-eaten.png'),
}
sound.loadSound('whistle', 'snd/trillerpfeife.wav')
sound.loadSound('kick', 'snd/kick.wav')
sound.loadSound('applause', 'snd/applause.wav')
gamestate = GameState(args.level)
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 worm for first joined player if host doesn't want to be a worm (TODO this is a bit ugly, code-wise)
if objId != -1:
if net is not None and net.isHost():
if args.nosnake:
if not gamestate.getWorms():
worm = Worm(math.floor(LEV_W/2),math.floor(LEV_H/2))
gamestate.objects[objId] = worm
print('created worm for player id=', objId)
return
# create ordinary player
x, y = spawnpoints[playerColor]
newPlayer = Player(TILE_W * x, TILE_H * y, playerColor)
playerColor += 1
playerColor %= 6
gamestate.objects[objId] = newPlayer
print('created player with id=', objId)
def removePlayer(objId):
del gamestate.objects[objId]
def controls():
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_LEFT:
actions.append(('move-left', ownId))
if e.key in KEYS_RIGHT:
actions.append(('move-right', ownId))
if e.key in KEYS_UP:
actions.append(('move-up', ownId))
if e.key in KEYS_DOWN:
actions.append(('move-down', 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_LEFT:
actions.append(('stop-left', ownId))
if e.key in KEYS_RIGHT:
actions.append(('stop-right', ownId))
if e.key in KEYS_UP:
actions.append(('stop-up', ownId))
if e.key in KEYS_DOWN:
actions.append(('stop-down', ownId))
if e.key in KEYS_FIRE:
actions.append(('fire-release', 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(('move-left', ownId))
elif e.value > JOY_DEADZONE:
actions.append(('move-right', ownId))
else:
if ownPlayer.xdir < 0:
actions.append(('stop-left', ownId))
if ownPlayer.xdir > 0:
actions.append(('stop-right', ownId))
if e.axis == 1:
if e.value < -JOY_DEADZONE:
actions.append(('move-up', ownId))
elif e.value > JOY_DEADZONE:
actions.append(('move-down', ownId))
else:
if ownPlayer.ydir < 0:
actions.append(('stop-up', ownId))
if ownPlayer.ydir > 0:
actions.append(('stop-down', ownId))
if e.type == pygame.JOYBUTTONDOWN:
pass
if e.type == pygame.JOYBUTTONUP:
pass
return True
def render():
screen.fill((0, 128, 0))
if tick < 180:
font.drawText(screen, 'SNAKE SOCCER!', 2, 2, fgcolor=(255,255,255))#, bgcolor=(0,0,0))
seconds = int(tick / 60)
minutes = int(seconds / 60)
matchtime = '%02i:%02i' % (minutes, seconds % 60)
font.drawText(screen, 'PTS: %02i'% gamestate.points, 32, 1, fgcolor=(255,255,255))
font.drawText(screen, matchtime, 34, 20, fgcolor=(255,255,255))
# render level
for y in range(LEV_H):
for x in range(LEV_W):
if gamestate.getLevel()[y][x] == '#':
screen.blit(tiles['#'], (x * TILE_W, y * TILE_H))
elif gamestate.getLevel()[y][x] == '.':
screen.blit(tiles['.'], (x * TILE_W, y * TILE_H))
# render objects: shadows
gamestate.getBall().drawShadow(screen, tiles)
for obj in gamestate.getBirds():
obj.drawShadow(screen, tiles)
# render objects: background
for obj in gamestate.objects.values():
if type(obj) is not Ball and type(obj) is not Bird:
obj.draw(screen, tiles, gamestate)
# render objects: foreground
gamestate.getBall().draw(screen, tiles, gamestate)
for obj in gamestate.getBirds():
obj.draw(screen, tiles, gamestate)
def update():
global actions, gamestate, ownPlayer, clients
if net is None or net.isHost():
for obj in gamestate.objects.values():
obj.update(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()
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)
if not obj:
continue
if action == 'move-left':
obj.moveLeft()
elif action == 'move-right':
obj.moveRight()
elif action == 'move-up':
obj.moveUp()
elif action == 'move-down':
obj.moveDown()
elif action == 'stop-left':
obj.stopLeft()
elif action == 'stop-right':
obj.stopRight()
elif action == 'stop-up':
obj.stopUp()
elif action == 'stop-down':
obj.stopDown()
elif action == 'fire-press':
obj.interact(gamestate)
elif action == 'fire-release':
obj.interact(gamestate, release=True)
actions = []
def init():
global gamestate, spawnpoints
# scan player spawnpoints
for y in range(LEV_H):
for x in range(LEV_W):
if gamestate.getLevel()[y][x] in ['1', '2', '3', '4', '5', '6']:
idx = int(gamestate.getLevel()[y][x]) - 1
spawnpoints[idx] = (x, y)
worm = Worm(math.floor(LEV_W/2),math.floor(LEV_H/2))
ball = Ball(gamestate)
bird = Bird(0,0,'v')
if net is not None and net.isHost(): # if host doesn't want to be a snake, create a normal player instead
if not args.nosnake:
gamestate.objects[-1] = worm
else:
createPlayer(-1)
gamestate.objects[-2] = ball
gamestate.objects[-3] = bird
init()
tick = 0
running = True
sound.playSound('whistle')
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()