From d47f4bfb5dbccb43c09d60d1f2deb4293c394700 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Wed, 4 Nov 2020 10:25:30 -0600 Subject: [PATCH] Add save functionality Now the game creates a save file which stores the amount of coins collected. Still working on storing hi-score --- Game Scenes/Game.tscn | 23 ++++---- Objects/Coin.gd | 6 +- Objects/Coin.tscn | 1 + Objects/Rocket.tscn | 133 +++++++++++++++++++++--------------------- Scripts/CoinLabel.gd | 5 +- Scripts/Game.gd | 11 +++- Scripts/Rocket.gd | 4 +- 7 files changed, 96 insertions(+), 87 deletions(-) diff --git a/Game Scenes/Game.tscn b/Game Scenes/Game.tscn index b095a3f..e0a95ca 100644 --- a/Game Scenes/Game.tscn +++ b/Game Scenes/Game.tscn @@ -15,21 +15,23 @@ position = Vector2( 512, 300 ) scale = Vector2( 2, 2 ) script = ExtResource( 2 ) -[node name="Camera2D" type="Camera2D" parent="Rocket"] -offset = Vector2( 0, -220 ) -current = true -smoothing_enabled = true - -[node name="Coins" type="Label" parent="Rocket/Camera2D"] -margin_left = 166.0 -margin_top = -254.0 -margin_right = 250.0 -margin_bottom = -239.0 +[node name="Coins" type="Label" parent="Rocket"] +margin_left = -41.9442 +margin_top = -56.8347 +margin_right = 42.0558 +margin_bottom = -41.8347 +align = 1 +valign = 1 script = ExtResource( 6 ) __meta__ = { "_edit_use_anchors_": false } +[node name="Camera2D" type="Camera2D" parent="Rocket"] +offset = Vector2( 0, -220 ) +current = true +smoothing_enabled = true + [node name="Score" type="Label" parent="Rocket"] margin_left = 13.0 margin_top = 9.0 @@ -46,7 +48,6 @@ __meta__ = { motion_scale = Vector2( 0.01, 0.01 ) [node name="Background" type="Sprite" parent="ParallaxBackground/ParallaxLayer"] -visible = false position = Vector2( 89.4544, -100.794 ) scale = Vector2( 5, 5 ) texture = ExtResource( 3 ) diff --git a/Objects/Coin.gd b/Objects/Coin.gd index 6a4bc17..497962e 100644 --- a/Objects/Coin.gd +++ b/Objects/Coin.gd @@ -1,7 +1,9 @@ extends Area2D +onready var gameNode = get_node("/root/Game") + func _on_Coin_body_entered(body): if body.name == "Rocket": - var numOfCoins = get_parent().get("numOfCoins") - numOfCoins += 1 + gameNode.numOfCoins += 1 queue_free() + diff --git a/Objects/Coin.tscn b/Objects/Coin.tscn index d756bf8..04f05bc 100644 --- a/Objects/Coin.tscn +++ b/Objects/Coin.tscn @@ -49,5 +49,6 @@ shape = SubResource( 1 ) scale = Vector2( 2, 2 ) frames = SubResource( 7 ) animation = "Spin" +frame = 2 playing = true [connection signal="body_entered" from="." to="." method="_on_Coin_body_entered"] diff --git a/Objects/Rocket.tscn b/Objects/Rocket.tscn index 539d7e1..bc4ae8b 100644 --- a/Objects/Rocket.tscn +++ b/Objects/Rocket.tscn @@ -187,314 +187,314 @@ region = Rect2( 544, 45, 68, 9 ) [sub_resource type="AtlasTexture" id=61] region = Rect2( 612, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=63] +[sub_resource type="AtlasTexture" id=62] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 0, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=64] +[sub_resource type="AtlasTexture" id=63] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 68, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=65] +[sub_resource type="AtlasTexture" id=64] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 136, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=66] +[sub_resource type="AtlasTexture" id=65] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 204, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=67] +[sub_resource type="AtlasTexture" id=66] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 272, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=68] +[sub_resource type="AtlasTexture" id=67] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 340, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=69] +[sub_resource type="AtlasTexture" id=68] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 408, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=70] +[sub_resource type="AtlasTexture" id=69] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 476, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=71] +[sub_resource type="AtlasTexture" id=70] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 544, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=72] +[sub_resource type="AtlasTexture" id=71] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 612, 0, 68, 9 ) -[sub_resource type="AtlasTexture" id=73] +[sub_resource type="AtlasTexture" id=72] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 0, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=74] +[sub_resource type="AtlasTexture" id=73] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 68, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=75] +[sub_resource type="AtlasTexture" id=74] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 136, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=76] +[sub_resource type="AtlasTexture" id=75] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 204, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=77] +[sub_resource type="AtlasTexture" id=76] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 272, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=78] +[sub_resource type="AtlasTexture" id=77] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 340, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=79] +[sub_resource type="AtlasTexture" id=78] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 408, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=80] +[sub_resource type="AtlasTexture" id=79] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 476, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=81] +[sub_resource type="AtlasTexture" id=80] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 544, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=82] +[sub_resource type="AtlasTexture" id=81] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 612, 9, 68, 9 ) -[sub_resource type="AtlasTexture" id=83] +[sub_resource type="AtlasTexture" id=82] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 0, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=84] +[sub_resource type="AtlasTexture" id=83] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 68, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=85] +[sub_resource type="AtlasTexture" id=84] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 136, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=86] +[sub_resource type="AtlasTexture" id=85] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 204, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=87] +[sub_resource type="AtlasTexture" id=86] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 272, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=88] +[sub_resource type="AtlasTexture" id=87] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 340, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=89] +[sub_resource type="AtlasTexture" id=88] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 408, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=90] +[sub_resource type="AtlasTexture" id=89] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 476, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=91] +[sub_resource type="AtlasTexture" id=90] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 544, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=92] +[sub_resource type="AtlasTexture" id=91] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 612, 18, 68, 9 ) -[sub_resource type="AtlasTexture" id=93] +[sub_resource type="AtlasTexture" id=92] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 0, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=94] +[sub_resource type="AtlasTexture" id=93] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 68, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=95] +[sub_resource type="AtlasTexture" id=94] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 136, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=96] +[sub_resource type="AtlasTexture" id=95] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 204, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=97] +[sub_resource type="AtlasTexture" id=96] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 272, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=98] +[sub_resource type="AtlasTexture" id=97] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 340, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=99] +[sub_resource type="AtlasTexture" id=98] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 408, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=100] +[sub_resource type="AtlasTexture" id=99] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 476, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=101] +[sub_resource type="AtlasTexture" id=100] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 544, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=102] +[sub_resource type="AtlasTexture" id=101] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 612, 27, 68, 9 ) -[sub_resource type="AtlasTexture" id=103] +[sub_resource type="AtlasTexture" id=102] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 0, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=104] +[sub_resource type="AtlasTexture" id=103] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 68, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=105] +[sub_resource type="AtlasTexture" id=104] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 136, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=106] +[sub_resource type="AtlasTexture" id=105] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 204, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=107] +[sub_resource type="AtlasTexture" id=106] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 272, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=108] +[sub_resource type="AtlasTexture" id=107] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 340, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=109] +[sub_resource type="AtlasTexture" id=108] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 408, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=110] +[sub_resource type="AtlasTexture" id=109] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 476, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=111] +[sub_resource type="AtlasTexture" id=110] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 544, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=112] +[sub_resource type="AtlasTexture" id=111] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 612, 36, 68, 9 ) -[sub_resource type="AtlasTexture" id=113] +[sub_resource type="AtlasTexture" id=112] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 0, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=114] +[sub_resource type="AtlasTexture" id=113] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 68, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=115] +[sub_resource type="AtlasTexture" id=114] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 136, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=116] +[sub_resource type="AtlasTexture" id=115] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 204, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=117] +[sub_resource type="AtlasTexture" id=116] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 272, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=118] +[sub_resource type="AtlasTexture" id=117] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 340, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=119] +[sub_resource type="AtlasTexture" id=118] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 408, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=120] +[sub_resource type="AtlasTexture" id=119] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 476, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=121] +[sub_resource type="AtlasTexture" id=120] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 544, 45, 68, 9 ) -[sub_resource type="AtlasTexture" id=122] +[sub_resource type="AtlasTexture" id=121] flags = 4 atlas = ExtResource( 1 ) region = Rect2( 612, 45, 68, 9 ) -[sub_resource type="SpriteFrames" id=62] +[sub_resource type="SpriteFrames" id=122] animations = [ { "frames": [ SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ), SubResource( 7 ), SubResource( 8 ), SubResource( 9 ), SubResource( 10 ), SubResource( 11 ), SubResource( 12 ), SubResource( 13 ), SubResource( 14 ), SubResource( 15 ), SubResource( 16 ), SubResource( 17 ), SubResource( 18 ), SubResource( 19 ), SubResource( 20 ), SubResource( 21 ), SubResource( 22 ), SubResource( 23 ), SubResource( 24 ), SubResource( 25 ), SubResource( 26 ), SubResource( 27 ), SubResource( 28 ), SubResource( 29 ), SubResource( 30 ), SubResource( 31 ), SubResource( 32 ), SubResource( 33 ), SubResource( 34 ), SubResource( 35 ), SubResource( 36 ), SubResource( 37 ), SubResource( 38 ), SubResource( 39 ), SubResource( 40 ), SubResource( 41 ), SubResource( 42 ), SubResource( 43 ), SubResource( 44 ), SubResource( 45 ), SubResource( 46 ), SubResource( 47 ), SubResource( 48 ), SubResource( 49 ), SubResource( 50 ), SubResource( 51 ), SubResource( 52 ), SubResource( 53 ), SubResource( 54 ), SubResource( 55 ), SubResource( 56 ), SubResource( 57 ), SubResource( 58 ), SubResource( 59 ), SubResource( 60 ), SubResource( 61 ) ], "loop": true, "name": "default", "speed": 60.0 }, { -"frames": [ SubResource( 63 ), SubResource( 64 ), SubResource( 65 ), SubResource( 66 ), SubResource( 67 ), SubResource( 68 ), SubResource( 69 ), SubResource( 70 ), SubResource( 71 ), SubResource( 72 ), SubResource( 73 ), SubResource( 74 ), SubResource( 75 ), SubResource( 76 ), SubResource( 77 ), SubResource( 78 ), SubResource( 79 ), SubResource( 80 ), SubResource( 81 ), SubResource( 82 ), SubResource( 83 ), SubResource( 84 ), SubResource( 85 ), SubResource( 86 ), SubResource( 87 ), SubResource( 88 ), SubResource( 89 ), SubResource( 90 ), SubResource( 91 ), SubResource( 92 ), SubResource( 93 ), SubResource( 94 ), SubResource( 95 ), SubResource( 96 ), SubResource( 97 ), SubResource( 98 ), SubResource( 99 ), SubResource( 100 ), SubResource( 101 ), SubResource( 102 ), SubResource( 103 ), SubResource( 104 ), SubResource( 105 ), SubResource( 106 ), SubResource( 107 ), SubResource( 108 ), SubResource( 109 ), SubResource( 110 ), SubResource( 111 ), SubResource( 112 ), SubResource( 113 ), SubResource( 114 ), SubResource( 115 ), SubResource( 116 ), SubResource( 117 ), SubResource( 118 ), SubResource( 119 ), SubResource( 120 ), SubResource( 121 ), SubResource( 122 ) ], +"frames": [ SubResource( 62 ), SubResource( 63 ), SubResource( 64 ), SubResource( 65 ), SubResource( 66 ), SubResource( 67 ), SubResource( 68 ), SubResource( 69 ), SubResource( 70 ), SubResource( 71 ), SubResource( 72 ), SubResource( 73 ), SubResource( 74 ), SubResource( 75 ), SubResource( 76 ), SubResource( 77 ), SubResource( 78 ), SubResource( 79 ), SubResource( 80 ), SubResource( 81 ), SubResource( 82 ), SubResource( 83 ), SubResource( 84 ), SubResource( 85 ), SubResource( 86 ), SubResource( 87 ), SubResource( 88 ), SubResource( 89 ), SubResource( 90 ), SubResource( 91 ), SubResource( 92 ), SubResource( 93 ), SubResource( 94 ), SubResource( 95 ), SubResource( 96 ), SubResource( 97 ), SubResource( 98 ), SubResource( 99 ), SubResource( 100 ), SubResource( 101 ), SubResource( 102 ), SubResource( 103 ), SubResource( 104 ), SubResource( 105 ), SubResource( 106 ), SubResource( 107 ), SubResource( 108 ), SubResource( 109 ), SubResource( 110 ), SubResource( 111 ), SubResource( 112 ), SubResource( 113 ), SubResource( 114 ), SubResource( 115 ), SubResource( 116 ), SubResource( 117 ), SubResource( 118 ), SubResource( 119 ), SubResource( 120 ), SubResource( 121 ) ], "loop": true, "name": "New Anim", "speed": 45.0 @@ -503,18 +503,17 @@ animations = [ { [node name="Rocket" type="KinematicBody2D"] [node name="Collider" type="CollisionShape2D" parent="."] -position = Vector2( 0, -13.901 ) +position = Vector2( 0, -5 ) shape = SubResource( 1 ) [node name="Body" type="Sprite" parent="."] -position = Vector2( 9.53674e-07, -9.2112 ) scale = Vector2( 0.263104, 0.474027 ) texture = ExtResource( 2 ) [node name="Flame" type="AnimatedSprite" parent="."] -position = Vector2( 0, 40 ) +position = Vector2( 0, 47.5 ) rotation = 1.5708 -frames = SubResource( 62 ) +frames = SubResource( 122 ) animation = "New Anim" -frame = 2 +frame = 6 playing = true diff --git a/Scripts/CoinLabel.gd b/Scripts/CoinLabel.gd index 582dab5..313e20d 100644 --- a/Scripts/CoinLabel.gd +++ b/Scripts/CoinLabel.gd @@ -1,5 +1,6 @@ extends Label +onready var gameNode = get_node("/root/Game") + func _process(delta): - var coins = str(get_parent().get_parent().get_parent().get("numOfCoins")) - text = coins + text = str(gameNode.numOfCoins) diff --git a/Scripts/Game.gd b/Scripts/Game.gd index 174167f..aa1a56e 100644 --- a/Scripts/Game.gd +++ b/Scripts/Game.gd @@ -8,6 +8,7 @@ var deltaSpawnTime = 1 var asteroidSpawnRepeat = 0 var hiscore = 0 var numOfCoins = 0 +onready var rocketNode = get_node("Rocket") func load_game(): var save_game = File.new() @@ -16,13 +17,17 @@ func load_game(): 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()) - var hiscore = data["hiscore"] - var numOfCoins = data["numOfCoins"] + hiscore = data["hiscore"] + numOfCoins = data["numOfCoins"] + +func _ready(): + load_game() func _process(delta): + if rocketNode.collided: + save_data() spawnTime = 500/get_viewport_rect().size.x * deltaSpawnTime time += delta - #print (numOfCoins) if time > spawnTime: asteroidSpawnRepeat += 1 time = 0 diff --git a/Scripts/Rocket.gd b/Scripts/Rocket.gd index efa6eba..feb79d9 100644 --- a/Scripts/Rocket.gd +++ b/Scripts/Rocket.gd @@ -4,16 +4,16 @@ var angle = 0 var difficulty var deltaDifficulty = 0 var explosionObj = load("res://Objects/Explosion.tscn") +var collided = false func _process(delta): difficulty = 600 + deltaDifficulty difficulty += 0.0005 var collision = move_and_collide(Vector2(difficulty * sin(angle) * delta, -difficulty * cos(angle) * delta)) if collision: + collided = true hide() - #scale = Vector2(0.01, 0.01) var explosion = explosionObj.instance() - #add_child(explosion) get_parent().add_child(explosion) explosion.position = position get_node("Collider").queue_free()