getting-started/flappy-bird/Player.gd

73 lines
2.2 KiB
GDScript3
Raw Normal View History

2024-04-18 15:52:41 +02:00
extends Area2D
# Signals
signal start # The bird starts
signal passed # The bird passes a Tower
signal hit # The bird is hit/flies off screen
var screen_size # The screen size, we want this at a later point
# Speed variables
2024-04-18 17:36:40 +02:00
@export var speed = 3 # How fast the player will move (pixels/sec).
@export var jump_speed = 4 # Speed at which the bird gains height after jumping
@export var speed_inc = 0.3 # Speed at which the bird falls
var speed_y = 0 # Y speed of the bird
2024-04-18 15:52:41 +02:00
var playing = false # State of the game
func start_game():
screen_size = get_viewport_rect().size
position = Vector2(0, screen_size.y / 2)
speed_y = 0
update_rotation()
2024-04-18 17:36:40 +02:00
# Called when the node is ready to be displayed
2024-04-18 15:52:41 +02:00
func _ready():
# We want the screen size to check if something is off screen
screen_size = get_viewport_rect().size
# Called every frame
func _process(_delta):
# Every frame we want to move the bird
# The player can influence the movement by pressing space
2024-04-18 17:36:40 +02:00
# An Area2D can have a position, split between x speed and y speed in a tuple
2024-04-18 15:52:41 +02:00
# Right now we want to bird to move at a set pace so x is constant
# It's up to you to change the y accordingly.
# Check if space is pressed
if Input.is_action_just_pressed("ui_accept"):
# Player pressed space -> Move the bird up
# Space is pressed, change the y speed so it gains height
# TODO 1
# When we're not playing and space is hit then the game starts -> emit the right signal
# TODO 2
2024-04-18 17:36:40 +02:00
pass # remove this pass if you implement the todo's
2024-04-18 15:52:41 +02:00
# Potential space are handled
# Decrease the y speed
# TODO 1
2024-04-18 16:02:58 +02:00
# Emit signal when a pipe is passed
# TODO 3
2024-04-18 15:52:41 +02:00
# Update the position with the new x and y speed
position += Vector2(speed, speed_y * speed)
# Rotate the bird
update_rotation()
# Rotate the bird
func update_rotation():
rotation = atan(speed_y / speed)
# We want to trigger this function when the bird hits a tower
# Connect the function with the Area2D signal 'body_entered(body: Node2D)'
# Do this by pressing on the Area2D, go to the 'Node' tab on the right side and right clicking on the desired signal.
func _on_Player_body_entered(_body):
# Set the right variables and emit the right signals so the game restarts
# TODO 2