Advertisement
Guest User

Untitled

a guest
Mar 14th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import turtle
  2. import random
  3.  
  4. # Set up screen
  5. win = turtle.Screen()
  6. win.title("Flappy Bird in Python (CodeHS)")
  7. win.bgcolor("skyblue")
  8. win.setup(width=500, height=600)
  9.  
  10. # Bird setup
  11. bird = turtle.Turtle()
  12. bird.shape("circle")
  13. bird.color("yellow")
  14. bird.penup()
  15. bird.goto(-100, 0)
  16. bird.dy = 0  # Bird's vertical speed
  17.  
  18. # Pipe lists
  19. pipes = []
  20.  
  21. # Gravity
  22. gravity = -0.3
  23.  
  24. def create_pipe():
  25.     """Creates a new pipe pair at the right edge of the screen."""
  26.     pipe_top = turtle.Turtle()
  27.     pipe_top.shape("square")
  28.     pipe_top.color("green")
  29.     pipe_top.penup()
  30.     pipe_top.shapesize(stretch_wid=10, stretch_len=2)
  31.     pipe_top.goto(200, random.randint(50, 200))
  32.    
  33.     pipe_bottom = turtle.Turtle()
  34.     pipe_bottom.shape("square")
  35.     pipe_bottom.color("green")
  36.     pipe_bottom.penup()
  37.     pipe_bottom.shapesize(stretch_wid=10, stretch_len=2)
  38.     pipe_bottom.goto(200, pipe_top.ycor() - 150)  # Space between pipes
  39.    
  40.     pipes.append((pipe_top, pipe_bottom))
  41.  
  42. def move_pipes():
  43.     """Moves pipes to the left and removes off-screen pipes."""
  44.     for pipe_top, pipe_bottom in pipes:
  45.         pipe_top.setx(pipe_top.xcor() - 3)
  46.         pipe_bottom.setx(pipe_bottom.xcor() - 3)
  47.  
  48.     # Remove pipes that move off-screen
  49.     for pipe_top, pipe_bottom in pipes[:]:
  50.         if pipe_top.xcor() < -250:
  51.             pipe_top.hideturtle()
  52.             pipe_bottom.hideturtle()
  53.             pipes.remove((pipe_top, pipe_bottom))
  54.  
  55. def check_collision():
  56.     """Checks if the bird collides with pipes or the ground."""
  57.     for pipe_top, pipe_bottom in pipes:
  58.         if (bird.xcor() + 10 > pipe_top.xcor() - 20 and
  59.             bird.xcor() - 10 < pipe_top.xcor() + 20):
  60.             if (bird.ycor() + 10 > pipe_top.ycor() - 50 or
  61.                 bird.ycor() - 10 < pipe_bottom.ycor() + 50):
  62.                 return True  # Collision detected
  63.    
  64.     # Check if bird hits ground or top of the screen
  65.     if bird.ycor() < -280 or bird.ycor() > 280:
  66.         return True
  67.  
  68.     return False
  69.  
  70. def flap():
  71.     """Makes the bird jump."""
  72.     bird.dy = 5  # Move bird up
  73.  
  74. def game_loop():
  75.     """Main game loop running at ~50 FPS."""
  76.     global gravity
  77.  
  78.     # Apply gravity
  79.     bird.dy += gravity
  80.     bird.sety(bird.ycor() + bird.dy)
  81.  
  82.     # Move pipes
  83.     move_pipes()
  84.  
  85.     # Create new pipes occasionally
  86.     if random.randint(1, 100) < 2:
  87.         create_pipe()
  88.  
  89.     # Check for collisions
  90.     if check_collision():
  91.         bird.goto(-100, 0)  # Reset bird position
  92.         bird.dy = 0
  93.         for pipe_top, pipe_bottom in pipes:
  94.             pipe_top.hideturtle()
  95.             pipe_bottom.hideturtle()
  96.         pipes.clear()  # Fixed this line
  97.  
  98.     # Update the screen
  99.     win.update()  # Refresh the screen manually
  100.  
  101. # Bind spacebar to flap function
  102. win.listen()
  103. win.onkeypress(flap, "space")
  104.  
  105. # Set up screen update delay
  106. win.tracer(0)  # Disable automatic screen update
  107.  
  108. # Start the game loop
  109. while True:
  110.     game_loop()  # Continuously run the game loop
  111.  
  112.     # Simulate FPS with a small delay (adjustable to make game faster/slower)
  113.     win.update()  # Make sure the screen updates after each loop
  114.  
  115.     # Sleep for a small time interval (approx 20ms to make FPS about 50)
  116.     turtle.delay(20)  # Simulate the 50 FPS (adjustable)
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement