-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappy bird.py
200 lines (163 loc) · 6.63 KB
/
Flappy bird.py
1
#import moduleimport randomimport sysimport pygamefrom pygame.locals import *#global variable for the gameFPS = 32SCREENWIDTH=289SCREENHEIGHT=511SCREEN = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT))GROUNDY=SCREENHEIGHT * 0.8GAME_SPRITES={}GAME_SOUNDS={}PLAYER = 'gallery/sprites/bird.png'BACKGROUND='gallery/sprites/pipe.png'#function for welcome screendef welcomeScreen(): Playerx=int(SCREENWIDTH/5) Playery=int((SCREENHEIGHT-GAME_SPRITES['Player'].get_height())/2) messagex=int((SCREENWIDTH - GAME_SPRITES['message'].get_widht())/2) messagey=int(SCREENHEIGHT * 0.13) basex = 0 while True: for event in pygame.event.get(): #if user clicks on cross button close the game if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() #if user presses upkey or space key ,starts the game elif event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP): return else: SCREEN.blit(GAME_SPRITES['background'],(0,0)) SCREEN.blit(GAME_SPRITES['Player'],(Playerx,Playery)) SCREEN.blit(GAME_SPRITES['message'],(messagex,messagey)) SCREEN.blit(GAME_SPRITES['base'],(basex,GROUNDY)) pygame.display.update() FPSCLOCK.tick(FPS)#creating the gamedef mainGame(): score = 0 Playerx=int(SCREENWIDTH/5) Playery=int(SCREENWIDTH/2) basex=0 #create 2 pipe for blitting on the screen newPipe1 = getRandomPipe() newPipe2 = getRandomPipe() #my list of upper pipes UpperPipes=[ {'x':SCREENWIDTH + 200,'y':newPipe1[0]['y']}, {'x':SCREENWIDTH + 200 + (SCREENWIDTH/2),'y':newPipe2[0]['y']}, ] #my list of lower pipes LowerPipes=[ {'x':SCREENWIDTH + 200, 'y':newPipe1[1]['y']}, {'x':SCREENWIDTH + 200 +(SCREENWIDTH/2),'y':newPipe2[1]['y']} ] pipeVelX = -4 PlayerVely=-9 PlayerMaxVely = 10 PlayerminVely = -8 PlayerAccY=1 PlayerFlapAccv=-8 PlayerFlapped=False while True: for event in pygame.event.get(): if event.type == QUIT or(event.type==KEYDOWN and event.key==K_ESCAPE): pygame.quit() sys.exit() if event.type == KEYDOWN and (event.key == K_SPACE or event.key==K_UP): if Player > 0: PlayerVely=PlayerFlapAccv PlayerFlapped=True GAME_SOUNDS['wing'].play() CrashTest = isCollide(Playerx,Playery,UpperPipes,LowerPipes) if CrashTest: return PlayerMidPos=Playerx+GAME_SPRITES['Player'].get_Width()/2 for pipe in UpperPipes: pipeMidPos=pipe['x']+GAME_SPRITES['pipe'][0].get_width()/2 if pipeMidPos<= PlayerMidPos<pipeMidPos +4: score +=1 print(f"Your score is {score}") GAME_SOUNDS['point'].play() if playerVelY < playerMaxVelY and not playerFlapped: playerVelY += playerAccY if playerFlapped: playerFlapped = False playerHeight = GAME_SPRITES['player'].get_height() playery = playery + min(playerVelY, GROUNDY - playery - playerHeight)# move pipes to the left for upperPipe, lowerPipe in zip(upperPipes, lowerPipes): upperPipe['x'] += pipeVelX lowerPipe['x'] += pipeVelX# Add a new pipe when the first is about to cross the leftmost part of the screenif 0 < upperPipes[0]['x'] < 5: newpipe = getRandomPipe() upperPipes.append(newpipe[0]) lowerPipes.append(newpipe[1])# if the pipe is out of the screen, remove itif upperPipes[0]['x'] < -GAME_SPRITES['pipe'][0].get_width(): upperPipes.pop(0) lowerPipes.pop(0)# Lets blit our sprites nowSCREEN.blit(GAME_SPRITES['background'], (0, 0))for upperPipe, lowerPipe in zip(upperPipes, lowerPipes): SCREEN.blit(GAME_SPRITES['pipe'][0], (upperPipe['x'], upperPipe['y'])) SCREEN.blit(GAME_SPRITES['pipe'][1], (lowerPipe['x'], lowerPipe['y']))SCREEN.blit(GAME_SPRITES['base'], (basex, GROUNDY))SCREEN.blit(GAME_SPRITES['player'], (playerx, playery))myDigits = [int(x) for x in list(str(score))]width = 0for digit in myDigits: width += GAME_SPRITES['numbers'][digit].get_width()Xoffset = (SCREENWIDTH - width) / 2for digit in myDigits: SCREEN.blit(GAME_SPRITES['numbers'][digit], (Xoffset, SCREENHEIGHT * 0.12)) Xoffset += GAME_SPRITES['numbers'][digit].get_width()pygame.display.update()FPSCLOCK.tick(FPS)def isCollide(playerx, playery, upperPipes, lowerPipes): if playery > GROUNDY - 25 or playery < 0: GAME_SOUNDS['hit'].play() return True for pipe in upperPipes: pipeHeight = GAME_SPRITES['pipe'][0].get_height() if (playery < pipeHeight + pipe['y'] and abs(playerx - pipe['x']) < GAME_SPRITES['pipe'][0].get_width()): GAME_SOUNDS['hit'].play() return True for pipe in lowerPipes: if (playery + GAME_SPRITES['player'].get_height() > pipe['y']) and abs(playerx - pipe['x']) < \ GAME_SPRITES['pipe'][0].get_width(): GAME_SOUNDS['hit'].play() return True return Falsedef getRandomPipe(): """ Generate positions of two pipes(one bottom straight and one top rotated ) for blitting on the screen """ pipeHeight = GAME_SPRITES['pipe'][0].get_height() offset = SCREENHEIGHT / 3 y2 = offset + random.randrange(0, int(SCREENHEIGHT - GAME_SPRITES['base'].get_height() - 1.2 * offset)) pipeX = SCREENWIDTH + 10 y1 = pipeHeight - y2 + offset pipe = [ {'x': pipeX, 'y': -y1}, # upper Pipe {'x': pipeX, 'y': y2} # lower Pipe ] return pipeif __name__ == "__main__": # This will be the main point from where our game will start pygame.init() # Initialize all pygame's modules FPSCLOCK = pygame.time.Clock() pygame.display.set_caption('Flappy Bird by CodeWithHarry') GAME_SPRITES['numbers'] = ( pygame.image.load('gallery/sprites/0.png').convert_alpha(), pygame.image.load('gallery/sprites/1.png').convert_alpha(), pygame.image.load('gallery/sprites/2.png').convert_alpha(), pygame.image.load('gallery/sprites/3.png').convert_alpha(), pygame.image.load('gallery/sprites/4.png').convert_alpha(), pygame.image.load('gallery/sprites/5.png').convert_alpha(), pygame.image.load('gallery/sprites/6.png').convert_alpha(), pygame.image.load('gallery/sprites/7.png').convert_alpha(),