Annotation of pyfrog/pyfrog.py, revision 1.5
1.1 nick 1: #!/usr/bin/python
2:
1.2 nick 3: import os, sys
1.1 nick 4: import pygame
1.2 nick 5: from pygame.locals import *
1.1 nick 6:
7: ##
8: ## Define our game initial variables
9: ##
1.2 nick 10: DEBUG = 1
11: DEBUG = 1
1.5 ! nick 12: VERSION = "$Id: pyfrog.py,v 1.4 2011-11-05 02:38:17 nick Exp $"
1.2 nick 13: TITLE = "PyFrog"
1.1 nick 14: SCREEN_WIDTH = 640
15: SCREEN_HEIGHT = 480
1.2 nick 16: COLORKEY = ( 255, 0, 255 )
1.5 ! nick 17: TRUE = 1
! 18: FALSE = 0
1.2 nick 19:
20: ##
21: ## Baddies
22: ##
23: VEHICLE = 0
24: LOG = 1
25: TURTLE = 2
26: GATOR = 3
27: SNAKE = 4
28: BEAVER = 5
29:
30: ##
31: ## Goal areas
32: ##
33: MAX_GOALS = 5
34:
35: ##
36: ## Logs
37: ##
38: SHORT_LOG = 4
39: MEDIUM_LOG = 6
40: LONG_LOG = 9
41: MAX_WOOD = 7
42:
43: ##
1.3 nick 44: ## Input
45: ##
46: UP = 273
47: DOWN = 274
48: RIGHT = 275
49: LEFT = 276
50:
51: ##
1.2 nick 52: ## Turtles
53: ##
54: DIVE_START_TIME = 50
55: DIVE_PHASE_TIME = 20
56: MAX_TURTLES = 9
57: TURTLE_ANIM_TIME= 5
58:
59: ##
60: ## Vehicles
61: ##
62: MAX_VEHICLES = 40
1.1 nick 63:
64: ##
65: ## Our points table
66: ##
67: SCORE_HOP = 10
68: SCORE_GOAL = 50
69: SCORE_LEVEL = 1000
70: SCORE_FLY = 150
71: SCORE_PINK = 200
72: SCORE_SECONDS = 10
73: HIGH_SCORE = 4630
74: SCORE_FREE_FROG = 2000
1.3 nick 75: LIVES = 3
76:
77: global screen
1.1 nick 78:
1.2 nick 79: ##
80: ## Some general game state stats
81: ##
1.3 nick 82: class mainGame( ):
1.2 nick 83: def __init__( self ):
1.3 nick 84: self.level = 0
1.5 ! nick 85: self.playing = FALSE
1.2 nick 86: self.goDelay = 0
87: self.score = 0
88: self.lives = 0
89: self.freefrog = 0
1.5 ! nick 90: self.drawBG = FALSE
1.2 nick 91:
1.5 ! nick 92: class images( ):
! 93: def __init__( self ):
! 94: self.title_image = 0
! 95: self.title_rect = 0
! 96: self.frogger_image = 0
! 97: self.frogger_rect = 0
! 98: self.background_image = 0
! 99: self.background_rect = 0
! 100:
! 101: class sounds( ):
! 102: def __init__( self ):
! 103: self.hop = 0
1.2 nick 104:
105: class Log( pygame.sprite.Sprite ):
106: def __init__( self ):
107: pygame.sprite.Sprite.__init__( self )
108: self.size = 0
1.5 ! nick 109: self.placement = [ 0, 0 ]
! 110: self.oPlacement = [ 0, 0 ]
1.2 nick 111: self.row = 0
112: self.speed = 0
113: self.pink = 0
114: self.gator = 0
115:
116: def update( self ):
117: self.placement += self.speed
118:
119: class Vehicles( pygame.sprite.Sprite):
120: def __init__( self ):
1.5 ! nick 121: self.placement = [ 0, 0 ]
! 122: self.oPlacement = [ 0, 0 ]
1.2 nick 123: self.direction = 0
124: self.row = 0
125: self.speed = 0
126: self.level = 0
127: self.image = 0
128:
129: def update( self ):
130: self.placement += self.speed
131:
132: def draw( self ):
133: print "D: Display Vehicle"
134:
135: class Goals( pygame.sprite.Sprite ):
136: def __init__( self ):
137: pygame.sprite.Sprite.__init__( self )
138: self.x, self.y, self.w, self.h = 0
139: self.occupied = 0
140: self.fly = 0
141: self.gator = 0
142:
1.5 ! nick 143: class pyFrog( ):
1.4 nick 144: def __init__( self ):
1.5 ! nick 145: # pygame.sprite.Sprite.__init( self )
! 146: self.pos = [0, 0]
! 147: self.oldPos = [0, 0]
! 148: self.direction = 0
! 149: self.location = 0
! 150: self.hopCount = 0
! 151: self.currentRow = 0
! 152: self.alive = 1
! 153: self.riding = 0
! 154: self.ridingType = 0
! 155: self.deathType = 0
! 156: self.deathCount = 0
1.4 nick 157:
1.2 nick 158: def main( ):
159: pygame.mixer.init( )
160: pygame.mixer.pre_init( 44100, -16, 2, 2048 )
161:
162: pygame.init( )
163:
1.5 ! nick 164: global game
1.2 nick 165: global screen
1.5 ! nick 166: global frog
! 167: global gfx
! 168: global snd
1.2 nick 169: screen = pygame.display.set_mode( ( SCREEN_WIDTH, SCREEN_HEIGHT ) )
1.5 ! nick 170: game = mainGame( )
! 171: frog = pyFrog( )
! 172: gfx = images( )
! 173: snd = sounds( )
! 174:
1.2 nick 175: pygame.display.set_caption( TITLE )
176: pygame.mouse.set_visible( 0 )
177:
178: beginGame( )
179:
180:
1.1 nick 181: def beginGame( ):
182: next_heartbeat = 0
1.5 ! nick 183: done = FALSE
1.1 nick 184:
1.4 nick 185: if loadMedia( ) <= 0:
186: print "Error: Failed to load graphics and audio!\n"
187: return
188:
1.2 nick 189: if DEBUG: print "D: Starting main game loop"
1.1 nick 190:
1.2 nick 191: clock = pygame.time.Clock( )
1.1 nick 192:
193: while 1:
194: for event in pygame.event.get( ):
1.2 nick 195: if keyEvents( event ): return
196:
197: # if pygame.GetTicks( ) >= next_heartbeat:
198: # next_hearbeat = pygame.GetTicks( ) + heartbeat( )
199:
200: clock.tick( heartbeat( ) )
201:
202: def keyEvents( event ):
203: if event.type == QUIT: return 1
204: elif event.type == KEYDOWN:
1.3 nick 205: print "Key Event Type: ", event.key
206:
1.2 nick 207: if event.key == K_ESCAPE: return 1
1.3 nick 208: elif event.key == K_p:
209: if game.level:
210: if game.playing:
1.5 ! nick 211: game.playing = FALSE
1.3 nick 212: else:
1.5 ! nick 213: game.playing = TRUE
1.2 nick 214: print "D: Pausing Game"
1.3 nick 215:
1.2 nick 216: elif event.key == K_1:
1.5 ! nick 217: if not game.level and not game.playing:
1.3 nick 218: game.level = 1
1.5 ! nick 219: game.playing = TRUE
1.3 nick 220: game.lives = LIVES
1.2 nick 221: print "D: Starting Game"
222:
1.5 ! nick 223: if game.level and game.playing and frog.alive:
! 224: if event.key == K_LEFT:
! 225: if not frog.direction:
! 226: print "D: Frogger going left"
! 227: frog.hopCount = 0
! 228: frog.direction = LEFT
! 229: #snd.hop.play()
! 230: if event.key == K_RIGHT:
! 231: if not frog.direction:
! 232: print "D: Frogger going right"
! 233: frog.hopCount = 0
! 234: frog.direction = RIGHT
! 235: #snd.hop.play()
! 236: #playSound( frogger.s_hop )
! 237: if event.key == K_UP:
! 238: if not frog.direction:
! 239: print "D: Frogger going up"
! 240: frog.hopCount = 0
! 241: frog.direction = UP
! 242: frog.currentRow += 1
! 243: #playSound( frogger.s_hop )
! 244: if event.key == K_DOWN:
! 245: if not frog.direction:
! 246: print "D: Frogger going down"
! 247: frog.hopCount = 0
! 248: frog.direction = DOWN
! 249: frog.direction -= 1
! 250: #playSound( frogger.s_hop )
1.2 nick 251: return 0
252:
253: def updateGameState( ):
1.3 nick 254: if game.lives <= 0:
255: game.goDelay += 1
1.2 nick 256: drawGameOver( )
257: if goDelay > 7:
1.5 ! nick 258: game.playing = FALSE
1.3 nick 259: game.lives = 0
260: game.level = 0
261: game.score = 0
262: game.freefrog = 0
1.5 ! nick 263: game.drawBG = FALSE
1.2 nick 264: # for i = 0; i < MAX_GOALS; i++:
265: # goals[i].occupied = 0
266: return 500
267:
268: drawGameScreen( )
269:
1.5 ! nick 270: return 50
1.2 nick 271:
272: def drawGameScreen( ):
1.5 ! nick 273: if frog.direction: moveFrog( )
! 274:
! 275: screen.blit( gfx.background_image, ( 0, 0 ) )
! 276:
! 277: pygame.display.flip( )
! 278:
! 279: def moveFrog( ):
! 280: frog.hopCount += 1
! 281: if frog.hopCount > 60:
! 282: print "D: Try Again"
! 283: frog.direction = FALSE
1.1 nick 284:
1.2 nick 285: def heartbeat( ):
286: ticks = 0;
1.3 nick 287: if game.level:
288: if game.playing:
1.2 nick 289: ticks = updateGameState( )
1.5 ! nick 290: if ticks <= 0: ticks = 50
1.2 nick 291: return ticks
292: else:
293: drawPauseScreen( )
294: return 500
295: else:
296: drawTitleScreen( )
297: return 500
298:
299: def drawGameOver( ):
300: print "D: Game Over man! Game Over!"
301:
302: def drawPauseScreen( ):
303: print "D: Game Paused"
304:
305: def drawTitleScreen( ):
1.5 ! nick 306: screen.blit( gfx.background_image, ( 0, 0 ) )
! 307: screen.blit( gfx.title_image, ( 35, 92 ) )
1.2 nick 308: pygame.display.flip( )
1.5 ! nick 309:
1.4 nick 310: def loadMedia( ):
311: print "D: Loading media"
1.5 ! nick 312: gfx.background_image, gfx.background_rect = loadImage( 'gameboard.png' )
! 313: gfx.frogger_image, gfx.frogger_rect = loadImage( 'frogger.png' )
! 314: gfx.title_image, gfx.title_rect = loadImage( 'pyfrog-title.png', -1 )
! 315:
! 316: snd.hop = loadSound( 'dp_frogger_hop.ogg' )
! 317:
1.4 nick 318: return 1
319:
1.2 nick 320: def loadImage( filename, colorKey = None ):
321: fullname = os.path.join( 'images', filename )
322:
323: try:
324: image = pygame.image.load( fullname )
325: except pygame.error, message:
326: print "ERROR: Failed to load image: " + fullname
327: raise SystemExit, message
1.1 nick 328:
1.2 nick 329: image = image.convert( )
330:
331: if colorKey is not None:
332: if colorKey is -1:
333: colorKey = image.get_at( ( 0, 0 ) )
334: image.set_colorkey( colorKey, RLEACCEL )
335:
336: return image, image.get_rect( )
337:
338: def loadSound( name ):
339: class noSound:
340: def play( self ): pass
341:
342: if not pygame.mixer or not pygame.mixer.get_init( ):
343: return noSound( )
344:
345: fullname = os.path.join( 'sounds', name )
346: if os.path.exists( fullname ) == False:
347: sound = pygame.mixer.Sound( fullname )
348: else:
349: print "ERROR: Audio file missing: " + fullname
350: return noSound
351:
352: return sound
1.5 ! nick 353:
! 354: ################################################################################
! 355: ## Begin Here
! 356: ################################################################################
! 357:
1.2 nick 358: if __name__ == '__main__': main()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>