The Legend of the Tomato Quest Tutorial - Items and End Game Conditions

This tutorial, we'll be looking at introducing an item to the game, the Legendary Tomato. When the player collects this item, after the boss is defeated, he will win the game. However, if the player is defeated before then, the player will lose.

The Legendary Tomato
To begin the tutorial, we need an image of a tomato to use for our item.



Download this image, and save it in the resources\images\ folder of your project.

Next open the LoadImages method within the Resources class in GameResources.cs and add the following line.

Now we can use this image in our game.

Items
Create a new source file called Item.cs, and add the following code as the template.

We first thing we want to do, is add 2 fields, a Sprite, and a boolean. Add the following fields to the Item class in Item.cs

Sprite will be used to store the sprite of the item, while the Dropped boolean, will be used to indicate whether the item is on the ground, or is being held by a character.

Finally, let's create the Item constructor class. Add the following constructor class to Item.cs

The first thing we do inside this constructor, is create the sprite that is to be stored in the Sprite field. Using the name that we passed in when we create the Item, we load the appropriate GameImage to create our sprite.

Next we set the UsePixelCollision value of Sprite to false, so that it uses rectangle collisions.

And finally, we default the Dropped value to false, indicating that it is being held by a character.

Giving Characters the Item
Before we start we need to add a field to the Character class in Game.cs.

This will enable us to make Character's hold items.

Next, Add the following method to the Item class in Item.cs.

With this method we can now both create a new item, and give an item to a character. So let's do that now.

Add the following field to the Game class in Game.cs.

We'll use this list to store our list of items, which at this stage will only be one item.

Now, add the following lines of code to Game constructor in Game.cs.

This new code, creates a new element in the _Items list, and adds a new Item, called Tomato. This will create a new item with the Tomato.png image we added earlier. We then give this item to the Leader character.

Dropping Items
Now, we'll look at the code to make our character's drop items.

Add the following method to the Item class in Item.cs.

With this, we can now set the Dropped value of an item to true. Now, add the following methods to the Character class in Character.cs.

With these methods, we can now make the Leader drop the item. Add the following lines of code to the UpdateAI method in the AIController class.

Now, whenever an AI with an item is defeated, it will drop the item.

Colliding and Drawing
Next, add the following methods to the Item class in Item.cs.

The first method, checks whether the specified character has collided with the Item or not, with this, we can find out whether the Player and the Item collide, which will help us with our victory conditions.

The second method merely draws the item, if it's dropped.

Before we draw our item, and check if the player collides with it, we need some sort of end game scene to play once the game has been won or lost.

End Game
The thing we need is a new source file, called EndGame.cs with the following template

Next, add the following fields, and accessors to the EndGame class in EndGame.cs

Now we have a way of finding if the game has ended, and whether the player has won or not. We can get or set these values, which is good, incase we want to reset the game instead of just quit.

Finally, add the following method to the EndGame class in EndGame.cs

Theres nothing overly spectacular about this method, all it does is simply check if the player has won or lost, and dispalys the appropriate message, this message will be shown until the player closes the window or hits the Return key.

Before we move to the next section, we need to set the defaults for our new EndGame booleans, add the following lines of code to the Game constructor in the Game class in Game.cs.

Now, everytime we create the game, the default values will be set to false.

Finishing Touches
Now we simply need to make some additions to our game to make the end game conditions complete.

Add the following sections of code to the Run method in the Game class in Game.cs

Now, whenever the player hits the item, and presses E, he will win the game. If the player is not alive, he has lost the game. We've also added a little notification to the bottom right corner of the screen, telling the player to hit e to collect items.

One final change needs to be made.

Modify the end condition of the while loop and add the following of code to the RunGame method in GameLogic.cs

And that's it, we can now win or lose the game.

The next tutorial will discuss how we could further improve the game, and what additions could be made.

The Completed Project
The source code for the completed project can be found [[Media:RPGTutorial13.zip|here]]. It is strongly recommended that you read through the source code, while reading the tutorial to get a better understanding of how things work.