All posts by Atomic Toddler

Level 11 Lesson 01 [Using Lists]

Introduction
This lesson will walk you through how to create and use the most common list scripts.  By the time your done, you’ll be thinking about ways that you can use lists in your game projects.

Part A – Basic List Scripts
In this part you will be introduced the creation of lists and the common pre-made scripts that you can use with lists.

Watch the video: 11-01-A
Notes on lists:  GameMaker_Lists.doc (cheat sheet seen in video)
Project File: L11-01-ADone.gmk


Challenge 1 – Inventory List
Start File: L11-01-X1Start.gmk

This challenge involves you setting up an inventory list for the player that keeps track of which food items that have been collected.  As the player picks up food, the name i

a) You’ll see that in the challenge room there are apples and burgers. When the player touches an apple the apple should be destroyed and you should add the word “apple” to a list (you can create this list in player->create). Do something similar for when the player contacts burgers.

b) Lets make it so that the player can only carry 0-5 items. Modify your code so that before collecting apples or burgers you check the size of the inventory list to make sure that they are carrying less than 5 items. If the player is already carrying 5 items then don’t destroy the apple or burger and don’t add it to the list.

c) Lets give the player the choice to eat an apple or burger by pressing the “A” or “B” key. If the player presses “A”, find out in what position the word “apple” is in the list using the ds_list_find_index script. If this script returns -1 you know that no apple was found in the list and you should show a message saying “you have no apples!”. If there is an apple in the list, show a message like “just ate apple!” and remove that apple from the list (you just found out where it is, so you should be able to remove it easily with the ds_list_delete script. Test this out to make sure that you can only eat what you have collected!

d) In the draw event of a draw object, find out the size of the list and draw it to the screen. As you collect items you should see the number go up and as you eat items you should see the value go down.

Challenge 1 Solution
Solution Video: 11-01-X1Solution
Solution File: L11-01-X1Done.gmk


Challenge 2 – Magical Touch

In this challenge you will create a list that will store the ‘id’ values of apples that the player touches. When the player contacts an apple, the apple’s id will be added to a list if it isn’t already in the list. When the “D” key is pressed, all the apples that have been touched will be destroyed. I like this challenge because it shows you that you can remember objects by storing their id’s in lists.

Preview Video: L11-01-X2-Preview
Start with: L11-01-X2Start.gmk

a) create a global list that will store the id’s of objects (just like any other list creation)

b) every time a player touches an apple, get the apples id and check to see if it already in the list or not (remember that when searching for a value in a list you use ds_list_find_index and if it returns zero or larger you know the index at which it was found, and -1 means that you didn’t find the value). If the apple id isn’t in the list, add the apple’s id into the list.

c) create a script called destroyFirstApple. This script should find out how many values are stored in the list. If there are no apples in the list, just exit. If there are apples in the list, find the first id in the list (remember that this is in index position 0, use ds_list_find_value). Once you know this id, use a with statement with this id to destroy this apple. Also delete this value from the list (delete the value in position 0). To test this, complete step d.

d) find the o_tester object. When it detects the “D” key pressed, run the destroyFirstApple script. When testing you should see that after you have touched a bunch of apples, the “D” key should delete the apples one at a time in the order you added them to the list!

e) go back to the destroyFirstApple script and add the following: after deleting the apple and it’s id from the list, check to see if the size of the list is larger than zero. If it is, then we have more apples to destroy and you should call the script destroyFirstApple again. Your code will end up looking something like this:

if ds_list_size(global.apples) > 0 {
  destroyFirstApple()
}

Now when you test this out, the script will (if there are still apples left) call itself to run again, and again, and again until there are no more apples left in the room. This is probably your first exposure to an idea called recursion. Recursion is when a script calls itself and a type of loop is created. It works!

Challenge 2 Solution
Solution Video: L11-01-X2-Solution
Solution File: L11-01-X2Done.gmk

Level 12 – Grid Game Concepts

Many beginning game programmers try their hand at making grid based games.  Some grid based games you might know are Pac-Man and Bomber Man.  The movement of the players is restricted by a maze-like layout in the room.

In this level you will be learn some of the basic concepts needed to help you make a grid based game.

Level 12 Lesson 01 [Basic Grid Concepts]

Introduction
Grid based games are a common type of game that many new game programmers like to create. One of the most popular grid based games of all time is Pac-Man. You can probably think of many others. In this lesson we will introduce you to some concepts that can help you create a grid based game. In Lesson 02 you will use some of your script and list knowledge to simulate some basic ghost movement in a Pac-Man type grid.

Part A – Sprite Sizes, Collision Boxes, Keyboard
There are many ways to set up a grid based game. In this video you will take a look at some simple ways to help setup your grid based game and get the player moving around the grid smoothly.

Watch the video: 12-01-A
Done File: L12-01-A-Done.gmk

Challenge 1 – Your Own Grid Based Game
For this challenge, start a new project.
Select sprites for your own grid based game that will consists of a player moving around with enemies chasing.
Use the concepts presented in the video to make sure that your sprites, room, and player movement will work properly.

Level 12 Lesson 02 [Guided Challenge]

Introduction
Lets get the ghosts moving around the grid using the concepts of collision detection, scripts, and lists.

Guided Challenge
For the three parts of the lesson that follow: watch the video and take notes on the basic ideas so that you will be able to apply them to your grid game that you set up in the last lesson.

Part A – Ghosts Moving Forward
Watch the video: 12-02-A
This part demonstrates creating a script to check if it is OK for a ghost to continue moving forward in the grid.


Part B – Detecting Intersections

Watch the video: 12-02-B
When game pieces are at an intersection, they might decided to turn or continue going straight. In this part we take a look at writing a script that will determine whether or not we are at an intersection.


Part C – Pick New Direction

Watch the video: 12-02-C
Our ghosts turn under two conditions: they can’t move forward or they detect they are at an intersection. We have written scripts to realize both of these situations. Now lets write a script that will pick a good, valid, new direction for movement!

Guided Challenge Solution
The video solution is basically Part A, B, and C together.
Solution File: 12-02-C-Done.gmk (contains A, B, and C done)

Level 13 – Intro

When you are programming there are times when you might want a block of code to repeat several times. Most programming languages have statements that allow you to loop your code. In this unit, we will take a look at several different types of loops and how you can start to use them in your games.

Then you will learn that loops can be used for more than just repeating code. You can use loops to perform some fairly clever tasks when you involved a loop counter (you’ll soon know what this is!). Loops get even better when you start using them with lists and arrays.

Every programmer should have a solid knowledge of using loops. So lets get started with a big level on loops.

Level 13 – Lesson 01 [Using Loops]

Introduction
Almost every programming language has the capability to loop code. The technical term is ‘iteration’ or ‘iterate’ which means to repeat. In this lesson we will take a look at some different types of loops you can code in GameMaker.

Part A – Basic Loop Structures
Watch the video: 13-01-A
Done File:  L13-01-A-Done.gmk


Challenge 1 – Loop Reading/Writing

Worksheet: LoopWorksheet01.pdf
Solutions: LoopWorksheet01_Answers.pdf

Practice some code reading and writing with loops and complete the worksheet. Answers are provided. It should be noted that there are several answers for each code segment. For example, k<5 as a condition is the same as k<=4 if you are dealing with integer values.

Part B – Using the Loop Counter
Watch the video: 13-01-B
Done File:  L13-01-B-Done.gmk

Challenge 2 – More Loop Reading/Writing

Worksheet: LoopWorksheet02.pdf
Solutions: LoopWorksheet02_Answers.pdf

A little more loop reading and writing, but this time they get a little trickier.

Part C – Loop Warning!
Watch the Video: 13-01-C
A quick video about avoiding the endless loop!
It happens to all of us.

 

Level 13 – Lesson 02 [Loops and Lists]

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.

Level 13 – Lesson 03 [List Loop Algorithms]

Now that you’ve seen how to fill and show a values in a list using loops you are ready to challenge yourself by figuring out some standard loop algorithms. The following algorithms will start on the easy side and then get harder. When you get stuck, just watch the solution video at the appropriate time for a walk through. Expect to get stuck on at least one or two of them – if they were all too easy you wouldn’t have any fun trying to figure them out.

Continue with your roller project from 13-02-A, or open 13-02-A-Done.gmk for my version and try to solve the next 5 algorithms.

Solution/Hints (Don’t peek until you’ve tried)
Solution Video: 13-03-A
Done File: 13-03-A-Done.gmk

Algorithm 01 – How Many Sixes?
Solved at 0:06

In the KEYPRESS “H” KEY EVENT
a) find out how many values in the list
b) create a counter and set it to zero
c) use a loop to cycle through each value in the list
d) in the loop, if the value is a 6, increase the counter
e) when the loop is done, display how many sixes were rolled!

Algorithm 02 – Total Of All Rolls
Solved at 2:36

In the KEYPRESS “T” KEY EVENT
a) find out how many items in the list
b) create a variable called total and set it equal to zero
c) use a loop to cycle through each value in the list
d) add each value onto the total variable
e) when the loop is done, display the total of the list

Algorithm 03 – Where’s The First One?
Solved at 4:56

In the KEYPRESS “F” KEY EVENT
a) find out how many items in the list
b) use a loop to go through all the values in the list
c) if the current value is equal to 1 then show the current index position in the list and then use
‘break’ statement to exit out of the loop.

Algorithm 04 – Largest Value in List
Solved at 8:26

Before starting this algorithm, change your existing rolling code so that the values placed into the list are 1-100 and not 1-6.

In the KEYPRESS “L” KEY EVENT
a) find the size of the list, if it is zero, exit this event
b) create a variable called largest and set it equal to the value of the first item in the list
c) use a loop and go through each value in the list
d) if the current value is larger than the variable largest, set largest equal to the value
e) when the loop is done, largest should be equal to the largest value in the list, show it!

Algorithm 05 – Where is the Smallest Value in List?
Solved at 13:06

Leave your rolling code set to fill the list with values 1-100.

In the KEYPRESS “M” KEY EVENT
a) find the size of the list, if it is zero, exit this event
b) create a variable called smallest and set it equal to the value of the first item in the list
create a variable called position and set it equal to 0
c) use a loop and go through each value in the list
d) if the current value is smaller than smallest, set smallest to the value and set the position
variable to the current index position you are on
e) when the loop is done, position should equal the index position of the smallest value – show it!

Smile!  Next lesson…

Level 13 – Lesson 04 [Game Loops n Lists]

Introduction
In this lesson you will be introduced to how you can combine your knowledge of loops and lists with the lists of game objects that GameMaker is keeping track of for you.  Lets walk through a few examples of using the instance_find script from earlier lessons inside of loops.

Part A – Fire At Closest Monster
Watch the Video:  13-04-A
Done-File: 13-04-A-Done.gmk


Part B – Splash Damage
Watch the Video: 13-04-B
Done-File: 13-04-B.gmk


Part C – Move Towards Weakest Monster

Watch the Video: 13-04-C
Done-File: 13-04-C.gmk


Challenge 1 – Repel Tower
Use the file 13-04-X1-Start.gmk
Add code to the repel tower ‘press space bar’ that uses a loop to go through all the monsters and repel each one.  For a simple repel, make monsters to the left of the tower move leftward, and monsters to the right of the tower move rightward.  If you think you are really good, make the monsters move away from the tower as if they are actually repelled.

Solution
Solution Video: 13-04-X1-Solution
Solution File: 13-04-X1-Done.gmk


Challenge 2 – Repel Tower Mod

Continue from your Challenge 1 File.
Make a simple modification so that only monsters with a shield value less than 50 are affected by the repel tower.

Solution
Solution Video: 13-04-X2-Solution
Solution File: 13-04-X2-Done.gmk

There we go!

Level 14 – Intro

Sometimes you create a new game object that uses most (or even all) of the exact same code as another object that you have already coded.   Sometimes you want an object to group several different objects so that they behave in similar ways. Sometimes you might like to control several objects at once using just one group name.

This is when the concept of Inheritance can help save you time, lines of repetitive code, and make your game design more efficient.  Inheritance gives you the ability to create relationships between objects and allow them to share coded events. Read on to learn more about this powerful concept!