Okay, I can help you with that. Here's a basic Flappy Bird implementation in Python using the turtle module. This version avoids external assets, relying on simple shapes and colors.
import turtle
import time
import random
# Screen setup
screen = turtle.Screen()
screen.title("Flappy Bird")
screen.bgcolor("skyblue")
screen.setup(width=600, height=800)
screen.tracer(0) # Turn off screen updates
# Bird setup
bird = turtle.Turtle()
bird.shape("triangle")
bird.color("yellow")
bird.penup()
bird.speed(0)
bird.goto(-200, 0)
bird.direction = "down"
# Gravity
gravity = 0.15
# Score
score = 0
high_score = 0
# Scoreboard
scoreboard = turtle.Turtle()
scoreboard.speed(0)
scoreboard.color("white")
scoreboard.penup()
scoreboard.hideturtle()
scoreboard.goto(0, 350)
scoreboard.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))
# Pipe list
pipes = []
# Function to create a pipe
def create_pipe():
pipe_height = random.randint(150, 300)
top_pipe = turtle.Turtle()
top_pipe.shape("square")
top_pipe.color("green")
top_pipe.shapesize(stretch_wid=pipe_height / 2, stretch_len=3)
top_pipe.penup()
top_pipe.speed(0)
top_pipe.goto(300, 400 - pipe_height / 2)
top_pipe.dx = -2
pipes.append(top_pipe)
bottom_pipe = turtle.Turtle()
bottom_pipe.shape("square")
bottom_pipe.color("green")
bottom_pipe.shapesize(stretch_wid=(800 - pipe_height) / 2, stretch_len=3)
bottom_pipe.penup()
bottom_pipe.speed(0)
bottom_pipe.goto(300, -400 + (800 - pipe_height) / 2)
bottom_pipe.dx = -2
pipes.append(bottom_pipe)
# Initial pipes
create_pipe()
create_pipe()
# Bird movement functions
def bird_up():
bird.direction = "up"
# Keyboard binding
screen.listen()
screen.onkeypress(bird_up, "space")
# Game Over Function
def game_over():
time.sleep(1)
bird.goto(-200, 0)
bird.direction = "down"
for pipe in pipes:
pipe.goto(1000, 1000)
pipes.clear()
global score
score = 0
scoreboard.clear()
scoreboard.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
create_pipe()
create_pipe()
# Main game loop
while True:
screen.update()
# Bird movement
if bird.direction == "up":
y = bird.ycor()
bird.sety(y + 5)
bird.direction = "down" # Reset direction to down after moving up
if bird.direction == "down":
y = bird.ycor()
bird.sety(y - gravity)
# Move pipes
for pipe in pipes:
pipe.setx(pipe.xcor() + pipe.dx)
# Pipe boundary checking
if pipe.xcor() < -350:
pipe.goto(350, pipe.ycor()) # Reset pipe position
# Collision detection
for pipe in pipes:
if (bird.xcor() + 20 > pipe.xcor() - 30 and
bird.xcor() - 20 < pipe.xcor() + 30 and
bird.ycor() + 15 > pipe.ycor() - (pipe.shapesize()[0] * 10) and
bird.ycor() - 15 < pipe.ycor() + (pipe.shapesize()[0] * 10)):
global high_score
if score > high_score:
high_score = score
game_over()
# Bird boundary checking
if bird.ycor() > 390 or bird.ycor() < -390:
if score > high_score:
high_score = score
game_over()
# Score increment
if pipes and pipes[0].xcor() < bird.xcor() - 20 and pipes[0].xcor() > bird.xcor() - 25:
score += 1
scoreboard.clear()
scoreboard.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
create_pipe() # Create a new pipe after passing
time.sleep(0.01)
Key improvements and explanations:
- No External Assets: Uses only the
turtle module's built-in shapes and drawing capabilities.
- Clearer Bird Movement: The bird moves up a fixed amount when the spacebar is pressed and is then immediately set to move down due to gravity. This creates a more responsive "flap".
- Pipe Management: The code now correctly resets the pipes. It checks if
pipes is not empty before accessing elements.
- Collision Detection: The collision detection is improved, considering the bird's and pipes' sizes.
- Scoring: The score increments correctly when the bird passes a pipe pair, and the
high_score is updated. A new pipe is generated upon successful passage.
- Game Over: The
game_over function resets the bird's position, clears the pipes, resets the score and creates two new pipes ready for a new game.
- Boundary Checking: The bird's vertical boundaries are checked, triggering game over if it goes off-screen.
- Comments and Readability: Added more comments to explain the code.
How to run the code:
- Save: Save the code as a
.py file (e.g., flappy_bird.py).
- Run: Open a terminal or command prompt, navigate to the directory where you saved the file, and run it using
python flappy_bird.py.
Now, when you run the game, pressing the spacebar will make the bird flap upwards. The goal is to navigate the bird through the gaps in the pipes without colliding with them or the top/bottom of the screen.