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

Slides:



Advertisements
Similar presentations
Getting to know Greenfoot
Advertisements

Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
Lecture 2 Introduction to C Programming
Introduction to C Programming
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
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
Movement. Fixed Movement setLocation (x, y) Makes the crab move to a fixed cell x,y Relative Movement The functions: getX() getY() Fetch the x and y coordinate.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Flash Workshop Flash Workshop :: Agenda  Introductions  Look at a few Flash Examples  Flash Web Sites  Flash Web Applications  Flash Games.
Program Design and Development
Kapi’olani Community College Art 258 Interface Programming II In-class Presentation Week 5A.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to C Programming
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Games and Simulations O-O Programming in Java The Walker School
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Input, Output, and Processing
Chapter 1 - Getting to know Greenfoot
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
M1G Introduction to Programming 2 5. Completing the program.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Controlling Program Flow with Decision Structures.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
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.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 4 - Finishing the Crab Game
Information and Computer Sciences University of Hawaii, Manoa
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Chapter 3 – Improving the Crab Game
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Multiple variables can be created in one declaration
Control Structures – Selection
The Little Crab Scenario
T. Jumana Abu Shmais – AOU - Riyadh
CMSC 202 Java Primer 2.
Games and Simulations O-O Programming in Java The Walker School
ICT Programming Lesson 4:
ICT Gaming Lesson 3.
Chapter 3: Selection Structures: Making Decisions
Object Oriented Programming in java
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Games and Simulations O-O Programming in Java The Walker School 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? The Walker School – Games and Simulations - 2010

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 Method Call The Walker School – Games and Simulations - 2010

Turning Parameter The Walker School – Games and Simulations - 2010 What happens to the crab if you use a negative number?

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 NAME( LIST OF PARAMETERS ): STATEMENTS 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 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 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 Conditional Statement The Walker School – Games and Simulations - 2010

Conditionals are Decision Statements Are if … then statements Condition Only executed if something is True. The Walker School – Games and Simulations - 2010 Instruction(s)

Conditional Types if (method()) { statement; } Branched Chained Nested …gives use the ability to check conditions and change the behavior of the program accordingly Branched Chained Nested if (method()) { statement; }

Watch – Making Things Move (Part I) http://www. youtube. com/watch The Walker School – Games and Simulations - 2010

API Views Documentation Source Code View View Contains Class Tree Author Constructors Methods The Walker School – Games and Simulations - 2010

Adding Random Behavior What percentage of time will the crab turn 5 degrees? Operator The Walker School – Games and Simulations - 2010

Operators Assignment (=) Comparison (==, <=, >=, !=, etc.) 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 + - * > < <= or >= == != || && % What do each of these operators do? The Walker School – Games and Simulations - 2010

Order of Operation PEMDAS Parentheses ( ) Exponentiation 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 Walker School – Games and Simulations - 2010 The acronym PEMDAS is a useful way to remember the order of operations

Dot Notation Used when calling a method defined in another class. Dot Class-name.method-name (parameters); The Walker School – Games and Simulations - 2010

Explain the behavior being expressed here. The Walker School – Games and Simulations - 2010

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

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 Find this behavior in the Java API? Comments 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 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 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 Remember to call the method in the act() method. The Walker School – Games and Simulations - 2010

Adding Keyboard Control static boolean isKeyDown(String key) Method Call Class Method Returns Either T or F Expects a String Any Key The Walker School – Games and Simulations - 2010

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

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.

Watch – Making Things Move (Part II) http://www. youtube. com/watch Mover “Helper” Support Class For more advanced students. The Walker School – Games and Simulations - 2010

Ending the Game In what class is this method called? The Walker School – Games and Simulations - 2010

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 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 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 Allows us to pass information to the constructor. 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. 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 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 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 Solution The Walker School – Games and Simulations - 2010

Adding a Win-State (counting worms) The Walker School – Games and Simulations - 2010

Instance Variables – Step #1 Create variables to hold the number of worms. The Walker School – Games and Simulations - 2010 An instance variable is memory that belongs to an object.

Assignment – Step #2 Create an assignment and initiate the variable to 0 at the beginning of the game. The Walker School – Games and Simulations - 2010 An assignment is a statement that enables us to store something into a variable. It is written with an equal = symbol.

Increment the Count – Step #3 The Walker School – Games and Simulations - 2010

Crab Win-State, Play Winner The Walker School – Games and Simulations - 2010

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

Adding a Second Player The Walker School – Games and Simulations - 2010

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

Lobster Keyboard Controls Solution The Walker School – Games and Simulations - 2010

World Population Solution The Walker School – Games and Simulations - 2010

Places to Hide The Walker School – Games and Simulations - 2010

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 Adds a rock to the world for the crab to hide behind. The Walker School – Games and Simulations - 2010 Adds a rock to the world for the crab to hide behind.

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

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

Done after Piano Project Adding A Win Message Done after Piano Project The Walker School – Games and Simulations - 2010

Adding a Win Message Step #1: Import the necessary Java libraries. The Walker School – Games and Simulations - 2010

Adding a Win Message (cont.) Step #2: Create the method. The Walker School – Games and Simulations - 2010

Adding a Win Message (cont.) Step #3: Cast the method in the CrabWinState(). The Walker School – Games and Simulations - 2010