7 Replies - 867 Views - Last Post: 22 January 2012 - 02:38 PM Rate Topic: -----

Topic Sponsor:

#1 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Game Engine Design

Posted 19 January 2012 - 08:37 AM

I've recently been enrolled in a 'Create A Video-game' course. While it turned out that it was mostly making Source maps for Team Fortress 2 and Portal 2, I convinced the teacher to let me work with Java to make a 2D platformer-like game. But I've run into a wall with some parts of the theory behind game development.

While I can make simple characters that move around the screen with WASD, when I try to make a couple of characters (one player controlled, the rest stationary) the class I use for drawing and the such gets extremely messy. Similar to that, I've also started doing animation with a single object and many frames, but I've found that it also gets very, very messy.

So I was wondering, what are some tips you have for making video games in Java using the Java2D graphics library (the default 2D graphics library). How would I make entity classes cleaner and with that make the main panel class cleaner and readable too.

Thank you!
~Crockeo

(TL;DR version: My classes turn out messy, and I would like some tips on how to make my game code more readable and cleaner)

Is This A Good Question/Topic? 0
  • +

Replies To: Game Engine Design

#2 modi123_1  Icon User is online

  • Suiter #2
  • member icon


Reputation: 3546
  • View blog
  • Posts: 14,964
  • Joined: 12-June 08

Re: Game Engine Design

Posted 19 January 2012 - 09:05 AM

I would suggest reworking your 'engine'. I have an OOP tutorial I did for VB.NET couched in terms of a video game. You might want to eyeball that for a structure - I would imagine it would be the same.

The general idea:
create entity1
create entity2
create entity3

start timer

Engine loop at each tick
entity1.do actions
entity2.do actions
entity3.do actions

force repaint (which makes entity1, entity2, entity3 paint current view)
repeat Engine loop 

Was This Post Helpful? 1
  • +
  • -

#3 ButchDean  Icon User is offline

  • Pro Games Programmer
  • member icon


Reputation: 689
  • View blog
  • Posts: 2,504
  • Joined: 26-November 10

Re: Game Engine Design

Posted 19 January 2012 - 09:08 AM

Yes, game code gets very messy very easily. I'm gathering you haven't heard of design patterns, and in particular, singletons. They are used extensively to ensure that single instances of objects are used and can essentially be seen anywhere in code where their declaration is visible.

Have a look into them.
Was This Post Helpful? 1
  • +
  • -

#4 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Re: Game Engine Design

Posted 19 January 2012 - 09:39 AM

@modi: That's similar to my similar style, but when I end up having many entities it starts making things very messy.

@ButchDean: I'll look into those 'singletons' and start to remake my code.

@All: I'll try to write some simple gameplay elements (ie a black square running around a window with WASD) to show you how I do it now, and how it could be improved.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • Lets-a play!
  • member icon

Reputation: 800
  • View blog
  • Posts: 3,691
  • Joined: 14-February 08

Re: Game Engine Design

Posted 19 January 2012 - 09:41 AM

You could check out my SDL tutorials linked in my signature to get an idea of how to structure your games, yes they are written in C++ but the principles are there. :)

Good luck!
Was This Post Helpful? 0
  • +
  • -

#6 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Re: Game Engine Design

Posted 19 January 2012 - 10:51 AM

Thanks for all the replies. I've gotten myself (or rather created) some source code from a simple interactive 'game'.

(The source code formatting got messed up from going from emacs to the forums)

Mainwindow.java:
import javax.swing.JFrame;

public class MainWindow extends JFrame {
    private MainPanel panel;

    public MainWindow() {
        super("Test code");

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setSize(300, 200);
        setLocationRelativeTo(getRootPane());

        panel = new MainPanel();
        add(panel);

        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MainWindow();
    }
}



MainPanel.java:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class MainPanel extends JPanel implements ActionListener {
    private Timer gameTimer;
    private Block characterBlock;

    public MainPanel() {
	setFocusable(true);

	gameTimer = new Timer(5, this);
	gameTimer.setRepeats(true);
	gameTimer.start();

	characterBlock = new Block(50, 50, 50, 50);

	addKeyListener(new KeyInput());

	this.setVisible(true);
    }

    public void paint(Graphics g) {
	Graphics2D g2d = (Graphics2D)g;

	g2d.setColor(new Color(255, 255, 255));
	g2d.fillRect(0, 0, getSize().width, getSize().height);

	g2d.setColor(new Color(0, 0, 0));
	g2d.fillRect((int)characterBlock.getX(), (int)characterBlock.getY(), (int)characterBlock.getWidth(), (int)characterBlock.getHeight());
    }

    private void step() {
	characterBlock.move();

	//X-Axis window collision
	if (characterBlock.getX() < 0)
	    characterBlock.setLocation((int)characterBlock.getX() + 1, (int)characterBlock.getY());

	if (characterBlock.getX() + characterBlock.getWidth() > getSize().width)
	    characterBlock.setLocation((int)characterBlock.getX() - 1, (int)characterBlock.getY());

	//Y-Axis window collision
	if (characterBlock.getY() < 0)
	    characterBlock.setLocation((int)characterBlock.getX(), (int)characterBlock.getY() + 1);

	if (characterBlock.getY() + characterBlock.getHeight() > getSize().height)
	    characterBlock.setLocation((int)characterBlock.getX(), (int)characterBlock.getY() - 1);
    }

    public void actionPerformed(ActionEvent e) {
	step();
	repaint();
    }

    private class KeyInput extends KeyAdapter {
	public void keyPressed(KeyEvent e) {
	    //X-Axis
	    if (e.getKeyCode() == KeyEvent.VK_A)
		characterBlock.setDX(-1);

	    if (e.getKeyCode() == KeyEvent.VK_D)
		characterBlock.setDX(1);

	    //Y-Axis
	    if (e.getKeyCode() == KeyEvent.VK_W)
		characterBlock.setDY(-1);

	    if (e.getKeyCode() == KeyEvent.VK_S)
		characterBlock.setDY(1);
	}

	public void keyReleased(KeyEvent e) {
	    //X-Axis
	    if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_D)
		characterBlock.setDX(0);

	    //Y-Axis
	    if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_S)
		characterBlock.setDY(0);
	}
    }
}



Block.java:
import java.awt.Rectangle;

public class Block extends Rectangle {
    int dX, dY;

    public Block(int argX, int argY, int argWidth, int argHeight) {
	super(argX, argY, argWidth, argHeight);

	dX = 0;
	dY = 0;
    }

    public void move() {
	x += dX;
	y += dY;
    }

    //Set functions
    public void setDX(int argDX) {
	dX = argDX;
    }

    public void setDY(int argDY) {
	dY = argDY;
    }

    //Get functions 
    public int getDX() {
	return dX;
    }

    public int getDY() {
	return dY;
    }
}



Now that you can look at a bit of my code, what would you say I could improve to make cleaner (or more streamlined) code.
Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 848
  • View blog
  • Posts: 6,182
  • Joined: 18-October 08

Re: Game Engine Design

Posted 21 January 2012 - 01:33 PM

If you're doing a platform game you'll probably want a tile engine. You can read my blog post about an object-oriented tile engine with Java.
Was This Post Helpful? 0
  • +
  • -

#8 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Re: Game Engine Design

Posted 22 January 2012 - 02:38 PM

I'll check it out SixOfEleven, thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1