Games and Simulations O-O Programming in Java The Walker School

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
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.
Program: Little Crab Mr Gano.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Road Map Introduction to object oriented programming. Classes
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Chapter 5 - Making Music: An On-Screen Piano
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Object Oriented Software Development
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Introduction to Java CS1316: Representing Structure and Behavior.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Visit to download CodeAwesomeness.
Grouping objects Introduction to collections 5.0.
CPS120: Introduction to Computer Science Decision Making in Programs.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Introduction to Java Java Translation Program Structure
I Power Higher Computing Software Development High Level Language Constructs.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
M1G Introduction to Programming 2 5. Completing the program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
OOP Basics Classes & Methods (c) IDMS/SQL News
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
 2005 Pearson Education, Inc. All rights reserved Arrays.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Newton’s Lab Games and Simulations O-O Programming in Java.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Chapter 4 - Finishing the Crab Game
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Chapter 5 – Making Music: An On-Screen Piano (Part 2 – Using Arrays)
Testing and Debugging.
Java Programming: Guided Learning with Early Objects
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Arrays, For loop While loop Do while loop
T. Jumana Abu Shmais – AOU - Riyadh
Games and Simulations O-O Programming in Java The Walker School
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Presentation transcript:

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

Piano World What are the Java keywords in this class? Piano Class Method that specifies the size and resolution of the world Piano Constructor

Java Key Words 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. The Walker School – Games and Simulations - 2010

Animating Keys Key Constructor Key Method The Walker School – Games and Simulations - 2010

Animated White Key Switches between two images: white-key and white-key down. The Walker School – Games and Simulations - 2010

Checking if a Key is Down Variable that stores “true” while the piano key is down, and “false” while it isn’t. The Walker School – Games and Simulations - 2010

Producing the Sound Calls the play method. Creates a play method. 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

Step #1 - Create Instance Variables Create a variable to hold the name of the key. Create a variable to hold the sound file. 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

Arguments and Parameters Arguments are values that control how the function does its job. Some functions can take more than one argument. How many parameters can a constructor take? Values that are passed from one function to another get assigned to variables called parameters. 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

One Possible Solution The Walker School – Games and Simulations - 2010

Step #3 – Construct Piano Places a key at the top of the keyboard. 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

One Possible Solution The Walker School – Games and Simulations - 2010

Loops For example: while (condition) { statement; … } Loops allow you to express commands repeatedly. The Walker School – Games and Simulations - 2010

Creating Piano Keys w/ Loop What is the local variable here? What is the problem here when you run it? The Walker School – Games and Simulations - 2010

Local Variables Example: int i = 0; while (i < 100) { statement; … When you create a local variable inside a function, it only exists inside the function, and you cannot use it outside. Example: int i = 0; while (i < 100) { statement; … i = i + 1; } Also, Local variables are declared inside the function and have no visibility modifier (private or public). 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. 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

One Possible Solution 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? The Walker School – Games and Simulations - 2010

One Possible Solution 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. The Walker School – Games and Simulations - 2010

One Big Problem 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. 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

White-Key Arrays 2D array (list) used for white keys 2D array (list) used for white key notes The Walker School – Games and Simulations - 2010

Lists [10, 20, 30, 40] (a list of ints) 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. [10, 20, 30, 40] (a list of ints) ["spam", "bungee", "swallow"] (a list of strings) Lists and strings—and other things that behave like ordered sets—are called sequences. The Walker School – Games and Simulations - 2010 A list within another list is said to be nested.

White-Key Method w/ While Loop Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom. Creates a local variable “I” and sets it’s initial value to “0” As long as “i” < the length of our white-key array, do the following… String concatenation Create a new white key and associate it with the sound file in the whiteNotes list then append “.wav” Add the key to a location on the keyboard. The Walker School – Games and Simulations - 2010 Count up by 1 each time through the loop…required.

Key Method w/ For Loop 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? 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

One Possible Solution Changes the key sounds down one octave. 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

Creating the Black Keys 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.

Step #1 – Instance Variable 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. The Walker School – Games and Simulations - 2010

Step #2 – Assignment 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. The Walker School – Games and Simulations - 2010

Programming Challenge What is the problem here? The Walker School – Games and Simulations - 2010

One Possible Solution (Step #1 – Instance Variable) Create 2 more variables to show an “upImage” or a “downImage” whether it is a white or black key. The Walker School – Games and Simulations - 2010

One Possible Solution (Step #2 – Assignment) 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)? The Walker School – Games and Simulations - 2010

Possible Compile Error Cannot find symbol – constructor Key(java.lang.String, java.lang.String, int, int) The Walker School – Games and Simulations - 2010

For Loop to Create Black Keys How can we use the white key array list to create our black keys? The Walker School – Games and Simulations - 2010

One Possible Solution (Step #3 –Methods) Check to see that your methods reflect these changes. 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

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

Making the White Key Width “Unfixed” (Part II) Call your “width” method in the White Key loop and change the fixed number “63” to the variable “actualWidth”. The Walker School – Games and Simulations - 2010 What would you do for the black keys?

Show A Message on the Board Step #1 – At the top of the Piano class Step #2 – At the bottom of the Piano class, under the last method. 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

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.

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

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.

Solution - Step 2 (develop a formula) b How can handled the missing black key in the 4th and 8th positions on the keyboard? h Greenfoot determines position from the middle of the key. h2 h1 w The Walker School – Games and Simulations - 2010 What mathematical formula could be used to model key placement? H

Key Positions White Keys 1st Key Position 2nd Key Position 3rd Key Position 4th Key Position (x, y) (x, y) (x, y) (x, y) w / 2, h1/2 w / 2 + w, h1/2 w / 2 + 2w, h1/2 w / 2 + 3w, h1/2 Black Keys 1st Key Position 2nd Key Position 3rd Key Position 4th Key Position (x, y) (x, y) (x, y) (x, y) The Walker School – Games and Simulations - 2010 w, h2/2 2w, h2/2 3w, h2/2 4w, h2/2

Solution - Step 2 (cont.) (develop a formula) b h How can we handled the missing black key in the 3rd, 7th, and 10th positions on the keyboard? h2 h1 Greenfoot determines position from the middle of the key. w The Walker School – Games and Simulations - 2010 H

One Possible Solution – Step 3 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. The Walker School – Games and Simulations - 2010

One Possible Solution – Step 4 Gets the width of the key. Needed because the black keys are not the same size as the white keys. 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