Chapter 4 Finishing the Crab Game 4 1
Chapter 4 Finishing the Crab Game
4. 1 - Adding Objects Automatically We are now getting close to having a playable little game. However, a few more things need to be done. The first problem that should be addressed is the fact that we always have to place the actors (the crab, lobsters, and worms) manually into the world. It would be better if that happened automatically.
Adding Objects Automatically Right Click on Crab. World and Select “Open editor”
Constructor for Crab. World Let us have a look at the Crab. World’s source code (Code 4. 1). (If you do not have your own crab game at this stage, use little-crab-4 for this chapter. )
Constructor for Crab. World public Crab. World() { super(560, 1); } • This Called the constructor of this class. A constructor looks quite similar to a method, but there are some differences: • A constructor has no return type specified between the keyword “public” and the name. • The name of a constructor is always the same as the name of the class.
Add a Crab to the World Since this constructor is executed every time a world is created, then, we can use it to automatically create our actors. If we insert code into the constructor to create an actor, that code will be executed as well. For example, X (0, 0) Y public Crab. World() { super(560, 1); add. Object ( new Crab(), 150, 100); } Create a New Crab and Add it at Location x=150 and y=100
Creating New Objects new Crab() add. Object ( new Crab(), 150, 100); new Crab() Creates a New Instance of the Class Crab When want to add a New Object to the level, We Have to specify where it goes
Calling add. Object()
Animating Images The next thing we shall discuss in relation to the crab scenario is animation of the crab image. To make the movement of the crab look a little better, we plan to change the crab so that it moves its legs while it is walking. Animation is achieved with a simple trick: We have two different images of the crab (in our scenario, they are called crab. png and crab 2. png), and we simply switch the crab’s image between these two versions fairly quickly. The position of the crab’s legs in these images is slightly different (Figure 4. 2). Crab with legs out Crab with legs in
Animating Images cont. . Crab with legs out Crab with legs in As we discussed previously, Animation is Achieved by Switching Between two or more Images
Calling add. Object() Now The Crab is Automatically Created in Crab. World Whenever you Reset or Compile
Declaring two Variables In this example, we have declared two variables in our Crab class. Both are of type Greenfoot. Image, and they are called image 1 and image 2. We will always write instance variable declarations at the top of our class, before the constructors and methods. Java does not enforce this, but it is a good practice so that we can always find variable declarations easily when we need to see them.
Using actor Constructors public Crab() { image 1 = new Greenfoot. Image ("crab. png"); image 2 = new Greenfoot. Image ("crab 2. png"); set. Image (image 1); } The constructor for the Crab class creates two images and assigns them to variables for use later.
Using actor Constructors import greenfoot. *; // (World, Actor, Greenfoot. Image, and Greenfoot) // comment omitted q The signature of a constructor does not include a return type. public class Crab extends Animal q The name of the constructor is the { private Greenfoot. Image image 1; same as the name of the class. private Greenfoot. Image image 2; q The constructor is automatically executed when a crab object is created. /* * Create a crab and initialize its two images. */ public Crab() { image 1 = new Greenfoot. Image ("crab. png"); image 2 = new Greenfoot. Image ("crab 2. png"); set. Image (image 1); } // methods omitted }
Using Object Variables to store non-primitive Information crab. png crab 2. png image 1 = new Greenfoot. Image (“crab. png”); image 2 = new Greenfoot. Image (“crab 2. png”);
Inspecting Object Variables
Assignment Statement An assignment is a statement that enables us to store something into a variable. It is written with an equals symbol: variable = expression; On the left of the equals symbol is the name of the variable we want to store into, on the right is the thing that we want to store. Since the equals symbol stands for assignment, it is also called the assignment symbol. We usually read it as “becomes”, like this: “variable becomes expression”. In our crab example, we write image 1 = new Greenfoot. Image(“crab. png”); image 2 = new Greenfoot. Image(“crab 2. png”);
v These two lines of code will create the two images we wish to use and store them into our two variables image 1 and image 2. v Following these statements, we have three objects (one crab and two images), and the crab’s variables contain references to the images. This is shown in Figure 4. 4.
Instance Variables (Objects) (private/ variable-type variable-name public) An instance variable is a variable that belongs to a single object (an instance of a class). For example, each Crab could have its own instance variable that stores what direction the Crab is going moving
Static Variables (Classes) (private/ static variable-type variable-name public) A static variable is a variable that is shared by all the objects of an entire class. For instance, the total number of Worms in the world could be stored as a static variable because this information should not be stored separately for each Worm object.
Calling add. Object()
Calling add. Object()
Calling add. Object()
Calling add. Object()
Calling populate() Create a Method called populate. World ()
Calling populate()
Primitive Variables A primitive variable stores information of a simple (or “primitive”) type. Primitive data types are all the data types we learned about in chapter 3. So a primitive variable could look like this: private int counter = 0;
Object Variables An object variable is best defined as a “non-primitive” variable. The information stored in an object variables cannot be expressed by a primitive data types, because the information is an entire object. The following slides will illustrate this idea.
Primitive and Object Variables We can have a look at all information stored by an Object. To do this we right-click an Object and select the “Inspect” option
Primitive and Object Variables Primitive Variables: The Crabs Location in the World Object (non-primitive) Variable: The Image representing the Crab
Greenfoot Images
Another set. Image() Option You can set an image with a String, or you can set it with a Greenfoot. Image object. Before now, we have set the image with a String. Now we will start using the Greenfoot. Image approach.
Using Object Variables to store non-primitive Information import greenfoot. *; Greenfoot) // (World, Actor, Greenfoot. Image, and // comment omitted public class Crab extends Animal { private Greenfoot. Image image 1; private Greenfoot. Image image 2; // methods omitted } // Variables that do not //store primitive values
Using Object Variables to store non-primitive Information public class Crab extends Animal { private Greenfoot. Image image 1; private Greenfoot. Image image 2; /* * Act - do whatever the crab wants to do. * This method is called whenever the 'Act' or 'Run' * button gets pressed in the environment. */ public void act() { check. Keypress(); move(); look. For. Worm(); } }
Using Object Variables to store non-primitive Information The Variables Have Not Been Initialized and Contain null
Switching Images Pseudo Code if current image is image 1 then use image 2 now otherwise use image 1 now Actual Java Code if ( get. Image() == image 1 ) set. Image (image 2); else set. Image (image 1);
if/else that Determines which image to use if ( get. Image() == image 1 ) { set. Image (image 2); } else { set. Image (image 1); }
if/else When Only One Statement if ( get. Image() == image 1 ) set. Image (image 2); else set. Image (image 1);
Switching Images /* * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (get. Image() == image 1) { set. Image (image 2); } else { set. Image (image 1); } check. Keypress(); move(); look. For. Worm(); }
Switching Images (separate method) /* * Switch the images of the Crab to make it appear as if the Crab is moving it's legs. * If the current image is image 1 switch it to image 2 and vice versa. */ public void switch. Image() { if (get. Image() == image 1) { set. Image (image 2); } else { set. Image (image 1); } }
Switching Images /* * Act - do whatever the crab wants to do. This method is called when * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { check. Keypress(); switch. Image(); move(); look. For. Worm(); }
Switching Images Right Click on the Crab Click on the switch. Image Method The Crab’s Legs will move
More Ideas q Using different images for the background and the actors q using more different kinds of actors q not moving forward automatically, but only when the up-arrow key is pressed q building a two-player game by interdicting a second keyboard-controlled class that listens to different keys q moving worms to random positions on the screen
Summary of Programming Techniques In this chapter, we have seen a number of new programming concepts. We have seen how constructors can be used to initialize objects. Constructors are always executed when a new object is created. We have seen how to use different kind of variables (instance vs. static, primitive vs. object) and assignment statements to store information, and how to access that information later.
Concept Summary
- Slides: 45