Added win/loss conditions

This commit is contained in:
Terry Hearst 2022-02-20 17:30:03 -05:00
parent b25ea62fe4
commit da4e4d9b28
2 changed files with 101 additions and 21 deletions

View file

@ -1,4 +1,4 @@
extends CenterContainer extends Control
const letter_count := 5 const letter_count := 5
@ -16,13 +16,15 @@ var letters := []
var target_word: String var target_word: String
var current_guess: int var current_guess: int
onready var error_text_color_default = $C/V/V/ErrorText.get("custom_colors/font_color")
func _ready() -> void: func _ready() -> void:
for _i in range(guess_count): for _i in range(guess_count):
var letter_array := [] var letter_array := []
for _j in range(letter_count): for _j in range(letter_count):
var letter := Letter.instance() var letter := Letter.instance()
letter_array.append(letter) letter_array.append(letter)
$V/LetterGrid.add_child(letter) $C/V/LetterGrid.add_child(letter)
letters.append(letter_array) letters.append(letter_array)
target_word = "godot".to_upper() target_word = "godot".to_upper()
@ -44,12 +46,15 @@ func guess_entered(guess: String) -> void:
return return
guess = guess.to_upper() guess = guess.to_upper()
if guess.length() != letter_count: if guess.length() != letter_count:
show_error("Word must be five characters.")
return return
var letters_remaining = [] var letters_remaining = []
for letter in target_word: for letter in target_word:
letters_remaining.append(letter) letters_remaining.append(letter)
hide_error()
# Mark all greens # Mark all greens
for i in range(letter_count): for i in range(letter_count):
var guess_letter := guess[i] var guess_letter := guess[i]
@ -65,8 +70,6 @@ func guess_entered(guess: String) -> void:
letters_remaining.remove(j) letters_remaining.remove(j)
break break
print(letters_remaining)
# Mark all yellows (and grays) # Mark all yellows (and grays)
for i in range(letter_count): for i in range(letter_count):
var letter_instance := letters[current_guess][i] as ColorRect var letter_instance := letters[current_guess][i] as ColorRect
@ -85,7 +88,37 @@ func guess_entered(guess: String) -> void:
else: else:
letter_instance.color = color_incorrect letter_instance.color = color_incorrect
current_guess += 1 current_guess += 1
var guess_text_node := find_node("GuessText") as LineEdit var guess_text_node := find_node("GuessText") as LineEdit
guess_text_node.text = "" guess_text_node.text = ""
var ended := false
var won := false
if guess == target_word:
ended = true
won = true
elif current_guess >= guess_count:
ended = true
if ended:
find_node("GuessButton").disabled = true
find_node("GuessText").editable = false
if won:
show_error("Congrats! You won!", Color(0.4, 0.9, 0.6))
else:
show_error("Game Over. The word was %s" % target_word, error_text_color_default)
func show_error(text: String, color = null):
$C/V/V/ErrorText.text = text
if typeof(color) == TYPE_COLOR:
$ErrorFadeOut.stop()
$C/V/V/ErrorText.add_color_override("font_color", color)
else:
$ErrorFadeOut.play("ErrorFadeOut")
func hide_error():
$C/V/V/ErrorText.text = ""
$ErrorFadeOut.stop()

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=4 format=2] [gd_scene load_steps=6 format=2]
[ext_resource path="res://fonts/Louis George Cafe Bold.ttf" type="DynamicFontData" id=1] [ext_resource path="res://fonts/Louis George Cafe Bold.ttf" type="DynamicFontData" id=1]
[ext_resource path="res://src/Main.gd" type="Script" id=2] [ext_resource path="res://src/Main.gd" type="Script" id=2]
@ -7,7 +7,27 @@
size = 72 size = 72
font_data = ExtResource( 1 ) font_data = ExtResource( 1 )
[node name="Main" type="CenterContainer"] [sub_resource type="DynamicFont" id=2]
size = 24
font_data = ExtResource( 1 )
[sub_resource type="Animation" id=3]
resource_name = "ErrorFadeOut"
length = 6.0
tracks/0/type = "value"
tracks/0/path = NodePath("C/V/V/ErrorText:custom_colors/font_color")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/keys = {
"times": PoolRealArray( 0, 5, 6 ),
"transitions": PoolRealArray( 1, 1, 1 ),
"update": 0,
"values": [ Color( 1, 0.31, 0.31, 1 ), Color( 1, 0.31, 0.31, 1 ), Color( 1, 0.309804, 0.309804, 0 ) ]
}
[node name="Main" type="Control"]
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
script = ExtResource( 2 ) script = ExtResource( 2 )
@ -18,21 +38,31 @@ color_incorrect = Color( 0.258824, 0.258824, 0.301961, 1 )
color_misplaced = Color( 0.698039, 0.74902, 0, 1 ) color_misplaced = Color( 0.698039, 0.74902, 0, 1 )
color_correct = Color( 0, 0.619608, 0.298039, 1 ) color_correct = Color( 0, 0.619608, 0.298039, 1 )
[node name="V" type="VBoxContainer" parent="."] [node name="C" type="CenterContainer" parent="."]
margin_left = 90.0 anchor_right = 1.0
margin_top = 393.0 anchor_bottom = 1.0
margin_right = 550.0 __meta__ = {
margin_bottom = 566.0 "_edit_use_anchors_": false
custom_constants/separation = 20 }
[node name="Title" type="Label" parent="V"] [node name="V" type="VBoxContainer" parent="C"]
margin_left = 90.0
margin_top = 377.0
margin_right = 550.0
margin_bottom = 582.0
custom_constants/separation = 20
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Title" type="Label" parent="C/V"]
margin_right = 460.0 margin_right = 460.0
margin_bottom = 82.0 margin_bottom = 82.0
custom_fonts/font = SubResource( 1 ) custom_fonts/font = SubResource( 1 )
text = "Godot Wordle" text = "Godot Wordle"
align = 1 align = 1
[node name="LetterGrid" type="GridContainer" parent="V"] [node name="LetterGrid" type="GridContainer" parent="C/V"]
margin_left = 230.0 margin_left = 230.0
margin_top = 102.0 margin_top = 102.0
margin_right = 230.0 margin_right = 230.0
@ -43,23 +73,40 @@ custom_constants/vseparation = 8
custom_constants/hseparation = 8 custom_constants/hseparation = 8
columns = 5 columns = 5
[node name="H" type="HBoxContainer" parent="V"] [node name="V" type="VBoxContainer" parent="C/V"]
margin_top = 122.0 margin_top = 122.0
margin_right = 460.0 margin_right = 460.0
margin_bottom = 173.0 margin_bottom = 205.0
[node name="ErrorText" type="Label" parent="C/V/V"]
margin_right = 460.0
margin_bottom = 28.0
custom_colors/font_color = Color( 1, 0.31, 0.31, 1 )
custom_fonts/font = SubResource( 2 )
align = 1
[node name="H" type="HBoxContainer" parent="C/V/V"]
margin_top = 32.0
margin_right = 460.0
margin_bottom = 83.0
custom_constants/separation = 12 custom_constants/separation = 12
[node name="GuessText" type="LineEdit" parent="V/H"] [node name="GuessText" type="LineEdit" parent="C/V/V/H"]
margin_right = 334.0 margin_right = 334.0
margin_bottom = 51.0 margin_bottom = 51.0
size_flags_horizontal = 3 size_flags_horizontal = 3
max_length = 5 max_length = 5
caret_blink = true
caret_blink_speed = 0.5
[node name="GuessButton" type="Button" parent="V/H"] [node name="GuessButton" type="Button" parent="C/V/V/H"]
margin_left = 346.0 margin_left = 346.0
margin_right = 460.0 margin_right = 460.0
margin_bottom = 51.0 margin_bottom = 51.0
text = "Guess" text = "Guess"
[connection signal="text_entered" from="V/H/GuessText" to="." method="_on_GuessText_text_entered"] [node name="ErrorFadeOut" type="AnimationPlayer" parent="."]
[connection signal="pressed" from="V/H/GuessButton" to="." method="_on_GuessButton_pressed"] anims/ErrorFadeOut = SubResource( 3 )
[connection signal="text_entered" from="C/V/V/H/GuessText" to="." method="_on_GuessText_text_entered"]
[connection signal="pressed" from="C/V/V/H/GuessButton" to="." method="_on_GuessButton_pressed"]