Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Breakout in Greenfoot Barb Ericson
Getting to know Greenfoot
How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
Games and Simulations O-O Programming in Java The Walker School
Program: Little Crab Mr Gano.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Code Club Session 3 Shark Eats Fish. Picture of finished product here.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to a Programming Environment
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
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
How to make Space Invaders
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 8 Fall 2010.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Java: Chapter 1 Computer Systems Computer Programming II.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Chapter 1 - Getting to know Greenfoot
IT253: Computer Organization
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
Game Maker Terminology
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Advanced Stuff Learning by example: Responding to the mouse.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Learning PowerPoint Presenting your ideas as a slide show… …on the computer!
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
31/01/ Selection If selection construct.
GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Introduction to Computer Programming - Project 1 Intro to Digital Technology.
How to create a basic game in Scratch. The Scratch Stage The Scratch stage is 480 pixels wide and 360 pixels high x increasesx decreases.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
Creating Scenarios In Greenfoot. Greenfoot Scenarios are made up of: Greenfoot Scenarios are made up of: A World class. A World class. Actor classes.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
DAY 4. MAKING SOMETHING ‘JUMP’ Simple! Move it a certain amount ‘up’ Glide back to your original spot.
Sound and more Animations
Chapter 4 - Finishing the Crab Game
Content Programming Overview The JVM A brief look at Structure
Debugging and Random Numbers
IF statements.
Let's Race! Typing on the Home Row
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
The Little Crab Scenario
An Introduction to VEX IQ Programming with Modkit
Go to =>
Breakout in Greenfoot Barb Ericson
Game Loop Update & Draw.
Creating a Simple Game in Scratch
Presentation transcript:

Lesson 2: Reading a program

Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step instructions If statements Adding sounds Variables

Let’s learn to read the program Open WombatWorld Open the code for WombatWorld

Understanding WombatWorld Import ??? – means I am going to use these classes Import command Class to import * Means important all classes found here!

Understanding WombatWorld Import ??? – means I am going to use these classes Notice the semi-colon! Almost all commands end in a semi-colon A few do not. This can be one of the most difficult things for new programmers.

Understanding WombatWorld Comments – do not affect the program or running of a program – They are for the programmer, not the computer Comments are either start with // and got until the end of the line Comments are everything between /* comment goes here **** cow goes moo */

Understanding WombatWorld Comments – do not affect the program or running of a program – They are for the programmer, not the computer Comments are either start with Comments are everything between

Understanding WombatWorld Defining a class public class WombatWorld extends World This says I am defining a public class called “WombatWord”. It is a sub-type of World, just like a human is a sub-type of animal. public – means anyone/class can see and use this private – means no one can see or use this World – is a class defined elsewhere in greenfoot. We imported it with import greenfoot.*;

Understanding WombatWorld Pretty colors in greenfoot mean something Words in red and colored are special words in Java. – Eg. import, public, class, extends, void – They cannot be used as names

Understanding WombatWorld Pretty colors in greenfoot mean something { and } (braces) are used to group code together in blocks. The colors match with braces

Understanding WombatWorld Everything in green is inside the class

Understanding WombatWorld Everything in yellow is inside the method or command Random leaves command

Review import – a class you want to use public class WombatWorld extends World – Defining a new public class that is a type of world { and } (braces) - blocks of code (colored) // and got until the end of the line /* more comments */ – Comments for the programmer not the computer

Objects Objects are particular instances of a class – Your pet dog, my pet cat, me, you Objects represent “real” concrete things. Because an object is “real” thing, you can actually change it and command it to things. New Objects must be created using new

Objects The “actors” or things in a Greenfoot game are objects, but each must be of a previously defined class. Let’s take a look.

Understanding WombatWorld.populate() Open WombatWorld Find the populate method public void populate( ) Who can see this method What this type Method returns Name of method arguments

Understanding WombatWorld.populate() Different example: (not from Wombat World) Find the definition of a word public Definition lookupDefinition( String Word ) Who can see this method What this type Method returns Name of method Arguments: What word to look up

Creating new objects To create a new object first create a variable to store the object in Then we request a new object or actor be created Set things about the object Then do something with the object

In populate() We create the Wombat We create the leaves We add them to the game

Let’s move forward How about we create a really big world? The world will be bigger than what we can see on screen – We will only see a small part of the map We will use code from ScrollWorld: –

Let’s move forward With this we can make… – Overhead bomber games – Debugging – finding and fixing mistakes – Starting from one else’s project – Practicing what we know – Reading documentation

Let’s start by creating a world Open ScrollWorld Project We will create a big map We will then populate the world with different objects and tasks

ScrollWorld Open ScrollWorld Notice DemoWorld is a subtype of ScrollWorld Open the code for DemoWorld Let’s look and see

DemoWorld

Name of our WorldParent (or super type)

DemoWorld Screen Size in cells Width, Height Map Size in cells Width, Height

DemoWorld How many pixels in a cell

Big moving world Let’s make the world really tall, but just as wide as the screen. Change full width and full height values to 600 and 10000

Big moving world Let’s move the Actors around We don’t need to move the bug because he follows the camera try setting the Apple to 700, 9700 move the other Actors Let’s set the camera position and direction Let’s put the camera at the bottom (600, 9800) type “this” then hit CTRL+SPACE for a list of methods use setCameraLocation use setCameraDirection and set the direction to -90 (UP) NOTE: 0 is right, 180 is left, 90 is down

Big moving world Now let’s make the world move. Create a new method call act() It should look like this public void act() { } Inside it call moveCamera( 5 );

Big moving world Your program should look like this. Compile and run it.

Let’s make hitting rocks bad When a bug hits a rock, let’s end the game. Open Bug We will first add steering then crashing Remove the body of the act method public void act() { } Let’s add some simple movement

Steering Make and if statement and test Greenfoot.isKeyDown(“left”) We don’t want “move” the bug, we want to slide it left or right For right setLocation( getX()+ 3, getY() ); For left setLocation( getX()- 3, getY() ); Try it out you should be able to move left and right

If, Conditions, and Comparisons Remember how if statements need true or false (boolean) values We can compare values – The result of a comparison is true or false Are two values equal== Are two values different!= We also have greater than > and less than <

If, Conditions, and Comparisons How do we say is X greater than 5 X > 5OR5 < X How about is X equal to 7 X == 7 How is variable professor equal to nothing? professor == nullORnull == professor Professor is equal to something (not null) professor != nullOR null != professor

We can even combine values Is X greater than 5 but less than 10 X > 5 && X < 10 Both parts must be true Is it true for X = 3 Is it true for X = 7 Is it true for X = 5

We can even combine values Is X greater than 5 OR less than 10 X > 5 || X < 10 at least one part must be trye Is it true for X = 3 Is it true for X = 7 Is it true for X = 5 Is it true for X = 13

We can even combine values X > 5 X < 10 X > 5 && X < 10

We can even combine values X > 5 X < 10 X > 5 || X < 10

Comparing Objects/Reference Objects and references are hard to compare and understand Comparison between Object checks if they are the same reference, and NOT that they are the same

Comparing Objects/Reference Person martin = new Person( “Martin” ); Person other = new Person( “Martin” ); What will martin == other return trueorfalse FALSE – because they are different referenced (made by different new operations)

Making hitting rocks bad After moving left or right, we will add a test to see if we hit a rock Make an if statement the test will use the method getOneIntersectingObject( Rock.class ) It returns a rock if the bug is intersecting one Otherwise it returns Nothing (null) Compare the returns object with null

Making hitting rocks bad It should look like this OR

Making hitting rocks bad End the game by calling Greenfoot.stop(); Add a sound if you would like Let’s add some text!

Saying Game Over Go back to the project Right click on Actor and select New subclass… Call it Text

Saying Game Over Let me explain this, then type it out

Saying Game Over Go back to Bug, just before calling Greenfoot.stop(); Create a new Text object Text text = new Text( “Game Over”, 200, 50, 36 ); Add the object to the center of the screen addObject( text, 300, 200 ); Try it in the game; drive into a rock.

Let’s make hitting rocks bad When a bug hits a rock, let’s end the game. Open Bug We will first add steering then crashing Remove the body of the act method public void act() { } Let’s add some simple movement

Making other thing move Open the Apple or the Mushroom Add an act method public void act() { } Let’s try simple back and forth We will use move again How will we detect when we reach an edge? getX() Where are the edges? 0 and the width – getWorld().getFullWidth() Then what? Flip the direction – setRotation( … );

Making other thing move Your code should look something like this

Firing bullets When the player presses space bar, we will fire bullets The bullet will move forward If the bullet hits something it can destroy, then both should be destroyed. First create a bullet class a sub type of scroll actor!

Firing bullets When the player presses space bar, we will fire bullets The bullet will move forward If the bullet hits something it can destroy, then both should be destroyed. First create a bullet class a sub type of scroll actor! Right click on ScrollActor select “new subclass…”

Firing a bullet Now open actor When the player presses space Create a new bullet Add it to the world At the same location in the world as the Bug

Firing a bullet Now run it Wait why are the bullet behind me? They aren’t going anywhere

Firing a bullet Open Bullet Create the act method public void act() { } Call move(10);

Firing a bullet Try it again! Wait now the bullets go left Oh, we never set a direction!

Firing a bullet Open Bug again Set the direction to up setRotation( -90 ); Try it!

Firing a bullet Bullets are too close together. Let’s slow them down We could limit the number of bullets Or we could limit the rate of fire We will only allow bullet to be fired if a certain amount of time has passed

Firing a bullet Bullets are too close together. Let’s slow how fast we shoot We will only allow bullet to be fired if a certain amount of time has passed In Bug, we need to create two variables in the class One for how often we are allowed to fire Another for how long has passed since the last bullet was shot

Firing a bullet Both will be integer values (whole numbers) Create the values like so Go to act() Change the if statement If space is down and myLastShot equal 0 Inside the if set myLastShot = myRateOfFire; At the end of act() subtract 1 from myLastShot We are counting down until we can fire again

Firing a bullet Your code should look something like this Notice I am using a if statement to stop subtracting when myLastShot gets to zero

Making bullets destroy things We need a way to test if an Actor can be destroyed Remember how we detected if the bug hit a rock? We need to remove the objects Remember how World and ScrollWorld have an AddObject method? Maybe they have a removeObject method Let’s double clicking one of them Last, what type should we test for? Rocks? Actors? Let’s create a special type for detecting what bullets can destroy

Making bullets destroy things Create a new class Edit > “New subclass…” Call it destroyable Make sure the class has not methods Replace the word class with interface An interface is like a class (or type) It only describe what the type can do – it can’t actually do anything or even exist. You need to create a subtype of the interface

Making bullets destroy things Let’s make rocks destroyable – they need to be subtypes We just need to say the a rock implements Destroyable Now a rock is Destroyable and a type of ScrollActor It is two things

Making bullets destroy things Open bullet Add the detect hitting things code from Bug Change Bug to Destroyable Inside the if statement Remove the Actor and bullet

Final touches Try it out Destroy some rocks How far should bullets go? Should they go for ever? Let’s limit how far bullets go Give them a time limit then remove them Like counting with shots Or once they are off screen remove them Since they only go up right now, If y < 0

Final touches Try it out Destroy some rocks How far should bullets go? Should they go for ever? Let’s limit how far bullets go Give them a time limit then remove them Like counting with shots Or once they are off screen remove them Since they only go up right now, If y < 0

Final touches OR

Extend the game with your ideas Add more rocks or objects Add more destroyable Add enemies that shoot bullets at you Need enemies, new bullets – fancy stuff If you get a mushroom you can shoot faster For a limited amount of time? For more help see the documentation or ask me!

Documentation API.pdf API.pdf