Chapter 4 - Finishing the Crab Game

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Chapter 10 - Additional Scenario Ideas Bruce Chittenden.
Getting your Feet Green
Getting to know Greenfoot
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.
Chapter 6 - Interacting Objects: Newton’s Lab
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Greenfoot Asteroids Mrs. C. Furman August 16, 2010.
Chapter 1 - Getting to know Greenfoot Bruce Chittenden.
Chapter 1 - Getting to know Greenfoot Acknowledgement: Michael Kolling & Bruce Chittenden.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Test review. When? Monday 9/27 in class Know Greenfoot How many classes? Top-most classes? What does each button do? Lowest child class?
Exam 2 Review. variable types boolean - true or false String - "hello" int - 14 double
“But I don't want to be a “programmer!” ActionScript for journalists Presentation by Mindy McAdams Flashjournalism.com.
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
chipKit Sense Switch & Control LED
CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th, 2011 Instructor: Scott Settembre.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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
Review of CIS 120 Concepts: What you said you want….
Greenfoot Game Programming Club.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction To Greenfoot
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Vex Robotics program three: using motors and sensors together.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Selection Statement Chapter 3. A Java Language Program Is a Class A Class has data declarations and methods A Method has statements or instructions to.
Greenfoot.
Chapter 10: Creating Images and Sound
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
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.
Starting Out with Alice: A Visual Introduction to Programming
Programming: Simple Control Structures
Chapter 3 – Improving the Crab Game
Programming: Simple Control Structures
Creating Games with Greenfoot
The Little Crab Scenario
Programming: Simple Control Structures
Organizing Memory in Java
Programming: Simple Control Structures
Defining Class Member Data/characteristics
Problem Solving with Data Structures using Java: A Multimedia Approach
Breakout in Greenfoot Barb Ericson
Programming: Simple Control Structures
CS139 October 11, 2004.
Programming: Simple Control Structures
Adding Behaviors (methods)
ICT Gaming Lesson 3.
Programming: Simple Control Structures
Methods, Data and Data Types
Greenfoot November 8, 2009.
Relational Expressions
More on Creating Classes
Programming: Simple Control Structures
Presentation transcript:

Chapter 4 - Finishing the Crab Game Bruce Chittenden

4.1 Adding Objects Automatically Right Click on CrabWorld and Select Open editor

Code 4.1 import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() super(560, 560, 1); }

Constructor for CrabWorld public CrabWorld() { super(560, 560, 1); } A Constructor has no Return Type Specified A Constructor Always has the Same Name as the Class Constructor is Automatically Executed whenever and Instance of the Class is Created

Constructor for CrabWorld public CrabWorld() { super(560, 560, 1); } Constructor has no Return Type Specified Between Keyword “public” and the Name Constructor Always has the Same Name as the Class Constructor is Automatically Executed Whenever and Instance of the Class is Created

Add a Crab to the World X (0, 0) public CrabWorld() { super(560, 560, 1); addObject ( new Crab(), 150, 100); } Y Create a New Crab and Add it at Location x=150 and y=100

4.2 Creating New Objects Creates a New Instance of the Class Crab new Crab() addObject ( new Crab(), 150, 100); Creates a New Instance of the Class Crab When We Create a New Object , We Must Do Something with It

Exercise 4.1

The Crab is Automatically Created in CrabWorld Exercise 4.1 The Crab is Automatically Created in CrabWorld

Exercise 4.2

Exercise 4.2

Exercise 4.3

Exercise 4.3

Create a Method called populateWorld () Exercise 4.4 Create a Method called populateWorld ()

Exercise 4.4

Exercise 4.5 Call to getRandomNumber () for each of the coordinates. I used a for loop so I did not have to type this line 10 times.

Exercise 4.5

4.3 Animating Images Crab with legs in Crab with legs out Animation is Achieved by Switching Between the Two Images

4.4 Greenfoot Images

Exercise 4.6

4.5 Instance Variables (Fields) private variable-type variable-name An instance variable is a variable that belongs to the object (an instance of a class).

Code 4.2 import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) // comment omitted public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; // methods omitted }

Exercise 4.7

Exercise 4.8 Variables for the Crabs Location in the World A Variable of the Crabs Image

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

Exercise 4.10 The Variables Have Not Been Initialized and Contain null

4.6 Assignment image1 = new GreenfootImage (“crab.png”);

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

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

Exercise 4.11 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); }

Exercise 4.12

4.8 Alternating The Images Pseudo Code if ( our current image is image1 ) then use image2 now else use image1 now Actual Code if ( getImage() == image1 ) setImage (image2); else setImage (image1);

Code 4.4 if ( getImage() == image1 ) { setImage (image2); } else

if/else When Only One Statement The Method getImage will return the actor’s current image. if ( getImage() == image1 ) setImage (image2); else setImage (image1); The == is a comparison operator, not to be confused with = which is an assignment operator.

Comparison Operators < <= == >= > != Operator Meaning less than <= less than or equal to == equal to >= greater than or equal to > greater than != not equal

4.9 The if/else Statement If ( condition) { statements; } else if-clause else-clause If the condition is TRUE the if-clause will be executed, otherwise the else-clause will be executed

Exercise 4.13 /* * 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();

Exercise 4.13

Exercise 4.14 /* * 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);

act Method /* * 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(); switchImage(); move(); lookForWorm(); }

Exercise 4.14

Exercise 4.15 Right Click on the Crab Click on the switchImage Method The Crab’s Legs will move

4.10 Counting Worms private int wormsEaten; wormsEaten = 0; An instance variable to store the current count of worms eaten private int wormsEaten; An assignment that initializes this variable to 0 at the beginning wormsEaten = 0; Code to increment our count each time we eat a worm wormsEaten = 0; Code that checks whether we have eaten eight worms and stops the game and plays the sound it we have wormsEaten = wormsEaten + 1;

Code 4.5 /* * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. */ public void lookForWorm() { if ( canSee(Worm.class) ) eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if ( wormsEaten ==8) Greenfoot.playSound ("fanfare.wav"); Greenfoot.stop(); }

Exercise 4.16

Exercise 4.16

Exercise 4.17

4.11 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 making new worms pop up when one is eaten (or at random times) many more that you can come up with yourselves

Exercise 4.18 /* * 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 (timeToSwitchImages >= 3) if (getImage() == image1) setImage (image2); else setImage (image1); timeToSwitchImages = 0; } timeToSwitchImages++;

Exercise 4.18

4.12 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 instance variables and assignment statements to store information, and how to access that information later.

Concept Summary