Mudanças entre as edições de "Godot Engine: Exercicio 1 - Resolução"

De Aulas
 
(3 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
 +
  
  
Linha 46: Linha 47:
  
 
* HUD (Node2D)
 
* HUD (Node2D)
** labelScore (Label)
+
** LabelScore (Label)
** score (Label)
+
** InfoScore (Label)
** labelTimer (Label)
+
** LabelTimer (Label)
** timer (Label)
+
** InfoTimer (Label)
** counter (Timer)
+
** Timer (Timer)
  
 
= Scripts =
 
= Scripts =
Linha 57: Linha 58:
  
 
<syntaxhighlight lang=python n>
 
<syntaxhighlight lang=python n>
extends KinematicBody2D
+
extends CharacterBody2D
  
 
var speed = 100
 
var speed = 100
Linha 64: Linha 65:
 
func _ready():
 
func _ready():
 
screensize = get_viewport_rect().size
 
screensize = get_viewport_rect().size
 +
 +
func kill():
 +
hide()
 +
$CollisionShape2D.disabled = true
  
 
func _process(delta):
 
func _process(delta):
Linha 69: Linha 74:
 
if Input.is_action_pressed("ui_down"):
 
if Input.is_action_pressed("ui_down"):
 
vel.y += speed
 
vel.y += speed
$AnimatedSprite.animation = "vertical"
+
$AnimatedSprite2D.animation = "vertical"
$AnimatedSprite.flip_v = false
+
$AnimatedSprite2D.flip_v = false
 
elif Input.is_action_pressed("ui_right"):
 
elif Input.is_action_pressed("ui_right"):
 
vel.x += speed
 
vel.x += speed
$AnimatedSprite.animation = "horizontal"
+
$AnimatedSprite2D.animation = "horizontal"
$AnimatedSprite.flip_h = false
+
$AnimatedSprite2D.flip_h = false
 
elif Input.is_action_pressed("ui_up"):
 
elif Input.is_action_pressed("ui_up"):
 
vel.y -= speed
 
vel.y -= speed
$AnimatedSprite.animation = "vertical"
+
$AnimatedSprite2D.animation = "vertical"
$AnimatedSprite.flip_v = true
+
$AnimatedSprite2D.flip_v = true
 
elif Input.is_action_pressed("ui_left"):
 
elif Input.is_action_pressed("ui_left"):
 
vel.x -= speed
 
vel.x -= speed
$AnimatedSprite.animation = "horizontal"
+
$AnimatedSprite2D.animation = "horizontal"
$AnimatedSprite.flip_h = true
+
$AnimatedSprite2D.flip_h = true
 
if vel.length() > 0:
 
if vel.length() > 0:
 
var info = move_and_collide(vel * delta)
 
var info = move_and_collide(vel * delta)
 
if info:
 
if info:
 
if ('Turtle' in info.get_collider().name):
 
if ('Turtle' in info.get_collider().name):
hide()
+
kill()
$CollisionShape2D.disabled
+
$AnimatedSprite2D.play()
$AnimatedSprite.play()
 
 
else:
 
else:
$AnimatedSprite.stop()
+
$AnimatedSprite2D.stop()
 
position.x = clamp(position.x, 25, screensize.x - 25)
 
position.x = clamp(position.x, 25, screensize.x - 25)
 
position.y = clamp(position.y, 25, screensize.y - 25)
 
position.y = clamp(position.y, 25, screensize.y - 25)
Linha 99: Linha 103:
  
 
<syntaxhighlight lang=python n>
 
<syntaxhighlight lang=python n>
extends KinematicBody2D
+
extends CharacterBody2D
  
 
const DOWN = 0
 
const DOWN = 0
Linha 129: Linha 133:
 
if direction == DOWN:
 
if direction == DOWN:
 
vel.y += speed
 
vel.y += speed
$AnimatedSprite.animation = "vertical"
+
$AnimatedSprite2D.animation = "vertical"
$AnimatedSprite.flip_v = false
+
$AnimatedSprite2D.flip_v = false
 
elif direction == RIGHT:
 
elif direction == RIGHT:
 
vel.x += speed
 
vel.x += speed
$AnimatedSprite.animation = "horizontal"
+
$AnimatedSprite2D.animation = "horizontal"
$AnimatedSprite.flip_h = false
+
$AnimatedSprite2D.flip_h = false
 
elif direction == UP:
 
elif direction == UP:
 
vel.y -= speed
 
vel.y -= speed
$AnimatedSprite.animation = "vertical"
+
$AnimatedSprite2D.animation = "vertical"
$AnimatedSprite.flip_v = true
+
$AnimatedSprite2D.flip_v = true
 
elif direction == LEFT:
 
elif direction == LEFT:
 
vel.x -= speed
 
vel.x -= speed
$AnimatedSprite.animation = "horizontal"
+
$AnimatedSprite2D.animation = "horizontal"
$AnimatedSprite.flip_h = true
+
$AnimatedSprite2D.flip_h = true
 
if vel.length() > 0:
 
if vel.length() > 0:
 
move_and_collide(vel * delta)
 
move_and_collide(vel * delta)
$AnimatedSprite.play()
+
$AnimatedSprite2D.play()
 
else:
 
else:
$AnimatedSprite.stop()
+
$AnimatedSprite2D.stop()
 
position.x = clamp(position.x, 25, screensize.x - 25)
 
position.x = clamp(position.x, 25, screensize.x - 25)
 
position.y = clamp(position.y, 25, screensize.y - 25)
 
position.y = clamp(position.y, 25, screensize.y - 25)
Linha 163: Linha 167:
 
score += 1
 
score += 1
  
func _process(delta):
+
func _process(_delta):
$timer.text = String(int($counter.time_left))
+
$InfoTimer.text = str(int($Timer.time_left))
$score.text = String(score)
+
$InfoScore.text = str(score)
if $timer.text == '0' and not end:
+
if $InfoTimer.text == '0' and not end:
 
get_parent().get_node('Player').kill()
 
get_parent().get_node('Player').kill()
 
end = true
 
end = true
$counter.stop()
+
$Timer.stop()
 
</syntaxhighlight>
 
</syntaxhighlight>

Edição atual tal como às 12h51min de 6 de maio de 2024



Afluentes : Jogos Digitais, Usabilidade, desenvolvimento web, mobile e jogos

Videoaula

Youtube

Descrição

Baseado na última aula, crie um jogo com as seguintes características:

  • 1 elemento caixa. Você usará as caixas para criar um labirinto e as bordas do jogo;
  • 1 elemento player pra você controlar. Ele não usa gravidade, e pode ir para cima, baixo, esquerda e direita, conforme setas do teclado;
  • 1 elemento monstro que se controla sozinho. Anda para lados aleatórios a uma velocidade aleatória a cada certo período de tempo ou timeout de 1 segundo;
  • Faça teste de colisão entre o player e o monstro. Caso o monstro toque no player, ele não se movimenta mais, está morto.

Algumas coisas foram vistas em aula, outras você terá que pesquisar para conseguir fazer o exercício.

Assets

Exemplo de Assets

Estrutura

  • Box (StaticBody2D):
    • Sprite
    • CollisionShape2D
  • Player (CharacterBody2D):
    • AnimatedSprite (horizontal|vertical)
    • CollisionShape2D (circle shape)
  • Turtle (CharacterBody2D):
    • AnimatedSprite (horizontal|vertical)
    • CollisionShape2D (circle shape)
  • World (Node2D)
    • TextureRect (Fundo)
    • Linkar
      • Player
      • Turtle (algumas)
      • Box (algumas)
      • HUD
  • HUD (Node2D)
    • LabelScore (Label)
    • InfoScore (Label)
    • LabelTimer (Label)
    • InfoTimer (Label)
    • Timer (Timer)

Scripts

Player

extends CharacterBody2D

var speed = 100
var screensize

func _ready():
	screensize = get_viewport_rect().size

func kill():
	hide()
	$CollisionShape2D.disabled = true

func _process(delta):
	var vel = Vector2()
	if Input.is_action_pressed("ui_down"):
		vel.y += speed
		$AnimatedSprite2D.animation = "vertical"
		$AnimatedSprite2D.flip_v = false
	elif Input.is_action_pressed("ui_right"):
		vel.x += speed
		$AnimatedSprite2D.animation = "horizontal"
		$AnimatedSprite2D.flip_h = false
	elif Input.is_action_pressed("ui_up"):
		vel.y -= speed
		$AnimatedSprite2D.animation = "vertical"
		$AnimatedSprite2D.flip_v = true
	elif Input.is_action_pressed("ui_left"):
		vel.x -= speed
		$AnimatedSprite2D.animation = "horizontal"
		$AnimatedSprite2D.flip_h = true
	if vel.length() > 0:
		var info = move_and_collide(vel * delta)
		if info:
			if ('Turtle' in info.get_collider().name):
				kill()
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
	position.x = clamp(position.x, 25, screensize.x - 25)
	position.y = clamp(position.y, 25, screensize.y - 25)

Turtle

extends CharacterBody2D

const DOWN = 0
const RIGHT = 1
const UP = 2
const LEFT = 3

var direction = DOWN
var speed = 100
var steps
var count_steps
var screensize

func _ready():
	randomize()
	screensize = get_viewport_rect().size
	new_goal()

func new_goal():
	direction = randi() % 4;
	steps = 50 + (randi() % 50)
	count_steps = 0

func _process(delta):
	count_steps += 1
	if count_steps > steps:
		new_goal()
	var vel = Vector2()
	if direction == DOWN:
		vel.y += speed
		$AnimatedSprite2D.animation = "vertical"
		$AnimatedSprite2D.flip_v = false
	elif direction == RIGHT:
		vel.x += speed
		$AnimatedSprite2D.animation = "horizontal"
		$AnimatedSprite2D.flip_h = false
	elif direction == UP:
		vel.y -= speed
		$AnimatedSprite2D.animation = "vertical"
		$AnimatedSprite2D.flip_v = true
	elif direction == LEFT:
		vel.x -= speed
		$AnimatedSprite2D.animation = "horizontal"
		$AnimatedSprite2D.flip_h = true
	if vel.length() > 0:
		move_and_collide(vel * delta)
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
	position.x = clamp(position.x, 25, screensize.x - 25)
	position.y = clamp(position.y, 25, screensize.y - 25)

HUD

extends Node2D

var score = 0
var end = false

func incScore():
	score += 1

func _process(_delta):
	$InfoTimer.text = str(int($Timer.time_left))
	$InfoScore.text = str(score)
	if $InfoTimer.text == '0' and not end:
		get_parent().get_node('Player').kill()
		end = true
		$Timer.stop()