Mudanças entre as edições de "Godot Engine: SkyFire"
De Aulas
(→Bomb) |
(→World) |
||
Linha 208: | Linha 208: | ||
screensize = get_viewport_rect().size | screensize = get_viewport_rect().size | ||
− | func _process( | + | func _process(_delta): |
if is_game_over: | if is_game_over: | ||
if Input.is_action_just_pressed("ui_accept"): | if Input.is_action_just_pressed("ui_accept"): | ||
Linha 214: | Linha 214: | ||
func _reset(): | func _reset(): | ||
+ | is_game_over = false | ||
score = 0 | score = 0 | ||
$Score.text = str(score) | $Score.text = str(score) | ||
var ship = Ship.instance() | var ship = Ship.instance() | ||
− | ship.position = Vector2(screensize.y - | + | ship.position = Vector2(screensize.y - 10, screensize.x / 2) |
add_child(ship) | add_child(ship) | ||
$GameOver.visible = false | $GameOver.visible = false | ||
− | + | $Music.play() | |
+ | $EnemyTimer.start() | ||
func change_score(pts): | func change_score(pts): | ||
Linha 230: | Linha 232: | ||
$GameOver.visible = true | $GameOver.visible = true | ||
$Music.stop() | $Music.stop() | ||
+ | $EnemyTimer.stop() | ||
func new_explosion(pos): | func new_explosion(pos): |
Edição das 10h42min de 19 de maio de 2023
Afluentes : Jogos Digitais, Usabilidade, desenvolvimento web, mobile e jogos
Assets
Estrutura
- BackGround (Node2D)
- A (Sprite)
- B (Sprite)
- Ship (KinematicBody2D)
- Sprite
- CollisionPolygon2D
- Enemy (KinematicBody2D)
- Sprite
- CollisionShape2D
- Bomb (KinematicBody2D)
- Sprite
- CollisionShape2D
- SoundBomb (AudioStreamPlayer)
- Explosion (StaticBody2D)
- AnimatedSprite
- CollisionShape2D
- SoundExplosion (AudioStreamPlayer)
- Nó (sinal) - _on_AnimatedSprite_finished()
- World (Node2D)
- Background (instancia)
- Ship (instancia)
- EnemiTimer (Timer)
- Music (AudioStreamPlayer)
- GameOver (TextureRect)
- Label (Label)
- Score (Label)
- Nó (sinal) - _on_EnemyTimer_timeout()
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 KinematicBody2D
var speed = 500
var screensize
var h
var half_w
var half_h
func _ready():
screensize = get_viewport_rect().size
h = $Sprite.texture.get_height()
half_w = $Sprite.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 KinematicBody2D
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 = $Sprite.texture.get_width()
h = $Sprite.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 info = move_and_collide(vec * delta)
if info:
var obj = info.get_collider()
if 'Ship' in obj.name:
kill()
obj.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():
queue_free()
get_parent().new_explosion(position)
Bomb
extends KinematicBody2D
var speed = 200
func _ready():
$SoundBomb.play()
func _process(delta):
var info = move_and_collide(Vector2(0, -speed) * delta)
if info:
var obj = info.get_collider()
if 'Enemy' in obj.name:
obj.kill()
queue_free()
get_parent().change_score(1)
if position.y < 0:
queue_free()
Explosion
extends StaticBody2D
func _ready():
$AnimatedSprite.play()
$SoundExplosion.play()
func _on_AnimatedSprite_animation_finished():
queue_free()
World
extends Node2D
var Ship = preload("res://Ship.tscn")
var Bomb = preload("res://Bomb.tscn")
var Enemy = preload("res://Enemy.tscn")
var 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.instance()
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.instance()
explosion.position = pos
add_child(explosion)
func new_bomb(pos):
var bomb = Bomb.instance()
bomb.position = pos
add_child(bomb)
func _on_EnemyTimer_timeout():
if is_game_over:
return
var enemy = Enemy.instance()
add_child(enemy)
$EnemyTimer.wait_time = (randi() % 2) + 1
$EnemyTimer.start()