Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D turn based multiplayer game  (Read 2527 times)
0 Members and 1 Guest are viewing this topic.
Offline cheeseburgers

Senior Newbie





« Posted 2012-02-28 21:29:49 »

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

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!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-02-29 16:40:23 »

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.
Offline Damocles
« Reply #2 - Posted 2012-02-29 17:08:25 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cheeseburgers

Senior Newbie





« Reply #3 - Posted 2012-03-02 05:04:51 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
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);
      }
   }

}
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-03-04 05:45:50 »

1  
timer = new Timer(5, this);

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

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

Offline cheeseburgers

Senior Newbie





« Reply #5 - Posted 2012-10-15 14:50:15 »

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.
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)
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
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".
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!