We’d love to hear from you. Whether you have a question that you’d like us to answer as part of our FAQ,  you’ve identified something that just doesn’t quite look right in one of our tutorials, you have a business opportunity or for any other reason, please use this simple form to contact us.

We’ll do our best to provide a prompt response.

Like what you see?...pass it on!

The Final Code

Here is a quick summary of the final state of all the scripts used in this game.

TitleScene.gd

extends Control


func _ready():
  globals.kills = 0
  globals.currentStage = 0


func _on_QuitGameButton_pressed():
  get_tree().quit()


func _on_NewGameButton_pressed():
  get_tree().change_scene("GameScene.tscn")

GameScene.gd

extends Node2D


enum GameState { Loading, Running, GameOver }
onready var State = GameState.Loading;
onready var enemyObj = preload("res://Enemy.tscn")

var Player = null
var cam = null

func _ready():
	var labelText = "Stage " + str(globals.currentStage)
	$Label.set_text(labelText)
	$AnimationPlayer.play("Stage Display")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	$HUD/Kills.set_text("Kills:" + str(globals.kills))
	pass

func _input(event):
	if(State == GameState.GameOver):
		get_tree().change_scene("TitleScene.tscn")
			
func startAnimationDone():
	$Label.visible = false
	Player = load("res://Player.tscn").instance()
	Player.position = Vector2(300,720/2)
	cam = Camera2D.new()
	cam.position.x = 360
	cam.make_current()
	Player.add_child(cam)
	add_child(Player)
	SpawnEnemies()
	State = GameState.Running
	
func _on_Area2D_area_entered(area):
	#Did we hit the trigger to queue boss fight?
	if(area.get_collision_layer_bit(4)):
		if(State == GameState.Running):
			Player.speed = 0
			globals.currentStage = globals.currentStage + 1
			get_tree().reload_current_scene()
		
	
func _on_GroundArea_area_entered(area):
	if(State == GameState.Running):
		Player.explode()
	
func PlayerDied():
	for i in Player.get_children():
		i.queue_free()
	remove_child(Player)
	State = GameState.GameOver
	
	$Label.set_text("Game Over")
	$Label.visible = true
	$Label.set_position(Vector2(1280/2 - 200, 720/2))

func SpawnEnemy(x,y):
	
	var enemy = enemyObj.instance()
	enemy.set_position(Vector2(x,y))
	add_child(enemy)

func SpawnEnemies():
	randomize()
	# One extra plane per stage
	for i in range(0,10+globals.currentStage):
		SpawnEnemy(700 + randi()%5000,randi()%int(get_viewport_rect().size.y))

func SpawnBossWave():
	for i in range(0,10):
		SpawnEnemy(3800 + randi()%220, randi()%720)

Player.gd

extends Node2D

var speed = 150
var verticalMovement = 0
const MAX_VERTICAL_MOVEMENT = 200
var bulletObj = null
const RATE_OF_FIRE = 3.0
var dying = false


onready var shotCooldown = $Timer
onready var explode = preload("res://Explosion.tscn").instance()


func _ready():
  bulletObj = load("res://Bullet.tscn")
  shotCooldown.wait_time = 1.0/RATE_OF_FIRE
  shotCooldown.one_shot = true
  

func _process(delta):
  move_local_x(speed*delta)
  if(self.position.y > 1  && self.position.y <= get_viewport_rect().size.y):
    move_local_y(verticalMovement*delta)
  else:
    if(self.position.y < 1): 
      move_local_y(10) #Bounce off top
      verticalMovement = 0
    if(self.position.y > get_viewport_rect().size.y): 
      move_local_y(-10) #Bounce off bottom
      verticalMovement = 0
  if(dying == true):
    if(shotCooldown.time_left == 0):
      get_node("/root/GameSceneRoot").PlayerDied()
      queue_free()
      print("Dead")

  func stop():
  speed = 0

func explode():
  explode.set_position(self.get_position())
  get_parent().add_child(explode)
  globals.kills = globals.kills + 1
  shotCooldown.wait_time = 2.5
  shotCooldown.start()
  $PlayerSprite.visible = false
  dying = true

func _input(event):
  if(event.is_action("PLAYER_UP")):
    if(verticalMovement >= -MAX_VERTICAL_MOVEMENT):
      verticalMovement-=10
  if(event.is_action("PLAYER_DOWN")):
    if(verticalMovement <= MAX_VERTICAL_MOVEMENT):
      verticalMovement+=10
  if(event.is_action("PLAYER_SHOOT")):
    if(shotCooldown.time_left == 0):
      var bullet = bulletObj.instance()
      bullet.position = self.get_position()
      bullet.position.y = bullet.position.y + 20
      get_node("/root/GameSceneRoot").add_child(bullet)
      shotCooldown.start()

func _on_Area2D_area_entered(area):
  #Layer 2 is another enemy
  if(area.get_collision_layer_bit(2)):
    explode()

Enemy.gd

extends Node2D


var sprite = null;
const sprites = [ "enemy1_blue.png", "enemy1_green.png",
        "enemy1_red.png", "enemy1_yellow.png",
        "enemy2_blue.png", "enemy2_pink.png",
        "enemy2_red.png", "enemy2_yellow.png"]
        

var speed = 100
onready var explode = preload("res://Explosion.tscn").instance()


func _ready():
  speed = speed + (globals.currentStage * 10)


func _enter_tree():
  sprite = Sprite.new()
  sprite.texture = load("res://assets/graphics/enemies/" + sprites[randi()%sprites.size()])
  add_child(sprite)
  

func _process(delta):
  move_local_x(-delta*speed)

func _on_Area2D_area_entered(area):
  #Hit by bullet
  if(area.get_collision_layer_bit(3)):  
    explode.set_position(self.get_position())
    get_parent().add_child(explode)
    globals.kills = globals.kills + 1
    queue_free()

Bullet.gd

extends Node2D


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
  move_local_x(delta * 400)

func _on_Area2D_area_entered(area):
  if(area.get_collision_layer_bit(2)):
    queue_free()

Explosion.gd

extends Node2D

func _ready():
  $AnimationPlayer.play("Explode")

globals.gd

extends Node

var currentStage = 1
var kills = 0

Making Things Explode<<PreviousTable of ContentsNext>>Building your Game for Windows

Like what you see?...pass it on!

script ends --->