Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import pygame
- import random
- from pygame.locals import *
- from itertools import cycle
- FPS = 30
- SCREEN_WIDTH = 288
- SCREEN_HEIGHT = 512
- BASEY = SCREEN_HEIGHT * 0.8
- IMAGES = {}
- PLAYERS_LIST = (
- # red bird
- (
- 'assets/sprites/redbird-upflap.png',
- 'assets/sprites/redbird-midflap.png',
- 'assets/sprites/redbird-downflap.png'
- ),
- # blue bird
- (
- 'assets/sprites/bluebird-upflap.png',
- 'assets/sprites/bluebird-midflap.png',
- 'assets/sprites/bluebird-downflap.png'
- ),
- # yellow bird
- (
- 'assets/sprites/yellowbird-upflap.png',
- 'assets/sprites/yellowbird-midflap.png',
- 'assets/sprites/yellowbird-downflap.png'
- ),
- )
- BACKGROUNDS_LIST = (
- 'assets/sprites/background-day.png',
- 'assets/sprites/background-night.png'
- )
- PIPES_LIST = (
- 'assets/sprites/pipe-green.png',
- 'assets/sprites/pipe-red.png'
- )
- def main():
- global SCREEN, FPSCLOCK
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Flappy Bird")
- IMAGES['message'] = pygame.image.load('assets/sprites/message.png').convert_alpha()
- IMAGES['base'] = pygame.image.load('assets/sprites/base.png').convert_alpha()
- while True:
- randBg = random.choice(BACKGROUNDS_LIST)
- IMAGES['background'] = pygame.image.load(randBg).convert()
- randPlayer = random.choice(PLAYERS_LIST)
- IMAGES['player'] = (
- pygame.image.load(randPlayer[0]).convert_alpha(),
- pygame.image.load(randPlayer[1]).convert_alpha(),
- pygame.image.load(randPlayer[2]).convert_alpha(),
- )
- movementInfo = showWelcomeAnimation()
- mainGame(movementInfo)
- def showWelcomeAnimation():
- pass
- playerIndex = 0
- playerIndexGen = cycle([0, 1, 2, 1])
- loopIter = 0
- baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
- playerShmVals = {'val': 0, 'dir': 1}
- messagex = int((SCREEN_WIDTH - IMAGES['message'].get_width()) / 2)
- messagey = int(SCREEN_HEIGHT * 0.12)
- basex = 0
- playerx = int(SCREEN_WIDTH * 0.2)
- playery = int((SCREEN_HEIGHT - IMAGES['player'][0].get_height()) / 2)
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == KEYDOWN and event.key == K_SPACE:
- return {
- 'playery': playery + playerShmVals['val'],
- 'basex': basex,
- 'playerIndexGen': playerIndexGen,
- }
- # adjust playery, playerIndex, basex
- if (loopIter + 1) % 5 == 0:
- playerIndex = next(playerIndexGen)
- loopIter = (loopIter + 1) % 30
- basex = -((-basex + 4) % baseShift)
- playerShm(playerShmVals)
- # draw sprites
- SCREEN.blit(IMAGES['background'], (0, 0))
- SCREEN.blit(IMAGES['message'], (messagex, messagey))
- SCREEN.blit(IMAGES['base'], (basex, BASEY))
- # SCREEN.blit(IMAGES['player'][0], (playerx, playery))
- SCREEN.blit(IMAGES['player'][playerIndex], (playerx, playery + playerShmVals['val']))
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- def mainGame(movementInfo):
- playerIndex = 0
- loopIter = 0
- playerIndexGen = movementInfo['playerIndexGen']
- playerx = int(SCREEN_WIDTH * 0.2)
- playery = movementInfo['playery']
- basex = movementInfo['basex']
- baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
- playerVelY = -9
- playerMaxVelY = 10
- playerMinVel = -8
- playerAccY = 1
- playerRot = 45
- playerVelRot = 3
- playerRotThr = 20
- playerFlapAcc = -9
- playerFlapped = False
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == KEYDOWN and event.key == K_SPACE:
- if playery > -2 * IMAGES['player'][0].get_height():
- playerVelY = playerFlapAcc
- playerFlapped = True
- # playerIndex change
- if (loopIter + 1) % 3 == 0:
- playerIndex = next(playerIndexGen)
- loopIter = (loopIter + 1) % 30
- # player movement
- if playerVelY < playerMaxVelY and not playerFlapped:
- playerVelY += playerAccY
- if playerFlapped:
- playerFlapped = False
- playerHeight = IMAGES['player'][playerIndex].get_height()
- playery += min(playerVelY, BASEY - playery - playerHeight)
- # draw sprites
- SCREEN.blit(IMAGES['background'], (0, 0))
- playerSurface = pygame.transform.rotate(IMAGES['player'][playerIndex], 0)
- SCREEN.blit(playerSurface, (playerx, playery))
- SCREEN.blit(IMAGES['base'], (basex, BASEY))
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- def playerShm(playerShm):
- if abs(playerShm['val']) == 8:
- playerShm['dir'] *= -1
- if playerShm['dir'] == 1:
- playerShm['val'] += 1
- else:
- playerShm['val'] -= 1
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement