Code Your Own Adventure (Part 2)

Step 1
Images

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

Temp1537911240
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

Temp1537911609
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

Temp1537911688
Information
Step 5
Dynamism

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

Planning
Step 6
Dynamic pages

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

Temp1537911848
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

Temp1537911980
Step 8
Add the basics
Temp1537912056
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

Temp1537912564
Check Your Work
Step 11
Temp1537912636 Temp1537912693
Step 12
Inventory System

Create a class to keep track of our inventory

Temp1537913530
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.

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

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

Temp1538082956
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

Temp1538083487
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.

Temp1538083680
Check Your Work
Step 20
We should now be able to add numbers to existing items
Temp1538083757
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
Temp1538084324
Step 23
Is the item in the inventory?
Temp1538084408
Step 24
If there are enough of the item remove them
Temp1538084523
Step 25
Return

Return true if successful, false if not

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

delete can remove a property from an object

Temp1538084728
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
Challenge
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

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

inv is an Inventory I created

Temp1538085850
Step 32
Make the i key toggle inventory display
Temp1538085931
Check Your Work
Step 33
You should now be able to toggle inventory display with i
Temp1538086001
Planning
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

Account

MVCode Clubs

Created By

Missing tiny jake wheaton

Access Level

premium

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