In this lab and homework, we will continue the game started in Homework 9 and will add multiple levels and a starting and menu scene to it.
Ex. 1. We will start by creating multiple scenes in the program started the last week. Reopen your Pipe Dream project.
a. Splash Scene
Add a separate introductory scene (also called "splash") to the project with a big label showing the name of the game, a big picture of the plumber, and any other decorative elements you may want to add.
Add a script to the root node of the scene. Define a countdown variable in the class and assign it the value 5.0 in the function _ready.
Then in the function _process, remove the value of delta form the countdown. If the countdown gets below 0, then load the scene main with the following statement (make sure that is the actual name of your main scene):
get_tree().change_scene_to_file("res://main.tscn")
Right-click on the tab of the scene and click on "Play This Scene". It should show for 5 seconds, then switch to the main scene.
We would also like the user to be able to skip through this scene by typing any key. Add the following function:
func _input(event): if event is InputEventKey: get_tree().change_scene_to_file("res://main.tscn")
The last thing to do with this scene is to make it the first scene. For that, go to Project Settings, Application, Run, and choose the splash scene as the Main Scene.
b. Text File
Let's save the level created in homework 9 into a text file and add a few more. Add the following function to the file main.gd:
func print_table(): for r in len(table[0]): var s = "" for c in len(table): s = s + str(table[c][r]) + " " print(s)
Add a call to this function from the function _ready at the end. Run the program once to see the result.
Then right-click in the Files area are create a new text file. Call it "level5.txt". Double-click to open the file, and write the numbers 16 and 12 separated by a space on the first line. After that, copy the output of the table resulting from the last function from the Output window into the text file.
Now we have stored the content of the table in a file. We also need to be able to retrieve it from there. Add the following function:
func read_table(filename): var f = FileAccess.open(filename, FileAccess.READ) var line = f.get_line().split(" ") var cols = int(line[0]) var rows = int(line[1]) table.resize(cols) for c in cols: table[c] = [] table[c].resize(rows) table[c].fill(-1) tile_map.clear_layer(0) var val for r in rows: line = f.get_line().split(" ") for c in cols: val = int(line[c]) set_cell(c, r, val) f.close()
In the function _ready, add the following lines:
var file = "res://level4.txt" read_table(file) print(table)
Run the program to check that the printed table after being read from the file has the same content as the first one that we printed.
c. Levels
Download the following files and add them to the project:
When we load the main scene from the splash screen, we want to start from level 1. In the main script, declare a static class variable called level and initialize it with the value 0.
Then in the script splash.gd, turn the code loading the main scene into a function called load_main. Then in that function, just before loading the scene, add the following lines:
var mainscript = preload("res://main.gd") mainscript.level = 1
Replace the line loading the script with a call to the function load_main both in the function _input and in the function _process.
In the main scene, add another button after Reset called Next. When the level is finished, this button will take us to the next level. Connect the pressed signal of this button to a function in the main script. For now, in this function, increment the level number and reload the scene.
Define a function called start_level. This function should check the value of the variable level and define the text file to be loaded into the tile map based on that. Then also assign an appropriate value to the number of holes left for that level. You'll probably need to go through them once to count them. Alternatively, this can be stored in an array defined at the top of the class or in the function itself.
Then in the function _ready, make a call to this function.
c. Stand-Alone Application
Let's create a stand-alone executable for this project. Go to the Project menu and click on Export. Then click on Add... and choose either Windows Desktop (if you work on Windows) or Web if you're on MacOS.
Click on the folder icon next to Export Path. Create a folder for the application and enter a name for the executable. You can edit the information on this page and add an icon for the executable if you want.
The first time you create an executable, Godot will need a template for that platform. Click on Manage Export Templates and then click Download and Install. After the installation is complete, when you open the Export dialog again, it should let you click on Export Project. If you look at the folder you configured, it should now contain the executable.
Ex. 2. a. End Scene
Add an end scene to the program where you write some credits and have a button that says Restart. This button should take you back to Level 1. similar function to the one from Ex. 1. d. to deal with the vertical movement of tiles, and a call to it in the appropriate place in the Player script. There should also be a button to quit the game, which can be done with
get_tree().quit()
b. End Level
In the previous homework, you implemented a winning condition for the game. Change the function attached to the Next button so that the scene is changed only if the user presses it while the number of holes left to fill is equal to 0.
Once everything is complete, export the program to a Windows or Web executable again and add it to the zip folder for the homework submission.
Take a screen shot of your running program, showing the content of the screen while the game is running, and save it as png or jpg. Create a zip file containing the project folder (the screenshot can be inside) and the executable folder. Submit both of them to Canvas, Assignments - Homework 10.