We are going to make a game where the player can choose the ingredients that make up a burger and find out how much our burger will cost.
Sprites
We have a lot of sprites for this game, they can all be found on your computer and can be added by clicking on the folder icon.
Make your sprites smaller and place them to the left so that the player will be able to click on them.
Burger Base
Let’s setup the burger base first, we want this sprite to sit in a location that will can use to stack the other parts of our burger on top of.
We setup a nice spot for our burger base.
Variables
We need the computer to do a few things:
- Add up and remember the total cost of the burger ingredients
- Remember the position of the last ingredient so that we can add items on top
- Only let the player add one ingredient at a time
To help us achieve these goals, we will create three variables:
Now, we can set these to some nice numbers on our game start:
Ingredients
All of our ingredients are going to share the same code, with the exception of the cost of the ingredient.
We want our player to click on the ingredient and have it fall to the burger base. We are going to have our sprite create a clone of the ingredient so that we don’t have to move it away from the left position.
Since we are going to be using a variable called ‘Y Stop Position’ which will remember the Y position of the last ingredient. We only want the player adding a single ingredient at a time.
We also have a variable called ‘CanAdd’ which we can use to see if our player can add a new ingredient. Our game starts with this set to 1, when a player clicks an ingredient we change it to ‘0’, then when the ingredient is in place, we then set it to ‘1’ again.
So our click will look like:
Now when our ingredient starts as a clone:
We have the sprite go to the front, we setup a position at the top of the burger base, then we have the ingredient move down to the value of our ‘Y Stop Position’ variable. Since we add to this every time an ingredient is added, this position will get larger and larger, so the ingredients stop higher on the screen.
We can now copy this code to our other ingredients and change the cost of the ingredient.
Burger Top
The burger top is the last item that makes up our burger, we want to be able to tell the player how much their burger will cost.
The code for this is very similar to the other ingredients:
However, we tell the user the total cost at the very end of the of the script.
Drink and Fries
Do we want to let our player select a drink and fries with that?
When the game starts, we move the sprite to the left and make it a little smaller. Once clicked we move it to the position next to our burger and make it a little larger. We also add to the ‘Total Cost’ so that we make sure our player gets charged correctly.