Mudanças entre as edições de "Godot Engine: SkyFire"

De Aulas
Linha 1: Linha 1:
 +
  
  
Linha 6: Linha 7:
  
 
= Screenshot =
 
= Screenshot =
 
+
Já vimos alguns games passo a passo, agora vamos tentar um desafio. A partir dos ''assets'', da estrutura do projeto, os ''scripts'' e algumas dicas, vamos tentar criar o game SkyFire. Abaixo temos um ''screenshot''.<center>[[Image:Godot skyfire.jpg|500px]]</center>
<center>[[Image:Godot skyfire.jpg|500px]]</center>
 
  
 
== Assets ==
 
== Assets ==
Linha 14: Linha 14:
  
 
= Estrutura =
 
= Estrutura =
 +
Veja que:
 +
 +
* todas as nossas Cenas possuem ''scripts''.
 +
* Coloquei uma tag [''script''] no nó que deve ser criado o script.
 +
* Depois de criado os ''scripts'', em alguns nós da cena devemos criar sinais.
 +
* Crie os sinais só depois da estrutura e do ''script'' criados.
  
* BackGround (Node2D)
+
Abaixo temos a estrutura de Cenas e nós.
 +
* BackGround (Node2D) [''script'']
 
** A (Sprite2D)
 
** A (Sprite2D)
 
** B (Sprite2D)
 
** B (Sprite2D)
* Ship (CharacterBody2D)
+
* Ship (CharacterBody2D) [''script'']
 
** Sprite2D
 
** Sprite2D
 
** CollisionShape2D
 
** CollisionShape2D
* Enemy (CharacterBody2D)
+
* Enemy (CharacterBody2D) [''script'']
 
** Sprite2D
 
** Sprite2D
 
** CollisionShape2D
 
** CollisionShape2D
* Bomb (CharacterBody2D)
+
* Bomb (CharacterBody2D) [''script'']
 
** Sprite2D
 
** Sprite2D
 
** CollisionShape2D
 
** CollisionShape2D
 
** SoundBomb (AudioStreamPlayer2D)
 
** SoundBomb (AudioStreamPlayer2D)
* Explosion (StaticBody2D)
+
* Explosion (StaticBody2D) [''script'']
 
** AnimatedSprite2D
 
** AnimatedSprite2D
 +
*** Nó (sinal) - ''_on_animated_sprite_2d_animation_finished()''
 
** CollisionShape2D
 
** CollisionShape2D
 
** SoundExplosion (AudioStreamPlayer2D)
 
** SoundExplosion (AudioStreamPlayer2D)
** Nó (sinal) - ''_on_animated_sprite_2d_animation_finished()''
+
* World (Node2D) [''script'']
* World (Node2D)
 
 
** Background (instancia)
 
** Background (instancia)
 
** Ship (instancia)
 
** Ship (instancia)
 
** EnemiTimer (Timer)
 
** EnemiTimer (Timer)
 +
*** Nó (sinal) - ''_on_enemy_timer_timeout()''
 
** Music (AudioStreamPlayer2D)
 
** Music (AudioStreamPlayer2D)
 
** GameOver (TextureRect)
 
** GameOver (TextureRect)
 
** Label (Label)
 
** Label (Label)
 
** Score (Label)
 
** Score (Label)
** Nó (sinal) - ''_on_enemy_timer_timeout()''
 
  
 
= Scripts =
 
= Scripts =

Edição das 08h34min de 21 de maio de 2024



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

Screenshot

Já vimos alguns games passo a passo, agora vamos tentar um desafio. A partir dos assets, da estrutura do projeto, os scripts e algumas dicas, vamos tentar criar o game SkyFire. Abaixo temos um screenshot.

Godot skyfire.jpg

Assets

Estrutura

Veja que:

  • todas as nossas Cenas possuem scripts.
  • Coloquei uma tag [script] no nó que deve ser criado o script.
  • Depois de criado os scripts, em alguns nós da cena devemos criar sinais.
  • Crie os sinais só depois da estrutura e do script criados.

Abaixo temos a estrutura de Cenas e nós.

  • BackGround (Node2D) [script]
    • A (Sprite2D)
    • B (Sprite2D)
  • Ship (CharacterBody2D) [script]
    • Sprite2D
    • CollisionShape2D
  • Enemy (CharacterBody2D) [script]
    • Sprite2D
    • CollisionShape2D
  • Bomb (CharacterBody2D) [script]
    • Sprite2D
    • CollisionShape2D
    • SoundBomb (AudioStreamPlayer2D)
  • Explosion (StaticBody2D) [script]
    • AnimatedSprite2D
      • Nó (sinal) - _on_animated_sprite_2d_animation_finished()
    • CollisionShape2D
    • SoundExplosion (AudioStreamPlayer2D)
  • World (Node2D) [script]
    • Background (instancia)
    • Ship (instancia)
    • EnemiTimer (Timer)
      • Nó (sinal) - _on_enemy_timer_timeout()
    • Music (AudioStreamPlayer2D)
    • GameOver (TextureRect)
    • Label (Label)
    • Score (Label)

Scripts

Background

extends Node2D

var speed = 100
var screensize
var h
var half_h

func _ready():
	screensize = get_viewport_rect().size
	h = $A.texture.get_height()
	half_h = h / 2

func _process(delta):
	$A.position.y += speed * delta
	$B.position.y += speed * delta
	if $A.position.y - half_h > screensize.y:
		$A.position.y -= h * 2
	if $B.position.y - half_h > screensize.y:
		$B.position.y -= h * 2

Ship

extends CharacterBody2D

const type = 'ship'
var speed = 500
var screensize
var h
var half_w
var half_h

func _ready():
	screensize = get_viewport_rect().size
	h = $Sprite2D.texture.get_height()
	half_w = $Sprite2D.texture.get_width() / 2
	half_h = h / 2

func _process(delta):
	var vec = Vector2()
	if Input.is_action_pressed("ui_left"):
		vec.x -= speed
	elif Input.is_action_pressed("ui_right"):
		vec.x += speed
	if Input.is_action_just_pressed("ui_select"):
		get_parent().new_bomb(Vector2(position.x, position.y - half_h - 20))
	var _info = move_and_collide(vec * delta)
	position.x = clamp(position.x, half_w, screensize.x - half_w)
	if get_parent().score < 0:
		kill()

func kill():
	queue_free()
	get_parent().new_explosion(position)
	get_parent().game_over()

Enemy

extends CharacterBody2D

const type = 'enemy'
const LEFT = 0
const RIGHT = 1

var screensize
var direction
var w
var h
var half_w
var half_h
var speed_y = 200
var speed_x = 300

func _ready():
	randomize()
	screensize = get_viewport_rect().size
	w = $Sprite2D.texture.get_width()
	h = $Sprite2D .texture.get_height()
	half_w = w / 2
	half_h = h / 2
	position.x = (randi() % int(screensize.x - w)) + half_w
	position.y = -100
	direction = randi() % 2
	
func _process(delta):
	var vec = Vector2()
	vec.y += speed_y
	if direction == RIGHT:
		vec.x += speed_x
		if position.x + half_w > screensize.x:
			direction = LEFT
	elif direction == LEFT:
		vec.x -= speed_x
		if position.x - half_w < 0:
			direction = RIGHT
	var collision = move_and_collide(vec * delta)
	if collision:
		var entity = collision.get_collider()
		if 'ship' in entity.type:
			kill()
			entity.kill()
	if position.y - half_h > screensize.y:
		get_parent().change_score(-1)
		queue_free()
	if get_parent().is_game_over:
		queue_free()

func kill():
	get_parent().new_explosion(position)
	queue_free()

Bomb

extends CharacterBody2D

const type = 'bomb'
var speed = 200

func _ready(): 
	$SoundBomb.play()

func _process(delta):
	var collision = move_and_collide(Vector2(0, -speed) * delta)
	if collision:
		var entity = collision.get_collider()
		if 'enemy' in entity.type:
			entity.kill() 
			queue_free()
			get_parent().change_score(1)
	if position.y < 0:
		queue_free()

Explosion

extends StaticBody2D

const type = 'explosion'

func _ready():
	$AnimatedSprite2D.play()
	$SoundExplosion.play()

func _on_animated_sprite_2d_animation_finished():
	queue_free()

World

extends Node2D

const Ship = preload("res://ship.tscn")
const Bomb = preload("res://bomb.tscn")
const Enemy = preload("res://enemy.tscn")
const Explosion = preload("res://explosion.tscn")
var score = 0
var is_game_over = false
var screensize

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

func _process(_delta):
	if is_game_over:
		if Input.is_action_just_pressed("ui_accept"):
			_reset()

func _reset():
	is_game_over = false
	score = 0
	$Score.text = str(score)
	var ship = Ship.instantiate()
	ship.position = Vector2(screensize.y - 10, screensize.x / 2)
	add_child(ship)
	$GameOver.visible = false
	$Music.play()
	$EnemyTimer.start()

func change_score(pts):
	score += pts
	$Score.text = str(score)

func game_over():
	is_game_over = true
	$GameOver.visible = true
	$Music.stop()
	$EnemyTimer.stop()

func new_explosion(pos):
	var explosion = Explosion.instantiate()
	explosion.position = pos
	add_child(explosion)

func new_bomb(pos):
	var bomb = Bomb.instantiate()
	bomb.position = pos
	add_child(bomb)

func _on_enemy_timer_timeout():
	if is_game_over:
		return
	var enemy = Enemy.instantiate()
	add_child(enemy)
	$EnemyTimer.wait_time = (randi() % 2) + 1
	$EnemyTimer.start()