Annotation of pyfrog/pyfrog.py, revision 1.2
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.1 nick 12: VERSION = "$Id$"
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: ##
! 42: ## Turtles
! 43: ##
! 44: DIVE_START_TIME = 50
! 45: DIVE_PHASE_TIME = 20
! 46: MAX_TURTLES = 9
! 47: TURTLE_ANIM_TIME= 5
! 48:
! 49: ##
! 50: ## Vehicles
! 51: ##
! 52: MAX_VEHICLES = 40
1.1 nick 53:
54: ##
55: ## Our points table
56: ##
57: SCORE_HOP = 10
58: SCORE_GOAL = 50
59: SCORE_LEVEL = 1000
60: SCORE_FLY = 150
61: SCORE_PINK = 200
62: SCORE_SECONDS = 10
63: HIGH_SCORE = 4630
64: SCORE_FREE_FROG = 2000
65:
1.2 ! nick 66: ##
! 67: ## Some general game state stats
! 68: ##
! 69: level = 0
! 70: playing = 0
! 71: goDelay = 0
! 72: score = 0
! 73: lives = 0
! 74: freefrog = 0
! 75: drawBG = 1
! 76: global screen
! 77:
! 78: class gameStats( ):
! 79: def __init__( self ):
! 80: self.level = 0
! 81: self.playing = 0
! 82: self.goDelay = 0
! 83: self.score = 0
! 84: self.lives = 0
! 85: self.freefrog = 0
! 86: self.drawBG = 0
! 87:
! 88:
! 89: class Log( pygame.sprite.Sprite ):
! 90: def __init__( self ):
! 91: pygame.sprite.Sprite.__init__( self )
! 92: self.size = 0
! 93: self.placement = ( 0, 0 )
! 94: self.oPlacement = ( 0, 0 )
! 95: self.row = 0
! 96: self.speed = 0
! 97: self.pink = 0
! 98: self.gator = 0
! 99:
! 100: def update( self ):
! 101: self.placement += self.speed
! 102:
! 103: class Vehicles( pygame.sprite.Sprite):
! 104: def __init__( self ):
! 105: self.placement = ( 0, 0 )
! 106: self.oPlacement = ( 0, 0 )
! 107: self.direction = 0
! 108: self.row = 0
! 109: self.speed = 0
! 110: self.level = 0
! 111: self.image = 0
! 112:
! 113: def update( self ):
! 114: self.placement += self.speed
! 115:
! 116: def draw( self ):
! 117: print "D: Display Vehicle"
! 118:
! 119: class Goals( pygame.sprite.Sprite ):
! 120: def __init__( self ):
! 121: pygame.sprite.Sprite.__init__( self )
! 122: self.x, self.y, self.w, self.h = 0
! 123: self.occupied = 0
! 124: self.fly = 0
! 125: self.gator = 0
! 126:
! 127: def main( ):
! 128: pygame.mixer.init( )
! 129: pygame.mixer.pre_init( 44100, -16, 2, 2048 )
! 130:
! 131: pygame.init( )
! 132:
! 133: global screen
! 134: screen = pygame.display.set_mode( ( SCREEN_WIDTH, SCREEN_HEIGHT ) )
! 135: pygame.display.set_caption( TITLE )
! 136: pygame.mouse.set_visible( 0 )
! 137:
! 138: beginGame( )
! 139:
! 140:
1.1 nick 141: def beginGame( ):
142: next_heartbeat = 0
143: done = 0
144:
1.2 ! nick 145: drawBackground( )
! 146:
! 147: if DEBUG: print "D: Starting main game loop"
1.1 nick 148:
1.2 ! nick 149: clock = pygame.time.Clock( )
1.1 nick 150:
151: while 1:
152: for event in pygame.event.get( ):
1.2 ! nick 153: if keyEvents( event ): return
! 154:
! 155: # if pygame.GetTicks( ) >= next_heartbeat:
! 156: # next_hearbeat = pygame.GetTicks( ) + heartbeat( )
! 157:
! 158: clock.tick( heartbeat( ) )
! 159:
! 160: def keyEvents( event ):
! 161: if event.type == QUIT: return 1
! 162: elif event.type == KEYDOWN:
! 163: if event.key == K_ESCAPE: return 1
! 164: elif event.key == K_P:
! 165: if level:
! 166: print "D: Pausing Game"
! 167: elif event.key == K_1:
! 168: if not level:
! 169: print "D: Starting Game"
! 170:
! 171: return 0
! 172:
! 173: def updateGameState( ):
! 174: if lives <= 0:
! 175: goDelay += 1
! 176: drawGameOver( )
! 177: if goDelay > 7:
! 178: playing = 0
! 179: lives = 0
! 180: level = 0
! 181: score = 0
! 182: freefrog = 0
! 183: drawBG = 0
! 184: # for i = 0; i < MAX_GOALS; i++:
! 185: # goals[i].occupied = 0
! 186: return 500
! 187:
! 188: drawGameScreen( )
! 189:
! 190: return 30
! 191:
! 192: def drawGameScreen( ):
! 193: print "D: Drawing main game screen"
1.1 nick 194:
1.2 ! nick 195: def heartbeat( ):
! 196: ticks = 0;
! 197: if level:
! 198: if playing:
! 199: ticks = updateGameState( )
! 200: if ticks <= 0: ticks = 30
! 201: return ticks
! 202: else:
! 203: drawPauseScreen( )
! 204: return 500
! 205: else:
! 206: drawTitleScreen( )
! 207: return 500
! 208:
! 209: def drawGameOver( ):
! 210: print "D: Game Over man! Game Over!"
! 211:
! 212: def drawPauseScreen( ):
! 213: print "D: Game Paused"
! 214:
! 215: def drawTitleScreen( ):
! 216: drawBackground( )
! 217:
! 218: def drawBackground( ):
! 219: background_image, background_rect = loadImage( 'gameboard.png' )
! 220: screen.blit( background_image, ( 0, 0 ) )
! 221: pygame.display.flip( )
! 222:
! 223: def loadImage( filename, colorKey = None ):
! 224: fullname = os.path.join( 'images', filename )
! 225:
! 226: try:
! 227: image = pygame.image.load( fullname )
! 228: except pygame.error, message:
! 229: print "ERROR: Failed to load image: " + fullname
! 230: raise SystemExit, message
1.1 nick 231:
1.2 ! nick 232: image = image.convert( )
! 233:
! 234: if colorKey is not None:
! 235: if colorKey is -1:
! 236: colorKey = image.get_at( ( 0, 0 ) )
! 237: image.set_colorkey( colorKey, RLEACCEL )
! 238:
! 239: return image, image.get_rect( )
! 240:
! 241: def loadSound( name ):
! 242: class noSound:
! 243: def play( self ): pass
! 244:
! 245: if not pygame.mixer or not pygame.mixer.get_init( ):
! 246: return noSound( )
! 247:
! 248: fullname = os.path.join( 'sounds', name )
! 249: if os.path.exists( fullname ) == False:
! 250: sound = pygame.mixer.Sound( fullname )
! 251: else:
! 252: print "ERROR: Audio file missing: " + fullname
! 253: return noSound
! 254:
! 255: return sound
! 256:
! 257: if __name__ == '__main__': main()
! 258: #init( )
! 259: #beginGame( )
! 260: #sys.quit( )
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>