Recap - Traffic Light
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 41 of 78.
Challenge
EasySimulate a traffic light. At the top level of the script, declare an enum named TrafficLight with the options RED, YELLOW and GREEN, and write three functions that each use a match on the light:
get_driver_action(light)returns"Stop"for red,"Caution"for yellow and"Go"for greenget_wait_time(light)returns30for red,3for yellow and25for greenget_next_light(light)returns the light that comes next: red turns green, green turns yellow and yellow turns red
The program reads two lines: the name of the first light (RED, YELLOW or GREEN) and how many lights to show (a whole number, 1 or more). For each light, print its name, its action and its wait time, then move to the next light. Last, print the sum of all the wait times.
For the inputs RED and 4, the output is:
RED: Stop for 30 seconds
GREEN: Go for 25 seconds
YELLOW: Caution for 3 seconds
RED: Stop for 30 seconds
Total wait: 88 secondsTry it yourself
extends Node
# Declare the TrafficLight enum here
# Write get_driver_action(), get_wait_time() and get_next_light() here
func _ready():
var first := OS.read_string_from_stdin()
var count := int(OS.read_string_from_stdin())
# Show the lights and the total wait time
pass
All lessons in Logic & Flow
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler