-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·347 lines (325 loc) · 11.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
#!/usr/bin/env python
#try:
# import psyco
# psyco.full()
# except ImportError:
# print("Get psyco!")
#import os
from random import random
#try:
import pygame, sys
#import Numeric as N
#from pygame.locals import *
import pygame.locals as pgl
surfarray = pygame.surfarray
#if not surfarray:raise ImportError
#except ImportError:
# raise ImportError, 'need pygame and numeric'
import settings
from math import hypot
pygame.init()
import display
display.loadtex("nothing.png")
import terrain
#import walker, ball, player #walker is like a lemming, and ball a simpler player
import player
import timer, time
import simplenet
import particles
import menu
#from cPickle import *
#import zlib
#from zlib import *
import OpenGL.GL as GL
pygame.display.set_caption("NeinJarz")
#i cant pickle events?
class netevent:
def __init__(self):
pass
# pos = None
pass
def bind(a, b, dist, stren=0.1):
xd = a.x-b.x
yd = a.y-b.y
td = hypot(xd, yd)
diffd = (dist-td)*stren
xd /=td
yd /=td
a.xvel +=xd*diffd
a.yvel +=yd*diffd
b.xvel -=xd*diffd
b.yvel -=yd*diffd
class gameStateClass:
def __init__(self):
self.walkers = []
self.clients = self. walkers
terrain.walkers = self.walkers #fixme i dont think i need or want this line
#but should improve the gamestate class, give it some funcs
#oh, and fix the freaking networking so its good!
self.walkers.append(player.playerclass(self, "The server", None))
class njgame:
def __init__(self):
terrain.loadimg("testlevel")
#terrain.loadimg("clev")
self.name = "damnidontcare"
display.init()
self.gamestate = gameStateClass()
self.walkers = self.gamestate.walkers
self.clients = self. walkers
simplenet.clients = self.clients
terrain.walkers = self.walkers
display.lookat(0, 0)
self.mx = 100
self.my = 100
self.mywalker = self.walkers[0]
self.menu = menu.Menu()
#terrain.explode(10,10,1000,0)
def findclient(self, netdata):
for c in self.clients:
if c.netdata == None: continue
found = False
if netdata[0] == c.netdata[0] and netdata[1] == c.netdata[1]:
found = True
if found == True:
#print c
return c
def addClient(self, cname, netdata):
for c in self.clients:
if c.netdata == None: continue
if c.netdata[0] == netdata[0]:
return None
newplayer = player.playerclass(self.gamestate, cname, netdata)
print( (cname, netdata), "joined")
self.clients.append(newplayer)
return newplayer
def doNet(self):
#if isServer:
# checktimeouts()
while simplenet.checkdata():
packet, peer = simplenet.nextpacket()
if packet is None: break
pType = packet[0]
pData = packet[1]
#print pType, pData
if simplenet.isServer:
cl = self.findclient(peer)
if cl != None: cl.lastpackettime = 0
if pType=="join":
cl = self.addClient(pData, peer)
if cl is None:
simplenet.sendto("joinfailed", "alreadyjoined?", peer)
else:
simplenet.sendto("yourein", "dosomethingcool", peer)
elif cl is None:
simplenet.sendto("whoyou", "i didnt get join message", peer)
elif pType == "event":
self.handleEvent(pData, cl)
else:#client
if pType=="gamestate":
self.gamestate = pData
self.clients = self.gamestate.clients
self.walkers = self.clients
for w in range(len(self.walkers)):
if self.walkers[w].name == self.name:
self.mywalker = self.walkers[w]
elif pType=="boom":
terrain.explode(pData[0], pData[1], pData[2], pData[3])
elif pType=="pastetex":
terrain.pastesurfID(pData[0], pData[1], pData[2], pData[3], pData[4])
elif pType=="addtps":
particles.addTextParticle(pData[0], pData[1], pData[2], pData[3], pData[4], pData[5])
elif pType=="addps":
particles.addParticle(pData[0], pData[1], pData[2], pData[3], pData[4], pData[5], pData[6])
#print pData
elif pType=="msg":
#addmsg(pData[0], pData[1])
print(pData)
elif pType=="whoyou":
simplenet.sendserv("join", self.name)
elif pType=="wakeup":
simplenet.sendserv("keepalive", "dude")
def run(self):
#self.runMenu()
#self.initNet()
while 1:
timer.tick()
self.doNet()
for event in pygame.event.get():
self.handleEvent(event, self.mywalker)
if not simplenet.isClient:
self.mainloop()
particles.update()
if simplenet.isServer:
self.drawstuff()
simplenet.sendall("gamestate", self.gamestate)
else:
#particles.update()
self.drawstuff()
#self.mainloop()
def handleEvent(self, event, cplayer):
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pgl.VIDEORESIZE:
print(event.dict['size'])
display.resize(event.dict['size'][0], event.dict['size'][1])
pygame.display.flip()
elif simplenet.isClient:
ne = netevent()
ne.type = event.type
if event.type == 2 or event.type == 3:
if event.key == pgl.K_ESCAPE:
sys.exit()
ne.key = event.key
simplenet.sendserv("event", ne)
elif event.type == pgl.MOUSEMOTION:
ne.pos = [event.pos[0], event.pos[1]]
simplenet.sendserv("event", ne)
elif event.type == pgl.MOUSEBUTTONDOWN:
ne.button = event.button
ne.pos = [event.pos[0], event.pos[1]]
simplenet.sendserv("event", ne)
elif event.type == pgl.MOUSEBUTTONUP:
ne.button = event.button
ne.pos = [event.pos[0], event.pos[1]]
simplenet.sendserv("event", ne)
return
if event.type == 2:#keypress
if event.key == pgl.K_DELETE:
if len(self.walkers) > 0:self.walkers.remove(self.walkers[0])
if event.key == pgl.K_w:
#for i in range(0, len(self.walkers)):self.walkers[i].upkey = 1
cplayer.upkey = 1
if event.key == pgl.K_ESCAPE:
sys.exit()
if event.key == pgl.K_m:
self.menu.runMenu()
if event.key == pgl.K_s:
cplayer.dnkey = 1
if event.key == pgl.K_a:
cplayer.ltkey = 1
if event.key == pgl.K_d:
cplayer.rtkey = 1
if event.key == pgl.K_SPACE:
cplayer.jumpkey = 1
if event.key == pgl.K_LCTRL:
cplayer.crouchkey = 1
if event.key == pgl.K_v:
cplayer.digkey = 1
if event.key == pgl.K_q:
cplayer.changegun(1)
if event.key == pgl.K_e:
cplayer.changegun(-1)
if event.type == 3:#keyrelease
if event.key == pgl.K_n:
newplayer = player.playerclass(self.gamestate, "A Bot " + str(len(self.gamestate.clients)), None)
newplayer.isbot = 1#int(random()*2)
newplayer.jumpkey = int(random()*2)
newplayer.x = cplayer.gmx
newplayer.y = cplayer.gmy
self.walkers.append(newplayer)
if event.key == pgl.K_w:
cplayer.upkey = 0
if event.key == pgl.K_s:
cplayer.dnkey = 0
if event.key == pgl.K_a:
cplayer.ltkey = 0
if event.key == pgl.K_d:
cplayer.rtkey = 0
if event.key == pgl.K_SPACE:
cplayer.jumpkey = 0
if event.key == pgl.K_LCTRL:
cplayer.crouchkey = 0
if event.key == pgl.K_v:
cplayer.digkey = 0
if event.type == pgl.MOUSEMOTION or event.type == pgl.MOUSEBUTTONUP or event.type == pgl.MOUSEBUTTONDOWN:
cplayer.msinx = event.pos[0]
cplayer.msiny = event.pos[1]
#print event.type
if event.type == pgl.MOUSEBUTTONUP:
if event.button == 1:
cplayer.unshoot()#jumpkey = 1
if event.button == 3:
cplayer.altunshoot()#jumpkey = 1
if event.type == pgl.MOUSEBUTTONDOWN:
#print event.button
if event.button == 4:
cplayer.changegun(1)
if event.button == 5:
cplayer.changegun(-1)
if event.button == 3:
cplayer.altshoot()
#terrain.explode(cplayer.gmx, cplayer.gmy, 90, 255)
if event.button == 1:
cplayer.shoot()#jumpkey = 1
#self.walkers.append(player.playerclass(mx, my))
#self.ownid = len(self.walkers)-1
#if event.button == 2:
# display.lookat(self.mx, self.my)
def mainloop(self):
#bind(self.walkers[4], self.walkers[5], 50)
for b in self.walkers:
b.update()
if b.kills > 9:
terrain.loadimg("clev")
b.kills = 0
remlist = []
for i in range(0, len(self.walkers)-1):
for r in range(i+1, len(self.walkers)):#FIXME
dist = 25
a=self.walkers[i]
b=self.walkers[r]
xd = a.x-b.x
yd = a.y-b.y
td = hypot(xd, yd)#+0.000000001
if (0<td< dist):
diffd = (dist-td)*0.49
xd /=td
yd /=td
a.xvel +=xd*diffd
a.yvel +=yd*diffd
b.xvel -=xd*diffd
b.yvel -=yd*diffd
a.movev(xd *diffd, yd *diffd)
b.movev(-xd *diffd, -yd *diffd)
if a.y < b.y:
a.canjump = 1
else:
b.canjump = 1
#particles.addParticle(a.x, b.y, 5, 3, "smoke.png")
#a.x+=xd*diffd
#a.y+=yd*diffd
#b.x-=xd*diffd
#b.y-=yd*diffd
for i in remlist:
self.walkers.remove(i)
#particles.update()
def drawradar(self):
GL.glLineWidth(2)
GL.glDisable(GL.GL_TEXTURE_2D)
GL.glColor4f(0.1, 0.95, 0.05, 0.3)
for i in self.clients:
if i != self.mywalker:
xd = (self.mywalker.x-i.x)*0.03
yd = (self.mywalker.y-i.y)*0.03
GL.glBegin(GL.GL_LINES)
GL.glVertex3f(self.mywalker.x,self.mywalker.y, 0.0)
GL.glVertex3f(self.mywalker.x-xd,self.mywalker.y-yd, 0.0)
GL.glEnd()
GL.glEnable(GL.GL_TEXTURE_2D)
def drawstuff(self):
if self.mywalker!=None:
display.lookat((self.mywalker.x+self.mywalker.gmx)/2, (self.mywalker.y+self.mywalker.gmy)/2)
self.drawradar()
display.clear()
display.applycam()
terrain.draw(display.camx,display.camy)
for b in self.walkers:
b.draw()
particles.draw()
self.menu.drawLog()
pygame.display.flip()
def startup():
game = njgame()
game.run()
if __name__ == '__main__':
startup()