<s> from itertools import cycle <EOL> import random <EOL> import sys <EOL> import pygame <EOL> from pygame . locals import * <EOL> FPS = <NUM_LIT:30> <EOL> SCREENWIDTH = <NUM_LIT> <EOL> SCREENHEIGHT = <NUM_LIT> <EOL> PIPEGAPSIZE = <NUM_LIT:100> <EOL> BASEY = SCREENHEIGHT * <NUM_LIT> <EOL> IMAGES , SOUNDS , HITMASKS = { } , { } , { } <EOL> PLAYERS_LIST = ( <EOL> ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> ) <EOL> BACKGROUNDS_LIST = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> PIPES_LIST = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def main ( ) : <EOL> global SCREEN , FPSCLOCK <EOL> pygame . init ( ) <EOL> FPSCLOCK = pygame . time . Clock ( ) <EOL> SCREEN = pygame . display . set_mode ( ( SCREENWIDTH , SCREENHEIGHT ) ) <EOL> pygame . display . set_caption ( '<STR_LIT>' ) <EOL> IMAGES [ '<STR_LIT>' ] = ( <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) , <EOL> pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) <EOL> ) <EOL> IMAGES [ '<STR_LIT>' ] = pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) <EOL> IMAGES [ '<STR_LIT:message>' ] = pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) <EOL> IMAGES [ '<STR_LIT>' ] = pygame . image . load ( '<STR_LIT>' ) . convert_alpha ( ) <EOL> if '<STR_LIT>' in sys . platform : <EOL> soundExt = '<STR_LIT>' <EOL> else : <EOL> soundExt = '<STR_LIT>' <EOL> SOUNDS [ '<STR_LIT>' ] = pygame . mixer . Sound ( '<STR_LIT>' + soundExt ) <EOL> SOUNDS [ '<STR_LIT>' ] = pygame . mixer . Sound ( '<STR_LIT>' + soundExt ) <EOL> SOUNDS [ '<STR_LIT>' ] = pygame . mixer . Sound ( '<STR_LIT>' + soundExt ) <EOL> SOUNDS [ '<STR_LIT>' ] = pygame . mixer . Sound ( '<STR_LIT>' + soundExt ) <EOL> SOUNDS [ '<STR_LIT>' ] = pygame . mixer . Sound ( '<STR_LIT>' + soundExt ) <EOL> while True : <EOL> randBg = random . randint ( <NUM_LIT:0> , len ( BACKGROUNDS_LIST ) - <NUM_LIT:1> ) <EOL> IMAGES [ '<STR_LIT>' ] = pygame . image . load ( BACKGROUNDS_LIST [ randBg ] ) . convert ( ) <EOL> randPlayer = random . randint ( <NUM_LIT:0> , len ( PLAYERS_LIST ) - <NUM_LIT:1> ) <EOL> IMAGES [ '<STR_LIT>' ] = ( <EOL> pygame . image . load ( PLAYERS_LIST [ randPlayer ] [ <NUM_LIT:0> ] ) . convert_alpha ( ) , <EOL> pygame . image . load ( PLAYERS_LIST [ randPlayer ] [ <NUM_LIT:1> ] ) . convert_alpha ( ) , <EOL> pygame . image . load ( PLAYERS_LIST [ randPlayer ] [ <NUM_LIT:2> ] ) . convert_alpha ( ) , <EOL> ) <EOL> pipeindex = random . randint ( <NUM_LIT:0> , len ( PIPES_LIST ) - <NUM_LIT:1> ) <EOL> IMAGES [ '<STR_LIT>' ] = ( <EOL> pygame . transform . rotate ( <EOL> pygame . image . load ( PIPES_LIST [ pipeindex ] ) . convert_alpha ( ) , <NUM_LIT> ) , <EOL> pygame . image . load ( PIPES_LIST [ pipeindex ] ) . convert_alpha ( ) , <EOL> ) <EOL> HITMASKS [ '<STR_LIT>' ] = ( <EOL> getHitmask ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) , <EOL> getHitmask ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) , <EOL> ) <EOL> HITMASKS [ '<STR_LIT>' ] = ( <EOL> getHitmask ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) , <EOL> getHitmask ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) , <EOL> getHitmask ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:2> ] ) , <EOL> ) <EOL> movementInfo = showWelcomeAnimation ( ) <EOL> crashInfo = mainGame ( movementInfo ) <EOL> showGameOverScreen ( crashInfo ) <EOL> def showWelcomeAnimation ( ) : <EOL> """<STR_LIT>""" <EOL> playerIndex = <NUM_LIT:0> <EOL> playerIndexGen = cycle ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> loopIter = <NUM_LIT:0> <EOL> playerx = int ( SCREENWIDTH * <NUM_LIT> ) <EOL> playery = int ( ( SCREENHEIGHT - IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_height ( ) ) / <NUM_LIT:2> ) <EOL> messagex = int ( ( SCREENWIDTH - IMAGES [ '<STR_LIT:message>' ] . get_width ( ) ) / <NUM_LIT:2> ) <EOL> messagey = int ( SCREENHEIGHT * <NUM_LIT> ) <EOL> basex = <NUM_LIT:0> <EOL> baseShift = IMAGES [ '<STR_LIT>' ] . get_width ( ) - IMAGES [ '<STR_LIT>' ] . get_width ( ) <EOL> playerShmVals = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> while True : <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == QUIT or ( event . type == KEYDOWN and event . key == K_ESCAPE ) : <EOL> pygame . quit ( ) <EOL> sys . exit ( ) <EOL> if event . type == KEYDOWN and ( event . key == K_SPACE or event . key == K_UP ) : <EOL> SOUNDS [ '<STR_LIT>' ] . play ( ) <EOL> return { <EOL> '<STR_LIT>' : playery + playerShmVals [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : basex , <EOL> '<STR_LIT>' : playerIndexGen , <EOL> } <EOL> if ( loopIter + <NUM_LIT:1> ) % <NUM_LIT:5> == <NUM_LIT:0> : <EOL> playerIndex = playerIndexGen . next ( ) <EOL> loopIter = ( loopIter + <NUM_LIT:1> ) % <NUM_LIT:30> <EOL> basex = - ( ( - basex + <NUM_LIT:4> ) % baseShift ) <EOL> playerShm ( playerShmVals ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ playerIndex ] , <EOL> ( playerx , playery + playerShmVals [ '<STR_LIT>' ] ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT:message>' ] , ( messagex , messagey ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] , ( basex , BASEY ) ) <EOL> pygame . display . update ( ) <EOL> FPSCLOCK . tick ( FPS ) <EOL> def mainGame ( movementInfo ) : <EOL> score = playerIndex = loopIter = <NUM_LIT:0> <EOL> playerIndexGen = movementInfo [ '<STR_LIT>' ] <EOL> playerx , playery = int ( SCREENWIDTH * <NUM_LIT> ) , movementInfo [ '<STR_LIT>' ] <EOL> basex = movementInfo [ '<STR_LIT>' ] <EOL> baseShift = IMAGES [ '<STR_LIT>' ] . get_width ( ) - IMAGES [ '<STR_LIT>' ] . get_width ( ) <EOL> newPipe1 = getRandomPipe ( ) <EOL> newPipe2 = getRandomPipe ( ) <EOL> upperPipes = [ <EOL> { '<STR_LIT:x>' : SCREENWIDTH + <NUM_LIT:200> , '<STR_LIT:y>' : newPipe1 [ <NUM_LIT:0> ] [ '<STR_LIT:y>' ] } , <EOL> { '<STR_LIT:x>' : SCREENWIDTH + <NUM_LIT:200> + ( SCREENWIDTH / <NUM_LIT:2> ) , '<STR_LIT:y>' : newPipe2 [ <NUM_LIT:0> ] [ '<STR_LIT:y>' ] } , <EOL> ] <EOL> lowerPipes = [ <EOL> { '<STR_LIT:x>' : SCREENWIDTH + <NUM_LIT:200> , '<STR_LIT:y>' : newPipe1 [ <NUM_LIT:1> ] [ '<STR_LIT:y>' ] } , <EOL> { '<STR_LIT:x>' : SCREENWIDTH + <NUM_LIT:200> + ( SCREENWIDTH / <NUM_LIT:2> ) , '<STR_LIT:y>' : newPipe2 [ <NUM_LIT:1> ] [ '<STR_LIT:y>' ] } , <EOL> ] <EOL> pipeVelX = - <NUM_LIT:4> <EOL> playerVelY = - <NUM_LIT:9> <EOL> playerMaxVelY = <NUM_LIT:10> <EOL> playerMinVelY = - <NUM_LIT:8> <EOL> playerAccY = <NUM_LIT:1> <EOL> playerFlapAcc = - <NUM_LIT:9> <EOL> playerFlapped = False <EOL> while True : <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == QUIT or ( event . type == KEYDOWN and event . key == K_ESCAPE ) : <EOL> pygame . quit ( ) <EOL> sys . exit ( ) <EOL> if event . type == KEYDOWN and ( event . key == K_SPACE or event . key == K_UP ) : <EOL> if playery > - <NUM_LIT:2> * IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_height ( ) : <EOL> playerVelY = playerFlapAcc <EOL> playerFlapped = True <EOL> SOUNDS [ '<STR_LIT>' ] . play ( ) <EOL> crashTest = checkCrash ( { '<STR_LIT:x>' : playerx , '<STR_LIT:y>' : playery , '<STR_LIT:index>' : playerIndex } , <EOL> upperPipes , lowerPipes ) <EOL> if crashTest [ <NUM_LIT:0> ] : <EOL> return { <EOL> '<STR_LIT:y>' : playery , <EOL> '<STR_LIT>' : crashTest [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : basex , <EOL> '<STR_LIT>' : upperPipes , <EOL> '<STR_LIT>' : lowerPipes , <EOL> '<STR_LIT>' : score , <EOL> '<STR_LIT>' : playerVelY , <EOL> } <EOL> playerMidPos = playerx + IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_width ( ) / <NUM_LIT:2> <EOL> for pipe in upperPipes : <EOL> pipeMidPos = pipe [ '<STR_LIT:x>' ] + IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_width ( ) / <NUM_LIT:2> <EOL> if pipeMidPos <= playerMidPos < pipeMidPos + <NUM_LIT:4> : <EOL> score += <NUM_LIT:1> <EOL> SOUNDS [ '<STR_LIT>' ] . play ( ) <EOL> if ( loopIter + <NUM_LIT:1> ) % <NUM_LIT:3> == <NUM_LIT:0> : <EOL> playerIndex = playerIndexGen . next ( ) <EOL> loopIter = ( loopIter + <NUM_LIT:1> ) % <NUM_LIT:30> <EOL> basex = - ( ( - basex + <NUM_LIT:100> ) % baseShift ) <EOL> if playerVelY < playerMaxVelY and not playerFlapped : <EOL> playerVelY += playerAccY <EOL> if playerFlapped : <EOL> playerFlapped = False <EOL> playerHeight = IMAGES [ '<STR_LIT>' ] [ playerIndex ] . get_height ( ) <EOL> playery += min ( playerVelY , BASEY - playery - playerHeight ) <EOL> for uPipe , lPipe in zip ( upperPipes , lowerPipes ) : <EOL> uPipe [ '<STR_LIT:x>' ] += pipeVelX <EOL> lPipe [ '<STR_LIT:x>' ] += pipeVelX <EOL> if <NUM_LIT:0> < upperPipes [ <NUM_LIT:0> ] [ '<STR_LIT:x>' ] < <NUM_LIT:5> : <EOL> newPipe = getRandomPipe ( ) <EOL> upperPipes . append ( newPipe [ <NUM_LIT:0> ] ) <EOL> lowerPipes . append ( newPipe [ <NUM_LIT:1> ] ) <EOL> if upperPipes [ <NUM_LIT:0> ] [ '<STR_LIT:x>' ] < - IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_width ( ) : <EOL> upperPipes . pop ( <NUM_LIT:0> ) <EOL> lowerPipes . pop ( <NUM_LIT:0> ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> for uPipe , lPipe in zip ( upperPipes , lowerPipes ) : <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , ( uPipe [ '<STR_LIT:x>' ] , uPipe [ '<STR_LIT:y>' ] ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , ( lPipe [ '<STR_LIT:x>' ] , lPipe [ '<STR_LIT:y>' ] ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] , ( basex , BASEY ) ) <EOL> showScore ( score ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ playerIndex ] , ( playerx , playery ) ) <EOL> pygame . display . update ( ) <EOL> FPSCLOCK . tick ( FPS ) <EOL> def showGameOverScreen ( crashInfo ) : <EOL> """<STR_LIT>""" <EOL> score = crashInfo [ '<STR_LIT>' ] <EOL> playerx = SCREENWIDTH * <NUM_LIT> <EOL> playery = crashInfo [ '<STR_LIT:y>' ] <EOL> playerHeight = IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_height ( ) <EOL> playerVelY = crashInfo [ '<STR_LIT>' ] <EOL> playerAccY = <NUM_LIT:2> <EOL> basex = crashInfo [ '<STR_LIT>' ] <EOL> upperPipes , lowerPipes = crashInfo [ '<STR_LIT>' ] , crashInfo [ '<STR_LIT>' ] <EOL> SOUNDS [ '<STR_LIT>' ] . play ( ) <EOL> if not crashInfo [ '<STR_LIT>' ] : <EOL> SOUNDS [ '<STR_LIT>' ] . play ( ) <EOL> while True : <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == QUIT or ( event . type == KEYDOWN and event . key == K_ESCAPE ) : <EOL> pygame . quit ( ) <EOL> sys . exit ( ) <EOL> if event . type == KEYDOWN and ( event . key == K_SPACE or event . key == K_UP ) : <EOL> if playery + playerHeight >= BASEY - <NUM_LIT:1> : <EOL> return <EOL> if playery + playerHeight < BASEY - <NUM_LIT:1> : <EOL> playery += min ( playerVelY , BASEY - playery - playerHeight ) <EOL> if playerVelY < <NUM_LIT:15> : <EOL> playerVelY += playerAccY <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> for uPipe , lPipe in zip ( upperPipes , lowerPipes ) : <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , ( uPipe [ '<STR_LIT:x>' ] , uPipe [ '<STR_LIT:y>' ] ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , ( lPipe [ '<STR_LIT:x>' ] , lPipe [ '<STR_LIT:y>' ] ) ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] , ( basex , BASEY ) ) <EOL> showScore ( score ) <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , ( playerx , playery ) ) <EOL> FPSCLOCK . tick ( FPS ) <EOL> pygame . display . update ( ) <EOL> def playerShm ( playerShm ) : <EOL> """<STR_LIT>""" <EOL> if abs ( playerShm [ '<STR_LIT>' ] ) == <NUM_LIT:8> : <EOL> playerShm [ '<STR_LIT>' ] *= - <NUM_LIT:1> <EOL> if playerShm [ '<STR_LIT>' ] == <NUM_LIT:1> : <EOL> playerShm [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> else : <EOL> playerShm [ '<STR_LIT>' ] -= <NUM_LIT:1> <EOL> def getRandomPipe ( ) : <EOL> """<STR_LIT>""" <EOL> gapY = random . randrange ( <NUM_LIT:0> , int ( BASEY * <NUM_LIT> - PIPEGAPSIZE ) ) <EOL> gapY += int ( BASEY * <NUM_LIT> ) <EOL> pipeHeight = IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_height ( ) <EOL> pipeX = SCREENWIDTH + <NUM_LIT:10> <EOL> return [ <EOL> { '<STR_LIT:x>' : pipeX , '<STR_LIT:y>' : gapY - pipeHeight } , <EOL> { '<STR_LIT:x>' : pipeX , '<STR_LIT:y>' : gapY + PIPEGAPSIZE } , <EOL> ] <EOL> def showScore ( score ) : <EOL> """<STR_LIT>""" <EOL> scoreDigits = [ int ( x ) for x in list ( str ( score ) ) ] <EOL> totalWidth = <NUM_LIT:0> <EOL> for digit in scoreDigits : <EOL> totalWidth += IMAGES [ '<STR_LIT>' ] [ digit ] . get_width ( ) <EOL> Xoffset = ( SCREENWIDTH - totalWidth ) / <NUM_LIT:2> <EOL> for digit in scoreDigits : <EOL> SCREEN . blit ( IMAGES [ '<STR_LIT>' ] [ digit ] , ( Xoffset , SCREENHEIGHT * <NUM_LIT:0.1> ) ) <EOL> Xoffset += IMAGES [ '<STR_LIT>' ] [ digit ] . get_width ( ) <EOL> def checkCrash ( player , upperPipes , lowerPipes ) : <EOL> """<STR_LIT>""" <EOL> pi = player [ '<STR_LIT:index>' ] <EOL> player [ '<STR_LIT:w>' ] = IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_width ( ) <EOL> player [ '<STR_LIT:h>' ] = IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_height ( ) <EOL> if player [ '<STR_LIT:y>' ] + player [ '<STR_LIT:h>' ] >= BASEY - <NUM_LIT:1> : <EOL> return [ True , True ] <EOL> else : <EOL> playerRect = pygame . Rect ( player [ '<STR_LIT:x>' ] , player [ '<STR_LIT:y>' ] , <EOL> player [ '<STR_LIT:w>' ] , player [ '<STR_LIT:h>' ] ) <EOL> pipeW = IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_width ( ) <EOL> pipeH = IMAGES [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . get_height ( ) <EOL> for uPipe , lPipe in zip ( upperPipes , lowerPipes ) : <EOL> uPipeRect = pygame . Rect ( uPipe [ '<STR_LIT:x>' ] , uPipe [ '<STR_LIT:y>' ] , pipeW , pipeH ) <EOL> lPipeRect = pygame . Rect ( lPipe [ '<STR_LIT:x>' ] , lPipe [ '<STR_LIT:y>' ] , pipeW , pipeH ) <EOL> pHitMask = HITMASKS [ '<STR_LIT>' ] [ pi ] <EOL> uHitmask = HITMASKS [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> lHitmask = HITMASKS [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> uCollide = pixelCollision ( playerRect , uPipeRect , pHitMask , uHitmask ) <EOL> lCollide = pixelCollision ( playerRect , lPipeRect , pHitMask , lHitmask ) <EOL> if uCollide or lCollide : <EOL> return [ True , False ] <EOL> return [ False , False ] <EOL> def pixelCollision ( rect1 , rect2 , hitmask1 , hitmask2 ) : <EOL> """<STR_LIT>""" <EOL> rect = rect1 . clip ( rect2 ) <EOL> if rect . width == <NUM_LIT:0> or rect . height == <NUM_LIT:0> : <EOL> return False <EOL> x1 , y1 = rect . x - rect1 . x , rect . y - rect1 . y <EOL> x2 , y2 = rect . x - rect2 . x , rect . y - rect2 . y <EOL> for x in xrange ( rect . width ) : <EOL> for y in xrange ( rect . height ) : <EOL> if hitmask1 [ x1 + x ] [ y1 + y ] and hitmask2 [ x2 + x ] [ y2 + y ] : <EOL> return True <EOL> return False <EOL> def getHitmask ( image ) : <EOL> """<STR_LIT>""" <EOL> mask = [ ] <EOL> for x in range ( image . get_width ( ) ) : <EOL> mask . append ( [ ] ) <EOL> for y in range ( image . get_height ( ) ) : <EOL> mask [ x ] . append ( bool ( image . get_at ( ( x , y ) ) [ <NUM_LIT:3> ] ) ) <EOL> return mask <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
