Below is the full, uninterrupted code for a Flappy Bird clone in Python using tkinter. This single-file implementation includes a main menu, increasing difficulty, high score tracking, and both keyboard and mouse controls.
import tkinter as tk
import random
class FlappyBird:
def __init__(self, root):
self.root = root
self.root.title("Flappy Bird Clone")
self.root.geometry("400x600")
self.root.resizable(False, False)
self.canvas = tk.Canvas(root, width=400, height=600, highlightthickness=0)
self.canvas.pack()
self.bird = None
self.pipes = []
self.gravity = 0.5
self.velocity = 0
self.score = 0
self.high_score = 0
self.stage = 1
self.game_running = False
self.menu_active = True
self.load_high_score()
self.show_main_menu()
def load_high_score(self):
try:
with open("highscore.txt", "r") as f:
self.high_score = int(f.read())
except FileNotFoundError:
self.high_score = 0
def save_high_score(self):
with open("highscore.txt", "w") as f:
f.write(str(self.high_score))
def show_main_menu(self):
self.canvas.delete("all")
self.menu_active = True
self.canvas.create_text(200, 100, text="Flappy Bird", font=("Arial", 30), tags="menu")
options = ["New Game", "High Scores", "Quit"]
self.menu_options = []
for i, option in enumerate(options):
y = 200 + i * 50
self.menu_options.append(self.canvas.create_text(200, y, text=option, font=("Arial", 20), tags="menu"))
self.selected_option = 0
self.highlight_selected_option()
self.canvas.bind("<KeyPress>", self.handle_menu_keys)
self.canvas.bind("<Button-1>", self.handle_menu_click)
def highlight_selected_option(self):
for i, option in enumerate(self.menu_options):
if i == self.selected_option:
self.canvas.itemconfig(option, fill="red")
else:
self.canvas.itemconfig(option, fill="black")
def handle_menu_keys(self, event):
if event.keysym == "Up":
self.selected_option = (self.selected_option - 1) % 3
self.highlight_selected_option()
elif event.keysym == "Down":
self.selected_option = (self.selected_option + 1) % 3
self.highlight_selected_option()
elif event.keysym == "Return":
self.handle_menu_selection()
def handle_menu_click(self, event):
for i, option in enumerate(self.menu_options):
bbox = self.canvas.bbox(option)
if bbox and bbox[0] < event.x < bbox[2] and bbox[1] < event.y < bbox[3]:
self.selected_option = i
self.handle_menu_selection()
break
def handle_menu_selection(self):
if self.selected_option == 0:
self.start_game()
elif self.selected_option == 1:
self.show_high_scores()
elif self.selected_option == 2:
self.root.destroy()
def show_high_scores(self):
self.canvas.delete("all")
self.canvas.create_text(200, 100, text="High Scores", font=("Arial", 30), tags="menu")
self.canvas.create_text(200, 200, text=f"High Score: {self.high_score}", font=("Arial", 20), tags="menu")
self.canvas.create_text(200, 300, text="Press Enter to return", font=("Arial", 20), tags="menu")
self.canvas.bind("<KeyPress>", self.return_to_menu)
def return_to_menu(self, event):
if event.keysym == "Return":
self.show_main_menu()
def start_game(self):
self.canvas.delete("all")
self.canvas.unbind("<KeyPress>")
self.canvas.unbind("<Button-1>")
self.menu_active = False
self.game_running = True
self.score = 0
self.stage = 1
self.velocity = 0
self.bird = self.canvas.create_rectangle(50, 300, 60, 320, fill="yellow", tags="bird")
self.pipes = []
self.spawn_pipes()
self.root.bind("<space>", self.flap)
self.game_loop()
def spawn_pipes(self):
gap = 150
pipe_width = 50
pipe_height = random.randint(50, 300)
x = 450
top_pipe = self.canvas.create_rectangle(x, 0, x + pipe_width, pipe_height, fill="green", tags="pipe")
bottom_pipe = self.canvas.create_rectangle(x, pipe_height + gap, x + pipe_width, 600, fill="green", tags="pipe")
self.pipes.append((top_pipe, bottom_pipe, x, False))
def flap(self, event=None):
if self.game_running:
self.velocity = -8
def check_collision(self):
bird_coords = self.canvas.coords(self.bird)
if bird_coords[1] <= 0 or bird_coords[3] >= 600:
return True
for pipe in self.pipes:
pipe_top_coords = self.canvas.coords(pipe[0])
pipe_bottom_coords = self.canvas.coords(pipe[1])
if (bird_coords[2] > pipe_top_coords[0] and bird_coords[0] < pipe_top_coords[2] and
(bird_coords[1] < pipe_top_coords[3] or bird_coords[3] > pipe_bottom_coords[1])):
return True
return False
def update_score(self):
for pipe in self.pipes:
if self.canvas.coords(self.bird)[0] > pipe[2] + 50 and not pipe[3]:
self.score += 1
pipe[3] = True
if self.score % 5 == 0 and self.stage < 10:
self.stage += 1
self.canvas.delete("score")
self.canvas.create_text(200, 50, text=f"Score: {self.score}", font=("Arial", 20), tags="score")
def game_over(self):
self.game_running = False
self.root.unbind("<space>")
if self.score > self.high_score:
self.high_score = self.score
self.save_high_score()
self.canvas.create_text(200, 300, text="Game Over", font=("Arial", 30), fill="red", tags="gameover")
self.canvas.create_text(200, 350, text=f"Score: {self.score}", font=("Arial", 20), tags="gameover")
self.canvas.create_text(200, 400, text="Press Enter to return", font=("Arial", 20), tags="gameover")
self.canvas.bind("<KeyPress>", self.return_to_menu_from_game)
def return_to_menu_from_game(self, event):
if event.keysym == "Return":
self.canvas.delete("all")
self.show_main_menu()
def game_loop(self):
if self.game_running:
self.velocity += self.gravity
self.canvas.move(self.bird, 0, self.velocity)
for pipe in self.pipes:
self.canvas.move(pipe[0], -self.stage, 0)
self.canvas.move(pipe[1], -self.stage, 0)
if self.canvas.coords(pipe[0])[2] < 0:
self.canvas.delete(pipe[0])
self.canvas.delete(pipe[1])
self.pipes.remove(pipe)
if len(self.pipes) < 2:
self.spawn_pipes()
self.update_score()
if self.check_collision():
self.game_over()
else:
self.root.after(16, self.game_loop)
if __name__ == "__main__":
root = tk.Tk()
game = FlappyBird(root)
root.mainloop()