import pgzrun
import random
TITLE = 'Flappy Bird'
WIDTH = 400
HEIGHT = 708
# ------------- SOME PRESET CONSTANTS --------------------
GAP = 130 # sets the distance between the pipes to be a constant
GRAVITY = 0.3 # sets the constant of gravity which will impact the falling speed of the bird
FLAP_STRENGTH = 6.5 # sets the flap strength which is how the bird will rise
SPEED = 3 # sets the horizontal speed of the pipes
# TODO: Bird.__init__(), Bird.reset(), Bird.update()
class Bird(Actor):
def __init__(self, image, pos):
super().__init__(image, pos)
# add more fields here as necessary
def reset(self):
# reset constants of the bird to their starting positions
def update(self):
# update position and speed of the bird
# TODO: Pipe.reset(), Pipe.update()
class Pipe(Actor):
def __init__(self, image, is_top): # Pipe takes in an image and an is_top bool to determine where to place the pipe
self.is_top = is_top
if is_top:
super().__init__(image, anchor=('left', 'bottom'))
else:
super().__init__(image, anchor=('left', 'top'))
def reset(self):
# reset position of the pipe
def update(self):
# update position of the pipe
# ------------- create bird, top pipe, bottom pipe, score, other global variables HERE --------------
# TODO: reset_pipes()
def reset_pipes():
# used to set the initial positions of the pipes
# ---- similar logic is used in Pipe.reset() -----
# 1. Use random.randint(INSERT_BOTTOM_OF_RANGE_HERE, INSERT_TOP_OF_RANGE_HERE) to create a pipe gap variable
# that will be the y_value where the gap between the pipes begins (so the y value of the bottom of the top pipe)
# 2. Call the .reset function on the top and bottom pipes (HINT: reset should take in the pipe gap variable)
# -------------- set initial pipe positions by calling reset_pipes -------------
# TODO: update()
def update():
# write the global update function to update both bird and pipes, and deal with collision logic
# 1. Check if bird hits the top or bottom pipes; if so, bird is dead. Act accordingly.
# 2. Check if bird is off the screen - flappybird only resets once the bird has fallen off the bottom of the screen
# after dying. When the bird is dead, reset everything
# 3. Check if the pipes have gone off the left side of the screen. If so, then the bird successfully made it through
# the gap and the player scored a point! Update variables accordingly and reset pipe position
# 4. If the bird isn't dead, it's on the screen, and the pipes are still on screen, update the position of the
# bird and pipe using the methods in their respective classes
# TODO: on_key_down()
def on_key_down():
# when space key pressed, if bird's still alive, flap the bird
# TODO: draw()
def draw():
screen.blit('background', (0, 0))
# draw the bird and pipes
pgzrun.go()