Alright. So we have a ship. Better still, we have a ship that flies. Lets take control.
Assuming Direct Control
Now, in most games, the player will control an object. That's what we want. Luckily, Greenfoot has methods in place for control. Its very simple. Replace the 'move 2' with:if (Greenfoot.isKeyDown("up"))
{
move(3);
} Compile and run. Now you should be able to control whether or not your ship flies to the edge of your screen.
But thats not good enough. We want 4 way control. How do we achieve this? With the method 'turn'.
if (Greenfoot.isKeyDown("left"))
{
turn(-2);
}The turn is -2 because we want it to turn to the left, or down on the axis. Play around with the numbers until you find a speed of moving and turning that you like. Oh, and extrapolate from this to add controls for right and down keys too.
Now, I'm going to assume you can understand the code posted (if not, please leave a comment and I'll try to explain), but let me just make sure.
If the up key is down, move 3 pixels.
That's what it reads, in simple language. But why does this work? Well, "if" is whats called a logic structure. If the condition in brackets ("()") resolves to true, execute whatever is in the curly braces ("{}").
"If" comes with a partner: "else". This executes if the if does not.
A simple example of this would be to expand on our if statement.
if (Greenfoot.isKeyDown("up"))
{
move(3);
}
else
{
//do nothing
}Oh, also, "//" identifies comments in the code. Comments are ignored by the compiler. These are very helpful for finding your way back through when you need to make changes. You can also make large segments of comments by wrapping it with "/*" and "*/".
So we have our if, then the condition: Greenfoot.isKeyDown("up"). What is this?
Method Man
It is called a method. We are first calling the Greenfoot library, and inside, looking for a method called 'isKeyDown'. This method takes a single 'parameter'. Parameters are what makes coding possible, and I'll explain what they do.Imagine if we had to make a method for each possible key press.
isUpKeyPressed
isDownKeyPressed
is_a_KeyPressed
isCtrlKeyPressed
...
That would be tedious.
The solution to this is parameters. Take this method, for instance.
public int add()
{
int answer = 1+1;
reurn answer;
}This method will create a variable "answer", and it will equal the result of the equation "1+1".
When we run it, we will get two. Every time. If you dont, your computer is broken.
But, if we add a parameter:
public int add(int x)
{
int answer = x + 1;
reurn answer;
}We can now return 1 more then whatever number we put in. Lets take it another step:
public int add(int x, int y)
{
int answer = x + y;
reurn answer;
}Now, we can get the answer to adding any two numbers. We would call the method and its parameters in brackets.
int help = add(60,39042); Hopefully this should give you a clue as to the power of parameters.
Where were we?
Ah yes. Moving and turning. By now, you should be able to control your ship as it soars through the reaches of space. By now you also will have had to re-compile several times, having to add your ship each time.Greenfoot comes with a helpful tool: save the world. When you add your actor, you can right click on the world (the image) and click 'save the world'. This will set this up as your default world layout.
I was planning on adding variable speeds, but after spending an hour trying to come up with a solution, I realized that I was missing the point of this: to get the basics down. My clone doesn't need to be good, it just needs to work. Moving on, we see that the next thing to work on is the ability to shoot lasers.
This should be fun :D