Wednesday, August 15, 2012

Follow-up: Exploding Apples in Kodu

This exercise was originally posted at: http://maritimeorca0.blogspot.com/2012/07/exploding-apples-in-kodu.html

The Task

Create code for exploding apples using the Kodu Game Lab.
By pressing a key on the keyboard or a button on a controller, a user-controllable character should launch an apple that explodes.
 

My Solution

You can find my Kodu save at: https://skydrive.live.com/redir?resid=2E6EFB80915ED5BD!3890
I enlarged the world by adding ground. I surrounded the playable area with a wall. I added Kodu as a playable character and a cycle as a target. I colored Kodu green and the Cycle red. I created an apple and changed its settings so that it is creatable. I gave the apple the following code:

When Bump, Do Damage It
When Bump, Do Boom Me
When Timer 60 Seconds, Do Vanish


This causes the apple to damage whatever it hits and explode on contact. If it doesn't hit anything, anything that runs into it can still cause it to explode for 60 seconds. To prevent problems with resources, the apple will disappear if nothing happens after that time.

Now that I have an exploding apple, I added the following code to Kodu:
When Keyboard Arrows, Do Move
When Keyboard Z, Do Launch Apple 1 Low


For use with controllers, I added the code:
When Gamepad L stick, Do Move
When Gamepad A button, Do Launch Apple 1 Low


This allows the arrow keys (or your controller's stick) to move Kodu around. Z on the keyboard (or the A buttton on your controller) will launch the exploding apple.

To make things a little more interesting, I decided to make the cycle move. I also added the ability to create more cycles as targets. I changed the settings for the cycle to be creatable. While in the settings, I enabled it to Show Hit Points and reduced the Max Created to 10 (to avoid overusing resources). I also added a red factory with Invulnerable selected in its settings with the following code:

When, Do Create Cycle 1 Once
When Keyboard +, Do Create Cycle 1
When Gamepad Y button, Do Create Cycle 1


This creates one cycle to begin with that originates at the factory and adds another each time you hit the + key (or the Y button on your controller). The code for the cycle is as follows:
When Always, Do Wander
When Health 0 Points, Do Score 1 point White


This will keep your potential targets moving, and it will keep track of how many cycles you destroy.
While thinking things through, I came up with an alternative approach to creating exploding apples. This other idea would be more like a grenade where the apple would explode and damage would come from shrapnel. For this, I added two new apples. One would be the new exploding apple, and the other would be used for the shrapnel. The shrapnel apple has been reduced to its minimum size for effect. For the normally sized grenade-like apple, here is the code.

When Timer 1 second, Do Switch Page 2
On the second page:
When Always, Do Launch Apple 3 North Strong Low Once
When Always, Do Launch Apple 3 North East Strong Low Once
When Always, Do Launch Apple 3 East Strong Low Once
When Always, Do Launch Apple 3 East South Strong Low Once
When Always, Do Launch Apple 3 South Strong Low Once
When Always, Do Launch Apple 3 South West Strong Low Once
When Always, Do Launch Apple 3 West Strong Low Once
When Always, Do Launch Apple 3 West North Strong Low Once
When Always, Do Boom Me


This code tells it to wait one second, switch to page two (partially for readability rather than necessity), launch a single shrapnel apple in each of the eight major directions, and explode. This code will execute fast enough that you will not realize the order. I should also point out that Kodu Game Lab executes in order, so the explosion will always occur after all of the shrapnel has been produced. To make the shrapnel apple’s effect, I gave it the following code:

When Bump, Do Damage
     When, Do Vanish Me
When Timer 1 second, Do Vanish


When the shrapnel apple hits an object, it will cause damage and disappear. Indenting the second line tells the code to execute with the previous condition. (The primary purpose for indenting lines is actually to allow multiple conditions to be applied to a DO command.) This damage can only occur during the quick burst (first 1 second) of the shrapnel apple before the shrapnel apple disappears.
To allow the new apples to be fired, I added the following code to Kodu:

When Keyboard X, Do Launch Apple 2 Low
When Gamepad B button, Do Launch Apple 2 Low


Since I didn't delete code, Kodu can still fire the old apples. For the new apples, you can hit X on your keyboard (or the B button on your controller).
 

Notes

This game could be revisited for future challenges.
 

Feel Free to Comment

Now that this exercise has concluded, feel free to share any thoughts that you had regarding results, your approach, and any other ideas that popped in your head regarding this exercise.
 

About Sharing

Anyone can play my solution to this exercise. I should be credited if anyone shares with others. I think that Kodu includes features to allow others to build off of existing projects and show those who were previously involved (I haven’t shared these files before, so I can’t be certain). This should be sufficient for credit, and I will allow others to build off of my solution. Unless otherwise specified, similar guidelines should apply to solutions from other contributors.


Upon completion of this exercise, please fill out the following form:
https://docs.google.com/spreadsheet/viewform?formkey=dDVHVnBhVy1nUXR5bEkwdllPb1o4MVE6MA
Responses to the above form are collected and stored in a spreadsheet that can be viewed at:
https://docs.google.com/spreadsheet/pub?key=0Agcdh--NHOS0dDVHVnBhVy1nUXR5bEkwdllPb1o4MVE&output=html

No comments:

Post a Comment