Simple 2D game based on avoiding asteroids while controlling a rocket
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

76 lines
2.5 KiB

extends Node2D
var time = 0
var asteroidObj = load("res://Objects/Asteroid.tscn")
var coinObj = load("res://Objects/Coin.tscn")
var spawnTime
var deltaSpawnTime = 1
var asteroidSpawnRepeat = 0
var hiscore = 0
var numOfCoins = 0
var lastscore = 0
var pause_button = preload("res://Images and Anims/In Game/pausebutton.png")
var play_button = preload("res://Images and Anims/In Game/playbutton.png")
onready var rocketNode = get_node("Rocket")
func load_game():
var save_game = File.new()
if not save_game.file_exists("user://savegame.save"):
return
save_game.open("user://savegame.save", File.READ)
while save_game.get_position() < save_game.get_len():
var data = parse_json(save_game.get_line())
hiscore = data["hiscore"]
numOfCoins = data["numOfCoins"]
func _ready():
load_game()
$CanvasLayer/PauseButton.texture_normal = play_button
$CanvasLayer/PauseButton.connect("pressed", self, "_pause_game")
func _process(delta):
if rocketNode.collided:
lastscore = $"Rocket".lastscore
save_data()
spawnTime = 500/get_viewport_rect().size.x * deltaSpawnTime
time += delta
if time > spawnTime:
asteroidSpawnRepeat += 1
time = 0
if spawnTime > 300/get_viewport_rect().size.x:
deltaSpawnTime *= 0.995
if has_node("Rocket"):
var asteroid = asteroidObj.instance()
add_child(asteroid)
asteroid.position.x = rand_range($Rocket.position.x - get_viewport_rect().size.x/2, $Rocket.position.x + get_viewport_rect().size.x/2)
asteroid.position.y = rand_range($Rocket.position.y - get_viewport_rect().size.y, $Rocket.position.y - 2 * get_viewport_rect().size.y)
asteroid.rotation = rand_range(0, 6.28)
if asteroidSpawnRepeat == 5:
asteroidSpawnRepeat = 0
if has_node("Rocket"):
var coin = coinObj.instance()
add_child(coin)
coin.position.x = rand_range($Rocket.position.x - get_viewport_rect().size.x/2, $Rocket.position.x + get_viewport_rect().size.x/2)
coin.position.y = rand_range($Rocket.position.y - get_viewport_rect().size.y, $Rocket.position.y - 2 * get_viewport_rect().size.y)
func save():
var save_dict = {
"hiscore" : hiscore,
"lastscore" : lastscore,
"numOfCoins" : numOfCoins
}
return save_dict
func _pause_game():
print(get_tree().paused)
get_tree().paused = !get_tree().paused
if (!get_tree().paused):
$CanvasLayer/PauseButton.texture_normal = play_button
else:
$CanvasLayer/PauseButton.texture_normal = pause_button
func save_data():
var save_game = File.new()
save_game.open("user://savegame.save", File.WRITE)
var data = call("save")
save_game.store_line(to_json(data))