Code Your Own Adventure (Part 2)

Step 1

We will be adding an Img class to handle images in our game.

Temp1537910953 Temp1537910981
Step 2
Add an array to keep track of what image is displayed on what page

Instead of "castle.png" find and add an image using the new asset button at the top of the page

Step 3
Add the code to draw the image for each page

Because each Img object has position and size in it, we can draw images everywhere with this simple code

Check Your Work
Step 4
you should now be able to add images to any of your pages!

just set the value of images[] for each page you want an image

Step 5

Dynamic coding changes values during runtime instead of using values set before the code is run

Step 6
Dynamic pages

We can read in the player's stat values to tell them their exact chance of success

Step 7
Start off by making your page a function

To go to this page, we will need to call this function instead of setting the page number to 16

Step 8
Add the basics
Step 9
Add a skill check

Because we are executing this function to generate the page, we can use the player's current sneaky stat to calculate their chance of success.

In this case we made it a simple calculation, but note that with sneaky over 10 a player would have a 110%(or more) chance of success.

Temp1537912317 Temp1537912355 Temp1537912385
Step 10
Add an option which is only available to certain classes

You will have to create and set a profession variable before this will work

Check Your Work
Step 11
Temp1537912636 Temp1537912693
Step 12
Inventory System

Create a class to keep track of our inventory

Step 13
Add a constructor

We will make the inventory contents an object, keeping track of each item as a property, the value of which is the number we have.

Step 14
Create a method to add objects to our inventory
Step 15
Add the name to contents

Using [] in an object will create/set the property of name in the object

Step 16
Create a test inventory

Add some fun items to it

Temp1538083160 Temp1538083181
Check Your Work
Step 17
What happens if we add an item we already have?
Temp1538083283 Temp1538083377

We are left with 1 funds instead of 11

Step 18
We need to check if we already have an item

Loop through the contents with a for in loop

Step 19
Add to the existing item

return exits the function, which stops the final line (which creates a new item) from overwriting our item.

Check Your Work
Step 20
We should now be able to add numbers to existing items
Step 21
Set a default of 1 item

Now if we don't give a number parameter it will default to one item instead of undefined

Temp1538083874 Temp1538083907
Step 22
Add a remove method
Step 23
Is the item in the inventory?
Step 24
If there are enough of the item remove them
Step 25

Return true if successful, false if not

Step 26
If none of the item remain, remove it from the inventory

delete can remove a property from an object

Check Your Work
Step 27
Test the method

if you don't have enough funds, the function will return false and your boots will not be uncursed

Temp1538084841 Temp1538084862
Step 28
Create a contains method

This function tells us whether or not the inventory contains at least the specified number of an item.

Contains is useful for item based special options in your story

Step 29
Create a display method
Step 30
Add a variable to keep track of whether or not the inventory should be displayed
Step 31
Call it conditionally in the draw function

inv is an Inventory I created

Step 32
Make the i key toggle inventory display
Check Your Work
Step 33
You should now be able to toggle inventory display with i
Step 34
Use your inventory in your game

Create cool items.

Add transactions and costs

If you want to display more than 17 items, you will need to improve your display function to show more items

Code Your Own Adventure (Part 2) Info


MVCode Clubs

Created By

Missing tiny jake wheaton

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach