#Godot first game

12 messages · Page 1 of 1 (latest)

blissful ermine
#

is the player in the game scene

nova agate
#

Is the player visibility on?

sweet light
sweet light
blissful ermine
#

what is the player position

sweet light
blissful ermine
#

what is the code

sweet light
#

there are three scripts

#

main enemy and player

#
extends Area2D
signal hit
#export allows us to see the variable on the inspector window
@export var speed = 400 #how fast the player will move
var screen_size #size of the game window

func _ready():
    #returning us the screen size
    screen_size = get_viewport_rect().size
    #hide the player when the game starts
    #hide()
func _process(delta):
    var velocity = Vector2.ZERO #player movment vector(speed and direction)
    if Input.is_action_pressed("move_right"):
        velocity.x += 1
    if Input.is_action_pressed("move_left"):
        velocity.x -= 1
    if Input.is_action_pressed("move_down"):
        velocity.y += 1
    if Input.is_action_pressed("move_up"):
        velocity.y -= 1
    if velocity.length() > 0:
        #normalized - reducing the vector length to 1 but keeps his direction
        velocity = velocity.normalized() * speed
        #$ - get_node()
        $AnimatedSprite2D.play()
    else:
        $AnimatedSprite2D.stop()
    position += velocity * delta
    #clamp - we use clamp to prevent from the player to get out of the boundries
    #clamp(our varient,minimum point,maximum point)
    position.x = clamp(position.x,0,screen_size.x)
    position.y = clamp(position.y,0,screen_size.y)
    #here we are changing our animations when we walk or going up or down
    if velocity.x != 0:
        #choose animation 
        $AnimatedSprite2D.animation = "walk"
        #flip the player verticlly
        $AnimatedSprite2D.flip_v = false
        if velocity.x < 0 flip the player horizontally
        $AnimatedSprite2D.flip_h = velocity.x < 0
    elif velocity.y != 0:
        $AnimatedSprite2D.animation = "up"
        #if velocity.y > 0 flip the player vertically
        $AnimatedSprite2D.flip_v = velocity.y >0
#
#signal function when rigidbody has contact with other body
func _on_body_entered(body):
    #hide() # Player disappears after being hit
    hit.emit()
    # Must be deferred as we can't change physics properties on a physics callback.
    #it wont let the hit signal to trigger more than once
    #set_deferred - wait to disable until its safe to do so
    $CollisionShape2D.set_deferred("disabled", true)
#reset the player after being hit
func start(pos):
    position = pos
    show()
    $CollisionShape2D.disabled = false```
sweet light