Implement a basic clicker game.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
da40b38fd2
commit
4bf15fdd97
|
@ -0,0 +1,44 @@
|
|||
extends Node2D
|
||||
|
||||
var player_score = 0
|
||||
var player_name = "Unknown"
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
player_name = get_player_name()
|
||||
update_player_text()
|
||||
|
||||
# Update the score text when the score changes
|
||||
func update_score_text():
|
||||
var score_text = "Score: {score}".format({"score": player_score})
|
||||
$ButtonContainer/PlayerScore.text = score_text
|
||||
|
||||
func update_player_text():
|
||||
var player_text = "Hello {name}".format({"name": player_name})
|
||||
$ButtonContainer/PlayerName.text = player_text
|
||||
|
||||
func _on_ClickerButton_pressed():
|
||||
player_score += 5
|
||||
update_score_text()
|
||||
|
||||
# Are we running in the web?
|
||||
func is_web_env():
|
||||
return OS.has_feature('JavaScript')
|
||||
|
||||
# Get the player name from the HTML shell
|
||||
func get_player_name():
|
||||
var name = "Unknown"
|
||||
if is_web_env():
|
||||
name = JavaScript.eval("getPlayerName()")
|
||||
return name
|
||||
|
||||
func exit_game(score):
|
||||
if is_web_env():
|
||||
JavaScript.eval("finishGame({score})".format({"score": score}))
|
||||
else:
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
func _on_QuitButton_pressed():
|
||||
exit_game(player_score)
|
52
Game.tscn
52
Game.tscn
|
@ -1,3 +1,53 @@
|
|||
[gd_scene format=2]
|
||||
[gd_scene load_steps=5 format=2]
|
||||
|
||||
[ext_resource path="res://kenpixel_future.ttf" type="DynamicFontData" id=1]
|
||||
[ext_resource path="res://Game.gd" type="Script" id=2]
|
||||
|
||||
[sub_resource type="DynamicFont" id=1]
|
||||
size = 32
|
||||
font_data = ExtResource( 1 )
|
||||
|
||||
[sub_resource type="DynamicFont" id=2]
|
||||
size = 20
|
||||
font_data = ExtResource( 1 )
|
||||
|
||||
[node name="MainWindow" type="Node2D"]
|
||||
script = ExtResource( 2 )
|
||||
|
||||
[node name="ButtonContainer" type="VBoxContainer" parent="."]
|
||||
margin_left = 247.0
|
||||
margin_top = 194.0
|
||||
margin_right = 838.0
|
||||
margin_bottom = 402.0
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
|
||||
[node name="PlayerName" type="Label" parent="ButtonContainer"]
|
||||
margin_right = 591.0
|
||||
margin_bottom = 40.0
|
||||
custom_fonts/font = SubResource( 1 )
|
||||
text = "Hello Player"
|
||||
|
||||
[node name="PlayerScore" type="Label" parent="ButtonContainer"]
|
||||
margin_top = 44.0
|
||||
margin_right = 591.0
|
||||
margin_bottom = 84.0
|
||||
custom_fonts/font = SubResource( 1 )
|
||||
text = "Score: 0"
|
||||
|
||||
[node name="ClickerButton" type="Button" parent="ButtonContainer"]
|
||||
margin_top = 88.0
|
||||
margin_right = 591.0
|
||||
margin_bottom = 119.0
|
||||
custom_fonts/font = SubResource( 2 )
|
||||
text = "Click Me!"
|
||||
|
||||
[node name="QuitButton" type="Button" parent="ButtonContainer"]
|
||||
margin_top = 123.0
|
||||
margin_right = 591.0
|
||||
margin_bottom = 154.0
|
||||
custom_fonts/font = SubResource( 2 )
|
||||
text = "I'm Done"
|
||||
[connection signal="pressed" from="ButtonContainer/ClickerButton" to="." method="_on_ClickerButton_pressed"]
|
||||
[connection signal="pressed" from="ButtonContainer/QuitButton" to="." method="_on_QuitButton_pressed"]
|
||||
|
|
|
@ -26,3 +26,7 @@ Godot devs) can be found between the `<!-- Start of Custom Game block -->` and
|
|||
[js-from-godot]: https://docs.godotengine.org/en/stable/getting_started/workflow/export/exporting_for_web.html#calling-javascript-from-script
|
||||
[custom-html-shell]: https://docs.godotengine.org/en/stable/tutorials/platform/customizing_html5_shell.html#doc-customizing-html5-shell
|
||||
[custom-fixed-size-template]: https://github.com/godotengine/godot/blob/master/misc/dist/html/fixed-size.html
|
||||
|
||||
## Credits
|
||||
|
||||
* [Fonts by Kenney](https://opengameart.org/content/kenney-fonts)
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue