Chapter 4 - Finishing the Crab Game

Slides:



Advertisements
Similar presentations
Getting your Feet Green
Advertisements

Getting to know Greenfoot
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 8 - Creating Images and Sound Bruce Chittenden.
Games and Simulations O-O Programming in Java The Walker School
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
MT311 Tutorial Li Tak Sing( 李德成 ). Uploading your work You need to upload your work for tutorials and assignments at the following site:
Road Map Introduction to object oriented programming. Classes
Chapter 41 Defining Classes and Methods Chapter 4.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
Object Oriented Software Development
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th, 2011 Instructor: Scott Settembre.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
Chapter 8 – Mouse Input, Images and Sound. Chapter 8 - Content In contrast to previous chapters, we will not build a complete scenario in this chapter.
Chapter 1 - Getting to know Greenfoot
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Greenfoot Game Programming Club.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Advanced Stuff Learning by example: Responding to the mouse.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Introduction To Greenfoot
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Chapter 4 - Finishing the Crab Game
Chapter 11: Scrolling with Greenfooot. What Is Scrolling? Scrolling is an important and often necessary concept in games. It can be used to: a)Convey.
Greenfoot.
Inner Classes.
Inner Classes 27-Dec-17.
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
User-Written Functions
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Chapter 3 – Improving the Crab Game
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Creating Games with Greenfoot
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
The Little Crab Scenario
Inheritance Basics Programming with Inheritance
Defining Classes and Methods
Variables ICS2O.
Organizing Memory in Java
CHAPTER 6 GENERAL-PURPOSE METHODS
Defining Class Member Data/characteristics
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CLASSES, AND OBJECTS A FIRST LOOK
Generics, Lambdas, Reflections
Methods, Data and Data Types
Greenfoot November 8, 2009.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
The beginning of media computation Followed by a demo
Inner Classes 25-Oct-19.
Presentation transcript:

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 CrabWorld and Select “Open editor”

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

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.

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

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

Calling addObject()

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

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

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

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.

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.

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.

Using Object Variables to store non-primitive Information crab.png crab2.png image1 = new GreenfootImage (“crab.png”); image2 = new GreenfootImage (“crab2.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 image1 = new GreenfootImage(“crab.png”); image2 = new GreenfootImage(“crab2.png”);

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.

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 addObject()

Calling addObject()

Calling addObject()

Calling addObject()

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

Calling populate()

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;

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

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 }

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(); }

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

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);

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

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

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();

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);

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(); }

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

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

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