Saturday, June 29, 2013

Step 3: I want you to take over control


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

Thursday, June 20, 2013

Step 2: Space; the final frontier

(edit: whoops.jpg - forgot to publish these. i wrote them all in an afternoon and then just... well lets move on, shall we?)

Ok. Let's create the world.

If you're starting out, I suggest you go back a step - follow the instructions to download and install Greenfoot.

If you're already there, cool.

You've started a new project and named it something clever. For me, since I'm making an asteroids clone, I named it meteors.

The first thing you may notice is the 'World' and 'Actors' areas. These are the two superclasses that form the basis for all of Greenfoot's interactions.

Right click (or whatever your right-click equivalent is, you crazy mac users) on the box 'World' and select 'New subclass'.

You will see a nice little form for creating a new subclass - this is what you will see for world and actors.

For now, all we need is one world. Space. Name your class (space) and select an image to represent it (backgrounds->space). Note: throughout this and the next few posts, I was incorrectly using lower case class names. It is a convention of java that class names always start with upper case letters. space = Space, spaceship = Spaceship, etc. Please just assume I'm using the correct casing.

Under the hood

This is an example of inheritence. The World superclass has a bunch of methods and properties (dont worry about what that means right now, just trust me), and creating the space class uses the World superclass.

If you dont believe me, which I dont blame you for, I can prove it. Right click on the 'space' subclass and select 'Open Editor'. See where it says "public class space extends World"? What this means is that the space class we just made uses the Greenfoot class 'World'. How does it know what that is? Look up. "import greenfoot.*;"

If we wanted to, we could dig into the greenfoot library to see whats going on under the under the hood, but we wont. Yet.

A whole new world

So we have our world. Its a big empty space, and thats fine. Lets populate it.

Close the code editor and right click on the Actor superclass. Greenfoot comes with a nice little rocket ship image, so that works perfectly for now. I called mine 'spaceship'.

If you're not seeing either of these classes, the problem is that you have to compile your code. That just means that youre building your code (and in greenfoot it means that you can now see/run your code). So do that now.

It's full of stars...

Now we can add an actor to our world. Right click on spaceship and select 'new spaceship()'. Your cursor turns into an image of the ship, and we can left click to place it in the world. Simple.

I have a problem with the size of space. Its not big enough. What can we do about it? Open the code editor on the space class. See where it says 'super(600,400,1);'?

This is an example of one of the methods within the World class. It takes 3 paramaters; Width, Height and Pixels, and creates a backdrop. How do I know this? Educated guess mostly. But try modifying the numbers. Dont forget to compile.

You may notice that your spaceship has disappeared. I dont know why that happened. But its something to find out. (This is just the best tutorial site).

Control; You must learn control!

Open the editor for the spaceship class. You will see 'public void act()'. This is the main space for what the ship does when you click 'act' or 'run'. It is the main function for actors: to act!

Lets start with something simple. Inside the act function, add a line that looks exactly like this:

move(2);

Now run your code. Your ship should fly away!

Next time: we will learn to control when it flies, and also turning, and also maybe rates of speed.

Excitement!