Pong game on the net (Java)

Implementing the network part into my pong game

Page 1 of 1

4 Replies - 5906 Views - Last Post: 10 November 2010 - 11:42 AM Rate Topic: -----

#1 Maverick89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Pong game on the net (Java)

Posted 03 November 2010 - 05:33 PM

Hello,

I am writing a project for a class I'm taking, but their expectations pretty much exceed the ammount of knowledge which I acquired on previous classes on my colledge. It is suppose to be a pong game which can be played over the internet, between 2 players. the code for the game itself is pretty much complete, but the main problem is the network part, so I am asking for your help concerning this little bugger. Can I avoid making a client/server application and just go for a direct IP connection ? Example : my client will be the "host", the other side will enter my IP, my app will think :"I'm receiving an IP, ok I will be the host the other guy is the client, I will initialize this..."
My knowledge concerning sockets and general network programming is modest at best, so I would appreciate any help here in writting the protocol for the connection and the network part of the code itself.

Thank you all in advance.


Here is the code I have written so far :


First the class which defines the panel, ball, players etc... :

package game;

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class PanelBall extends JPanel implements Runnable {

	private static final long serialVersionUID = 1L;
	// position of the X and Y axis, player positions
	private int ballX = 10, ballY = 110, player1X = 10, player1Y = 100,
			player2X = 600, player2Y = 100;
	Thread thread;
	int right = 5; // right
	int left = -5; // left
	int up = 5; // up
	int down = -5; // down
	int width, height; // size of the ball
	// result
	int contPlay1 = 0, contPlay2 = 0;
	boolean player1FlagUp, player1FlagDown, player2FlagUp, player2FlagDown;
	boolean game, gameOver;

	public PanelBall() {
		game = true;
		thread = new Thread(this);
		thread.start();
	}

	// make a player
	public void paintComponent(Graphics gc) {
		setOpaque(false);
		super.paintComponent(gc);

		// make the ball

		gc.setColor(Color.black);
		gc.fillOval(ballX, ballY, 8, 8);

		// make a panel
		gc.fillRect(player1X, player1Y, 10, 25);
		gc.fillRect(player2X, player2Y, 10, 25);

		// make the scoreboard
		gc.drawString("Player1: " + contPlay1, 25, 10);
		gc.drawString("Player2: " + contPlay2, 150, 10);

		if (gameOver)
			gc.drawString("Game Over", 100, 125);
	}

	// Position of the X and Y axis for the ball
	public void drawBall(int nx, int ny) {
		ballX = nx;
		ballY = ny;
		this.width = this.getWidth();
		this.height = this.getHeight();
		repaint();
	}

	// Here we receive from the game container class the key pressed
	public void keyPressed(KeyEvent evt) {
		switch (evt.getKeyCode()) {
		// Move ship 1
		case KeyEvent.VK_W:
			player1FlagUp = true;
			break;
		case KeyEvent.VK_S:
			player1FlagDown = true;
			break;

		// Move ship 2
		case KeyEvent.VK_UP:
			player2FlagUp = true;
			break;
		case KeyEvent.VK_DOWN:
			player2FlagDown = true;
			break;
		}
	}

	// Here we receive from the game container class the key released
	public void keyReleased(KeyEvent evt) {
		switch (evt.getKeyCode()) {
		// Movement for player 1
		case KeyEvent.VK_W:
			player1FlagUp = false;
			break;
		case KeyEvent.VK_S:
			player1FlagDown = false;
			break;

		// Movement for player 2
		case KeyEvent.VK_UP:
			player2FlagUp = false;
			break;
		case KeyEvent.VK_DOWN:
			player2FlagDown = false;
			break;
		}
	}

	// Move player 1
	public void moverPlayer1() {
		if (player1FlagUp == true && player1Y >= 0)
			player1Y += down;
		if (player1FlagDown == true && player1Y <= (this.getHeight() - 25))
			player1Y += up;
		drawPlayer1(player1X, player1Y);
	}

	// Move player 2
	public void moverPlayer2() {
		if (player2FlagUp == true && player2Y >= 0)
			player2Y += down;
		if (player2FlagDown == true && player2Y <= (this.getHeight() - 25))
			player2Y += up;
		drawPlayer2(player2X, player2Y);
	}

	// Position on Y for the player 1
	public void drawPlayer1(int x, int y) {
		this.player1X = x;
		this.player1Y = y;
		repaint();
	}

	// Position on Y for the player 2
	public void drawPlayer2(int x, int y) {
		this.player2X = x;
		this.player2Y = y;
		repaint();
	}

	public void run() {
		// TODO Auto-generated method stub
		boolean leftRight = false;
		boolean upDown = false;

		while (true) {

			if (game) {

				// Ball movement left-right
				if (leftRight) {
					// to the right
					ballX += right;
					if (ballX >= (width - 8))
						leftRight = false;
				} else {
					// to the left
					ballX += left;
					if (ballX <= 0)
						leftRight = true;
				}

				// Ball movement up-down
				if (upDown) {
					// up
					ballY += up;
					if (ballY >= (height - 8))
						upDown = false;

				} else {
					// down
					ballY += down;
					if (ballY <= 0)
						upDown = true;
				}
				drawBall(ballX, ballY);

				
				try {
					Thread.sleep(50);
				} catch (InterruptedException ex) {

				}

				// move player 1
				moverPlayer1();

				// move player 2
				moverPlayer2();

				// result for player 1
				if (ballX >= (this.getWidth() - 8))
					contPlay1++;

				// result for player 2
				if (ballX == 0)
					contPlay2++;

				// Game over.
				// when the score reaches this value the game is over
				if (contPlay1 == 11 || contPlay2 == 11) {
					game = false;
					gameOver = true;
				}

				// The ball hit player 1's wall
				if (ballX <= player1X + 10 && ballY >= player1Y
						&& ballY <= (player1Y + 25))
					leftRight = true;

				// The ball hit player 2's wall
				if (ballX >= (player2X - 5) && ballY >= player2Y
						&& ballY <= (player2Y + 25))
					leftRight = false;
			}
		}
	}

}



Class which defines the game window itself :

package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Toolkit;

public class Main extends JFrame {

	private static final long serialVersionUID = 1L; // Eclipse added this
														// automatically

	private JPanel jContentPane = null;

	private PanelBall panel = null; // This is the panel of the game class

	private JMenuBar jJMenuBar = null;

	private JMenu jMenuFile = null;

	private JMenu jMenuAbout = null;

	private JMenuItem jMenuItemQuit = null;

	private JMenuItem jMenuItemExitGame = null;

	private PanelBall getPanel() {
		if (panel == null) {
			panel = new PanelBall(); // The panel is created
		}
		return panel;
	}

	/**
	 * This is the default constructor
	 */
	public Main() {
		super();
		initialize();
		// Listeners for the keyboard
		this.addKeyListener(new KeyAdapter() {
			// Method for the key pressed
			public void keyPressed(KeyEvent evt) {
				formKeyPressed(evt);
			}

			// Method for the key released
			public void keyReleased(KeyEvent evt) {
				formKeyReleased(evt);
			}
		});

	}

	// Here i'm stating the method that will send the key pressed to the game
	// class
	private void formKeyPressed(KeyEvent evt) {
		panel.keyPressed(evt);
	}

	// Here i'm stating the method that will send the key released to the game
	// class
	private void formKeyReleased(KeyEvent evt) {
		panel.keyReleased(evt);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setResizable(false);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/icons/pingpong.png")));
		this.setJMenuBar(getJJMenuBar());
		this.setBounds(new Rectangle(312, 184, 250, 250)); // Position on the
															// desktop
		this.setMinimumSize(new Dimension(640, 480));
		this.setMaximumSize(new Dimension(640, 480));
		this.setContentPane(getJContentPane());
		this.setTitle("Pong Zone");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuFile());
			jJMenuBar.add(getJMenuAbout());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenuFile
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuFile() {
		if (jMenuFile == null) {
			jMenuFile = new JMenu();
			jMenuFile.setText("File");
			jMenuFile.add(getJMenuItemQuit());
			jMenuFile.add(getJMenuItemExitGame());
		}
		return jMenuFile;
	}

	/**
	 * This method initializes jMenuAbout
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuAbout() {
		if (jMenuAbout == null) {
			jMenuAbout = new JMenu();
			jMenuAbout.setText("About");
		}
		return jMenuAbout;
	}

	/**
	 * This method initializes jMenuItemQuit
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemQuit() {
		if (jMenuItemQuit == null) {
			jMenuItemQuit = new JMenuItem();
			jMenuItemQuit.setText("Quit");
			jMenuItemQuit
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							new First().setVisible(true);
							dispose();
						}
					});
		}
		return jMenuItemQuit;
	}

	/**
	 * This method initializes jMenuItemExitGame
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemExitGame() {
		if (jMenuItemExitGame == null) {
			jMenuItemExitGame = new JMenuItem();
			jMenuItemExitGame.setText("ExitGame");
			jMenuItemExitGame
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.exit(1);
						}
					});
		}
		return jMenuItemExitGame;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main thisClass = new Main();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

}


Class which defines the starting window and menu :

package game;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import java.awt.event.KeyEvent;

public class First extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton localGame = null;
	private JButton internetGame = null;
	private JLabel jLabelPong = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenuFile = null;
	private JMenu jMenuAbout = null;
	private JMenuItem jMenuItemStartNewLocal = null;
	private JMenuItem jMenuItemStartNewNet = null;
	private JMenuItem jMenuItemExit = null;
	private JLabel jLabelBackground = null;

	/**
	 * This is the default constructor
	 */
	public First() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setName("Start window");
		this.setLocation(new Point(400, 200));
		this.setSize(new Dimension(453, 353));
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/icons/pingpong.png")));
		this.setResizable(false);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("Pong Zone");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {

			jLabelBackground = new JLabel();
			jLabelBackground.setBounds(new Rectangle(0, 0, 451, 301));
			jLabelBackground.setIcon(new ImageIcon(getClass().getResource(
					"/icons/puma-ping-pong-table.jpg")));
			jLabelBackground.setText("JLabel");
			jLabelPong = new JLabel();
			jLabelPong.setBounds(new Rectangle(75, 30, 298, 104));
			jLabelPong.setEnabled(true);
			jLabelPong.setHorizontalTextPosition(SwingConstants.CENTER);
			jLabelPong.setHorizontalAlignment(SwingConstants.CENTER);
			jLabelPong.setFont(new Font("Felix Titling", Font.BOLD
					| Font.ITALIC, 42));
			jLabelPong.setForeground(new Color(255, 215, 0));
			jLabelPong.setText("Pong Zone");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.white);
			jContentPane.add(getLocalGame(), null);
			jContentPane.add(getInternetGame(), null);
			jContentPane.add(jLabelPong, null);
			jContentPane.add(jLabelBackground, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes localGame
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLocalGame() {
		if (localGame == null) {
			localGame = new JButton();
			localGame.setBounds(new Rectangle(30, 210, 166, 46));
			localGame.setText("Start local game");
			localGame.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new Main().setVisible(true);
					dispose();
				}
			});

		}
		return localGame;
	}

	/**
	 * This method initializes internetGame
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getInternetGame() {
		if (internetGame == null) {
			internetGame = new JButton();
			internetGame.setBounds(new Rectangle(225, 210, 166, 46));
			internetGame.setText("I want to play online");
			internetGame.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new Online().setVisible(true);
					dispose();

				}
			});
		}
		return internetGame;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuFile());
			jJMenuBar.add(getJMenuAbout());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenuFile
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuFile() {
		if (jMenuFile == null) {
			jMenuFile = new JMenu();
			jMenuFile.setText("File");
			jMenuFile.add(getJMenuItemStartNewLocal());
			jMenuFile.add(getJMenuItemStartNewNet());
			jMenuFile.add(getJMenuItemExit());
		}
		return jMenuFile;
	}

	/**
	 * This method initializes jMenuAbout
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuAbout() {
		if (jMenuAbout == null) {
			jMenuAbout = new JMenu();
			jMenuAbout.setText("About");
		}
		return jMenuAbout;
	}

	/**
	 * This method initializes jMenuItemStartNewLocal
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemStartNewLocal() {
		if (jMenuItemStartNewLocal == null) {
			jMenuItemStartNewLocal = new JMenuItem();
			jMenuItemStartNewLocal.setText("Start new local game");
			jMenuItemStartNewLocal
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							new Main().setVisible(true);
							dispose();
						}
					});
		}
		return jMenuItemStartNewLocal;
	}

	/**
	 * This method initializes jMenuItemStartNewNet
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemStartNewNet() {
		if (jMenuItemStartNewNet == null) {
			jMenuItemStartNewNet = new JMenuItem();
			jMenuItemStartNewNet.setText("Start new internet game");
		}
		return jMenuItemStartNewNet;
	}

	/**
	 * This method initializes jMenuItemExit
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemExit() {
		if (jMenuItemExit == null) {
			jMenuItemExit = new JMenuItem();
			jMenuItemExit.setText("Exit");
			jMenuItemExit
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.exit(0);
						}
					});
		}
		return jMenuItemExit;
	}

	public static void main(String[] args) {
		First prvi = new First();
		prvi.setVisible(true);
	}
}



Class which defines the online visual class :

package game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;

public class Online extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabelPong = null;
	private JLabel jLabelBackground = null;
	private JLabel jLabelInsertIP = null;
	private JTextField jTextFieldIPAddress = null;
	private JButton jButtonConnect = null;
	private JButton jButtonCancel = null;

	/**
	 * This is the default constructor
	 */
	public Online() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(436, 319);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/icons/pingpong.png")));
		this.setContentPane(getJContentPane());
		this.setTitle("Pong Zone");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelBackground = new JLabel();
			jLabelBackground.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			jLabelBackground.setIcon(new ImageIcon(getClass().getResource(
					"/icons/puma-ping-pong-table.jpg")));
			jLabelBackground.setBounds(new Rectangle(0, 0, 421, 286));
			jLabelBackground.setText("JLabel");
			jLabelInsertIP = new JLabel();
			jLabelInsertIP.setBounds(new Rectangle(30, 150, 251, 46));
			jLabelInsertIP.setHorizontalAlignment(SwingConstants.CENTER);
			jLabelInsertIP.setHorizontalTextPosition(SwingConstants.CENTER);
			jLabelInsertIP.setFont(new Font("Dialog", Font.BOLD, 14));
			jLabelInsertIP.setForeground(new Color(255, 215, 0));
			jLabelInsertIP.setText("Please enter your host's IP address");
			jLabelPong = new JLabel();
			jLabelPong.setBounds(new Rectangle(75, 30, 298, 104));
			jLabelPong.setEnabled(true);
			jLabelPong.setHorizontalTextPosition(SwingConstants.CENTER);
			jLabelPong.setHorizontalAlignment(SwingConstants.CENTER);
			jLabelPong.setFont(new Font("Felix Titling", Font.BOLD
					| Font.ITALIC, 42));
			jLabelPong.setForeground(new Color(255, 215, 0));
			jLabelPong.setText("Pong Zone");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.white);
			jContentPane.add(jLabelPong, null);
			jContentPane.add(jLabelInsertIP, null);
			jContentPane.add(getJTextFieldIPAddress(), null);
			jContentPane.add(getJButtonConnect(), null);
			jContentPane.add(getJButtonCancel(), null);
			jContentPane.add(jLabelBackground, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextFieldIPAddress
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldIPAddress() {
		if (jTextFieldIPAddress == null) {
			jTextFieldIPAddress = new JTextField();
			jTextFieldIPAddress.setBounds(new Rectangle(30, 210, 241, 46));
		}
		return jTextFieldIPAddress;
	}

	/**
	 * This method initializes jButtonConnect
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonConnect() {
		if (jButtonConnect == null) {
			jButtonConnect = new JButton();
			jButtonConnect.setBounds(new Rectangle(315, 135, 91, 46));
			jButtonConnect.setText("Start");
		}
		return jButtonConnect;
	}

	/**
	 * This method initializes jButtonCancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setBounds(new Rectangle(315, 210, 91, 46));
			jButtonCancel.setText("Cancel");
			jButtonCancel
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							new First().setVisible(true);
							dispose();
						}
					});
		}
		return jButtonCancel;
	}

} // @jve:decl-index=0:visual-constraint="297,4"




P.s. For some reason I am not allowed to attach the .jar file with the code done so far, so here is an external link for the thing Ping pong

Is This A Good Question/Topic? 0
  • +

Replies To: Pong game on the net (Java)

#2 Maverick89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Pong game on the net (Java)

Posted 05 November 2010 - 11:52 AM

Haha, over 100 people looked at this and said zoicks :)

Ok to make things a bit simpler, I want to make a TCP protocol for this thing, which will connect a server aspect of the program and 2 clients. I generally know what the protocol looks like, but I'm not sure how to make the server load the up and down commands from the client sides and beam them back the relevant information. Hope this cleares the air a bit...
Was This Post Helpful? 0
  • +
  • -

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Pong game on the net (Java)

Posted 09 November 2010 - 06:50 PM

I am going to assume that you are familiar with the Java API regarding networking. If you are not familiar with it, check out this tutorial: http://download.orac...ientServer.html

That link should give you an idea of how a basic client/server server looks like. When sending data, you typically want to send them in packets with prefixed with an ID. This ID lets the program know what type of message was sent, allowing it to process it specifically.

You should have a routine that strips the ID from the beginning of the data stream and processes it accordingly. Here is a basic example...
switch (ID)
{
  case 0:
    // Display console input saying that the server has shutdown.
  break;
}


Once you strip the beginning of the the message, you can strip the rest of the message because you'll know how it is constructed.
// Write 2 (which is a chat message) to the data stream.
// Write "This is a chat message" to the data stream.
// Send data stream.
// On other end, receive data stream.
// Strip the first bits of data from the data stream, which tells us what kind of message it is.
// Its a "chat message" so strip the rest of the data, which is the chat message.



Networking works pretty much the same as your general game-type applications. Initialize, Receive Input, Give output, Repeat step 2. On both ends, the application keeps trying to read a message. When it reads a message, it dissects it and performs the appropriate behavior based on the received message.

In your basic application you probably only need two different packet types: start moving and stop moving. When the player pushes a key down.. it sends a "start moving" message to the other side. The other side receives the "start moving" message and moves the other player until it receives a "stop moving" message.

To build messages you could send a delimited string: "MSGID|DATA|DATA|DATA|DATA". An example for our scenario: "1|1|1"
The first first '1' is the message ID, which stands for a "move message". Next is the "start or stop moving" data, which is 1, meaning start moving(0 meaning stop). The last is "up or down" data, which is 1.. meaning up (0 would mean down). Or you could do it this way: "1|start|up" or "MOVE|START|UP" Doesn't matter. Although I wouldn't send strings of data. Instead, I would write and read bytes, which is a lot more efficient.

I can't give you the exact pieces of code for this because I am fairly new to the Java API (coming from C++). Although the resource link I just gave you (first time I read it is right now) gives you a lot of direction to implement the concept.
Was This Post Helpful? 1
  • +
  • -

#4 Maverick89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Pong game on the net (Java)

Posted 10 November 2010 - 04:47 AM

Hmmm, I get where your idea is going, makes sence. I will try to implement this into my thing-a-magig over the next few days and will come back to you with results. Thanks mate !
Was This Post Helpful? 0
  • +
  • -

#5 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 782
  • View blog
  • Posts: 7,610
  • Joined: 05-May 05

Re: Pong game on the net (Java)

Posted 10 November 2010 - 11:42 AM

I think I wrote a tutorial that introduces you to client/server stuff in JAVA.. Didn't I?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1