Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import random
- # Set up screen
- win = turtle.Screen()
- win.title("Flappy Bird in Python (CodeHS)")
- win.bgcolor("skyblue")
- win.setup(width=500, height=600)
- # Bird setup
- bird = turtle.Turtle()
- bird.shape("circle")
- bird.color("yellow")
- bird.penup()
- bird.goto(-100, 0)
- bird.dy = 0 # Bird's vertical speed
- # Pipe lists
- pipes = []
- # Gravity
- gravity = -0.3
- def create_pipe():
- """Creates a new pipe pair at the right edge of the screen."""
- pipe_top = turtle.Turtle()
- pipe_top.shape("square")
- pipe_top.color("green")
- pipe_top.penup()
- pipe_top.shapesize(stretch_wid=10, stretch_len=2)
- pipe_top.goto(200, random.randint(50, 200))
- pipe_bottom = turtle.Turtle()
- pipe_bottom.shape("square")
- pipe_bottom.color("green")
- pipe_bottom.penup()
- pipe_bottom.shapesize(stretch_wid=10, stretch_len=2)
- pipe_bottom.goto(200, pipe_top.ycor() - 150) # Space between pipes
- pipes.append((pipe_top, pipe_bottom))
- def move_pipes():
- """Moves pipes to the left and removes off-screen pipes."""
- for pipe_top, pipe_bottom in pipes:
- pipe_top.setx(pipe_top.xcor() - 3)
- pipe_bottom.setx(pipe_bottom.xcor() - 3)
- # Remove pipes that move off-screen
- for pipe_top, pipe_bottom in pipes[:]:
- if pipe_top.xcor() < -250:
- pipe_top.hideturtle()
- pipe_bottom.hideturtle()
- pipes.remove((pipe_top, pipe_bottom))
- def check_collision():
- """Checks if the bird collides with pipes or the ground."""
- for pipe_top, pipe_bottom in pipes:
- if (bird.xcor() + 10 > pipe_top.xcor() - 20 and
- bird.xcor() - 10 < pipe_top.xcor() + 20):
- if (bird.ycor() + 10 > pipe_top.ycor() - 50 or
- bird.ycor() - 10 < pipe_bottom.ycor() + 50):
- return True # Collision detected
- # Check if bird hits ground or top of the screen
- if bird.ycor() < -280 or bird.ycor() > 280:
- return True
- return False
- def flap():
- """Makes the bird jump."""
- bird.dy = 5 # Move bird up
- def game_loop():
- """Main game loop running at ~50 FPS."""
- global gravity
- # Apply gravity
- bird.dy += gravity
- bird.sety(bird.ycor() + bird.dy)
- # Move pipes
- move_pipes()
- # Create new pipes occasionally
- if random.randint(1, 100) < 2:
- create_pipe()
- # Check for collisions
- if check_collision():
- bird.goto(-100, 0) # Reset bird position
- bird.dy = 0
- for pipe_top, pipe_bottom in pipes:
- pipe_top.hideturtle()
- pipe_bottom.hideturtle()
- pipes.clear() # Fixed this line
- # Update the screen
- win.update() # Refresh the screen manually
- # Bind spacebar to flap function
- win.listen()
- win.onkeypress(flap, "space")
- # Set up screen update delay
- win.tracer(0) # Disable automatic screen update
- # Start the game loop
- while True:
- game_loop() # Continuously run the game loop
- # Simulate FPS with a small delay (adjustable to make game faster/slower)
- win.update() # Make sure the screen updates after each loop
- # Sleep for a small time interval (approx 20ms to make FPS about 50)
- turtle.delay(20) # Simulate the 50 FPS (adjustable)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement