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

55
Piano Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

Upload: martha-underwood

Post on 24-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

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

Piano

Games and SimulationsO-O Programming in Java

The Walker School

The Walker School – Games and Simulations - 2010

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

Piano World

Piano Constructor

Piano Class

Method that specifies the size and resolution of the world

What are the Java keywords in this class?

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

Java Key Words

The Walker School – Games and Simulations - 2010

Keywords are words which have a predefined meaning in the language; because of this, programmers cannot use keywords as names for variables, methods, classes, or as any other identifier.

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

Animating Keys

KeyConstructor

KeyMethod

The Walker School – Games and Simulations - 2010

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

The Walker School – Games and Simulations - 2010

Animated White Key

Switches between two images: white-key and white-key down.

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

The Walker School – Games and Simulations - 2010

Variable that stores “true” while the piano key is down, and “false” while it isn’t.

Checking if a Key is Down

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

The Walker School – Games and Simulations - 2010

Producing the Sound

Creates a play method.

Calls the play method.

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

Abstraction

• A concept or idea not associated with any specific instance. In the case of the piano, we are creating a general Key class that will act the same with each key, i.e. play a sound when a key is pressed.

• Thus, we avoid repeating many lines of code.

The Walker School – Games and Simulations - 2010

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

The Walker School – Games and Simulations - 2010

Step #1 - Create Instance Variables

Create a variable to hold the name of the key.

Create a variable to hold the sound file.

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

Step #2 - Assignment

Add parameters to the constructor, so that bits of information can be passed in when the object is constructed.

The Walker School – Games and Simulations - 2010

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

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

How many parameters can a constructor take?

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

Programming Challenge

What needs to be added in our play() method to pass a sound from the constructor to the play method which activates a key?

The Walker School – Games and Simulations - 2010

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

One Possible Solution

The Walker School – Games and Simulations - 2010

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

Step #3 – Construct Piano

The Walker School – Games and Simulations - 2010

Places a key at the top of the keyboard.

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

Programming Challenge

• Write code to create a second piano key that plays a middle-g (sound file 3g.wav), when the “f” key is pressed. Place the key exactly to the left of the first key.

The Walker School – Games and Simulations - 2010

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

One Possible Solution

The Walker School – Games and Simulations - 2010

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

Loops

• For example: while (condition){

statement;statement;…

}

The Walker School – Games and Simulations - 2010

Loops allow you to express commands repeatedly.

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

Creating Piano Keys w/ Loop

The Walker School – Games and Simulations - 2010

What is the problem here when you run it?

What is the local variable here?

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

Local Variables

• Example: int i = 0;while (i < 100) { statement; statement; …

i = i + 1; }

The Walker School – Games and Simulations - 2010

Most common error is to forget to increment the loop counter. If you don’t do it the variable will not change. This would produce an infinite loop.

When you create a local variable inside a function, it only exists inside thefunction, and you cannot use it outside.

Also, Local variables are declared inside the function and have no visibility modifier (private or public).

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

Programming Challenge

• What do you need to do to make sure that the keys are not on top of each other? You will need to figure out how to move each key to a different location on the x access. Each key is 63 pixels wide.

The Walker School – Games and Simulations - 2010

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

One Possible Solution

The Walker School – Games and Simulations - 2010

You only have to change the x variable in the coordinates, as all of the keys need to be the same distance from the top.

But this causes us another problem. What is it and what’s the solution?

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

One Possible Solution

The Walker School – Games and Simulations - 2010

Greenfoot refers to the center point of an object, so the first key is placed at x-coordinates 0 and half the key is off the board.

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

One Big Problem

The Walker School – Games and Simulations - 2010

The problem here is that all of our keys now play middle “g” sound. This is because the are created with a loop and then placed in different positions.

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

Arrays

• Data structures that hold information. It can hold many variables and thus can store many values. Sometimes lists are used, rather than arrays.

• Elements in the array can be accessed using an index (the number inside the [ ] brackets).

The Walker School – Games and Simulations - 2010

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

White-Key Arrays

The Walker School – Games and Simulations - 2010

2D array (list) used for white keys

2D array (list) used for white key notes

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

Lists

A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements.

Lists and strings—and other things that behave like ordered sets—are called sequences.

A list within another list is said to be nested.

[10, 20, 30, 40] (a list of ints)["spam", "bungee", "swallow"] (a list of strings)

The Walker School – Games and Simulations - 2010

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

White-Key Method w/ While Loop

The Walker School – Games and Simulations - 2010

Creates a local variable “I” and sets it’s initial value to “0”

Create a new white key and associate it with the sound file in the whiteNotes list then append “.wav”

As long as “i” < the length of our white-key array, do the following…

Add the key to a location on the keyboard.

Count up by 1 each time through the loop…required.

String concatenation

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

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

Key Method w/ For Loop

The Walker School – Games and Simulations - 2010

What is the structural difference between these 2 loops?

Is one loop structure better than the other for what we need we need to create the keys for our piano?

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

Programming Challenge

• The sounds folder of the program contains the key sounds from other octaves on the piano. How would you change the octave played by the piano?

The Walker School – Games and Simulations - 2010

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

One Possible Solution

The Walker School – Games and Simulations - 2010

Changes the key sounds down one octave.

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

Activity

• Find different sound files online or use audio recorder software (e.g. Audacity) to record sounds from every day life. Move these into the sound folder of your program. Substitute these sounds for the piano sounds in your array. Create a piece of music with these sounds.

The Walker School – Games and Simulations - 2010

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

Creating the Black Keys

The Walker School – Games and Simulations - 2010

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

The Issue

• We could go back and create everything over for the black keys that we did for the white keys, but that would not be very efficient. What we want is a Key class that will play either a white key or a black key. This is also abstraction, as we can use this method for either type of key. If we have a general Key class, the we can extend it to playing the keys for other types of instruments, say a set of trombone notes.

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

Step #1 – Instance Variable

The Walker School – Games and Simulations - 2010

Here we have a variable that will hold the key name of type String for either a white or black key. We also have a variable that will hold a sound. So we don’t need to make any changes from before.

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

Step #2 – Assignment

The Walker School – Games and Simulations - 2010

You will need to add 2 parameters to pass the variables each time you create an instance of a key, whether it is black or white.

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

Programming Challenge

The Walker School – Games and Simulations - 2010

What is the problem here?

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

One Possible Solution (Step #1 – Instance Variable)

The Walker School – Games and Simulations - 2010

Create 2 more variables to show an “upImage” or a “downImage” whether it is a white or black key.

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

One Possible Solution (Step #2 – Assignment)

The Walker School – Games and Simulations - 2010

Assign these variables to the constructor so they are created each time a white or black key is created.

Why do we need setImage(upImage)?

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

Possible Compile Error

The Walker School – Games and Simulations - 2010

Cannot find symbol – constructor Key(java.lang.String, java.lang.String, int, int)

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

For Loop to Create Black Keys

The Walker School – Games and Simulations - 2010

How can we use the white key array list to create our black keys?

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

One Possible Solution (Step #3 –Methods)

The Walker School – Games and Simulations - 2010

Check to see that your methods reflect these changes.

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

Programming Challenge

• What would you need to do to be able to handle different key images? The current key is 63 pixels in width. Since this number is hard coded into our program, if we used a key image of a different size, they would not be in the same place on the board. Therefore we need to abstract this method to handle different key sizes. What would we need to do?

The Walker School – Games and Simulations - 2010

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

Making the White Key Width “Unfixed” (Part I)

Create methods to return the actual width and height of a key in the Key class.

The Walker School – Games and Simulations - 2010

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

Making the White Key Width “Unfixed” (Part II)

The Walker School – Games and Simulations - 2010

Call your “width” method in the White Key loop and change the fixed number “63” to the variable “actualWidth”.

What would you do for the black keys?

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

Show A Message on the Board

The Walker School – Games and Simulations - 2010

Step #1 – At the top of the Piano class

Step #2 – At the bottom of the Piano class, under the last method.

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

Show a Message Board (cont.)Step #3 – Call method, in constructor.

The Walker School – Games and Simulations - 2010

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

Activity

• Go back to the Crab World project. Add a message that appears when the game is won. How would you make the font size bigger, or a different font? Research the java.awt.Font library.

The Walker School – Games and Simulations - 2010

Steps for this are in the Crab World Project.

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

Advanced Challenge

• Using fixed numbers in your code, such as 140 and 63 in the above statement is usually not the best solution. It makes your code vulnerable to breading when things change. For example is we replaced our keys with nicer keys made in PhotoShop that were of a different size, our code would not place them correctly. How would you abstract the makeKeys() method to allow for different key images.

The Walker School – Games and Simulations - 2010

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

Solution – Step 1 (get height and width of keys)

The Walker School – Games and Simulations - 2010

In the Key class write a method to return the width and height of your key image.

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

The Walker School – Games and Simulations - 2010

Solution - Step 2 (develop a formula)

h2

b

h1

w

h

HWhat mathematical formula could be used to model key placement?

Greenfoot determines position from the middle of the key.

How can handled the missing black key in the 4th and 8th positions on the keyboard?

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

Key Positions

The Walker School – Games and Simulations - 2010

1st Key Position

w / 2, h1/2

(x, y)

2nd Key Position

(x, y)

w / 2 + w, h1/2

3rd Key Position

(x, y)

w / 2 + 2w, h1/2

4th Key Position

(x, y)

w / 2 + 3w, h1/2

White Keys

Black Keys

1st Key Position

(x, y)

2nd Key Position

(x, y)

3rd Key Position

(x, y)

4th Key Position

(x, y)

w, h2/2 2w, h2/2 3w, h2/2 4w, h2/2

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

The Walker School – Games and Simulations - 2010

Solution - Step 2 (cont.) (develop a formula)

h2

b

h1

w

h

H

Greenfoot determines position from the middle of the key.

How can we handled the missing black key in the 3rd, 7th, and 10th positions on the keyboard?

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

One Possible Solution – Step 3

The Walker School – Games and Simulations - 2010

Create a variable to hold the width of the key.

Set the width to 0.

Call the width from the Key class.

Multiplies the actual with of the key with location of the key and adds 54 which is needed to place the first key a proper distance from the edge.

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

One Possible Solution – Step 4

The Walker School – Games and Simulations - 2010

Gets the width of the key.

Needed because the black keys are not the same size as the white keys.

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

Extended Programming Challenge

• Create a piano keyboard that plays different instrument sounds like a synthesizer. Collect icons of various instruments, add them to the bottom of the Piano image. When you click on one of these icons, it should change the array lists and play the set of sounds associated with that instrument, rather than the piano.

The Walker School – Games and Simulations - 2010