crab world l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Crab World PowerPoint Presentation
Download Presentation
Crab World

Loading in 2 Seconds...

play fullscreen
1 / 64

Crab World - PowerPoint PPT Presentation


  • 302 Views
  • Uploaded on

Crab World . Games and Simulations O-O Programming in Java The Walker School. The Walker School – Games and Simulations - 2010. Crab World. Have students play the working game for a few minutes. What are the objects in this world?. What are the classes in this world?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Crab World' - pilar


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
crab world

Crab World

Games and Simulations

O-O Programming in Java

The Walker School

The Walker School – Games and Simulations - 2010

crab world2
Crab World

Have students play the working game for a few minutes.

What are the objects in this world?

What are the classes in this world?

The Walker School – Games and Simulations - 2010

object oriented programming
Object Oriented Programming

The discipline of object-oriented programming is based on methods, subroutines that are attached to objects or object classes. In the compilation technique called threaded code, the executable program is basically a sequence of subroutine calls.

The Walker School – Games and Simulations - 2010

movement
Movement

Method

Call

The Walker School – Games and Simulations - 2010

turning
Turning

Parameter

What happens to the crab if you use a negative number?

The Walker School – Games and Simulations - 2010

standard java class
Standard Java Class

Import (Library) Statements

Class

Signature

Instance Variables

(not shown)

Constructors

(not shown)

Method

Signature

The Walker School – Games and Simulations - 2010

functions and methods
Functions and Methods

A function is a named sequence of statements that performs a desired operation. This operation is specified in a function

definition.

NAME( LIST OF PARAMETERS ):

STATEMENTS

There can be any number of statements inside the function, but they have to

be indented from the left margin.

Creating a new function gives you an opportunity to name a group of statements. Functions can simplify a program by hiding a complex computation behind a single command and by using English words in place of arcane code. Creating a new function can make a program smaller by eliminating repetitive code.

The Walker School – Games and Simulations - 2010

method call
Method Call

This conditional statement uses a built in function to turn an object if it hits the edge of the world.

Method

Body

Method

Call

The Walker School – Games and Simulations - 2010

function or method call
Function (or Method) Call

The value or variable, which is called the argument of the function, has to be enclosed in parentheses. It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value.

turn (speed);

The Walker School – Games and Simulations - 2010

dealing with screen edges
Dealing with Screen Edges

Conditional

Statement

The Walker School – Games and Simulations - 2010

conditionals are decision statements
Conditionals are Decision Statements

Are if … then

statements

Condition

Only executed if something is True.

Instruction(s)

The Walker School – Games and Simulations - 2010

conditional types
Conditional Types

…gives use the ability to check conditions and change the behavior of the program accordingly

  • Branched
  • Chained
  • Nested

if (method())

{

statement;

statement;

}

watch making things move part i http www youtube com watch v kuwt0k4k4 o
Watch – Making Things Move (Part I)http://www.youtube.com/watch?v=KUwt0K4K4_o

The Walker School – Games and Simulations - 2010

api views
API Views

Documentation

View

Source Code

View

Contains

Class Tree

Author

Constructors

Methods

The Walker School – Games and Simulations - 2010

adding random behavior
Adding Random Behavior

What percentage of time will the crab turn 5 degrees?

Operator

The Walker School – Games and Simulations - 2010

operators
Operators

Operators are special symbols that represent computations like addition and multiplication. The values the operator uses are called operands.

Assignment (=)

Comparison (==, <=, >=, !=, etc.)

Logical: &&, ||, ! (and, or, not)

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence.

The Walker School – Games and Simulations - 2010

common operators
Common Operators
  • +
  • -
  • *
  • >
  • <
  • <= or >=
  • ==
  • !=
  • ||
  • &&
  • %

What do each of these operators do?

The Walker School – Games and Simulations - 2010

order of operation
Order of Operation

The order of evaluation depends on the rules of precedence.

PEMDAS

Parentheses ( )

Exponentiation

Multiplication and Division

Operators with the same precedence are evaluated from left to right.

The acronym PEMDAS is a useful way to remember the order of operations

The Walker School – Games and Simulations - 2010

dot notation
Dot Notation

Used when calling a method defined in another class.

Dot

Notation

Class-name.method-name (parameters);

The Walker School – Games and Simulations - 2010

slide20

Explain the behavior being expressed here.

The Walker School – Games and Simulations - 2010

adding a new subclass
Adding a New Subclass

1. Name the world.

2. Choose a new object.

1. Right click on

class.

2. Choose new

subclass.

The Walker School – Games and Simulations - 2010

inheritance
Inheritance

Subclass

The Crab class inherits all of the methods of the Animal class, while the

Animal Class inherits all of the methods of the Greenfoot Actor class.

The Walker School – Games and Simulations - 2010

class diagram relationships
Class Diagram Relationships

What do crabs eat? or What eats a crab?

A “crab” is-a “animal”

The Walker School – Games and Simulations - 2010

creating new methods
Creating New Methods

Find this behavior in the Java API?

Comments

Method

Statement

Imports the behavior from the java.lang.Class to the Worm class.

New methods need to be called in the act() method.

The Walker School – Games and Simulations - 2010

commenting new methods
Commenting New Methods

Comments

Each time you add a new method, you should include a comment explaining what it does. Your comments should be clear and concise, free of spelling and grammatical errors. Think of yourself having to read another programmers code. Would you want it to be easy to read?

The Walker School – Games and Simulations - 2010

programming challenge
Programming Challenge
  • Add a new actor to your program, say a lobster or a human that might eat the crab. You will need to copy the act method from the crab to the lobster, and other methods, such as lookForWorm(), which you will need to change to lookForCrab().

The Walker School – Games and Simulations - 2010

one possible solution
One Possible Solution

Remember to call the method in the act() method.

The Walker School – Games and Simulations - 2010

adding keyboard control
Adding Keyboard Control

static booleanisKeyDown(String key)

Method Call

Class

Method

Returns

Either T or F

Expects a

String

Any

Key

The Walker School – Games and Simulations - 2010

programming challenge29
Programming Challenge
  • Add additional keyboard controls to make the crab move up or down. What other keyboard functionality could you add? For example, you could make the crab jump back 10 pixels when it encounters a lobster if you hit the “j” key.

The Walker School – Games and Simulations - 2010

one possible solution30
One Possible Solution

The Walker School – Games and Simulations - 2010

adding sound
Adding Sound

Challenge: Search the internet for other sound files and add a new sound to the Crab class when it east a worm.

The Walker School – Games and Simulations - 2010

watch making things move part ii http www youtube com watch v qwruhxipdik feature related
Watch – Making Things Move (Part II)http://www.youtube.com/watch?v=QWruHXIPDIk&feature=related

Mover “Helper” Support Class

For more advanced students.

The Walker School – Games and Simulations - 2010

ending the game
Ending the Game

In what class is this

method called?

The Walker School – Games and Simulations - 2010

creating new objects
Creating New Objects

Java keyword “new” allows us to create new objects of any of the existing classes.

The Walker School – Games and Simulations - 2010

java keywords http en wikipedia org wiki list of java keywords
Java Keywordshttp://en.wikipedia.org/wiki/List_of_Java_keywords

Search the list; What is one other keyword that we have used already and what does it mean?

The Walker School – Games and Simulations - 2010

constructors
Constructors
  • A special kind of method that is always automatically executed whenever an instance of a class is created.
  • A constructor has no return type (static or void) between the keyword “public” and the name.
  • The name of the constructor is always the same as the name of the class.

The Walker School – Games and Simulations - 2010

parameter list
Parameter List

Allows us to pass information to the constructor.

The Walker School – Games and Simulations - 2010

arguments and parameters
Arguments and Parameters

Arguments are values that control how the function does its job.

Some functions can take more than one argument.

Values that are passed from one function to another get assigned to variables called parameters.

The Walker School – Games and Simulations - 2010

adding objects automatically
Adding Objects Automatically

To populate a world automatically add characters to the “World” class.

You can add each object individually, or you can create a method that adds actors randomly.

The Walker School – Games and Simulations - 2010

programming challenge40
Programming Challenge
  • Combine what you have learned about random behavior and adding new objects to create a method that would add a random number of worms up to 30 and a random number of lobsters up to 3.

The Walker School – Games and Simulations - 2010

one possible solution41
One Possible Solution

The Walker School – Games and Simulations - 2010

adding a win state counting worms

Adding a Win-State(counting worms)

The Walker School – Games and Simulations - 2010

instance variables step 1
Instance Variables – Step #1

Create variables to hold the number of worms.

An instance variable is memory that belongs to an object.

The Walker School – Games and Simulations - 2010

assignment step 2
Assignment – Step #2

Create an assignment and initiate the variable to 0 at the beginning of the game.

An assignment is a statement that enables us to store something into a variable. It is written with an equal = symbol.

The Walker School – Games and Simulations - 2010

increment the count step 3
Increment the Count – Step #3

The Walker School – Games and Simulations - 2010

crab win state play winner
Crab Win-State, Play Winner

The Walker School – Games and Simulations - 2010

lobster win state play winner
Lobster Win-State, Play Winner

Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom. This is referred to a the Flow of Execution.

The Walker School – Games and Simulations - 2010

programming challenge48
Programming Challenge
  • The current state of your games allows a player to win if their crab eats 8 worms. How would you make sure there were at least 8 worms added to each new game, if you were using a random number generator?

The Walker School – Games and Simulations - 2010

one possible solution49
One Possible Solution

The Walker School – Games and Simulations - 2010

adding a second player

Adding a Second Player

The Walker School – Games and Simulations - 2010

programming challenge51
Programming Challenge
  • Add a second player where one of you plays the crab and another plays the lobster. Each time the game starts, there will be one crab and one lobster. You will need to add keyboard controls for each character. Make sure the key controls are on opposite sides of the keyboard. The number of worms at the beginning of the game can be random.

The Walker School – Games and Simulations - 2010

crab keyboard controls solution
Crab Keyboard Controls Solution

The Walker School – Games and Simulations - 2010

lobster keyboard controls solution
Lobster Keyboard Controls Solution

The Walker School – Games and Simulations - 2010

world population solution
World Population Solution

The Walker School – Games and Simulations - 2010

places to hide

Places to Hide

The Walker School – Games and Simulations - 2010

programming challenge56
Programming Challenge
  • Add places for the crab to hide from the lobster, such as a rock or a beach house. Your choices are to add one rock all the time, or to add a random number of rocks. Probably, shouldn’t be more than 3 objects at a time. Also, how would you make sure that there is always at least one object for the crab to hide behind.

The Walker School – Games and Simulations - 2010

add a rock
Add a Rock

Adds a rock to the world for the crab to hide behind.

The Walker School – Games and Simulations - 2010

random number of rocks
Random Number of Rocks

Remember to comment out the firstRock object.

Otherwise you will get the random number +1 rock.

The Walker School – Games and Simulations - 2010

adding a penalty

Adding a Penalty

The Walker School – Games and Simulations - 2010

extended programming challenge
Extended Programming Challenge
  • Create a penalty for the crab and the lobster. If the crab touches the edge of the world, then another worm is added to the world and his score goes down by 1. If the lobster touches the edge of the world, then a point is added to the crab’s score.

The Walker School – Games and Simulations - 2010

adding a win message

Adding A Win Message

Done after Piano Project

The Walker School – Games and Simulations - 2010

adding a win message62
Adding a Win Message

Step #1: Import the necessary Java libraries.

The Walker School – Games and Simulations - 2010

adding a win message cont
Adding a Win Message (cont.)

Step #2: Create the method.

The Walker School – Games and Simulations - 2010

adding a win message cont64
Adding a Win Message (cont.)

Step #3: Cast the method in the CrabWinState().

The Walker School – Games and Simulations - 2010