-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
executable file
·456 lines (346 loc) · 13.8 KB
/
game.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import sys
import pygame
import world
from world import Box2D
import pgview
import cwiid
class Game(object):
def __init__(self):
# Pygame and clock init
pygame.init()
self.clock=pygame.time.Clock()
# Setup screen
self.screen=pygame.display.set_mode((1024,500))
self.screen.fill(pgview.BG_COLOR)
# Setup screen areas
screen_size=self.screen.get_size()
hud_top=(0,0)
hud_size=MinimumSize('arial',12,5)
hud_size=(hud_size[0]+40,screen_size[1])
arena_top=(hud_size[0],0)
arena_size=(screen_size[0]-hud_size[0],screen_size[1])
arena_rect=pygame.Rect(arena_top,arena_size)
arena_surf=self.screen.subsurface(arena_rect)
self.world=world.World(arena_surf)
self.sprites=pygame.image.load('Devilish-MainBall.png')
self.sprites.set_colorkey((0,248,0),pygame.RLEACCEL)
p1_sprite=self.sprites.subsurface(pygame.Rect((2,29),(20,20))).copy()
p2_sprite=self.sprites.subsurface(pygame.Rect((2,6),(20,20))).copy()
self.players=[
Player(self.world,self.world.balls,
p1_sprite,'Mouse',(0,0,255),arena_top),
Player(self.world,self.world.balls,
p1_sprite,'Wiimote',(0,0,255),arena_top),
#Player(self.world,self.world.balls,
# p2_sprite,'Mouse',(255,0,0),arena_top),
]
hud_rect=pygame.Rect(hud_top,hud_size)
hud_surf=self.screen.subsurface(hud_rect)
self.hud=HUD(hud_surf,self.players,'arial',12,(255,255,255),(0,0,0))
def run(self):
#self.menu()
pygame.display.update()
while True:
self.clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.world.makeBall()
elif event.key == pygame.K_ESCAPE:
sys.exit()
for player in self.players:
if player.control=='Mouse':
player.mouseInputs(self.world,event)
for player in self.players:
if player.control=='Wiimote': player.wmInputs(self.world)
self.world.update(self.players,self.hud)
rectlist=[]
rectlist.append(self.world.draw())
if self.hud.hasChanged():
rectlist.append(self.hud.draw())
pygame.display.update(rectlist)
class Player(object):
playerCount=0
mouseOffset=(0,0)
def __init__(self,world,balls,
sprite,control='Mouse',color=(0,0,255),mouseOffset=(0,0)):
Player.playerCount+=1
self.playerNum=Player.playerCount
self.control=control
self.color=color
self.sprite=sprite
self.world=world
self.balls=balls
self.ball=None
self.points=0
self.active=False
self.hud=None
self.mouseOffset=mouseOffset
if self.control=="Wiimote":
# Setup Wiimotes
print 'Press 1+2 on your Wiimote now'
# TODO try catch
self.wm=cwiid.Wiimote()
self.wm.rpt_mode=cwiid.RPT_BTN | cwiid.RPT_ACC
self.wmstate=self.wm.state
def mouseInputs(self,world,event):
if event.type == pygame.MOUSEBUTTONDOWN:
# Activate ball
if event.button == 1:
pos=(event.pos[0]-self.mouseOffset[0],
event.pos[1]-self.mouseOffset[1])
self.activateBall(pos)
# Next ball
if event.button == 3: self.selectNextBall()
# Deactivate ball
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.deactivateBall()
# Update
elif event.type == pygame.MOUSEMOTION and self.active:
pos=(event.pos[0]-self.mouseOffset[0],
event.pos[1]-self.mouseOffset[1])
self.updateControl(world,pos)
def wmInputs(self,world):
wmstate=self.wm.state
# Next ball
if (wmstate['buttons'] & cwiid.BTN_A) and \
not (self.wmstate['buttons'] & cwiid.BTN_A):
self.selectNextBall()
# Activate ball
if (wmstate['buttons'] & cwiid.BTN_B) and \
not (self.wmstate['buttons'] & cwiid.BTN_B) :
self.activateBall(wmstate['acc'])
# Deactivate ball
if not (wmstate['buttons'] & cwiid.BTN_B) and \
(self.wmstate['buttons'] & cwiid.BTN_B):
self.deactivateBall()
# Push ball
if (wmstate['buttons'] & cwiid.BTN_B) and self.active:
self.updateControl(world,wmstate['acc'])
self.wmstate=wmstate
def selectNextBall(self):
self.balls.selectNext(self)
def activateBall(self,pos):
self.balls.activate(self)
if self.ball!=None:
self.active=True
self.createControl(pos)
def deactivateBall(self):
if self.active==True and self.ball!=None:
self.active=False
self.balls.deactivate(self)
self.destroyControl()
def createControl(self,vect):
if self.control=='Mouse':
self.joint=self.world.createJoint(self.ball,vect)
elif self.control=='Wiimote':
center=self.ball.body.massData.center
fx=vect[0]*self.ball.body.GetMass()
fy=vect[1]*self.ball.body.GetMass()
self.ball.body.ApplyForce(Box2D.b2Vec2(fx,fy), center)
def destroyControl(self):
if self.control=='Mouse':
self.world.destroyJoint(self.joint)
elif self.control=='Wiimote':
world.destroyForce(self.force)
def updateControl(self,world,vect):
# Mouse vect=position
# Wiimote vect=acceleration
if self.control=='Mouse' and self.active==True:
self.ball.body.isSpleeping=False
vect=world.s2wPos(vect)
self.joint.target=Box2D.b2Vec2(vect[0], vect[1])
elif self.control=='Wiimote':
point=self.ball.body.getMassData.center
print vect
fx=vect[0]*self.ball.body.GetMass()
fy=vect[1]*self.ball.body.GetMass()
print fx
print fy
self.ball.body.ApplyForce(Box2D.b2Vec2(fx,fy), point)
def incrementPoints(self,ball):
self.points+=1
self.hud.update()
if ball is self.ball:
self.ball=None
self.active=False
def MinimumSize(font,font_size,num_players,line_spacing=5):
font=pygame.font.SysFont(font,font_size)
line_size=font.size('Player 9: 100')
line_size=(line_size[0],line_size[1]+line_spacing)
return (line_size[0],line_size[1]*num_players)
class HUD(object):
def __init__(self,surface,players,font,font_size,font_color,bgcolor,line_spacing=5):
for player in players:
player.hud=self
self.surface=surface
self.font=pygame.font.SysFont(font,font_size)
self.font_color=font_color
self.bg_color=bgcolor
self.line_spacing=line_spacing
line_height=MinimumSize(font,font_size,1,line_spacing)[1]
text_size=MinimumSize(font,font_size,len(players),line_spacing)
hud_size=(text_size[0]+10,text_size[0]+10)
hud_top=pgview.centerRectangles((0,0),surface.get_size(),hud_size)
if hud_top==None:
print 'HUD larger than area'
sys.exit()
hud_top=(hud_top[0],10)
text_top=pgview.centerRectangles(hud_top,hud_size,text_size)
if text_top==None:
print 'Text larger than HUD'
sys.exit()
# x and y so that text is centered
pos=[text_top]
y=text_top[1]
for player in players:
y+=line_height
pos.append((text_top[0],y))
self.pos=pos
self.players=players
self.surface=surface
self.changed=False
self.surface.fill(self.bg_color,pygame.Rect(hud_top,hud_size))
self.update()
def update(self):
for i in range(len(self.players)):
txt='Player ' + str(self.players[i].playerNum) + ': ' \
+ str(self.players[i].points)
text=self.font.render(txt,True,self.font_color,self.bg_color)
rect=pygame.Rect(self.pos[i],(text.get_width(),text.get_height()))
self.surface.blit(text,rect)
self.changed=True
def hasChanged(self):
if self.changed:
self.changed=False
return True
else: return False
def draw(self):
return self.surface.get_rect(topleft=self.surface.get_offset())
class Menu(object):
def __init__(self,screen,optionList,
font_type,font_size,border_spacing,item_spacing):
"""optionList[i]=(option text,callback) """
self.screen=screen
self.optionList=optionList
self.font=pygame.font.SysFont(font_type,font_size)
self.font=font_type
self.font_size=font_size
self.border_spacing=border_spacing
self.item_spacing=item_spacing
self.highlightedItem=None
def menuSize(self):
itemSize=self.itemSize()
menuSize=(itemSize[0],(itemSize[1]+item_spacing)*len(self.optionList))
return menuSize
def itemSize(self):
line_size=self.font.size(' '.ljust(self.maxlen()))
return (line_size[0]+border_spacing,line_size[1]+2*border_spacing)
def maxLength(self):
maxlen=0
for option,callback in self.optionList:
if len(option)>maxlen: maxlen=len(option)
return maxlen
def showMenu(self):
self.screen_back=self.screen.copy()
menu_size=self.menuSize('arial',12,2,5,optionList)
top_menu=centerRectangles((0,0),screen.size,menu_size())
menuItem_top=top_menu
menuItem_size=self.itemSize()
self.menuItem_size=menuItem_size()
self.menuItem_top=[]
self.text_top=[]
for option,callback in self.optionList:
# Menu item rectangle
surf=pygame.Surface(menuItem_size)
surf.fill((255,255,255))
# Menu item text
text_size=self.font.size(option)
text_surf=self.font.render(option,True,(0,0,0),(255,255,255))
text_top=centerRectangles((0,0),menuItem_size,text_size)
surf.blit(text_surf,text_top)
# Blit item on screen
self.screen.blit(surf,menuItem_top)
# Store positions
self.text_top.append(text_top)
self.menuItem_top.append(menuItem_top)
# Next item
menuItem_top=(menuItem_top[0],menuItem_top[1]+menuItem_size[1]+self.item_spacing)
pygame.display.uptdate()
def highlightItem(self,index):
text_surf=self.font.render(option,True,(0,0,255),(255,255,255))
self.screen.blit(text_surf,self.text_top[index])
self.hightlightedItem=index
def unhighlight(self):
if self.hilightedItem==None: return
text_surf=self.font.render(option,True,(0,0,0),(255,255,255))
self.screen.blit(text_surf,self.text_top[self.highlightedItem])
def highlightDown(self):
self.unhighlightItem()
if self.highlightedItem==None: index=0
else:
index=self.highlightededItem+1
if index>len(self.menuItem_top)-1: index=0
self.highlightItem(index)
def highlightUp(self):
self.unhighlightItem()
if self.highlightedItem==None: index=len(self.menuItem_top)-1
else:
index=self.highlightedItem-1
if index<0: index==len(self.menuItem_top)-1
self.highlightItem(index)
def highlightMouseOver(self,pos):
mouseOver=None
for i in range(len(self.menuItem_top)):
top=self.menuItem_top[i]
size=self.menuItem_size
if pos[0]<top[0] or pos[1]<top[1]: break
elif pos[0]<top[0]+size[0] and pos[1]<top[1]+size[1]:
mouseOver=i
break
if mouseOver!=None:
self.unhighlight()
self.highlight(mouseOver)
def callback(self):
if self.highlightedItem!=None:
index=self.highlightedItem
self.optionList[index][1]()
def getCallback(self,pos):
for i in range(len(self.menuItem_top)):
top=self.menuItem_top[i]
size=self.menuItem_size
if pos[0]<top[0] or pos[1]<top[1]: break
elif pos[0]<top[0]+size[0] and pos[1]<top[1]+size[1]:
return self.optionList[i][1]
# def getCallbackHighlighted(self)
# return self.optionList[self.highlightedItem][1]
def run(self):
self.showMenu()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.quit()
elif event.key == pygame.K_UP:
self.highlightDown()
elif event.key == pygame.K_DOWN:
self.highlightUp()
elif event.key == pygame.K_ENTER:
self.callback()
for player in self.players:
if player.control=='Mouse':
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.callback()
elif event.type == pygame.MOUSEMOTION:
self.highlightMouseOver(event.pos)
#for player in self.players:
# if player.control=='Wiimote': player.wmInputs(self.world)
def quit(self):
self.screen.blit(self.screen_backup)
pygame.display.uptdate()