Tuesday, July 30, 2013

Step 4: Acting classes


By now, we're looking at almost a game. Now comes the hard part. Shooting.

More specifically, the hard part is making sure that your lasers dont just stay on the screen forever. This took me quite a long time, with lots of false starts and failed attempts before I solved it. So I'm not going to give you the solution right away. I'mma make you work for it.

First, we have to add a laser as another actor. You may be thinking 'shouldnt it be a sub-class of the ship?'. That's what I thought. Trust me, for now, we're better off with seperate classes.

So do the thing, add the actor class, pick your image. Greenfoot is so handy for this, but after finishing this project, I dont think I'm going to stay in this development environment. As nice as the quick adding of classes/actors/worlds is, I'd way rather have a real coding environment like Sublime Text or Eclipse.

That being said, once you've picked your image, we have to get the ship to fire the laser.

Adding a new object to the world is very easy. All you have to do is instanciate the world class and your actor class, and then add the actor to the world. What does that look like?

Actor laser;
laser = new Laser(x, y);
World world;
world = getWorld();
world.addObject(laser, x, y);


You may be asking 'what are those x's and y's? when I try to run this, it gives me errors.'. This is true. And its because I modified the constructor class of the laser class.

What?

When 'Actor laser' is called, it is declaring 'laser' to be a variable of type 'Actor'. When you call 'laser = new Laser()', you are creating a new instance of that class. In the class, when a new instance is created, it first calls the constructor.

public Laser(int x, int y)
    {
         startX = x;
         startY = y;
    }


Any class constructor in java looks like this. The class name (capitalized), followed by the parameters, followed by the commands. Now, startX and startY are whats called 'properties' of the class. They are not functions, they are just attributes. They are declared earlier, using this:

private int startX;
private int startY;


Private just means that no other class can access them directly (this is a good thing). Just like all variable declarations in java, we then tell it what to expect (int: whole numbers, 0,1,35,-20 etc) and what to call it.

So, if you can tell me what the earlier code is doing, you get a prize. If not, you get the same prize: me telling you what it means! Huray!

Here's what it is: the spaceship class is creating a new instance of the laser class, then getting the world as it stands, and adding the laser to the world. Beautiful, really.

Now, we need to set x and y. We do this before the 'actor laser' bit, by getting the current location of the ship.

int x = getX();
int y = getY();


Simple enough. The getX and getY methods are part of Greenfoot. Handy tools.

So go ahead and compile and try. What? It's only placing the lasers where your ship is?

Well, thats something at least. Here, lets quickly fix that issue.

move(5); inside the act function. I hope you beat me to it and knew to do it, cause that means youre learning.

Now the lasers should fire, and never leave the screen, but they are all firing in the same direction, regardless of your position. Thats because we need to set the rotation.

int rotation = getRotation();
// actor stuff, world stuff, add object
laser.setRotation(rotation);


This should have you firing at will.

Do'h. I forgot to bind the firing to a key press. See if you can figure it out.

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!

Saturday, March 30, 2013

Step 1: Dr. Strangelove or: How I Learned to Stop Worrying and Love IDEs


Well that was embarassing.

After registering the blog name, and telling the internet I would, I ended up spending over a year not learning java.

Good thing no one was actually paying attention yet.

So here we are; ready to start (actually) diving in to java. Whats the first step?

The Green Footprint

Well, for the basics of game development, I thought I'd start with the wonderful (free) platform Greenfoot.

Greenfoot is a basic java setup masked by a teaching tool. It helps simplify the learning of classes, actors and drawing methods. In order to get something going quickly, this is the way to go.

So we start by downloading Greenfoot, and making sure we have a JDK - Java Development Kit - from oracle. Download and install them both, just using the default settings. I'd start with the JDK first; that way it's likely that Greenfoot's installation will take care of things like environment variables.

Once you have everything installed, you can start with the tutorial scenario if you'd like, but I will try and go over most of what it covers (as I learn it).

While I wait for those to download (acreage internet sucks), lets talk more about the plan. I plan to make a game.

The plan

I'm not going to publicly develop it (not yet, anyways), so for learning, I will be trying to clone various simple games. The first on this list is going to be a bit ambitious: an asteroids clone.

Asteroids, for those of you too young to remember (or too lazy to google it) is a game where you fly around in a fixed screen and shoot asteroids. Each time you hit an asteroid, it breaks into smaller and smaller pieces. When you clear the level of asteroids, you move on to a harder level.

On our clone, once you clear the level, you win.

I never said it would be a good clone.

So, what do we need to acomplish for an asteroid clone?

Well, we need to be able to control the ship. Move in 4 directions, change speed, and shoot. The lasers need to have a set distance and an animation.

Next we will need the asteroids. They will need to move in random directions.

Then, we need collision detection: if a laser hits an asteroid, break it down. Also (and this was not in the original, but presents a nice challenge), I would like to have the asteroids bounce off each other. So in the event of asteroid collision, the two asteroids will bouce away in the opposite direction (at 75% speed).

This is a fairly long list of goals, but makes a good challenge. Lets get started. Next blog.

(Sorry).

Monday, September 26, 2011

Step 0: The Introduction

If you're reading this, I assume you have at least a passing interest in Java, (the programming language, not the beverage) programming, or blogs. Hopefully this should scratch your itch.

So, what is it?

In short, this will be a chronicle of my personal Journey into Java. I have 4 main goals with this project:

  • Re-learn the Java programming language
  • Build an awesome game I have in mind
  • Learn how to develop Android mobile apps
  • Get in the habit of blogging more regularly

With that in mind, for the succeeding of the first goal, I'm going to be digging through an old textbook of mine from when I took an Intro to Java course. That should serve as the perfect starting off point.

So, why should I care?

As I take this journey, in the early stages, it should double as an excellent tutorial for anyone who wants to learn basic programming in Java, and how to achieve such things. As things progress, I hope that it will also serve as a good 'trial by fire' tutorial for Java game development - how to start from scratch and build something, laid out step by step, with hilarious commentary. Finally, once the mobile development begins, it should also be a great tool for beginners in that area.

Really, I'm just hoping to make all the mistakes so that others can have an easier time with it. Lets hope I succeed!(?)

The first step will be covered in the my next post entitled: 'Step 1: Dr. Strangelove or: How I Learned to Stop Worrying and Set Up the Java Runtime and Java Environment Variables'.