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