PART THREE
IMAGES
LOAD IMAGES
Res Folder
Create a folder at the project level called res
This is the folder which will store all of your images.
To get started, feel free to use the Media Package provided by Mr. M
Images
Create a class called Images in the core package
This class will contain public static variables.
This breaks our rules, but it's okay because they are just images.
It's hard to accidentally cause trouble with image files!
Write a method to load some of the images used in your program. For now, start with the terrain images.
In your Game's init() method, call this method in a static way: Images.loadImages();
REPLACE COLOR WITH IMAGES
Terrain Class
Replace everything related to Color with Image.
For now, set the default image to Image.gray. We shouldn't ever see this image!
Grass and Dirt
Set images for grass and dirt in the constructor.
CHECKPOINT
Run your code and see...
Images instead of solid colors
WATER AND SOIL
Water Class
Create a new terrain class to represent water, using Dirt and Grass as an example.
Design Decision - How To Implement Soil
In our game, the player can only plant crops in soil, rather than just plain dirt. We'd also like to make the player have the ability to change dirt tiles into soil.
At this point, there are two reasonable implementations:
Create a seperate class for Soil, and add code to Cell which will manage the transformation of one Terrain type into another..
Have the Dirt class represent the different modes that Dirt can exist in - as plain dirt, dry soil, or wet soil.
We'll be using the second approach for this project - not because it's better, but because it's the simplest implementation. You can always modify this later!
Adding Soil to the Dirt Class
Create two boolean variables to represent the two states: soil and wet.
Modify the constructor to take these variables as parameters so that you can create tiles that start out as Dirt.
Rather than setting the image directly in the constructor, we should create a method called setImage().
This is because we'll want to change the image each time we modify the state of a Dirt object.
By making this method, we'll avoid having to duplicate code.
MAP FILE
Adding Soil and Water to the map
Create a new map file that contains:
Grass
Dirt
Dry Soil
Water
Select any symbols to represent these new terrains
CHECKPOINT
Run your code and see...
Grass, Dirt, Soil, and Water are all visible