Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- flappybird.py
- Contributors: Josh, Anacan, Eric
- Date: 11/12/19
- Description: Makes flappy bird
- """
- import pygame
- import neat
- import os
- import random
- import time
- #######################
- WINDOW_WIDTH = 800
- WINDOW_HEIGHT = 800
- pygame.init()
- win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
- #######################
- images = [pygame.transform.scale2x(pygame.image.load(os.path.join("images","bird1.png"))),pygame.transform.scale2x(pygame.image.load(os.path.join("images","bird2.png"))),pygame.transform.scale2x(pygame.image.load(os.path.join("images","bird3.png")))]
- backgroundImage = pygame.transform.scale(pygame.image.load(os.path.join("images","bg.png")),(800,800))
- pipe_img = pygame.transform.scale2x(pygame.image.load(os.path.join("images","pipe.png")))
- class Bird:
- def __init__(self,x,y):
- self.x = x #initial x and y positions
- self.y = y
- self.animationCounter = 0
- self.animation = images[0]
- self.isAlive = True
- def flap(self):
- self.y -= 10
- def grav(self):
- self.y +=5
- def ani(self):
- if(self.animationCounter == 0):
- self.animationCounter +=1
- elif(self.animationCounter == 1):
- self.animationCounter +=1
- elif(self.animationCounter == 2):
- self.animationCounter = 0
- self.animation = images[self.animationCounter]
- def isOffScreen(self):
- if(self.y<= 0 or self.y >= 730):
- return True
- else:
- return False
- def get_mask(self):
- return pygame.mask.from_surface(self.animation)
- #makes Pipe class
- #pipes are what are going to move, eventually will have variability in height
- class Pipe:
- #space that will always be there for bird to go through
- gap = 200
- def __init__(self, x):
- self.x = x
- self.height = 0
- self.vel = 5
- self.top = 0
- self.bot = 0
- self.pipeTop = pygame.transform.flip(pipe_img, False, True)
- self.pipeBot = pipe_img
- self.passed = False
- self.set_height()
- def set_height(self):
- self.height = random.randrange(50,450)
- self.top = self.height - self.pipeTop.get_height()
- self.bot = self.height + self.gap
- def move(self):
- self.x -= self.vel
- def drawPipe(self, win):
- win.blit(self.pipeTop, (self.x,self.top))
- win.blit(self.pipeBot, (self.x,self.bot))
- def collide(self, bird, win):
- #masks look at the pixels within the image and see if the pixels are touching
- birdMask = bird.get_mask()
- topPipeMask = pygame.mask.from_surface(self.pipeTop)
- botPipeMask = pygame.mask.from_surface(self.pipeBot)
- topOffset = (self.x - bird.x, self.top - round(bird.y))
- botOffset = (self.x - bird.x, self.bot - round(bird.y))
- bPoint = birdMask.overlap(botPipeMask, botOffset)
- tPoint = birdMask.overlap(topPipeMask, topOffset)
- if bPoint or tPoint:
- print("collision")
- return True
- return False
- def pipe_bird_interaction(pipe, bird):
- pipes = [pipe]
- rem = []
- add_pipe = False
- for pipe in pipes:
- pipe.drawPipe(win)
- pipe.move()
- if pipe.collide(bird, win):
- bird.isAlive = False
- if pipe.x + pipe.pipeTop.get_width() < 0:
- rem.append(pipe)
- if not pipe.passed and pipe.x < bird.x:
- pipe.passed = True
- add_pipe = True
- if add_pipe:
- #score += 1
- pipes.append(Pipe(WINDOW_WIDTH))
- add_pipe = False
- def draw(win,bird,pipe):
- bird.ani()
- win.blit(backgroundImage, (0,0))
- win.blit(bird.animation, (bird.x,bird.y))
- if(bird.animationCounter<2):
- bird.animationCounter+=1
- else:
- bird.animationCounter=0
- pipe.drawPipe(win)
- pygame.display.update()
- def main():
- bird = Bird(150,150)
- pipe = Pipe(800)
- run = True
- while(run):
- if(bird.isAlive):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- keys = pygame.key.get_pressed()
- if(keys[pygame.K_UP]):
- bird.flap()
- else:
- bird.grav()
- pipe_bird_interaction(pipe,bird)
- draw(win, bird, pipe)
- if(bird.isOffScreen()):
- bird.isAlive=False
- else:
- print('ohno')
- break
- win.blit(backgroundImage, (0,0))
- pygame.display.update()
- pygame.time.delay(5000)
- pygame.quit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement