-
Notifications
You must be signed in to change notification settings - Fork 0
/
PytBullets.py
384 lines (253 loc) · 12.5 KB
/
PytBullets.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
__author__ = 'Phi'
import pygame, math, sys, random, math, getopt
from pygame.locals import *
import BulletSprite
import World
# **********************************************************
class Game:
def usage(self):
print "Pytbullet 2014 (ppoupon@free.fr)"
print " [-h/help] "
print " [x/hex] use hex map : default"
print " [o/oct] use octogonal map"
print " [w:size/width=size] columns number : default = 17"
print " [l:size/lines=size] lines number : default = 15"
print " [s:size/bullet_size=size] bullets size : default = 40"
print " [f:size/frame=size] frame rate : default = 50"
print " [i:size/init_fill_lines=size] initial fill lines : default = 9"
# ===========================================
def __init__(self, pArgv ):
try:
lOpts, lArgs = getopt.getopt( pArgv, "hd:sxow:l:b:f:c:i:m:",
["help","debug:=","sound","hex","oct","width=","lines=","bullet_size=","frame=", "colors=", "init_fill_lines=", "max_shot="])
except getopt.GetoptError:
self.usage()
sys.exit(2)
lDebug =False
self.cSoundUse = False
lModeHex = True;
lColumns = 17
lLines = 15
lBulletSize = 40
self.cFrame = 50
lInitFillLines=9
lColors=6
lMaxShot=8
for lOpt, lArg in lOpts:
# print "Op:", lOpt , " Arg=", lArg
if lOpt in ("-h", "--help"):
self.usage()
sys.exit()
elif lOpt in ("-x", "--hex"):
lModeHex = True
elif lOpt in ("-s", "--sound"):
self.cSoundUse= True
elif lOpt in ("-o", "--oct"):
lModeHex = False
elif lOpt in ("-d", "--debug"):
lDebug = int(lArg)
elif lOpt in ("-w", "--width"):
lColumns = int(lArg)
if lColumns < 5 or lColumns > 100 :
print "Bad lColumn number ", lColumns
sys.exit(2)
elif lOpt in ("-l", "--lines"):
lLines = int(lArg)
if lLines < 5 or lLines > 100 :
print "Bad line number ", lLines
sys.exit(2)
elif lOpt in ("-b", "--bullet_size"):
lBulletSize = int(lArg)
if lBulletSize < 3 or lBulletSize > 100:
print "Bad bullet size ", lBulletSize
sys.exit(2)
elif lOpt in ("-f", "--frame"):
lFrame = int(lArg)
if lFrame >= 20 and lFrame <= 100 :
self.cFrame = lFrame
else:
print "Bad Frame rate ", lFrame
sys.exit(2)
elif lOpt in ("-c", "--colors"):
lColors = int(lArg)
if lColors< 2 or lColors > 12 :
print "Bad colors number ", lColors
sys.exit(2)
elif lOpt in ("-i", "--init_fill_lines"):
lInitFillLines = int(lArg)
if lInitFillLines< 2 or lInitFillLines > lLines-2 :
print "Bad init fill lines number ", lInitFillLines
sys.exit(2)
elif lOpt in ("-m", "--max_shot"):
lMaxShot = int(lArg)
if lMaxShot < 2 or lMaxShot> 12 :
print "Bad Max Shot ", lMaxShot
sys.exit(2)
self.initSound()
self.cScreen = pygame.display.set_mode((1024, 768))
self.cClock = pygame.time.Clock()
self.cWorld = World.World( self, lDebug, lModeHex, lColumns, lLines, lBulletSize, lColors, lInitFillLines, lMaxShot);
self.cRect = self.cScreen.get_rect()
self.cBullet = BulletSprite.BulletSprite( random.randint( 1, self.cWorld.MAX_BULLET_TYPE ), self.cWorld )
self.cNextBullet = BulletSprite.BulletSprite( random.randint( 1, self.cWorld.MAX_BULLET_TYPE ), self.cWorld )
self.cBulletGroup = pygame.sprite.RenderPlain(self.cBullet)
self.cMemMousePos = (0.0,0.0);
self.resetGame()
#====================================
def initSound( self ):
if self.cSoundUse :
self.cSoundOn = True
else:
self.cSoundOn = False
if self.cSoundUse :
# self.cMixer = pygame.mixer.init(44100, -16, 2, 4096)
self.cMixer = pygame.mixer.init()
if self.cMixer == None:
self.cSoundUse = self.cSoundOn = False
print "Sound initialization failed ! ", pygame.get_error()
if self.cSoundUse == False:
self.cSoundFire = None
self.cSoundPut =None
self.cSoundScroll = None
self.cSoundDestroy = None
self.cSoundGameWin = None
self.cSoundGameOver = None
else:
print "MaxChannel:" , pygame.mixer.get_num_channels()
self.cSoundFire = pygame.mixer.Sound( "Fire.wav" )
self.cSoundPut = pygame.mixer.Sound( "Put.wav" )
self.cSoundScroll = pygame.mixer.Sound( "Scroll.wav" )
self.cSoundDestroy = pygame.mixer.Sound( "Destroy.wav" )
self.cSoundGameWin = pygame.mixer.Sound( "GameWin.wav" )
self.cSoundGameOver = pygame.mixer.Sound( "GameOver.wav" )
#====================================
def resetGame( self ):
self.cBullet = BulletSprite.BulletSprite( random.randint( 1, self.cWorld.MAX_BULLET_TYPE ), self.cWorld )
self.cNextBullet = BulletSprite.BulletSprite( random.randint( 1, self.cWorld.MAX_BULLET_TYPE ), self.cWorld )
self.cBulletGroup = pygame.sprite.RenderPlain(self.cBullet)
self.cMemMousePos = (0.0,0.0);
self.cWorld.resetWorld()
self.restart = False
#====================================
def playSound( self, pSound, pLoops=0, pMaxtime=0, pFade=0 ):
if self.cSoundOn and pSound != None:
pSound.play( pLoops, pMaxTime, pFade )
#====================================
def stopSound( self, pSound ):
a =0
# if self.cSoundOn and pSound != None:
# pSound.stop()
#====================================
def fadeOutSound( self, pSound, pFade ):
a=0
# if self.cSoundOn and pSound != None:
#y pSound.stop()
#====================================
def execEvent( self ):
for lEvent in pygame.event.get():
if lEvent.type == pygame.MOUSEMOTION:
if self.cWorld.cState == self.cWorld.STATE_TARGET:
# on est en mode viseur, on fait bouger le vecteur
self.cMemMousePos = lEvent.pos
continue
# return True
elif lEvent.type == pygame.MOUSEBUTTONDOWN and self.cWorld.cState == self.cWorld.STATE_TARGET:
# -----------------------------
# on teste si on a clique sur un boutons de commande
if self. cWorld.cListButton.testExec( lEvent.pos ) == True :
# yes !
if self.restart == True :
return False
continue
self.cWorld.cState = self.cWorld.STATE_FIRE
self.cBullet.cMoving = True
self.playSound( 0, 10, 3)
self.cWorld.cTurn += 1
lDiffX = lEvent.pos[0]-self.cBullet.cNewPosition[0]
lDiffY = lEvent.pos[1]-self.cBullet.cNewPosition[1]
if lDiffY > -self.cWorld.BULLET_H :
lDiffY = -self.cWorld.BULLET_H
self.cMemMousePos = (lDiffX, lDiffY)
lNorm = math.sqrt( self.cMemMousePos[0]*self.cMemMousePos[0]+self.cMemMousePos[1]*self.cMemMousePos[1]);
self.cBullet.cSpeedX = self.cMemMousePos[0] / (lNorm)
self.cBullet.cSpeedY = self.cMemMousePos[1] / (lNorm)
# -----------------------------
# on teste si on a appuyer sur une touche
elif hasattr( lEvent, 'key'):
if lEvent.key == K_UP and lEvent.type == pygame.KEYDOWN:
self.cWorld.cState = self.cWorld.STATE_TARGET
self.cBulletGroup.empty()
self.cBullet = BulletSprite.BulletSprite( self.cWorld.getRandomBullet(), self.cWorld )
self.cBulletGroup.add(self.cBullet)
elif lEvent.key == K_r:
self.cWorld.flipMemTabBullet()
elif lEvent.key == K_h and lEvent.type == pygame.KEYDOWN:
self.cWorld.cModeHex = not self.cWorld.cModeHex
elif lEvent.key == K_ESCAPE:
sys.exit(0)
elif lEvent.type == pygame.QUIT:
sys.exit(0)
#====================================
def run(self):
# USER INPUT
self.cDeltaTime = self.cClock.tick( self.cFrame )
# ====================
if self.cWorld.cState == self.cWorld.STATE_GAME_STOP:
sys.exit()
# ====================
else :
# ============== Winner ? ==============
if self.cWorld.countAllBullet() == 0:
# print "GameWin Game"
self.cWorld.gameWinAnimation( 300 )
self.restart = True
return False
# Scrolling ?
if self.cWorld.cShotRemain <= 0 :
self.cWorld.countBulletType()
self.cWorld.cShotRemain = self.cWorld.MAX_SHOT
lResult = self.cWorld.scrollDownRange();
# ============= Game Over ? =============
if lResult == False :
# print "GameOver"
self.cWorld.gameOverAnimation( 200 )
self.restart = True
return False
lMemMovingState = self.cBullet.cMoving
if self.execEvent() == False:
return False
# We return to target mode where bullet has finish to move
if self.cWorld.cState == self.cWorld.STATE_FIRE and self.cBullet.cMoving == False:
self.cWorld.cState = self.cWorld.STATE_TARGET
self.playSound( self.cSoundPut )
# new bullet
self.cBulletGroup.empty()
self.cBullet = self.cNextBullet;
self.cBulletGroup.add(self.cBullet)
self.cNextBullet = BulletSprite.BulletSprite( self.cWorld.getRandomBullet(), self.cWorld )
if self.cWorld.cState == self.cWorld.STATE_TARGET or self.cWorld.cState == self.cWorld.STATE_FIRE:
self.cBulletGroup.update( self.cDeltaTime, self.cRect ) # call the resolution of world
return True
# RENDERING
def render( self ):
self.cScreen.fill((150, 150, 150))
if self.cWorld.cState == self.cWorld.STATE_TARGET:
self.cWorld.drawVector( self.cScreen, self.cMemMousePos);
self.cWorld.draw( self.cScreen )
self.cWorld.drawScoreNextMenu( self.cScreen, self.cNextBullet )
self.cBulletGroup.draw( self.cScreen )
return True
#====================================
# **********************************************************
# MAIN
pygame.init()
lGame = Game( sys.argv[1:] )
while 1 :
while lGame.restart == False:
if lGame.run() == True :
if lGame.render() == True :
pygame.display.flip()
lGame.restart = False
lGame.resetGame()
if lGame.cSoundUse:
pygame.mixer.quit()