Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. """
  2. flappybird.py
  3. Contributors: Josh, Anacan, Eric
  4. Date: 11/12/19
  5. Description: Makes flappy bird
  6. """
  7. import pygame
  8. import neat
  9. import os
  10. import random
  11. import time
  12. #######################
  13. WINDOW_WIDTH = 800
  14. WINDOW_HEIGHT = 800
  15. pygame.init()
  16. win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
  17. #######################
  18. 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")))]
  19. backgroundImage = pygame.transform.scale(pygame.image.load(os.path.join("images","bg.png")),(800,800))
  20. pipe_img = pygame.transform.scale2x(pygame.image.load(os.path.join("images","pipe.png")))
  21.  
  22.  
  23.  
  24. class Bird:
  25.  
  26.  
  27. def __init__(self,x,y):
  28. self.x = x #initial x and y positions
  29. self.y = y
  30. self.animationCounter = 0
  31. self.animation = images[0]
  32. self.isAlive = True
  33.  
  34. def flap(self):
  35. self.y -= 10
  36.  
  37. def grav(self):
  38. self.y +=5
  39.  
  40.  
  41. def ani(self):
  42. if(self.animationCounter == 0):
  43. self.animationCounter +=1
  44. elif(self.animationCounter == 1):
  45. self.animationCounter +=1
  46. elif(self.animationCounter == 2):
  47. self.animationCounter = 0
  48. self.animation = images[self.animationCounter]
  49.  
  50. def isOffScreen(self):
  51. if(self.y<= 0 or self.y >= 730):
  52. return True
  53. else:
  54. return False
  55.  
  56. def get_mask(self):
  57. return pygame.mask.from_surface(self.animation)
  58.  
  59.  
  60. #makes Pipe class
  61. #pipes are what are going to move, eventually will have variability in height
  62. class Pipe:
  63. #space that will always be there for bird to go through
  64. gap = 200
  65. def __init__(self, x):
  66. self.x = x
  67. self.height = 0
  68. self.vel = 5
  69.  
  70. self.top = 0
  71. self.bot = 0
  72.  
  73. self.pipeTop = pygame.transform.flip(pipe_img, False, True)
  74. self.pipeBot = pipe_img
  75.  
  76. self.passed = False
  77.  
  78. self.set_height()
  79.  
  80. def set_height(self):
  81. self.height = random.randrange(50,450)
  82. self.top = self.height - self.pipeTop.get_height()
  83. self.bot = self.height + self.gap
  84.  
  85. def move(self):
  86. self.x -= self.vel
  87.  
  88. def drawPipe(self, win):
  89. win.blit(self.pipeTop, (self.x,self.top))
  90. win.blit(self.pipeBot, (self.x,self.bot))
  91.  
  92. def collide(self, bird, win):
  93. #masks look at the pixels within the image and see if the pixels are touching
  94. birdMask = bird.get_mask()
  95. topPipeMask = pygame.mask.from_surface(self.pipeTop)
  96. botPipeMask = pygame.mask.from_surface(self.pipeBot)
  97.  
  98. topOffset = (self.x - bird.x, self.top - round(bird.y))
  99. botOffset = (self.x - bird.x, self.bot - round(bird.y))
  100.  
  101. bPoint = birdMask.overlap(botPipeMask, botOffset)
  102. tPoint = birdMask.overlap(topPipeMask, topOffset)
  103.  
  104. if bPoint or tPoint:
  105. print("collision")
  106. return True
  107.  
  108. return False
  109.  
  110. def pipe_bird_interaction(pipe, bird):
  111. pipes = [pipe]
  112. rem = []
  113. add_pipe = False
  114. for pipe in pipes:
  115. pipe.drawPipe(win)
  116. pipe.move()
  117. if pipe.collide(bird, win):
  118. bird.isAlive = False
  119. if pipe.x + pipe.pipeTop.get_width() < 0:
  120. rem.append(pipe)
  121.  
  122. if not pipe.passed and pipe.x < bird.x:
  123. pipe.passed = True
  124. add_pipe = True
  125. if add_pipe:
  126. #score += 1
  127. pipes.append(Pipe(WINDOW_WIDTH))
  128. add_pipe = False
  129.  
  130.  
  131.  
  132.  
  133. def draw(win,bird,pipe):
  134. bird.ani()
  135. win.blit(backgroundImage, (0,0))
  136. win.blit(bird.animation, (bird.x,bird.y))
  137. if(bird.animationCounter<2):
  138. bird.animationCounter+=1
  139. else:
  140. bird.animationCounter=0
  141. pipe.drawPipe(win)
  142. pygame.display.update()
  143.  
  144.  
  145.  
  146. def main():
  147. bird = Bird(150,150)
  148. pipe = Pipe(800)
  149. run = True
  150. while(run):
  151. if(bird.isAlive):
  152. for event in pygame.event.get():
  153. if event.type == pygame.QUIT:
  154. run = False
  155. keys = pygame.key.get_pressed()
  156. if(keys[pygame.K_UP]):
  157. bird.flap()
  158. else:
  159. bird.grav()
  160. pipe_bird_interaction(pipe,bird)
  161. draw(win, bird, pipe)
  162.  
  163. if(bird.isOffScreen()):
  164. bird.isAlive=False
  165. else:
  166. print('ohno')
  167. break
  168.  
  169.  
  170.  
  171.  
  172. win.blit(backgroundImage, (0,0))
  173. pygame.display.update()
  174. pygame.time.delay(5000)
  175. pygame.quit()
  176.  
  177.  
  178.  
  179.  
  180.  
  181. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement