Skip to content

Instantly share code, notes, and snippets.

@yaashwardhan
Last active February 10, 2021 13:33
Show Gist options
  • Save yaashwardhan/3ba72c9916da26a188481169e82dbd04 to your computer and use it in GitHub Desktop.
Save yaashwardhan/3ba72c9916da26a188481169e82dbd04 to your computer and use it in GitHub Desktop.
flappy_gist_2
"""
Code for the Bird Class
"""
class Bird():
animation_imgs = all_bird_imgs
# while the bird moves up and down
maximum_rotation = 24
# velocity with which we rotate the bird
rotation_velocity = 18
# flap animation duration
flap_animation = 8
def __init__(self, x, y, rip):
# x, y are the starting coordinates, rip (rest in peace) is the boolean value that checks if the bird is alive or not
self.x = x
self.y = y
self.rip = rip
self.tilt = 0
self.ticks = 0
self.vel = 0
self.height = self.y
self.img_count = 0
self.img = self.animation_imgs[0]
def rip_animation(self):
# positive velocity will make it go in down as the y coordinate of the pygame window increases as we go down
self.vel = 10
self.ticks = 0
# if bird is rip (by hitting a pipe) then it will turn the bird red and move it to the ground where we will remove it from the list of birds
self.height = window_height
def move(self):
self.ticks = self.ticks + 1
# d stands for displacement
d = self.vel * (self.ticks) + 1.5 * self.ticks**2
if d >= 14:
d = 14
if d < 0:
d -= 2
self.y = self.y + d
if d < 0 or self.y < self.height + 50:
if self.tilt < self.maximum_rotation:
self.tilt = self.maximum_rotation
else:
if self.tilt > -90:
self.tilt -= self.rotation_velocity
def jump(self):
# since top left corner of pygame window has coordinates (0,0), so to go upwards we need negative velocity
self.vel = -10
self.ticks = 0
self.height = self.y
def draw(self, win):
# img_count will represent how many times have we already shown image
self.img_count = self.img_count + 1
# condition to check if the bird is alive
if self.rip == False:
# checking what image of the bird we should show based on the current image count
if self.img_count <= self.flap_animation:
self.img = self.animation_imgs[0]
elif self.img_count <= self.flap_animation * 2:
self.img = self.animation_imgs[1]
elif self.img_count <= self.flap_animation * 3:
self.img = self.animation_imgs[2]
elif self.img_count <= self.flap_animation * 4:
self.img = self.animation_imgs[1]
elif self.img_count == self.flap_animation * 4 + 1:
self.img = self.animation_imgs[0]
self.img_count = 0
# this will prevent flapping of the birds wings while going down
if self.tilt <= -80:
self.img = self.animation_imgs[1]
self.img_count = self.flap_animation * 2
# condition if the bird is rip
elif self.rip == True:
self.tilt = -90
self.img = self.animation_imgs[3]
self.rip_animation()
# to rotate image in pygame
rotated_image = pygame.transform.rotate(self.img, self.tilt)
new_rect = rotated_image.get_rect(
center=self.img.get_rect(topleft=(self.x, self.y)).center)
# blit means draw, here we will draw the bird depending upon its tilt
win.blit(rotated_image, new_rect.topleft)
# since we want pixel perfect collision, and not just have a border around the bird, we mask the bird
def get_mask(self):
return pygame.mask.from_surface(self.img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment