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”.