2D turn based multiplayer game

I want to make a 2D turn based multiplayer “Tank Wars” game. Now, the problem is, I’ve never actually made a single Java game. While I go through a couple of tutorials on Java game programming, I thought I throw out a couple of ideas about the game structure and a question or two.

Classes

  • Game (main, initResources, gameLoop, endGame) - contains a StateManager
  • StateManager (setCurrentState, updateState, drawState) - contains several States
  • State (abstract init, abstract update, abstract draw) - base class for different game states
  • MenuState (init, update, draw) - here you can choose to host (triggers a host and client thread) or join (triggers a client thread), switches to GameState
  • GameState (init, update, draw) - contains all the entities, updates game logic etc
  • Entities (get/setPos, get/setAlive, abstract update, abstract draw) - base class for all entities, like players/tanks, projectiles etc

Kind of a rough description but I hope you catch my drift.

Now I’m still pretty shaky when it comes to certain concepts. First of all, is it a good idea or even possible to make host/client threads the way I described above? Let’s say we have two players:

Player A

  1. Clicks the “Host Game” button
  2. A host thread is started in the background acting as the server
  3. A client thread is started, connecting to the server
  4. The State changes from MenuState to GameState
  5. The client thread receives information from the server in order to update the GameState
  6. The GameState draws the scene using the updated info

Player B

  1. Clicks the “Join Game” button
  2. A client thread is started, connecting to the server
  3. The State changes from MenuState to GameState
  4. The client thread receives information from the server in order to update the GameState
  5. The GameState draws the scene using the updated info

This is roughly how I imagine it to be, but since I haven’t actually made a multiplayer game before, I have no idea.

Another thing is, where exactly do I put all the instances of my entities? Is there an instance of every entity on the server and on every client, only that the entities are not being drawn on the server but only their status is being updated through the input provided by the clients and then sent out to all clients in order to redraw those entities? Could someone give me a simple example of how this usually is done in games like this?

Well, I’m off to the tutorial section!

Cheers!

If you’ve never written a java based game before, starting with a threaded, networked, multiplayer game is a tall order. If it’s turn-based, consider using HTTP for your communications, since there’s plenty of tools in the java world for serving up web apps. Forget about threads, since the web backend will also take care of concurrency naturally.

As for where you keep state, such as the instances of your entities: on the server. Your clients should keep as little state as possible. Partly this is to cut down on the ease of cheating, but it also saves you a whole lot of stress trying to sync up several different sets of state. You really don’t want to have to use a two-phase-commit transaction manager just to move a tank one hex.

(basically a thin client without any gamelogic, and only displaying the scene and sending/receiving commands)

Anyhow, try not to make an onlineclient yet, and make a singleplayergame to learn about that part first.
-> narrow your learningcurve to suit your skills.

I’m struggling a bit with key input. I did some Java tutorials and had everything working just fine, until I decided to move things around and now the input seems to do nothing. I then tried to go back as far as possible, but I still can’t seem to get things working again. The tank is being drawn, but that’s about it. Might be cause it’s 5am, but I just don’t see it …

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;


/**
 * .
 */
public class GameBoard extends JPanel implements ActionListener {
	
	// Timer
	private Timer timer;
	
	// Entities
	//private ArrayList<Entity> entities;
	private Tank tank;
	
	public GameBoard() {
		
		addKeyListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.WHITE);
		setDoubleBuffered(true);		
		setSize(GameServer.W_WIDTH, GameServer.W_HEIGHT);
		
		// Initialize entities
		//entities = new ArrayList<Entity>();
		//entities.add(new Tank(1, true, 10, 10, 1));
		tank = new Tank(1, true, 200, 100, 1);
		
		// Timer
		timer = new Timer(5, this);
        timer.start();
	}
	
	public void doLogic() {
		/*for (int i = 0; i < entities.size(); i++)
			entities.get(i).update();*/
		
		tank.update();
	}
		
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		/*for (int i = 0; i < entities.size(); i++) {
			if (entities.get(i).isAlive())
				entities.get(i).draw(g);
		}*/
		
		tank.draw(g);
				
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}
	
	public void actionPerformed(ActionEvent e) {
		doLogic();
		repaint();
	}
	
	private class TAdapter extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			tank.keyReleased(e);
		}
		
		public void keyPressed(KeyEvent e) {
			tank.keyPressed(e);
		}
	}

}

You’re running this at 200FPS?!?!?

Try extending JComponent instead please, a JPanel was not made to capture focus :slight_smile:

Hi, I’m back!

I gave up on this project a couple of weeks after starting this thread. It simply was way too frustrating.

[quote]If you’ve never written a java based game before, starting with a threaded, networked, multiplayer game is a tall order.
[/quote]
That’s what made it so frustrating. This was part of a Java course I had about a year ago at my university. It was an 8 week course and this project was just one part of it. We never had any Java lessons before that course, never got to make a single game and never wrote a single line of network code. Still the teacher referred to it as being a “small” project that shouldn’t be an issue. Yeah right!

Anyhow, I’m back at it because I can’t stand leaving things unfinished. While I’m trying to get back into Java and whatever crappy code I wrote back then, I’ve got a couple of new questions.

[quote](basically a thin client without any gamelogic, and only displaying the scene and sending/receiving commands)
[/quote]
That’s what I’m aiming for. Now there’s some confusion on where to put what and how the components should interact with one another. I’ve decided to get the frame for my client right before trying to finish the server. This is what I’ve got so far:

Game

package tw.client;

import javax.swing.JFrame;


public class Game extends JFrame {

	public static final int W_WIDTH = 800;
	public static final int W_HEIGHT = 600;
	
	private static final int FRAMES_PER_SECOND = 25;
	private static final int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
	
	private GameBoard gameBoard;
	private boolean running = false;
	
	public static ClientThread client;
	
	public Game() {
		
		// Initialize the frame
		setTitle("Game Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(W_WIDTH, W_HEIGHT);
		setLocationRelativeTo(null);		
		setResizable(false);
		setVisible(true);
		
		// Add board to frame and give focus
		gameBoard = new GameBoard();
		add(gameBoard);
		gameBoard.requestFocusInWindow();
		
		// Start game loop
		running = true;
		run();
	}
	
	/**
	 * Stops the game loop.
	 */
	public void stop() {
		running = false;
	}

	/**
	 * Starts the game loop.
	 */
	private void run() {
		long nextGameTick = System.nanoTime() / 1000000;
		long sleepTime;
		
		while (running) {
			gameBoard.updateGame();
			gameBoard.repaint();
			
			nextGameTick += SKIP_TICKS;
			sleepTime = nextGameTick - System.nanoTime() / 1000000;

			if (sleepTime > 0) {
				try{
					Thread.sleep(sleepTime);
				}catch(Exception e){}
			}
		}
	}

	
	/**
	 * MAIN METHOD
	 * @param args
	 */
	public static void main(String[] args) {
		client = new ClientThread();
		
		new Game();
	}
}

ClientThread

package tw.client;

import java.awt.*;
import java.io.IOException;
import java.net.*;

public class ClientThread implements Runnable {

	private static final int PORT = 2727;
	
	public void Client() throws IOException {
		init();
	}
	
	public void init() throws IOException {
		System.out.println("Initializing ...");
		InetAddress addr = InetAddress.getLocalHost();
		Socket socket = new Socket(addr, PORT);
		System.out.println("The new socket: " + socket);
	}
	
	public void run() {
		// Receive data from server
		
		// Send requests to server		
	}

}

GameBoard

package tw.client;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.text.html.parser.Entity;


/**
 * .
 */
public class GameBoard extends JComponent implements KeyListener {
	
	//private StatusPanel statusPanel;
	private ArrayList<Entity> entities;
	
	public GameBoard() {
		
		// Setup panel
		setFocusable(true);
		setBackground(Color.LIGHT_GRAY);
		setDoubleBuffered(true);
		setSize(Game.W_WIDTH, Game.W_HEIGHT);

		// Setup key listener
		addKeyListener(this);
		//setFocusTraversalKeysEnabled(false);
			
		// Add status panel
		/*statusPanel = new StatusPanel();
		add(statusPanel);
		
		// Container for all entities
		entities = new ArrayList<Entity>();*/
	}
	
	/**
	 * Update game.
	 */
	public void updateGame() {
		// Receive game data from server to update gameBoard/entities
	}
		
	/**
	 * Draw game.
	 */
	public void paintComponent(Graphics g) {
		// Clear off-screen bitmap
		super.paintComponent(g);
		
		// Cast to Graphics2D for more options
		Graphics2D g2d = (Graphics2D) g;
		
		// Draw updated gameBoard/entities
	}
	
	
	// Handling key input
	/////////////////////////////////////////////
	/**
	 * Key input - when key is pressed.
	 */
	public void keyPressed(KeyEvent e) {
		// Ask server if my turn
		// If my turn: 		Send request to server to change angle/power of gun
		// If not my turn: 	Display some msg
	}
	
	/**
	 * Key input - when key is released.
	 */
	public void keyReleased(KeyEvent e) {
		// Ask server if my turn
		// If my turn:		Send request to change ammunition and/or fire gun
		// If not my turn: 	Display some msg
	}
	
	public void keyTyped(KeyEvent e) {
		// do nothing
	}

}

The updateGame method is the receiving one, the key input methods the sending ones (through the clientThread obviously).

  • What is the best way to make them interact with the server through the clientThread? What is the easiest way to give them access to clientThread?
  • The size of all the game data (entities, player status etc) is probably not that big. Can I push the whole entity array from the server to the clients for every update, or should I only send data for those things that have been altered in some way?

Cheers!

EDIT: made the clientThread “public static”.