So we can see LobsterWorld is a type of World. We can also add a single line comment using // The line 7Ĩ public class LobsterWorld extends World was created when you right clicked the world object and selected new subclass. These are ignored by java and are really just for programmers to help other programmers explain what parts of the code do. super(600, 400, 1) Anything that is between /* */ are just comments. * */ public LobsterWorld() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. * (your name) (a version number or a date) */ public class LobsterWorld extends World /** * Constructor for objects of class LobsterWorld. You should see something similar to the following: import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class LobsterWorld here. We can do this by either double clicking LobsterWorld, or by right clicking it and selecting open editor. Lets look at the default code that s been added. If you do receive errors then greenfoot will take you to where it sees a problem. These will not be removed if Java detects errors in the code. 6ħ If you click the Compile button then then the greyed lines should be removed. In other words turning your code into code that your computer will understand. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM. This means that it hasn t been compiled yet. You should now notice you have created a child class of World called LobsterWorld Notice that LobsterWorld is greyed out. This image will be tiled if it does not take up the whole screen. 2Ħ Call this LobsterWorld and select the image sand.jpg which is under backgrounds. move forward, turn left, eat leaf If we click pause in our scenario it stops. its height, speed, or its location Methods: These are the actions that an object has. Properties: These store values for each instance of an object. Invoke a Method Directly Objects (actors) have properties and methods. Wombats like to eat leaves, so add a couple of leaf objects in their path and they should eat them. If you have done this correctly you should see a wombat running around the edge. This terminology wont make a lot of sense yet, but we will revisit it again. What you have done here is to add an instance (object) of the class Wombat. We can click on the actor (wombat in this case), then hold down shift, or right click the actor and select new Wombat(). There are two ways of doings this manually. Nothing will happen, so don t be surprised as we haven t actually told it to do anything! Lets add our first actor to our world. Click the run button in the Controls area. If any of your classes are grey, then they haven t been compliled 1Ģ Adding Objects to Your World We will see later how we can automatically add objects to the world, but in the meantime we can add these manually when the scenario is running. Compile allows you to turn you code into machine code that the computer understands. Use these buttons to run your code once it s compiled. leaf) that you want to have in your world. You will have to make an Actor class for each type of object (e.g. To the right we see the class and underneath we see how it looks visually. When you open this you see the following: This represents a world that we have named and created. Choose the Scenario, then Open and select Wombat. Greenfoot creates scenarios (projects) that allow us to create a variety of programs such as games and simulations Lets open a scenario called Wombats that comes as a default when you install greenfoot. 1 Introduction to Greenfoot Greenfoot is an IDE (Integrated Development Environment) that is built on Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |