Annotation of pyfrog/pyfrog.py, revision 1.3
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.3 ! nick 12: VERSION = "$Id: pyfrog.py,v 1.2 2011-06-16 23:29:25 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:
129: def main( ):
130: pygame.mixer.init( )
131: pygame.mixer.pre_init( 44100, -16, 2, 2048 )
132:
133: pygame.init( )
134:
135: global screen
136: screen = pygame.display.set_mode( ( SCREEN_WIDTH, SCREEN_HEIGHT ) )
137: pygame.display.set_caption( TITLE )
138: pygame.mouse.set_visible( 0 )
139:
140: beginGame( )
141:
142:
1.1 nick 143: def beginGame( ):
144: next_heartbeat = 0
145: done = 0
146:
1.2 nick 147: drawBackground( )
148:
149: if DEBUG: print "D: Starting main game loop"
1.1 nick 150:
1.2 nick 151: clock = pygame.time.Clock( )
1.1 nick 152:
153: while 1:
154: for event in pygame.event.get( ):
1.2 nick 155: if keyEvents( event ): return
156:
157: # if pygame.GetTicks( ) >= next_heartbeat:
158: # next_hearbeat = pygame.GetTicks( ) + heartbeat( )
159:
160: clock.tick( heartbeat( ) )
161:
162: def keyEvents( event ):
163: if event.type == QUIT: return 1
164: elif event.type == KEYDOWN:
1.3 ! nick 165: print "Key Event Type: ", event.key
! 166:
1.2 nick 167: if event.key == K_ESCAPE: return 1
1.3 ! nick 168: elif event.key == K_p:
! 169: if game.level:
! 170: if game.playing:
! 171: game.playing = 0
! 172: else:
! 173: game.playing = 1
1.2 nick 174: print "D: Pausing Game"
1.3 ! nick 175:
1.2 nick 176: elif event.key == K_1:
1.3 ! nick 177: if not game.level:
! 178: game.level = 1
! 179: game.playing = 1
! 180: game.lives = LIVES
1.2 nick 181: print "D: Starting Game"
182:
183: return 0
184:
185: def updateGameState( ):
1.3 ! nick 186: if game.lives <= 0:
! 187: game.goDelay += 1
1.2 nick 188: drawGameOver( )
189: if goDelay > 7:
1.3 ! nick 190: game.playing = 0
! 191: game.lives = 0
! 192: game.level = 0
! 193: game.score = 0
! 194: game.freefrog = 0
! 195: game.drawBG = 0
1.2 nick 196: # for i = 0; i < MAX_GOALS; i++:
197: # goals[i].occupied = 0
198: return 500
199:
200: drawGameScreen( )
201:
202: return 30
203:
204: def drawGameScreen( ):
205: print "D: Drawing main game screen"
1.1 nick 206:
1.2 nick 207: def heartbeat( ):
208: ticks = 0;
1.3 ! nick 209: if game.level:
! 210: if game.playing:
1.2 nick 211: ticks = updateGameState( )
212: if ticks <= 0: ticks = 30
213: return ticks
214: else:
215: drawPauseScreen( )
216: return 500
217: else:
218: drawTitleScreen( )
219: return 500
220:
221: def drawGameOver( ):
222: print "D: Game Over man! Game Over!"
223:
224: def drawPauseScreen( ):
225: print "D: Game Paused"
226:
227: def drawTitleScreen( ):
228: drawBackground( )
229:
230: def drawBackground( ):
1.3 ! nick 231: background_image, background_rect = loadImage( 'tempgameboard.png' )
1.2 nick 232: screen.blit( background_image, ( 0, 0 ) )
233: pygame.display.flip( )
234:
235: def loadImage( filename, colorKey = None ):
236: fullname = os.path.join( 'images', filename )
237:
238: try:
239: image = pygame.image.load( fullname )
240: except pygame.error, message:
241: print "ERROR: Failed to load image: " + fullname
242: raise SystemExit, message
1.1 nick 243:
1.2 nick 244: image = image.convert( )
245:
246: if colorKey is not None:
247: if colorKey is -1:
248: colorKey = image.get_at( ( 0, 0 ) )
249: image.set_colorkey( colorKey, RLEACCEL )
250:
251: return image, image.get_rect( )
252:
253: def loadSound( name ):
254: class noSound:
255: def play( self ): pass
256:
257: if not pygame.mixer or not pygame.mixer.get_init( ):
258: return noSound( )
259:
260: fullname = os.path.join( 'sounds', name )
261: if os.path.exists( fullname ) == False:
262: sound = pygame.mixer.Sound( fullname )
263: else:
264: print "ERROR: Audio file missing: " + fullname
265: return noSound
266:
267: return sound
1.3 ! nick 268: game = mainGame( )
1.2 nick 269: if __name__ == '__main__': main()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>