flappy_bird_game
flappy_bird_game
flappy_bird_game
extends Node2D
# Game variables
var current_game_state : GameState = GameState.MENU
var score : int = 0
var high_score : int = 0
func _ready():
# Initialize game
reset_game()
func _process(delta):
match current_game_state:
GameState.MENU:
handle_menu_input()
GameState.PLAYING:
handle_playing_state(delta)
GameState.GAME_OVER:
handle_game_over_input()
func handle_menu_input():
# Start the game on first jump
if Input.is_action_just_pressed("jump"):
start_game()
func start_game():
# Reset game state
reset_game()
func handle_playing_state(delta):
# Spawn pipes at regular intervals
pipe_spawn_timer += delta
if pipe_spawn_timer >= pipe_spawn_interval:
spawn_pipe()
pipe_spawn_timer = 0.0
func handle_game_over_input():
# Restart game on jump input
if Input.is_action_just_pressed("jump"):
start_game()
func spawn_pipe():
var pipe : Node2D = pipe_scene.instantiate()
pipe.position = Vector2(
get_viewport_rect().size.x,
randomized_height
)
pipe_container.add_child(pipe)
func _on_pipe_score():
score += 1
func save_high_score():
# Save high score to config file
var config = ConfigFile.new()
config.set_value("game", "high_score", high_score)
config.save("user://game_config.cfg")
func load_high_score():
# Load high score from config file
var config = ConfigFile.new()
var err = config.load("user://game_config.cfg")
if err == OK:
high_score = config.get_value("game", "high_score", 0)
func _physics_process(delta):
# Only process physics if game is in playing state
if game.current_game_state != game.GameState.PLAYING:
return
# Apply gravity
velocity.y += gravity * delta
func game_over():
# Trigger game over state
game.current_game_state = game.GameState.GAME_OVER
game.game_over_label.text = "Game Over\nScore: %d\nHigh Score: %d" %
[game.score, game.high_score]
game.game_over_label.visible = true
$CollisionSound.play()
signal score_point
func _process(delta):
# Move pipe to the left
position.x -= scroll_speed * delta
func _on_score_area_body_entered(body):
if body.is_in_group("player"):
emit_signal("score_point")