-
Notifications
You must be signed in to change notification settings - Fork 0
/
pongserver_v6.py
422 lines (332 loc) · 10.7 KB
/
pongserver_v6.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import math, random, os, sys, time
from ctypes import *
import pygame
from pygame import sprite
from pygame.locals import *
import threading
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
"""
Tmeplate:
This file is used as a template for pygame programing.
Do not pollute this file with appending other codes.
"""
(width, height) =(720,720)
background_colour = (255,255,255)
ball_colour = (255,0,0)
elasticity = 0.80
pygame.init()
pygame.display.set_caption('Template')
#pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((width, height))
# used for spritegroup
playergroup = []
master = None
class GGHandler(BaseHTTPRequestHandler):
def do_GET( self ):
try:
print self.headers
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('<p>good afternoon!</p>')
print self.client_address
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def runHTTP():
print '[HTTPSERV]run httpserver...'
try:
ggs.serve_forever()
except:
pass
finally:
print '[HTTPSERV]stop httpserver.'
port = 7788
ggs = HTTPServer( ('', port), GGHandler )
"""
[function name, usage, arguments]
#screen.blit( source, dest, area=None );
usage:
draw one image/(source surface) onto another
argument:
source - source surface
dest - dest can be either pairs of coordinates representing the upper left corner of the source or dest.
area - a small portion of source surface to draw
#surface.convert()
usage:
change the pixel format of an image with corresponding to the monitor. It help fasten the speed whenever SDL requires to do blit-action.
#surface.convert_alpha()
usage:
change the pixel format of an image including pixel alpha. Basically speaking, it enable transparency of an image.
"""
def drawDemo(bg):
font = pygame.font.Font(None, 36)
text = font.render("This is a template.", 1, (10,10,10))
textpos = text.get_rect()
textpos.center = bg.get_rect().center
bg.blit( text, textpos )
( x, y ) = ( textpos.x, textpos.y )
for i in range(1,5):
textpos = ( x, y-20*i )
bg.blit( text, textpos )
textpos = ( x, y+20*i )
bg.blit( text, textpos )
def isRunning():
is_running = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
elif event.key == pygame.K_DOWN:
if master is not None:
master.reset()
elif event.key == pygame.K_UP:
pass
return is_running
def load_png( fname, radius = 50 ):
if fname.endswith('png'):
w,h = [ (width, height) if fname.startswith('bg') else (102,77) ][0]
try:
image = pygame.image.load(fname)
image = pygame.transform.scale( image, (w, h) )
image = [ image.convert() if image.get_alpha() is None else image.convert_alpha() ][0]
except pygame.error, msg:
print 'Cannot load image', bg_fname
raise SystemExist, msg
return image, image.get_rect()
else:
color = [ (50,50,250,200) if not fname.startswith("ball") else ball_colour ][0]
circle = pygame.Surface( [radius*2]*2, SRCALPHA, 32 )
circle = circle.convert_alpha()
circle_rect = pygame.draw.circle( circle, color, [radius]*2, radius )
return circle, circle_rect
### detect collision of circles
### a, b are Surface objects
def detectCC(a, b):
ax,ay = a.rect.center
bx,by = b.rect.center
#return a.rect.colliderect(b.rect)
return [ True if math.sqrt ( ((ax - bx )**2) + ((ay - by)**2) ) < ( a.radius+b.radius ) else False ][0]
### calclulate collision of circles
### a, b are Surface objects
def calCC(a, b, v):
### a: ball, b: player
ax,ay = a.rect.center
bx,by = b.rect.center
xdiff = (ax-bx)
ydiff = (ay-by)
if ydiff == 0:
if xdiff > 0:
angle = math.pi
else:
angle = 0
elif xdiff == 0:
if ydiff > 0:
angle = -math.pi*0.5
else:
angle = math.pi*0.5
else:
theta = math.atan(abs(float(ydiff)/float(xdiff)))
if ydiff > 0 and xdiff > 0:
### 4rd
angle = theta
elif ydiff < 0 and xdiff > 0:
### 1st
angle = -theta
elif ydiff > 0 and xdiff < 0:
### 3rd
angle = math.pi - theta
elif ydiff < 0 and xdiff < 0:
### 2nd
angle = -math.pi + theta
# print "a({0:d},{1:d})_b({2:d},{3:d})_deg({4:5.2f})".format( ax, ay, bx, by, math.degrees(angle) )
return angle
class Ball(sprite.Sprite):
def __init__( self, xy, vector, radius = 50 ):
sprite.Sprite.__init__( self )
self.origin = xy
self.vector_o = vector
self.radius = radius
self.image, self.rect = load_png( 'ball', radius )
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.vector = vector
self.rect.topleft = map(int, xy)
self.lasthit = None
self.numshit = 0
self.lasthitangle = None
self.istatic = True
def update( self ):
newpos = self.calcNewpos( self.rect, self.vector )
( angle0,v0 ) = ( angle,v ) = self.vector
bdl,bdr,bdt,bdb = borders
if bdt.colliderect(newpos) or bdb.colliderect(newpos):
if (bdt.colliderect(newpos) and self.lasthit == bdt) or (bdb.colliderect(newpos) and self.lasthit == bdb):
print "[WARNING] for some reasons, ball may stay in the border for a short while. Here we work around this issume."
#sys.exit("# of hit:{0:d}".format(self.numshit))
else:
self.lasthit = [ bdt if bdt.colliderect(newpos) else bdb ][0]
self.numshit += 1
angle = -angle
x,y = self.rect.center
#print ' top/ down, ({4:d},{5:d}),({3:6.2f}->{0:6.2f}), ({1:5.2f},{2:5.2f})'.format(
# math.degrees(angle), v*math.cos(angle), v*math.sin(angle), math.degrees(angle0), x, y )
elif bdl.colliderect(newpos) or bdr.colliderect(newpos):
if (bdl.colliderect(newpos) and self.lasthit == bdl) or (bdr.colliderect(newpos) and self.lasthit == bdr):
print "[WARNING] for some reasons, ball may stay in the border for a short while. Here we work around this issume."
#sys.exit("# of hit:{0:d}".format(self.numshit))
else:
self.lasthit = [ bdl if bdl.colliderect(newpos) else bdr ][0]
self.numshit += 1
angle = math.pi - angle
x, y = self.rect.center
if math.degrees(angle) >= 360.:
angle -= math.pi*2
elif math.degrees(angle) < -360.:
angle += math.pi*2
#print 'left/right, ({4:d},{5:d}),({3:6.2f}->{0:6.2f}), ({1:5.2f},{2:5.2f})'.format(
# math.degrees(angle), v*math.cos(angle), v*math.sin(angle), math.degrees(angle0), x, y )
else:
for p in playergroup:
if self.istatic:
v = 0
if detectCC( self,p ):
self.lasthit = p
angle = calCC( self,p, v )
if self.istatic:
v = p.getMove()
if v == (0,0):
ax,ay = self.rect.center
bx,by = p.rect.center
r = self.radius + p.radius - math.sqrt( (ax-bx)**2 + (ay-by)**2 )
v = ( r*math.cos(angle), r*math.sin(angle) )
#v = [ math.ceil(x) for x in v if abs(x) < 1 ]
v = [ math.ceil(x) if abs(x) <1 else x for x in v ]
self.rect = newpos
self.vector = ( angle,v )
def calcNewpos( self, rect, vector ):
( angle,v ) = vector
if self.istatic:
if v is 0:
v = (0,0)
return rect.move(v)
else:
return rect.move( v*math.cos(angle),v*math.sin(angle) )
def reset( self ):
self.rect.topleft = map(int, self.origin)
self.vector = self.vector_o
self.lasthit = None
class Bat(sprite.Sprite):
def __init__( self, name, xy, radius = 25 ):
sprite.Sprite.__init__( self )
self.radius = radius
self.image, self.rect = load_png( name, radius )
self.area = screen.get_rect()
self.name = name
self.speed = 10
self.state = "still"
self.origin = xy
self.rect.center = xy
self.movpos = [0]*2
self.isfocused = False
self.mouse_motion = None
def update( self ):
focused, p, m = self.mouse_motion
if focused:
if not self.isfocused:
self.isfocused = True
self.rect.center = [ self.radius ]*2
self.rect.move_ip( p )
### modify the cursor's postion to right at the center of Bat.
self.rect = self.rect.move( map(int,[-self.rect.w*0.5]*2) )
else:
self.rect = self.rect.move(m)
#print 'mouse:', p, ', vb:',self.rect.center
else:
self.isfocused = False
self.rect.center = self.origin
def move( self, b, p, m ):
self.mouse_motion = ( b,p,m )
def getMove( self ):
if self.mouse_motion is not None:
b,p,m = self.mouse_motion
return m
return (0,0)
def main():
### http server
thr = threading.Thread(target=runHTTP)
thr.start()
### fill background
bg = pygame.Surface( screen.get_size() )
bg = bg.convert()
bg.fill( background_colour )
#bgimg, bgrect = load_png('bg_dark.png')
bgimg, bgrect = bg, bg.get_rect()
bgwimg, bgwrect = load_png('bg_wire.png')
### auxiliary line
nblock = width/30
for i in range(1,nblock):
pygame.draw.line( bgimg, (55, 55, 55), (0, height*i/nblock), (width, height*i/nblock), 2 )
pygame.draw.line( bgimg, (55, 55, 55), (width*i/nblock, 0), (width*i/nblock, height), 2 )
### border
global borders
thickness = height*0.05
BDL = pygame.draw.rect( bgimg, (35,0,0), pygame.Rect( 0, 0, thickness, height ) )
BDR = pygame.draw.rect( bgimg, (35,0,0), pygame.Rect( width-thickness,0,thickness, height ) )
BDT = pygame.draw.rect( bgimg, (35,0,0), pygame.Rect( 0, 0, width, thickness ) )
BDB = pygame.draw.rect( bgimg, (35,0,0), pygame.Rect( 0, height-thickness, width, thickness ) )
borders = [BDL, BDR, BDT, BDB]
### demo
#drawDemo(bg)
### ball
global master
speed = 0
rand = ( ( 0.1*( random.randint( 5,8 ) ) ) )
master = ball = Ball( ( width*0.3,height*0.3 ), ( math.radians(0),speed ) )
### player
global playergroup
player = Bat( "player", ( -width,-height ) )
playergroup.append(player)
### ball sprite
ballsprite = sprite.RenderPlain( ball )
### player sprite
playersprites = sprite.RenderPlain( player )
### draw background-color
#screen.blit( bg, (0,0) )
screen.blit( bgimg, bgrect )
screen.blit( bgwimg, bgwrect )
### flip
pygame.display.flip()
clock = pygame.time.Clock()
t_start = time.time()
while (1):
### detect mouse/keyboard event
if not isRunning():
ggs.socket.close()
t_c = time.time()
while thr.is_alive():
if time.time() - t_c > 3:
print 'timeout!'
break
print '[pygame] left gracefully.'
break
clock.tick(30)
player.move( pygame.mouse.get_focused(), pygame.mouse.get_pos(), pygame.mouse.get_rel() )
t_measure = [ 1/30. if clock.get_fps() < 30 else 1./clock.get_fps() ][0]
### blit
screen.blit( bgimg, ball.rect, ball.rect )
screen.blit( bgwimg, ball.rect, ball.rect )
screen.blit( bgimg, player.rect, player.rect )
screen.blit( bgwimg, player.rect, player.rect )
### update
ballsprite.update()
playersprites.update()
### draw
ballsprite.draw( screen )
playersprites.draw( screen )
pygame.display.set_caption( 'fps: {0:4.2f}'.format(clock.get_fps() ))
pygame.display.flip()
if __name__ == '__main__':
main()