Introduction
You have learned that you can keep many values inside of a list. Now you are going to learn how you can use loops to go through the list and do clever tasks like search for values, find max/min values, count items, and many others that you will be able to adapt and use in your games. The routines presented in this lesson will be the foundation for the tasks we take on in the next lesson – so learn them well!
Part A – Using Loops with Lists
Preview Video: 13-02-A-Preview
This part can be a video lesson or a challenge depending on how comfortable you feel with using lists and loops so far. So look at the steps below and if they look easy enough, go for it and code it yourself. If not, follow the video to be guided through.
Have your list cheat sheet nearby to review the ds_list scripts you will use in this project. You will make a 6 sided die roller simulator and store the results in lists then perform basic tasks on the list.
Step 01
Create a new project.
Step 02
Create an object called o_roller and add it into a room. You will give the roller object a bunch of key press events to perform tasks related to rolling a 6 sided die many times.
Step 03
In the CREATE EVENT
Create a list. Don’t add anything to it yet. Just create it.
Step 04
In the KEYPRESS “R” EVENT
You will simulate rolling a 6 sided die 20 times.
a) Use the appropriate ds_list script to clear the list
b) Use a loop to fill the list with 20 values that range in value from 1-6
Step 05
In the KEYPRESS “S” EVENT
You will show the list in the debug window. If you didn’t watch the short video on printing to the debug window, go back and watch it now!
a) find out how many items in the list
b) use a show_message to show the size of the list
c) create a loop that will start at slot 0 and go to the end of the list
in the loop, get the value from the slot in the list and print it out
(use show_debug_message instead of show_message)
Step 06
In the KEYPRESS “C” EVENT
Use the appropriate ds_list script to clear the list.
Step 07
In the DRAW EVENT
You will draw the values in the list to the screen.
a) find out how many items in the list
b) use a loop that will start at 0 and go to the end of the list
in the loop you will get the value from the appropriate slot in the list
print it out at a different y position each time
(refer to the ‘hearts’ example in video lesson 13-01-B)
Step 08
Smile.
Need help doing this?
Watch the video: 13-02-A
Done File: L13-02-A-Done.gmk
*Keep your project! You will continue using it in the next lesson.