asteroids games and simulations o-o programming in java the walker school the walker school –...

71
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Upload: harvey-mcdaniel

Post on 17-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Asteroids

Games and SimulationsO-O Programming in Java

The Walker School

The Walker School – Games and Simulations - 2010

Page 2: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Asteroids

The Walker School – Games and Simulations - 2010

Take some time to play with Asteroids. What can the program do and what can’t it do?

Page 3: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Discovering Keyboard Controls

The Walker School – Games and Simulations - 2010

Which keyboard key is used to fire a bullet?

Page 4: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Explosions

The Walker School – Games and Simulations - 2010

Place an explosion into a running scenario. Does it work? What does it do?

Page 5: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Painting Stars

The Walker School – Games and Simulations - 2010

Page 6: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a New Scenario

The Walker School – Games and Simulations - 2010

Directions1.Create a new scenario.2.Save it as “asteriods-lastname”.

Page 7: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create the World

The Walker School – Games and Simulations - 2010

Directions1.Create a new subClass called “Space”.2.Leave the background empty.

Page 8: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Enlarge the Background

The Walker School – Games and Simulations - 2010

Directions:1.Change the size of the world (600, 400, 1).2.Change the comment.

Page 9: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Painting Backgrounds

The Walker School – Games and Simulations - 2010

Part IV - http://www.youtube.com/watch?v=rvZ0ErpfHKM&feature=related

Page 10: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Import the Java Color Library

The Walker School – Games and Simulations - 2010

Page 11: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Use Java to Paint Black Background

The Walker School – Games and Simulations - 2010

Page 12: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a method createStars()

The Walker School – Games and Simulations - 2010

Comment it out until we create the method.

Page 13: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Method Body

The Walker School – Games and Simulations - 2010

Takes a parameter of type integer, as we stated we wanted 300 stars.

Page 14: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create The Stars

The Walker School – Games and Simulations - 2010

Step through this loop and explain what is happening.

Page 15: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

For Loops

The Walker School – Games and Simulations - 2010

for (initialization; loop-condition; increment){ loop-body;}

•The initialization part declares and initializes a loop variable. This variable is often called i, and often initialized to 0.•The loop condition checks whether our loop variable is still less than a given limit. If it is, the loop will continue.•The increment section simply increments the loop variable.

Page 16: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Vary Star Brightness

The Walker School – Games and Simulations - 2010

Page 17: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Rocket Firing

The Walker School – Games and Simulations - 2010

Page 18: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Turning

The Walker School – Games and Simulations - 2010

Page 19: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Flying Forward

The Walker School – Games and Simulations - 2010

Page 20: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Flying Forward

The Walker School – Games and Simulations - 2010

Calls the move() method from SmoothMover class.

What happens?

Page 21: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

What Do We Need To Do?• Add initial movement to the rocket. • To add movement, we need to add force to the

rocket.• And we need to change its vector.• So we’ll need to call this vector from the Vector

abstract class.• The vector needs 2 variables: direction (dy) and

length (dx).

The Walker School – Games and Simulations - 2010

So where are we going to do this?

Page 22: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Adding Drift

The Walker School – Games and Simulations - 2010

What happens when you change the variables in the vector?

Page 23: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

What Do We Need To Do?

• Create variables to handle each rocket image. • Add a method called ignite to checkKeys(), so

that we when hit the “up” button, the rocket image is changed.

• Define a method called ignite. It will need a boolean parameter and a void return type.

The Walker School – Games and Simulations - 2010

Page 24: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Method Stub to Ignite Rocket

The Walker School – Games and Simulations - 2010

Remember to create an instance variable called boosterOn.

Page 25: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Igniting the Rocket Boosters

The Walker School – Games and Simulations - 2010

Page 26: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Colliding with Asteroids

The Walker School – Games and Simulations - 2010

Page 27: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

What Do We Need to Do?

• If we collide with an asteroid,• Remove the rocket from the world;• Place an explosion into the world;• Show final score (game over);

The Walker School – Games and Simulations - 2010

Page 28: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Methods for Collision Detectionfile:///C:/Greenfoot/doc/API/index.html

The Walker School – Games and Simulations - 2010

Both methods accept a parameter of type Class, which means we can check for intersections with a specific class of objects if we want to.

Which of these 2 methods do we want to use and why?

Page 29: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Colliding with Asteroids

The Walker School – Games and Simulations - 2010

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

Page 30: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Creating Better Explosions

• Part I - http://www.youtube.com/watch?v=rgZ-_vQ2aIc• Part II - http://www.youtube.com/watch?v=bdv4ZgTcYyY&feature=related• Part III - http://www.youtube.com/watch?v=OFuDEt0xifM&feature=related• Part IV - http://www.youtube.com/watch?v=XMJi4_bzuLw&feature=related• Part V - http://www.youtube.com/watch?v=TKYwL8p_11A&feature=related

The Walker School – Games and Simulations - 2010

Page 31: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

What Do We Need To Do?

• Add abstract classes SmoothMover and Vector from Greenfoot.

• Create a constructor for Debris• Update the move() method

Page 32: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Casting

The Walker School – Games and Simulations - 2010

Page 33: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create the Score Board

The Walker School – Games and Simulations - 2010

Page 34: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Import Java Libraries

The Walker School – Games and Simulations - 2010

Page 35: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create the Assignment Variables

The Walker School – Games and Simulations - 2010

Page 36: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create the Constructors

Why are their 2 constructors?

How is this an example of overloading?

The Walker School – Games and Simulations - 2010

Page 37: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Make Scoreboard Image

The Walker School – Games and Simulations - 2010

So, what else needs to be done?

Page 38: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Activity – Change Attributes

• Modify the ScoreBoard class:– Change the text shown on it.– Change the color of the text.– Change the background and frame colors.– Change the font size so that your text fits well.– Change the width of the scoreboard to suit your

text.

The Walker School – Games and Simulations - 2010

Page 39: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Homework – Scoreboard

• Search the Greenfoot library for other games that have scoreboards. Study how they were built. What attributes do they have that this board does not? What could you add to your scoreboard?

The Walker School – Games and Simulations - 2010

Page 40: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add a Variable for the Counter

The Walker School – Games and Simulations - 2010

Page 41: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Assign the Counter to the Constructor

The Walker School – Games and Simulations - 2010

Page 42: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Call the Counter When Game Over

The Walker School – Games and Simulations - 2010

How can you make sure that the counter is in the middle of the board without hard coding the numbers?

Page 43: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Ensure the Scoreboard is in the Middle of the Background

The Walker School – Games and Simulations - 2010

Where do we call the gameOver() method?

Page 44: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Call the Game Over Method

The Walker School – Games and Simulations - 2010

Example of Casting

Page 45: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Casting

• Casting is the technique of telling the compiler a more precise type for our object then it can work out for itself. We do this by writing the class name (Space) in parentheses before the method call. Once we have done this we can call methods defined in Space

• space.gameOver();Casting does not change the type of the object returned.

The Walker School – Games and Simulations - 2010

Page 46: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add a Proton Wave

What does it do?

What doesn’t it do?

The Walker School – Games and Simulations - 2010

Page 47: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Import the Library

The Walker School – Games and Simulations - 2010

We are going to need the Java List class because we are going to create a list of images to create a proton wave that increases in size.

Page 48: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Assign Variables

The Walker School – Games and Simulations - 2010

How do we make the wave grow?

Page 49: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add A List (Array) to Store Image Set

The Walker School – Games and Simulations - 2010

How many images will this list store?

Page 50: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Constructer for the Wave

The Walker School – Games and Simulations - 2010

Page 51: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Method to Initialize Images

The Walker School – Games and Simulations - 2010

Page 52: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Instance Field to Set Image Count

Page 53: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Method to Initialize Images

The Walker School – Games and Simulations - 2010

Rewrite the While loop using a For loop. What needs to change?

Page 54: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Method Stub to Grow Wave

The Walker School – Games and Simulations - 2010

Page 55: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Call Method to Grow the Wave

The Walker School – Games and Simulations - 2010

Page 56: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add Statements to Grow Wave

The Walker School – Games and Simulations - 2010

Square [] brackets, not curly {} brackets here

Page 57: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add Sound to the Proton Wave

The Walker School – Games and Simulations - 2010

Page 58: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create a Method Stub to Ignite Proton Wave

The Walker School – Games and Simulations - 2010

Where do we create this method?

Page 59: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Call Method When “Z” is Pressed

The Walker School – Games and Simulations - 2010

What is the problem with the proton wave?

1. It can be released too easily!

2. It doesn’t do anything to the asteroids!

Page 60: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Create Reload and Delay for Wave

The Walker School – Games and Simulations - 2010

Page 61: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add an Instance Variable for the Delay

The Walker School – Games and Simulations - 2010

Where do we create these variables?

Page 62: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Assign the Delay to the Constructor

The Walker School – Games and Simulations - 2010

Page 63: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Add Delay Count to act() Method

The Walker School – Games and Simulations - 2010

Page 64: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Program the Delay into the Wave

The Walker School – Games and Simulations - 2010

Page 65: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Interacting with Objects in a Range

The Walker School – Games and Simulations - 2010

Page 66: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Program a Method Stub to Check for Collision

The Walker School – Games and Simulations - 2010

Purpose is to check to see if the wave touches and asteroid.

So, in what Class do we create this method?

Page 67: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Call it in the Wave act() Method

The Walker School – Games and Simulations - 2010

Page 68: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Write Statements to Check for Collision

The Walker School – Games and Simulations - 2010

Why didn’t we use the “getIntersectingObjects() method?

Range is the radius of the circle (wave).

Page 69: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

hit() Method

The Walker School – Games and Simulations - 2010

What are the parameters for this method and what does it return?

Page 70: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Evaluating the Damage Constant

The Walker School – Games and Simulations - 2010

What happens when you change the damage constant of the proton wave?

Page 71: Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

For Further Development

• Fix the score counter.• Add new asteroids when all have been cleared.• Add a level counter. Every time all asteroids are

destroyed you go up a level.• Add an end of level sound.• Add an indicator showing the load state of the

proton wave.• Add a shield.

The Walker School – Games and Simulations - 2010