Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 - Finishing the Crab Game

Similar presentations


Presentation on theme: "Chapter 4 - Finishing the Crab Game"— Presentation transcript:

1 Chapter 4 - Finishing the Crab Game

2 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.

3 Adding Objects Automatically
Right Click on CrabWorld and Select “Open editor”

4 Constructor for CrabWorld
Let us have a look at the CrabWorld’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.)

5 Constructor for CrabWorld
public CrabWorld() { super(560, 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.

6 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 CrabWorld() { super(560, 560, 1); addObject ( new Crab(), 150, 100); } Create a New Crab and Add it at Location x=150 and y=100

7 Creating New Objects new Crab() addObject ( 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

8 Calling addObject()

9 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 crab2.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

10 Animating Images cont.. Crab with legs in Crab with legs out As we discussed previously, Animation is Achieved by Switching Between two or more Images

11 Now The Crab is Automatically Created in CrabWorld
Calling addObject() Now The Crab is Automatically Created in CrabWorld Whenever you Reset or Compile

12 Declaring two Variables
In this example, we have declared two variables in our Crab class. Both are of type GreenfootImage, and they are called image1 and image2. 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.

13 Using actor Constructors
public Crab() { image1 = new GreenfootImage ("crab.png"); image2 = new GreenfootImage ("crab2.png"); setImage (image1); } The constructor for the Crab class creates two images and assigns them to variables for use later.

14 Using actor Constructors
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) // comment omitted public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; /* * Create a crab and initialize its two images. */ public Crab() image1 = new GreenfootImage ("crab.png"); image2 = new GreenfootImage ("crab2.png"); setImage (image1); } // methods omitted The signature of a constructor does not include a return type. The name of the constructor is the same as the name of the class. The constructor is automatically executed when a crab object is created.

15 Using Object Variables to store non-primitive Information
crab.png crab2.png image1 = new GreenfootImage (“crab.png”); image2 = new GreenfootImage (“crab2.png”);

16 Inspecting Object Variables

17 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 image1 = new GreenfootImage(“crab.png”); image2 = new GreenfootImage(“crab2.png”);

18 These two lines of code will create the two images we wish to use and store them into our two variables image1 and image2. 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.

19 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

20 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.

21 Calling addObject()

22 Calling addObject()

23 Calling addObject()

24 Calling addObject()

25 Create a Method called populateWorld ()
Calling populate() Create a Method called populateWorld ()

26 Calling populate()

27 Primitive Variables private int counter = 0;
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;

28 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.

29 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

30 Primitive and Object Variables
Primitive Variables: The Crabs Location in the World Object (non-primitive) Variable: The Image representing the Crab

31 Greenfoot Images

32 Another setImage() Option
You can set an image with a String, or you can set it with a GreenfootImage object. Before now, we have set the image with a String. Now we will start using the GreenfootImage approach.

33 Using Object Variables to store non-primitive Information
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) // comment omitted public class Crab extends Animal { private GreenfootImage image1; // Variables that do not private GreenfootImage image2; //store primitive values // methods omitted }

34 Using Object Variables to store non-primitive Information
public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; /* * 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() checkKeypress(); move(); lookForWorm(); }

35 Using Object Variables to store non-primitive Information
The Variables Have Not Been Initialized and Contain null

36 Switching Images Pseudo Code Actual Java Code
if current image is image1 then use image2 now otherwise use image1 now Actual Java Code if ( getImage() == image1 ) setImage (image2); else setImage (image1);

37 if/else that Determines which image to use
if ( getImage() == image1 ) { setImage (image2); } else { setImage (image1);

38 if/else When Only One Statement
if ( getImage() == image1 ) setImage (image2); else setImage (image1);

39 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 (getImage() == image1) setImage (image2); } else setImage (image1); checkKeypress(); move(); lookForWorm();

40 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 image1 switch it to image2 and vice versa. */ public void switchImage() { if (getImage() == image1) setImage (image2); } else setImage (image1);

41 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() { checkKeypress(); switchImage(); move(); lookForWorm(); }

42 Switching Images Right Click on the Crab
Click on the switchImage Method The Crab’s Legs will move

43 More Ideas Using different images for the background and the actors
using more different kinds of actors not moving forward automatically, but only when the up-arrow key is pressed building a two-player game by interdicting a second keyboard-controlled class that listens to different keys moving worms to random positions on the screen

44 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.

45 Concept Summary


Download ppt "Chapter 4 - Finishing the Crab Game"

Similar presentations


Ads by Google