In the last lesson (Lesson 2), we created the Tic-Tac-Toe Board and GameBall classes. We also added GameBall objects to the Board object. In this lesson, we will add a Player class such that a Player object interacts with GameBall objects in a meaningful way.
The game should work this way:
This game takes two players, who will take turn clicking. When the game starts, all nine cells on the board are blank. When the first player clicks at a blank cell, a Gold ball will be placed in that cell; when the second player clicks at a blank cell, a Steel ball will be placed in that cell.




Create a Player Class
To interact with the GameBall objects, we need a class called Player. Right click on the Actor button and select New subclass… from the pop-up menu. At New class name, enter "Player", then import the first image, ant.png, by selecting animals->ant.png, and click OK.
Now the Player class has been created but we still need to import another image to represent another player. To do so, right click on the Player icon and select Set Image… from the drop-down list. Import another image, ant-with-food.png, by selecting animals->ant-with-food.png.
NOTE: You can select other images but make sure they are not larger than 30x30 pixels (for the cells are 60x60 pixels each), or else the game would not work as planned.
Add States to GameBall Class
Before adding interaction between the Player class and the GameBall class, we need to add states to the GameBall class. Three states are needed: UNCLICKED, GOLD, and STEEL. We will add a member variable-ballState-to hold the state information and three member functions-setGold, setSteel, and reset-to access and control the member variable.
A Java class can have member variables and member functions. Member variables are like states or settings of an object, whereas member functions are mechanism to access and control these settings. When an object is created, it's assigned a unique segment of memory space to hold its member variables. Moreover, an object has access to a shared set of class functions.
Take GameBall class for example, if we create two GameBall objects (object of the GameBall class type) called ball_one and ball_two, then each of them will have a ballState variable and will have access to setGold(), setSteel(), and reset() functions.
To implement the three states, we use Java's enumerated type. An enumerated type has a finite number of named values. For example:
To declare variables of this type:
When the GameBall object is first created, we would like its state to be UNCLICKED. As the game goes on, players set the GameBall state via set functions.
This is the code for GameBall so far:
Next, let's add code to change a GameBall's look according to its state. Since a GameBall extends from the Actor class, it inherits a set of functions from the Actor class. Inheritance is a very important concept in Object-Oriented Programming. Simply put, it's a way to build new classes based on existing classes. When a class extends or inherits from another class, it's said to be the subclass of that class, which is called the superclass. A subclass has all member variables and functions that its superclass has, and more.A subclass can has additional variables and functions to those of its superclass, and they often do.

The Actor class can have many subclasses; in fact, most new classes you created in Greenfoot are subclasses of the Actor class. GameBall and Player are both subclasses of the Actor.

The Actor class has a public function called setImage, which changes the image file path. Since GameBall inherits from Actor, it can call setImage function like this:
Or like this:



| public class A { private void f1( ) { … }; public void f2( ){ … f1( ); //OK }; } |
public class B{ public void f3(){ A objA = new A( ); objA.f1( ); //ERROR objA.f2( ); //OK } } |
The completed code of GameBall class is now as shown:
You may have noticed the empty act function which I will explain in the next step.
Let Player interact with GameBall
Next, we will add codes to the Player class so Player objects can interact with the GameBall objects. The Player class has two states: PLAYER1 and PLAYER2. Similar to what was done to GameBall, add an enumerated type call "PlayerMode", a member variable called "mode", and two public functions: "setPlayer1" and "setPlayer2".
Next, let's add code to Player's act function so that a Player object handles mouse clicks. The act function is called automatically and repeatedly by the Greenfoot framework; if you want your Actor to perform a task repeatedly, you would put the code related to that task inside the act function.
The first we need to add to act is the code to "glue" the Player to the mouse:
This piece of code, when added to act function, will move the Player object to wherever the mouse is. Next, we will add code to check whether the Player object has collided with a GameBall object when the mouse clicks. This is the code to do so:
With this code, each time the mouse clicks, the Player object would first check whether it has collided with a GameBall, is yes (ball is not null), then it switches mode to another player by calling either setPlayer1 or setPlayer2. Moreover, when player1 is playing, each mouse click turns a ball from to STEEL; on the other hand, when player2 is playing, each mouse click turns the ball to GOLD.

The complete code for the Player class thus is now:
Finally, add the code to the Board class to add one Player to the board.
Test the Game
Save all three classes-Board, Player, and GameBall-and compile the game by clicking the "Compile All" button. Then hit Run to try.

You should be able to click each cell and turn them to gold or steel balls.

If you have trouble running the example, you would need to debug your program. To do so, select Controls>Act, and you should see your error in red.

One common error in this lesson is forgetting to import the image files. For example, if the "ant.png" file is not imported, you would see this error:

To download the source code for this lesson, click HERE.
This concludes Lesson 3. In Lesson 4, we will complete the Tic-Tac-Toe game by adding code to determine how the game is won and code to jazz up the game a bit. I will also show you how to export a Greenfoot game and how to post it on either Greenfoot site or your own site.
- Comments
!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."
| < Prev | Next > |
|---|





