Advertisement
OllieHinde

Pong Server

Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. package States;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.io.EOFException;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10.  
  11. import com.sun.glass.events.KeyEvent;
  12.  
  13. import Main.AbstractState;
  14. import Main.MainPanel;
  15. import Main.StateManager;
  16.  
  17. public class Pong extends AbstractState {
  18.    
  19.     private int width;
  20.     private int height;
  21.    
  22.     private int paddleWidth;
  23.     private int paddleHeight;
  24.     private int paddleSpeed;
  25.    
  26.     private ObjectOutputStream output;
  27.     private ObjectInputStream input;
  28.     private ServerSocket server;
  29.     private Socket connection;
  30.    
  31.     private int serverXPos;
  32.     private int serverYPos;
  33.     private int clientXPos;
  34.     private int clientYPos;
  35.    
  36.     private boolean up = false;
  37.     private boolean down = false;
  38.    
  39.     public Pong(StateManager sm) {
  40.         this.sm = sm;
  41.         init();
  42.         try {
  43.             //image loading
  44.         }
  45.         catch(Exception exception) {
  46.             exception.printStackTrace();
  47.         }
  48.         new Thread(networkingThread).start();
  49.     }
  50.    
  51.     Runnable networkingThread = new Runnable() {
  52.         public void run() {
  53.             startServer();
  54.         }
  55.     };
  56.    
  57.     public void init() {
  58.         width = MainPanel.WIDTH;
  59.         height = MainPanel.HEIGHT;
  60.        
  61.         paddleWidth = 20;
  62.         paddleHeight = 80;
  63.         paddleSpeed = 3;
  64.        
  65.         clientXPos = width - paddleWidth - 20;
  66.         clientYPos = 30;
  67.         serverXPos = 20;
  68.         serverYPos = height / 2 - paddleHeight / 2;
  69.     }
  70.    
  71.     public void update() {
  72.        if(up) {
  73.            serverYPos -= paddleSpeed;
  74.        }
  75.        if(down) {
  76.            serverYPos += paddleSpeed;
  77.        }
  78.        if(serverYPos < 0) {
  79.            serverYPos = 0;
  80.        }
  81.        if(serverYPos + paddleHeight > height) {
  82.            serverYPos = height - paddleHeight;
  83.        }
  84.     }
  85.  
  86.     public void draw(Graphics2D g) {
  87.         g.setColor(Color.BLACK);
  88.         g.fillRect(0, 0, width, height);
  89.         g.setColor(Color.WHITE);
  90.         g.fillRect(serverXPos, serverYPos, paddleWidth, paddleHeight);
  91.         g.fillRect(clientXPos, clientYPos, paddleWidth, paddleHeight);
  92.     }
  93.    
  94.     public void keyPressed(int k) {
  95.         if(k == KeyEvent.VK_UP) {
  96.             up = true;
  97.             down = false;
  98.         }
  99.         if(k == KeyEvent.VK_DOWN) {
  100.             up = false;
  101.             down = true;
  102.         }
  103.        
  104.     }
  105.    
  106.     public void keyReleased(int k) {
  107.         if(k == KeyEvent.VK_UP) {
  108.             up = false;
  109.         }
  110.         if(k == KeyEvent.VK_DOWN) {
  111.             down = false;
  112.         }
  113.     }
  114.    
  115.     //Server Setup
  116.     public void startServer() {
  117.         try {
  118.             server = new ServerSocket(2199, 1000);
  119.             while(true) {
  120.                 try {
  121.                     waitForConnection();
  122.                     setupStreams();
  123.                     while(true) {
  124.                         sendingData();
  125.                         receivingData();
  126.                     }
  127.                 }
  128.                 catch(EOFException e) {
  129.                     //Disconnected Unexpectedly
  130.                     e.printStackTrace();
  131.                 }
  132.                 finally {
  133.                     //Close Connection
  134.                     closeConnection();
  135.                 }
  136.             }
  137.         }
  138.         catch(IOException e) {
  139.             e.printStackTrace();
  140.         }
  141.     }
  142.    
  143.     //Wait for Connection
  144.     private void waitForConnection() throws IOException {
  145.         System.out.println("Waiting for a connection...");
  146.         connection = server.accept();
  147.         System.out.println("Now connected to " + connection.getInetAddress().getHostName());
  148.     }
  149.    
  150.     //Send and Receive Data
  151.     private void setupStreams() throws IOException {
  152.         output = new ObjectOutputStream(connection.getOutputStream());
  153.         output.flush();
  154.         input = new ObjectInputStream(connection.getInputStream());
  155.     }
  156.    
  157.     //Sending Data
  158.     public void sendingData() {
  159.         try {
  160.             output.writeObject(serverYPos);
  161.             output.flush();
  162.         }
  163.         catch(IOException e) {
  164.             e.printStackTrace();
  165.         }
  166.     }
  167.    
  168.     //Receiving Data
  169.     public void receivingData() throws IOException {
  170.         try {
  171.             clientYPos = (int) input.readObject();
  172.         }
  173.         catch(ClassNotFoundException e) {
  174.             e.printStackTrace();
  175.         }
  176.     }
  177.    
  178.     //Close Connection
  179.     private void closeConnection() {
  180.         try {
  181.             output.close();
  182.             input.close();
  183.             connection.close();
  184.         }
  185.         catch(IOException e) {
  186.             e.printStackTrace();
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement