Advertisement
EdgeLordKirito

Godot

Jan 11th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | Source Code | 0 0
  1. extends Area2D
  2.  
  3. @export var speed = 400 # How fast the player will move (pixels/sec).
  4. var screen_size
  5.  
  6. # Called when the node enters the scene tree for the first time.
  7. func _ready():
  8. screen_size = get_viewport_rect().size
  9.  
  10.  
  11. # Called every frame. 'delta' is the elapsed time since the previous frame.
  12. func _process(delta):
  13. var velocity = Vector2.ZERO # The player's movement vector.
  14. if Input.is_action_pressed("move_right"):
  15. velocity.x += 1
  16. if Input.is_action_pressed("move_left"):
  17. velocity.x -= 1
  18. if Input.is_action_pressed("move_down"):
  19. velocity.y += 1
  20. if Input.is_action_pressed("move_up"):
  21. velocity.y -= 1
  22.  
  23. if velocity.length() > 0:
  24. velocity = velocity.normalized() * speed
  25. $AnimatedSprite2D.play()
  26. else:
  27. $AnimatedSprite2D.stop()
  28.  
  29. position += velocity * delta
  30. position = position.clamp(Vector2.ZERO, screen_size)
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement