In this lesson you will build upon your basic script writing by creating scripts that require arguments (or sometimes called parameters). Arguments and parameters are pieces of information that you give a script in order for it to complete its task. Designing a script that uses arguments usually gives the script more flexibility in the way that it can perform it’s task. Adding arguments to your scripts can greatly reduce the amount of code you have to type with larger projects. Check out the videos to see how.
Part A – Scripts and Arguments
Watch the video: 08-02-A
DoneFile: Lv08_02_A.gmk
Challenge 1 – Make Some Scripts
Use: 08-012-A.gmk to start
Take the existing project and modify it to include these three scripts.
a) There are several objects that can give the player hp; apples, hamburger, and potions. Take a look at the existing code when a player collides with these three objects. Replace the increasing hp code with a script called givePlayerHealth that requires one argument: the amount of hp to give the player. The script should give the player the specified amount of hp and also check to make sure that the player doesn’t go over 100 hp. Then replace the code in the apple, hamburger, and potion with your script.
b) The player currently gets 1 point for hitting a ghost with an arrow. Write a script that allows you to give the player any amount of points. The script will require one parameter which represents the amount of points to give the player. The script will check to see if the player has reached 10 points, and if they have go to the game over room. Once you have this script written, use it to give the player 1 point when they hit a ghost and 5 points when they destroy a ghost. Test it out!
c) Write a script called dropRandomItem that requires no parameters, and creates either an apple, burger, or potion when a ghost is destroyed (after being hit by a player arrow). Add this script in the appropriate location in the program.
Challenge 1 Solution
Solution Video: 08-02-X1
Solution File: 08-02-X1Done.gmk