work for lesson 8: finishing up

This commit is contained in:
Emmet
2024-11-25 15:25:41 -06:00
parent 7edd9837f2
commit e7bfcf5fa2
12 changed files with 112 additions and 40 deletions

11
game_over_screen.gd Normal file
View File

@ -0,0 +1,11 @@
extends Node2D
func _ready() -> void:
$CanvasLayer/VBoxContainer/Label2.text = "YOUR SCORE: "+str(Global.score)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
if Input.is_action_just_pressed("laser"):
Global.score = 0
get_tree().change_scene_to_file("res://level.tscn")

View File

@ -1,8 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://cngln1r230py"]
[gd_scene load_steps=3 format=3 uid="uid://cngln1r230py"]
[ext_resource type="Script" path="res://game_over_screen.gd" id="1_gjpts"]
[ext_resource type="PackedScene" uid="uid://c23nfyyeioxcv" path="res://bg.tscn" id="1_xlemo"]
[node name="GameOverScreen" type="Node2D"]
script = ExtResource("1_gjpts")
[node name="CanvasLayer" type="CanvasLayer" parent="."]

3
global.gd Normal file
View File

@ -0,0 +1,3 @@
extends Node
var score := 0

View File

@ -8,6 +8,8 @@ radius = 5.0
height = 54.0
[node name="Laser" type="Area2D"]
collision_layer = 2
collision_mask = 0
script = ExtResource("1_je83x")
[node name="Sprite2D" type="Sprite2D" parent="."]

View File

@ -5,6 +5,8 @@ var laser_scene: PackedScene = load("res://laser.tscn")
var star_scene: PackedScene = load("res://star.tscn")
var num_stars = 100
var health = 3
func _ready():
var rng := RandomNumberGenerator.new()
var width = get_viewport().get_visible_rect().size[0]
@ -23,6 +25,14 @@ func _on_meteor_timer_timeout() -> void:
var meteor = meteor_scene.instantiate()
$Meteors.add_child(meteor)
meteor.connect('collision',_on_meteor_collision)
func _on_meteor_collision():
health -= 1
get_tree().call_group('ui','set_health',health)
if health == 0:
get_tree().change_scene_to_file("res://game_over_screen.tscn")
func _on_player_laser(pos) -> void:
var laser = laser_scene.instantiate()
$Lasers.add_child(laser)

View File

@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://level.gd" id="1_6hg4u"]
[ext_resource type="PackedScene" uid="uid://cxw7wbwterb7u" path="res://player.tscn" id="1_48d8j"]
[ext_resource type="PackedScene" uid="uid://c23nfyyeioxcv" path="res://bg.tscn" id="2_gvq1c"]
[ext_resource type="Texture2D" uid="uid://mw23y37wfrjm" path="res://assets/PNG/UI/playerLife1_blue.png" id="3_kfkpw"]
[ext_resource type="PackedScene" uid="uid://d2kq441mv37fr" path="res://ui.tscn" id="4_53jg4"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_abc51"]
size = Vector2(40, 1080)
@ -27,6 +27,7 @@ metadata/_edit_lock_ = true
metadata/_edit_lock_ = true
[node name="StaticBody2D" type="StaticBody2D" parent="Borders"]
collision_mask = 0
[node name="Right Wall" type="CollisionShape2D" parent="Borders/StaticBody2D"]
position = Vector2(1900, 540)
@ -53,43 +54,7 @@ autostart = true
[node name="Lasers" type="Node2D" parent="."]
metadata/_edit_lock_ = true
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="MarginContainer" type="MarginContainer" parent="CanvasLayer"]
anchors_preset = 1
anchor_left = 1.0
anchor_right = 1.0
offset_left = -40.0
offset_bottom = 85.0
grow_horizontal = 0
theme_override_constants/margin_top = 16
[node name="Label" type="Label" parent="CanvasLayer/MarginContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 50
text = "0"
horizontal_alignment = 2
[node name="MarginContainer2" type="MarginContainer" parent="CanvasLayer"]
offset_right = 40.0
offset_bottom = 40.0
theme_override_constants/margin_left = 16
theme_override_constants/margin_top = 16
[node name="HBoxContainer" type="HBoxContainer" parent="CanvasLayer/MarginContainer2"]
layout_mode = 2
theme_override_constants/separation = 12
[node name="TextureRect" type="TextureRect" parent="CanvasLayer/MarginContainer2/HBoxContainer"]
layout_mode = 2
texture = ExtResource("3_kfkpw")
stretch_mode = 3
[node name="Label" type="Label" parent="CanvasLayer/MarginContainer2/HBoxContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 30
text = "3"
vertical_alignment = 1
[node name="UI" parent="." instance=ExtResource("4_53jg4")]
[connection signal="laser" from="Player" to="." method="_on_player_laser"]
[connection signal="timeout" from="MeteorTimer" to="." method="_on_meteor_timer_timeout"]

View File

@ -3,6 +3,8 @@ extends Area2D
var velocity
var angular_velocity
signal collision
func _ready() -> void:
var rng := RandomNumberGenerator.new()
var width = get_viewport().get_visible_rect().size[0]
@ -28,4 +30,10 @@ func _process(delta):
rotation += angular_velocity * delta
func _on_body_entered(body:Node2D) -> void:
pass # Replace with function body.
collision.emit()
func _on_area_entered(area: Area2D) -> void:
Global.score += 150
area.queue_free()
queue_free()

View File

@ -4,6 +4,8 @@
[ext_resource type="Texture2D" uid="uid://baifmn2j7f820" path="res://assets/PNG/Meteors/meteorBrown_big1.png" id="1_m5pls"]
[node name="Meteor" type="Area2D"]
collision_layer = 0
collision_mask = 2
script = ExtResource("1_hsdcx")
[node name="Sprite" type="Sprite2D" parent="."]
@ -12,4 +14,5 @@ texture = ExtResource("1_m5pls")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
polygon = PackedVector2Array(-33, -40, 23, -40, 48, -1, 34, 31, 10, 29, -23, 40, -50, 10)
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@ -4,6 +4,7 @@
[ext_resource type="Script" path="res://player.gd" id="1_lh8st"]
[node name="Player" type="CharacterBody2D"]
collision_layer = 2
script = ExtResource("1_lh8st")
[node name="PlayerImage" type="Sprite2D" parent="."]

View File

@ -14,12 +14,20 @@ config/name="Space Shooter"
run/main_scene="res://level.tscn"
config/features=PackedStringArray("4.3", "Forward Plus")
[autoload]
Global="*res://global.gd"
[display]
window/size/viewport_width=1920
window/size/viewport_height=1080
window/size/mode=3
[global_group]
ui=""
[input]
left={

10
ui.gd Normal file
View File

@ -0,0 +1,10 @@
extends CanvasLayer
func set_health(health):
print(health)
$HealthMarginContainer/HealthBox/HealthLabel.text = str(health)
func _on_score_timer_timeout() -> void:
Global.score += 100
$ScoreMarginContainer/ScoreLabel.text = str(Global.score)

49
ui.tscn Normal file
View File

@ -0,0 +1,49 @@
[gd_scene load_steps=3 format=3 uid="uid://d2kq441mv37fr"]
[ext_resource type="Texture2D" uid="uid://mw23y37wfrjm" path="res://assets/PNG/UI/playerLife1_blue.png" id="1_6jkth"]
[ext_resource type="Script" path="res://ui.gd" id="1_vrtns"]
[node name="UI" type="CanvasLayer" groups=["ui"]]
script = ExtResource("1_vrtns")
[node name="ScoreMarginContainer" type="MarginContainer" parent="."]
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -20.0
offset_right = 20.0
offset_bottom = 85.0
grow_horizontal = 2
theme_override_constants/margin_top = 16
[node name="ScoreLabel" type="Label" parent="ScoreMarginContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 50
text = "0"
horizontal_alignment = 1
[node name="HealthMarginContainer" type="MarginContainer" parent="."]
offset_right = 40.0
offset_bottom = 40.0
theme_override_constants/margin_left = 16
theme_override_constants/margin_top = 16
[node name="HealthBox" type="HBoxContainer" parent="HealthMarginContainer"]
layout_mode = 2
theme_override_constants/separation = 12
[node name="HealthSymbol" type="TextureRect" parent="HealthMarginContainer/HealthBox"]
layout_mode = 2
texture = ExtResource("1_6jkth")
stretch_mode = 3
[node name="HealthLabel" type="Label" parent="HealthMarginContainer/HealthBox"]
layout_mode = 2
theme_override_font_sizes/font_size = 30
text = "3"
vertical_alignment = 1
[node name="ScoreTimer" type="Timer" parent="."]
autostart = true
[connection signal="timeout" from="ScoreTimer" to="." method="_on_score_timer_timeout"]