Skip to content

Commit

Permalink
Merge pull request #2 from amalija-ramljak/dajana-question_card
Browse files Browse the repository at this point in the history
Dajana question card
  • Loading branch information
amalija-ramljak authored May 9, 2020
2 parents 61caff1 + 209ac75 commit d929e60
Show file tree
Hide file tree
Showing 15 changed files with 408 additions and 11 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export_presets.cfg
# Mono-specific ignores
.mono/
data_*/
.DS_Store
Binary file added fonts/.DS_Store
Binary file not shown.
Binary file added leveldata/.DS_Store
Binary file not shown.
27 changes: 27 additions & 0 deletions leveldata/level1_questions_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"0": {
"text": "Ovo je tekst prvog pitanja?",
"correctAnswer": "0",
"answers": ["odg a", "odg b", "odg c", "odg d"]
},
"1": {
"text": "Ovo je tekst drugog pitanja?",
"correctAnswer": "1",
"answers": ["odg a", "odg b", "odg c", "odg d"]
},
"2": {
"text": "Ovo je tekst treceg pitanja?",
"correctAnswer": "2",
"answers": ["odg a", "odg b", "odg c", "odg d"]
},
"3": {
"text": "Ovo je tekst cetvrtog pitanja?",
"correctAnswer": "3",
"answers": ["odg a", "odg b", "odg c", "odg d"]
},
"4": {
"text": "Ovo je tekst petog pitanja?",
"correctAnswer": "0",
"answers": ["odg a", "odg b", "odg c", "odg d"]
}
}
22 changes: 22 additions & 0 deletions leveldata/level1_situations_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"0": {
"text": "Ovo je tekst prve situacije",
"consequence": 1
},
"1": {
"text": "Ovo je tekst druge situacije",
"consequence": 0
},
"2": {
"text": "Ovo je tekst trece situacije",
"consequence": 0
},
"3": {
"text": "Ovo je tekst cetvrte situacije",
"consequence": 1
},
"4": {
"text": "Ovo je tekst pete situacije",
"consequence": 1
}
}
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ window/stretch/aspect="expand"

[rendering]

environment/default_clear_color=Color( 0.505882, 0.913725, 0.686275, 1 )
environment/default_clear_color=Color( 0.878431, 0.678431, 0.556863, 1 )
environment/default_environment="res://default_env.tres"
Binary file added scenes/.DS_Store
Binary file not shown.
21 changes: 11 additions & 10 deletions scenes/components/LevelSelector.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
[ext_resource path="res://fonts/QuickSand/Quicksand-Regular.ttf" type="DynamicFontData" id=1]
[ext_resource path="res://scenes/components/LevelSelector.gd" type="Script" id=2]

[sub_resource type="StyleBoxFlat" id=4]
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.458824, 0.458824, 0.458824, 1 )

[sub_resource type="StyleBoxFlat" id=5]
[sub_resource type="StyleBoxFlat" id=2]
bg_color = Color( 0.188235, 0.188235, 0.188235, 1 )

[sub_resource type="DynamicFont" id=1]
[sub_resource type="DynamicFont" id=3]
size = 32
extra_spacing_char = 2
font_data = ExtResource( 1 )

[sub_resource type="DynamicFont" id=2]
[sub_resource type="DynamicFont" id=4]
size = 14
font_data = ExtResource( 1 )

[sub_resource type="DynamicFont" id=3]
[sub_resource type="DynamicFont" id=5]
size = 20
font_data = ExtResource( 1 )

Expand All @@ -29,8 +29,9 @@ margin_top = 150.0
margin_right = -20.0
margin_bottom = 250.0
rect_scale = Vector2( 0.99872, 1 )
custom_styles/disabled = SubResource( 4 )
custom_styles/normal = SubResource( 5 )
custom_styles/disabled = SubResource( 1 )
custom_styles/normal = SubResource( 2 )
action_mode = 0
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
Expand All @@ -54,7 +55,7 @@ margin_bottom = 99.0
margin_right = 420.0
margin_bottom = 45.0
rect_min_size = Vector2( 420, 45 )
custom_fonts/font = SubResource( 1 )
custom_fonts/font = SubResource( 3 )
text = " Level"
valign = 1

Expand All @@ -63,7 +64,7 @@ margin_top = 49.0
margin_right = 420.0
margin_bottom = 99.0
rect_min_size = Vector2( 420, 50 )
custom_fonts/font = SubResource( 2 )
custom_fonts/font = SubResource( 4 )
text = " Prođena sva pitanja: 0
Trenutno odgovorena pitanja: 0/0"
valign = 1
Expand All @@ -75,7 +76,7 @@ margin_bottom = 99.0
rect_min_size = Vector2( 100, 0 )
size_flags_horizontal = 8
size_flags_vertical = 1
custom_fonts/font = SubResource( 3 )
custom_fonts/font = SubResource( 5 )
text = "Highscore:
0"
align = 1
Expand Down
80 changes: 80 additions & 0 deletions scenes/components/QuestionCard.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
extends Node
export (PackedScene) var QuestionFeedbackCard

var AnswersButtonGroup
var question
var questions
var unansweredKeys
var answeredKeys = Array()
var randomIndex
var randomQuestionKey
var answerIndex = [0, 1, 2, 3]

func _ready():
#group buttons so they act as radio buttons
AnswersButtonGroup = ButtonGroup.new()
$Answers/optionA.set_button_group(AnswersButtonGroup)
$Answers/optionB.set_button_group(AnswersButtonGroup)
$Answers/optionC.set_button_group(AnswersButtonGroup)
$Answers/optionD.set_button_group(AnswersButtonGroup)

#load json file with questions and parse it to the questions dictionary
questions = self.load("res://leveldata/level1_questions_list.json")
questions = JSON.parse(questions).result
#fill unansweredKeys array with questions dictionary keys
unansweredKeys = questions.keys()

#ensure that a new seed will be used each time (if you want non-reproducible shuffling)
randomize()
#generate random key for the questions dictionary
randomIndex = randi()%unansweredKeys.size()

#select randomly chosen question from the questions dictionary
randomQuestionKey = unansweredKeys[randomIndex]
question = questions[str(randomQuestionKey)]

#shuffle answers order
answerIndex.shuffle()

set_question_txt(question.text)
set_answers_txt()

func load(var path):
var file = File.new()
file.open(path, File.READ)
var content = file.get_as_text()
file.close()
return content

func set_question_txt(var txt):
$QuestionText.text = " %s" % txt

func set_answers_txt():
$Answers/optionA.text = " %s" % question.answers[answerIndex[0]]
$Answers/optionB.text = " %s" % question.answers[answerIndex[1]]
$Answers/optionC.text = " %s" % question.answers[answerIndex[2]]
$Answers/optionD.text = " %s" % question.answers[answerIndex[3]]

func _on_Button_pressed():
queue_free()
var question_feedback = preload("res://scenes/components/QuestionFeedbackCard.tscn").instance()
self.get_parent().add_child(question_feedback)
#selected correct answer
if ((AnswersButtonGroup.get_pressed_button()==$Answers/optionA && str(answerIndex[0])==question.correctAnswer)
|| (AnswersButtonGroup.get_pressed_button()==$Answers/optionB && str(answerIndex[1])==question.correctAnswer)
|| (AnswersButtonGroup.get_pressed_button()==$Answers/optionC && str(answerIndex[2])==question.correctAnswer)
|| (AnswersButtonGroup.get_pressed_button()==$Answers/optionD && str(answerIndex[3])==question.correctAnswer)):
question_feedback.positive_feedback()

#if answer is correct, remove question key from the unansweredKeys array
unansweredKeys.remove(randomIndex)
#...and add it to the answeredKeys array
answeredKeys.append(randomQuestionKey)

#if unansweredKeys array is empty, refill it
if(unansweredKeys.empty()):
unansweredKeys=answeredKeys
answeredKeys.clear()
#selected wrong answer or no answer selected
else:
question_feedback.negative_feedback()
79 changes: 79 additions & 0 deletions scenes/components/QuestionCard.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[gd_scene load_steps=6 format=2]

[ext_resource path="res://scenes/components/QuestionCard.gd" type="Script" id=2]

[sub_resource type="ButtonGroup" id=1]
resource_local_to_scene = false

[sub_resource type="ButtonGroup" id=2]
resource_local_to_scene = false

[sub_resource type="ButtonGroup" id=3]

[sub_resource type="ButtonGroup" id=4]
resource_local_to_scene = false

[node name="QuestionCard" type="Panel"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -189.0
margin_top = -168.0
margin_right = 186.0
margin_bottom = -10.0
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="QuestionText" type="Label" parent="."]
margin_right = 375.0
margin_bottom = 14.0
text = "Pitanje"
align = 1

[node name="Answers" type="GridContainer" parent="."]
margin_top = 18.0
margin_right = 375.0
margin_bottom = 126.0

[node name="optionA" type="CheckBox" parent="Answers"]
margin_right = 24.0
margin_bottom = 24.0
action_mode = 0
group = SubResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="optionB" type="CheckBox" parent="Answers"]
margin_top = 28.0
margin_right = 24.0
margin_bottom = 52.0
action_mode = 0
group = SubResource( 2 )

[node name="optionC" type="CheckBox" parent="Answers"]
margin_top = 56.0
margin_right = 24.0
margin_bottom = 80.0
action_mode = 0
group = SubResource( 3 )

[node name="optionD" type="CheckBox" parent="Answers"]
margin_top = 84.0
margin_right = 24.0
margin_bottom = 108.0
action_mode = 0
group = SubResource( 4 )

[node name="Button" type="Button" parent="."]
margin_top = 130.0
margin_right = 375.0
margin_bottom = 150.0
text = "Potvrdi odgovor"
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"]
16 changes: 16 additions & 0 deletions scenes/components/QuestionFeedbackCard.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extends Panel

func _on_Button_pressed():
queue_free()

func positive_feedback():
$QuestionFeedback.text = "Tocan odgovor!"
$QuestionConsequence.text = "+1 bod"

func negative_feedback():
$QuestionFeedback.text = "Pogresan odgovor!"
$QuestionConsequence.text = "-1 bod"

func set_total_points():
$TotalPointsLabel.text = "10 bodova"

64 changes: 64 additions & 0 deletions scenes/components/QuestionFeedbackCard.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://scenes/components/QuestionFeedbackCard.gd" type="Script" id=1]

[node name="QuestionFeedbackCard" type="Panel"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -189.0
margin_top = -168.544
margin_right = 186.0
margin_bottom = -10.5438
rect_pivot_offset = Vector2( 260.512, 313.021 )
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="QuestionFeedback" type="Label" parent="."]
margin_left = -0.328552
margin_top = 0.328552
margin_right = 373.671
margin_bottom = 35.3286
text = "Tocan odgovor! / Pogresan odgovor!"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="QuestionConsequence" type="Label" parent="."]
anchor_top = 0.5
anchor_right = 1.0
anchor_bottom = 0.5
margin_top = -23.0
margin_bottom = 2.0
text = "+1 / -1 bod"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TotalPointsLabel" type="Label" parent="."]
anchor_top = 0.5
anchor_right = 1.0
anchor_bottom = 0.5
margin_left = 0.328552
margin_top = 18.9557
margin_right = 0.328552
margin_bottom = 38.9557
text = "Ukupan broj bodova: 10"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Button" type="Button" parent="."]
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_top = -28.5424
margin_bottom = -8.54236
text = "U redu"
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"]
Loading

0 comments on commit d929e60

Please sign in to comment.