Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Making a java game. Kind of. Not really. filter_list
Author
Message
Making a java game. Kind of. Not really. #1
Programming a game in Java

using public online code, giving you a step forward in making a game, and explaining it in a probably confusing way.

Index
  • 0x00 - Introduction
  • 0x01 - Setting it up
  • 0x02 - The boring stuff first
  • 0x03 - Loops and stuff
  • 0x04 - Getting the window to work
  • 0x05 - Buffer Strategy
  • 0x06 - Graphics. Finally.

0x00 - Introduction

Hello. Basically, I will be teaching you how to program a game in Java. No, It's more of a "I got all this code online, and I put it in here" type of thing. I'll do it my way though. There are better languages out there, however Java is pretty easy. I highly recommend you download eclipse. Eclipse is extremely useful, and will help you a lot. If you already program in java, odds are you have it. Anyways, I will be doing this tutorial as if you don't know anything about java.

0x01 - Setting it up

Alright. Once you download eclipse, you should be asked to select a workspace. Find a good folder to stick your files in.
For me, I just put it on my flashdrive.
Once it's set up, you will want to make a new java project.
For my example, the project will simply be called 'tutorialgame'
Spoiler: Setting up the Project
[Image: fCs6a.png]

After that, you will want to add a class, of course. To do that, right click on your project, and click New> Class
These are what I put, and I would recommend you to do the same.
Spoiler: Adding a class
[Image: FfdGJ.png]


0x02 - The boring stuff first

Well, we aren't going to start programming our game yet. Right now, we are going to start making our 'blueprint' you could say. Anyways, you should add 2 integers. A width, and a height. If you know java, go ahead. Right now, I'll try to explain it.

Now, this is all personal choice, but set your width to a good number. Let's say 300.
If you want your game in 16 * 9 aspect ratio, just set the height as that.
And, because that is kind of small, you do want a scale to make it bigger. This will be in use later.
Code:
package net.hackforums.tut; //This is the package we are located in

public class Game {                                            //This is the class
    public static int width = 300;                        //We set the width to 300 pixels
    public static int height = width / 16 * 9;        //We set the height so it will be a 16:9 ratio window.
    public static int scale = 3;                           //This will allow everything to be 3 times bigger. But, it's not in use yet. Forget about it.
}

Awesome. Now you want to create a thread. It's basically a mini process. This is easy. I'll explain it below. I'm not that great at teaching, so I'll do something, and explain it afterwards.
Code:
package net.hackforums.tut;

public class Game implements Runnable { //Allows you to do Thread(this);
    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3;
    
    private Thread thread;
    
    public synchronized void start() { //Thread
        thread = new Thread(this, "game"); //New thread will contain this game class
        thread.start(); //Starts thread
    }
}

Now, what did we do? We made it so when we call start() it creates thread, and starts the thread.
However, What if we want to stop our thread? (Cool think about java is if you close a tab with a java applet on it, it might still run.)
So, what do you do?
Code:
public synchronized void stop() {
        try {
        thread.join();  //We closed it, so let's join it.
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
Simple as it is.


0x03 - Loops and stuff
You should add a private boolean, and call it 'running'
Set it so it's false on default
Code:
private boolean running = false;
So, this will just be an indicator if the game is running or not.

You will need a run method, along with the running boolean. So, what you can do is
Code:
    public void run() {
        while (running) {  //My oh my. This gets ran a TON every second
            
        }
    }

Oh, but wait. Running will never be true. So we need to add it in our start()
Code:
yada yada void start() {
        running = true;

But wait, then it can never turn off! So you have to set running to false when it's stops.
Code:
yada yada void stop() {
        running = false;


0x04 - Getting the window to work

So we got the game loop, and all that jazz. Now we actually need a window. (Unless you want to make a text game, which this does not cover)
Anyways. Let's go.

Your going to need a Jframe object (Be sure to import it)
So, you need to write this.
Code:
import javax.swing.JFrame;
And
Code:
private JFrame frame;
JFrame is basically a window, for you noobies.
Now, you will want to add graphics on your window. To do this, you will need to use canvas.
So let your class extend to Canvas
Code:
public class Game extends Canvas implements Runnable {
You will need to import it.
Code:
import java.awt.Canvas;

It's useful. Trust me. It makes it so your class 'inherits' everything in Canvas. So, Game is a subclass of Canvas.

When we first start our game, this is what will happen. And because we already have the variables, we can just use those. Awesome, right?
Code:
    public Game() {
        Dimension size = new Dimension(width * scale, height * scale);
        setPreferredSize(size);
        frame = new JFrame();
    }

Beautiful right. Nothing better than boring old code. So, we really can't do anything yet, except look at this code. Let's do something, Step one, add a main method
I'll just tab the important parts
Code:
public static void main(String[] args) { //Main method. Important. This is what really gets run.
    Game game = new Game(); //Object
    game.frame.setResizable(false); //Makes the JFrame non-resizeable
    game.frame.setTitle("hackforums"); //Makes the JFrame's title hackforums
    game.frame.add(game); //Adds the game
    game.frame.pack(); //Get size
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //IMPORTANT. Java will keep running if you don't add this.
    game.frame.setLocationRelativeTo(null); //opens it in the middle.
    game.frame.setVisible(true); //Visible.

    game.start(); //Runs start()
}


0x05 - Buffer Strategy
A buffer is kind of like a storage space, like images. It's hard to explain.
You want to add this.
Code:
public void run() {
    while (running) {
        update(); //Will work on this later
        render();
    }
}
public void update() { //Will work on this later
}

public void render() {
    BufferStrategy bs = getBufferStrategy(); //Of course import buffer strategy.
    if (bs == null) {
        createBufferStrategy(3); //You should use 3. More or less, isn't so great.
        return;
    }
}


0x06 - Graphics. Finally.

This is the part I actually like. This makes it mine. This makes it so I'm not just looking at code, and telling you to type it in. No.
This, allows you to be you. And it's beautiful.

Anyways, Remember render()?
Code:
public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
}

Add Graphics g = bs.getDrawGraphics(); in render() so you can actually do stuff.
Also, set the color to be black, and draw a rectangle. the rectangle will be the background
Code:
public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0,0,getWidth(),getHeight());
    g.dispose();
    bs.show();
}
Awesome. Try that.
Go crazy. drawString, drawArc, drawRect, so many things to choose.

If you missed anything, all the code is below
Spoiler:
Code:
package net.hackforums.tut;


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable { private static final long serialVersionUID = 1L;

public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;

private Thread thread;
private JFrame frame;
private boolean running = false;

public Game() {
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);

    frame = new JFrame();
}

public synchronized void start() {
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
}

public synchronized void stop() {
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    while (running) {
        update();
        render();
    }
}


public void update() {
}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.dispose();
    bs.show();
}

public static void main(String[] args) {
    Game game = new Game();
    game.frame.setResizable(false);
    game.frame.setTitle("hackforums");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

    game.start();
}

}


This took a long time. This should give you a big step forward in making your game.
All this can be found on the internet in a simpler way.
However, this helped me remember a lot. So, I kind of did this for me.
If you for some reason learned something from this. I'm proud of you.

Reply

RE: Making a java game. Kind of. Not really. #2
Great tutorial. I might try my hand at this soon. :3

Reply

RE: Making a java game. Kind of. Not really. #3
Sweet, I have been wanting to make a java game for a long time using openGl but I have no story ideas Sad
[Image: hZbv6.gif]

Reply

RE: Making a java game. Kind of. Not really. #4
I love your title lol

I got started with java a long time ago but I just got bored with it :p

Reply

RE: Making a java game. Kind of. Not really. #5
See thats why i dont mess with java or even 2d. when im bored i just make a new model or maybe even a new level and add it to a game. like im helping this guy with his fps. But java is not my cup of jo. But nice tutorial anyway.

Reply







Users browsing this thread: 1 Guest(s)