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)
Game Engine Design
Page 1 of 17 Replies - 867 Views - Last Post: 22 January 2012 - 02:38 PM
Topic Sponsor:
Replies To: Game Engine Design
#2
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:
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
#3
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.
Have a look into them.
#4
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.
@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.
#5
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!
Good luck!
#6
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:
MainPanel.java:
Block.java:
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.
(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.
#8
Re: Game Engine Design
Posted 22 January 2012 - 02:38 PM
I'll check it out SixOfEleven, thank you!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|