64
Crab World Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Crab World

  • Upload
    hakan

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

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?. - PowerPoint PPT Presentation

Citation preview

Page 1: Crab World

Crab World

Games and SimulationsO-O Programming in Java

The Walker School

The Walker School – Games and Simulations - 2010

Page 2: Crab World

Crab World

The Walker School – Games and Simulations - 2010

What are the objects in this world?

Have students play the working game for a few minutes.

What are the classes in this world?

Page 3: Crab World

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

Page 4: Crab World

Movement

The Walker School – Games and Simulations - 2010

MethodCall

Page 5: Crab World

Turning

The Walker School – Games and Simulations - 2010

Parameter

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

Page 6: Crab World

Standard Java Class

The Walker School – Games and Simulations - 2010

Method Signature

ClassSignature

Import (Library) Statements

Constructors(not shown)

Instance Variables(not shown)

Page 7: Crab World

Functions and MethodsA function is a named sequence of statements that performs a desired operation. This operation is specified in a functiondefinition.

NAME( LIST OF PARAMETERS ):STATEMENTS

There can be any number of statements inside the function, but they have tobe 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

Page 8: Crab World

Method Call

The Walker School – Games and Simulations - 2010

MethodCall

Method Body

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

Page 9: Crab World

Function (or Method) CallThe 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

Page 10: Crab World

Dealing with Screen Edges

The Walker School – Games and Simulations - 2010

ConditionalStatement

Page 11: Crab World

Conditionals are Decision Statements

The Walker School – Games and Simulations - 2010

Are if … thenstatements

Only executed if something is True.

Condition

Instruction(s)

Page 12: Crab World

Conditional Types

• Branched• Chained• Nested

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

if (method()) statement; statement;

Page 13: Crab World

Watch – Making Things Move (Part I)http://www.youtube.com/watch?v=KUwt0K4K4_o

The Walker School – Games and Simulations - 2010

Page 14: Crab World

API Views

Documentation View

Source CodeView

Contains1. Class Tree2. Author3. Constructors4. Methods

The Walker School – Games and Simulations - 2010

Page 15: Crab World

Adding Random Behavior

The Walker School – Games and Simulations - 2010

What percentage of time will the crab turn 5 degrees?

Operator

Page 16: Crab World

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

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

Assignment (=)Comparison (==, <=, <=, etc.)Logical (and, or, not)

The Walker School – Games and Simulations - 2010

Page 17: Crab World

Common Operators

• +• -• *• >• <• <= or >=

• ==• !=• ||• &• %

The Walker School – Games and Simulations - 2010

What do each of these operators do?

Page 18: Crab World

Order of Operation

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

The order of evaluation depends on the rules of precedence.

PEMDASParentheses ( )ExponentiationMultiplication and Division

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

The Walker School – Games and Simulations - 2010

Page 19: Crab World

Dot Notation

The Walker School – Games and Simulations - 2010

Dot Notation

Used when calling a method defined in another class.

Class-name.method-name (parameters);

Page 20: Crab World

The Walker School – Games and Simulations - 2010

Explain the behavior being expressed here.

Page 21: Crab World

Adding a New Subclass

The Walker School – Games and Simulations - 2010

1. Right click onclass.2. Choose newsubclass.

1. Name the world.2. Choose a new

object.

Page 22: Crab World

Inheritance

The Walker School – Games and Simulations - 2010

The Crab class inherits all of the methods of the Animal class, while theAnimal Class inherits all of the methods of the Greenfoot Actor class.

Subclass

Page 23: Crab World

Class Diagram Relationships

The Walker School – Games and Simulations - 2010

A “crab” is-a “animal”

What do crabs eat? or What eats a crab?

Page 24: Crab World

Creating New Methods

The Walker School – Games and Simulations - 2010

Comments

Method Statement

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

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

Find this behavior in the Java API?

Page 25: Crab World

Commenting New Methods

The Walker School – Games and Simulations - 2010

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?

Page 26: Crab World

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

Page 27: Crab World

One Possible Solution

The Walker School – Games and Simulations - 2010

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

Page 28: Crab World

Adding Keyboard Control

The Walker School – Games and Simulations - 2010

static boolean isKeyDown(String key)

ClassMethod

ReturnsEither T or F

Expects aString

AnyKey

Method Call

Page 29: Crab World

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

Page 30: Crab World

One Possible Solution

The Walker School – Games and Simulations - 2010

Page 31: Crab World

Adding Sound

The Walker School – Games and Simulations - 2010

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

Page 32: Crab World

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

The Walker School – Games and Simulations - 2010

Mover “Helper” Support Class

For more advanced students.

Page 33: Crab World

Ending the Game

The Walker School – Games and Simulations - 2010

In what class is thismethod called?

Page 34: Crab World

Creating New Objects

The Walker School – Games and Simulations - 2010

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

Page 35: Crab World

Java Keywordshttp://en.wikipedia.org/wiki/List_of_Java_keywords

The Walker School – Games and Simulations - 2010

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

Page 36: Crab World

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

Page 37: Crab World

Parameter List

The Walker School – Games and Simulations - 2010

Allows us to pass information to the constructor.

Page 38: Crab World

Arguments and ParametersArguments 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

Page 39: Crab World

Adding Objects Automatically

The Walker School – Games and Simulations - 2010

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.

Page 40: Crab World

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

Page 41: Crab World

One Possible Solution

The Walker School – Games and Simulations - 2010

Page 42: Crab World

Adding a Win-State(counting worms)

The Walker School – Games and Simulations - 2010

Page 43: Crab World

Instance Variables – Step #1

The Walker School – Games and Simulations - 2010

An instance variable is memory that belongs to an object.

Create variables to hold the number of worms.

Page 44: Crab World

Assignment – Step #2

The Walker School – Games and Simulations - 2010

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.

Page 45: Crab World

Increment the Count – Step #3

The Walker School – Games and Simulations - 2010

Page 46: Crab World

Crab Win-State, Play Winner

The Walker School – Games and Simulations - 2010

Page 47: Crab World

Lobster Win-State, Play Winner

The Walker School – Games and Simulations - 2010

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.

Page 48: Crab World

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

Page 49: Crab World

One Possible Solution

The Walker School – Games and Simulations - 2010

Page 50: Crab World

Adding a Second Player

The Walker School – Games and Simulations - 2010

Page 51: Crab World

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

Page 52: Crab World

Crab Keyboard Controls Solution

The Walker School – Games and Simulations - 2010

Page 53: Crab World

Lobster Keyboard Controls Solution

The Walker School – Games and Simulations - 2010

Page 54: Crab World

World Population Solution

The Walker School – Games and Simulations - 2010

Page 55: Crab World

Places to Hide

The Walker School – Games and Simulations - 2010

Page 56: Crab World

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

Page 57: Crab World

Add a Rock

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

The Walker School – Games and Simulations - 2010

Page 58: Crab World

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

Page 59: Crab World

Adding a Penalty

The Walker School – Games and Simulations - 2010

Page 60: Crab World

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

Page 61: Crab World

Adding A Win Message

Done after Piano Project

The Walker School – Games and Simulations - 2010

Page 62: Crab World

Adding a Win MessageStep #1: Import the necessary Java libraries.

The Walker School – Games and Simulations - 2010

Page 63: Crab World

Adding a Win Message (cont.)

Step #2: Create the method.

The Walker School – Games and Simulations - 2010

Page 64: Crab World

Adding a Win Message (cont.)

The Walker School – Games and Simulations - 2010

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