Skip to content

Instantly share code, notes, and snippets.

@sergiosvieira
Last active August 24, 2024 15:08
Show Gist options
  • Save sergiosvieira/7b5d63066a83072b72cdf339023f1bf6 to your computer and use it in GitHub Desktop.
Save sergiosvieira/7b5d63066a83072b72cdf339023f1bf6 to your computer and use it in GitHub Desktop.
Minicurso - Primeiro jogo com Godot

Slide 33

extends Area2D

@export var speed = 400 # Quão rápido o jogador se moverá (pixels/segundo).
var screen_size # Tamanho da janela do jogo.

# Chamado quando o nó entra na árvore de cenas pela primeira vez.
# A função _ready() é chamada quando um nó entra na árvore de cenas, o que é um bom momento para descobrir o tamanho da janela do jogo:
func _ready() -> void:
    screen_size = get_viewport_rect().size


# Chamado a cada quadro. 'delta' é o tempo decorrido desde o quadro anterior.
# Normalizando
# import numpy as np
# velocity = np.array([4, 3])
# norm_velocity = (velocity / np.linalg.norm(velocity))
# np.hypot(norm_velocity[0], norm_velocity[1])
# = np.float64(1.0)
func _process(delta):
	var velocity = Vector2.ZERO # The player's movement vector.
	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:
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment