Chapter 3 – Improving the Crab Game

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Microsoft® Small Basic
Getting your Feet Green
Getting to know Greenfoot
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.
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
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
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
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
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Chapter 1 - Getting to know Greenfoot
Greenfoot Game Programming Club.
Can I get your number? By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Improving the Crab Mrs. C. Furman August 19, 2010.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Computer Science Up Down Controls, Decisions and Random Numbers.
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.
Chapter 4 - Finishing the Crab Game
LOGIC GATES. INTRODUCTION TO LOGIC GATES Boolean functions may be practically implemented by using electronic gates. The following points are important.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Making Choices with if Statements
The Selection Structure
Mindsensors PSP-Nx Controller Simon Game
Programming Concepts (Part B) ENGR 10 Introduction to Engineering
Let's Race! Typing on the Home Row
Creating Games with Greenfoot
Version 2.
The Little Crab Scenario
CIS16 Application Development and Programming using Visual Basic.net
Making Logical Decisions (IF-THEN-ELSE)
Defining Class Member Data/characteristics
We’re moving on to more recap from other programming languages
Breakout in Greenfoot Barb Ericson
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Logical Operations In Matlab.
CS139 October 11, 2004.
Chapter 5: Control Structure
While Loops and If-Else Structures
Conditional Logic Presentation Name Course Name
Learning Objectives Explain how selection is used to change a program output Decompose a problem with inputs to help design a program Describe the use.
Robotics Programming Using Shaft Encoders
Adding Behaviors (methods)
ICT Gaming Lesson 3.
Robotics Programming Using Shaft Encoders
Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei
Methods, Data and Data Types
Greenfoot November 8, 2009.
Relational Expressions
Robotics Programming Using Shaft Encoders
creating a ecosystems model in net logo
Conditionals and Loops
WJEC GCSE Computer Science
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Bishopston Comprehensive School
Presentation transcript:

Chapter 3 – Improving the Crab Game

3.1 - Adding Random Behavior: more complicated behaviors topics: random behavior, keyboard control, sound concepts: dot notation, random numbers, defining methods, comments We’ll use a Greenfoot Utility A Greenfoot utility is a method supplied by the Greenfoot environment, so the method starts with “Greenfoot.” in its name (“Dot-notation”). Greenfoot.getRandomNumber(limit) This will give us a random number between 0 and 20. The limit is --20 This method returns a random number that can be any value from zero up to but not including the given limit (so from zero to limit-1)

Comparison Operators An example: if (5 > 1) {move();} Comparison Operators are used to compare two numerical values. They always produce a definitive result, either true or false (i.e. they produce a single boolean value). Comparison Operators are often used as the condition within an if – else structure. An example: if (5 > 1) {move();}

Comparison Operators Note: a and b are numerical values < less than ex: if (a < b) { … <= less than or equal ex: if (a <= b) { … == equal ex: if (a == b) { … != not equal ex: if (a != b) { … >= greater than or equal ex: if (a >= b) { … > greater than ex: if (a > b) { …

Logical Operators An example: if (5 < 1 && 0 > 1) {move();} Logical Operators are used to logically link two boolean values. They always produce a definitive result, either true or false (i.e. they produce a single boolean value). Comparison Operators are often used as part of the condition in an if – else structure. An example: if (5 < 1 && 0 > 1) {move();}

Logical Operators Note: a and b are numerical values && Logical AND Ex: if (a < b && b < c) { … Note: both a < b and b < c must be true, or the entire condition evaluates to false || Logical OR Ex: if (a < b || b < c) { … Note: either a < b or b < c must be true, or the entire condition evaluates to false

Logical Operators (Truth Table) One way to visualize all possible sets of inputs -> outputs of a logical operation are Truth Tables. Inputs Results AND OR a b a && b a || b true false

What does it do. // This class define a crab. Code 3 What does it do? // This class define a crab. Code 3.1 from your textbook public void act( ) { if ( atWorldEdge( ) ) turn( 17 ); } if ( Greenfoot.getRandomNumber( 100 ) < 10 ) turn( 15 ); move( ); Gives you random numbers from 0 to 99 Note: The random number will be always <100 and <10 approx. 10% of the time

What does it do? Crabs tend to walk erratically, so this makes the behavior more realistic. But the crab always turns to the right. How can we make the crab turn either left or right randomly? 10% of the time the crab turns 15 degrees Clockwise (to the right)

What does it do? public void act( ) { if ( atWorldEdge( ) ) turn( 17 ); } if ( Greenfoot.getRandomNumber( 100 ) < 10 ) turn( Greenfoot.getRandomNumber( 46 ) ); move( ); Now it turns from 0 to 45 Degrees to the right

What does it do? public void act( ) { if ( atWorldEdge( ) ) turn( 17 ); } if ( Greenfoot.getRandomNumber( 100 ) < 10 ) turn( Greenfoot.getRandomNumber( 91 ) - 45 ); move( ); Now it turns from 0 to 90 minus 45 That equals 0-45 to 90-45 That equal -45 to 45

What does it do? All Crab Behavior is Independently Random Because Greenfoot.getRandomNumber () Returns a Different Random Number on Each Call

Adding Worms Right Click on the Animal Class and then Click on New subclass

3.2 - Adding worms Crabs like to eat worms We can add new actor classes to a Greenfoot Scenario New subclass from one of the exsiting actor classes. Add new class Worm (Subclass of class Animal)

Adding Worms Choose the worm image

Adding Lobsters Right Click on the Animal Class and then Click on New subclass

Animating Actors Worm facing right Worm facing left (Worm.png) (Worm2.png) Animation is Achieved by Switching Between the two or more Images

The Worm act() method (if-else) Keeps count of calls to Act() method private int counter = 0; private int imageNumber = 1; public void act() { counter = counter + 1; if (counter == 4) { if (imageNumber == 1) { // image 1 so switch to image 2 setImage("worm2.png"); imageNumber = 2; } else { // image 2 so switch to image 1 setImage("worm.png"); imageNumber = 1; } counter = 0; } } Keeps track of which image is showing

The Worm act() method (&&) Keeps count of calls to Act() method private int counter = 0; private int imageNumber = 1; public void act() { counter = counter + 1; if (counter == 4 && imageNumber == 1) { counter = 0; setImage("worm2.png"); imageNumber = 2; } if (counter == 4 && imageNumber == 2) { counter = 0; setImage("worm.png"); imageNumber = 1; } } Keeps track of which image is showing

The Worm act() method (comb.) Keeps count of calls to Act() method private int counter = 0; private int imageNumber = 1; public void act() { counter = counter + 1; if (counter == 4 && imageNumber == 1) { counter = 0; setImage("worm2.png"); imageNumber = 2; } else if (counter == 4 && imageNumber == 2) { counter = 0; setImage("worm.png"); imageNumber = 1; } } Keeps track of which image is showing

Eating worms Now we can add new behavior to the crab: When the crab runs into a worm, it eats it.

Crabs Ignore Worms

Crabs Eat Worms

3.4 - Creating new methods Adding new behavior to the crab – turning at the edge of the world, occasional random turn, and eating worms. Add new method called “lookForWorm”. The first four lines are a comment, which is ignored by computer. We have to call the method “ lookForWorm(); form act() method

Complete the source code after this restricting show below

3.5 - Adding a Lobsters Choose the lobster image

The Lobster methods – 3.5 – Adding a Lobster Copy the complete act method from the Crab class into the Lobster class. Also copy the complete lookForWorm, turnAtEdge and randomTurn methods. Change the Lobster code so that it looks for crabs, rather than worms. You can do that by changing every occurrence of “Worm” in the source code to “Crab”. For instance, where Worm.class is mentioned, change it to Crab.class. Also change the name lookForWorm to lookForCrab. Make sure to update your comments.

New Lobster act() method public void act() { turnAtEdge(); randomTurn(); move(); lookForCrab(); }

Did Crabs Eat all the Worms?

Adding more complicated behaviors: Keyboard control boolean Greenfoot.isKeyDown( String key ) Checks whether a given key is currently pressed down Greenfoot.isKeyDown("left") returns true if the left arrow key is currently pressed down. Greenfoot.isKeyDown("right") returns true if the right arrow key is currently pressed down.

Create checkKeypress() method for the Crab class

New Crab act() method public void act() { checkKeypress(); // no more random turns move(); lookForWorm(); }

New Lobster lookForCrab() method /* * Check whether we have stumbled upon a crab. * If we have, eat it. If not, do nothing. */ public void lookForCrab() { if ( canSee(Crab.class) ) eat(Crab.class); Greenfoot.stop(); } Ends the game

3.8 - Adding Sound to Crab class Another improvement to our game is the addition of sounds. Again, a method in the Greenfoot class helps us with this. /* * 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"); } Plays Sound Effect

Adding Sound to Lobster class /* * Check whether we have stumbled upon a crab. * If we have, eat it. If not, do nothing. */ public void lookForCrab() { if ( canSee(Crab.class) ) eat(Crab.class); Greenfoot.playSound("au.wav"); Greenfoot.stop(); } Plays Sound Effect

Playing the Game The little-crab-4 version of this scenario shows the solution to this. It is a version of the project that includes all the functionality we have discussed so far: worms, lobsters, keyboard control, and sound (Figure 3.4).