Greenfoot Computer Programming Lesson 2 Created by NW
Greenfoot Computer Programming Lesson 2 Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are taken from http: //www. greenfoot. org/doc/ so credit to them too!
Objectives • Develop awareness of Java code. • Be able to apply learning to programming Greenfoot. • Understand how to detect Actors and create methods. Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are taken from http: //www. greenfoot. org/doc/ so credit to them too!
Chapter 2: The First Program: Little Crab • We will use the little-crab scenario for this chapter. • Once we open our scenario, you should notice that there is the usual Actor class, an Animal class, and the Crab class. • Animal and Crab inherit instructions from the Animal class. A Crab is an Animal, and an Animal is an Actor! • When we first use this program the crab does nothing!
Chapter 2: The First Program: Little Crab • Making the crab move – Open the editor to look at the source code. • We look at the act method. The first line of the method is called the signature of the method. • The lines of code between the braces is called the body. • We can put the command - > move( ) ; in the body to make the crab, well, let’s see… move ! • move( ) ; -> is technically called a method call. • We can also make the crab turn! • Use the turn method call
Chapter 2: The First Program: Little Crab • use turn(5); • The number 5 in the parentheses specifies how many degrees the crab could turn. This is called the parameter. • The degree value is specified out of 360 degrees, so we can use 0 and 359. • In the wombat and leaves scenario, the wombats were limited to the edge of the world. The wombats really just got stuck on the edge of the world. • The crab will reach the world edge and turn around.
Chapter 2: The First Program: Little Crab • The method that will help us to help the crab find the edge of the world is boolean at. World. Edge( ) - The return type of boolean will return a value of true or false when the crab gets to the edge of the world. • When we look at the act method for crab, we see the reserved word if (remember Rodney the Robot – selection structure : ) ). This will test to see if the crab is at the edge of the world or not! If it is, it will turn!
Last lesson…. Last lesson we learned how to program the simple movement of an Ant. (our actor). Now our ant is getting hungry after all his twisting and turning so lets give him something to eat! 1. Right click on “ACTOR” and choose “new subclass…. ” Choose a food you would like your ant to eat – don’t forget to give it a name.
Now add this to your ANT code Change “Hamburger” to whatever you called you food – don’t forget Java is case Se. Ns. At. Iv. E!!!
You noticed nothing happens any different…. . YET! Add this: Change “Hamburger” to whatever you called you food – don’t forget Java is case Se. Ns. At. Iv. E!!!
PLAY! Now add your ANT and its food into the world and play. Note what happens when your ant easts it food!
Now we have played its time to tidy up! • When you change your code it is called Refactoring. • We are going to refactor our code so that it is easier to read – at the moment the top half deals with moving our ant and the bottom half its food. • It will be easier if we split them up – this may be important later on if we need to change the code.
Save the world It’s a bit of a pain when our game is over and we need to rebuild the world every time. Lets save it! Create your layout of how you would like your game to start. Then RIGHT CLICK on the world and choose “save the world” – this adds code automatically into our class. Now after we have played the game – and click RESET our world reappears.
- Slides: 12