Advertisement
Guest User

Pong in Java

a guest
Oct 5th, 2015
2,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.36 KB | None | 0 0
  1. Window.java:
  2.  
  3. package com.beckwith.main;
  4.  
  5. import java.awt.Dimension;
  6. import java.awt.event.KeyListener;
  7.  
  8. import javax.swing.JFrame;
  9.  
  10. public class Window extends JFrame {
  11.     private static final long serialVersionUID = 1L;
  12.  
  13.     public Window(String name, int width, int height) {
  14.         setTitle(name);
  15.         setPreferredSize(new Dimension(width, height));
  16.         setMinimumSize(new Dimension(width, height));
  17.         setMaximumSize(new Dimension(width, height));
  18.         setResizable(false);
  19.         setLocationRelativeTo(null);
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         setVisible(true);
  22.     }
  23.  
  24.     public void addGameInstance(Game game) {
  25.         add(game);
  26.     }
  27.  
  28.     public void addListener(KeyListener listener) {
  29.         this.addKeyListener(listener);
  30.     }
  31. }
  32.  
  33.  
  34. Game.java:
  35.  
  36. package com.beckwith.main;
  37.  
  38. import java.awt.Color;
  39. import java.awt.Font;
  40. import java.awt.FontMetrics;
  41. import java.awt.Graphics;
  42. import java.awt.event.KeyEvent;
  43. import java.awt.event.KeyListener;
  44.  
  45. import javax.swing.JPanel;
  46.  
  47. public class Game extends JPanel implements Runnable, KeyListener {
  48.     private static final long serialVersionUID = 1L;
  49.     public static final int GAME_WIDTH = 1600;
  50.     public static final int GAME_HEIGHT = 900;
  51.  
  52.     private String[] instructions = { "'A' for One Player", "'S' for Two Player", "'D' for No Player",
  53.                                     "WASD for Player One Movement", "Arrow Keys for Player Two Movement", "SPACE to Start/Reset ball",
  54.                                     "ESC to quit", "******Only Two Player is working right now******"};
  55.  
  56.     private boolean menu = true;
  57.     private boolean game = false;
  58.     private boolean onePlayer = true, twoPlayer = false, noPlayer = false;
  59.  
  60.     private Thread t;
  61.     private boolean running = false;
  62.  
  63.     public static int scoreOne;
  64.     public static int scoreTwo;
  65.     public static String scoreOneString;
  66.     public static String scoreTwoString;
  67.  
  68.     protected Player playerOne;
  69.     protected Player playerTwo;
  70.     protected Ball ball;
  71.     protected static int ballSpeed;
  72.     public static boolean startDirection;
  73.  
  74.     // BOOLEANS THAT WILL BE USED FOR SMOOTHER MOVEMENT
  75.     private boolean right = false, left = false, up = false, down = false, space = false;
  76.     private boolean rightTwo = false, leftTwo = false, upTwo = false, downTwo = false;
  77.  
  78.     public Game() {
  79.         playerOne = new Player(0, 360, 15, 200, ID.PLAYER_ONE);
  80.         playerTwo = new Player(GAME_WIDTH - 21, 360, 15, 200, ID.PLAYER_TWO);
  81.         ball = new Ball(0, 0, 50, 50, ID.BALL);
  82.     }
  83.  
  84.     // INITIALIZES NEW THREAD AND STARTS IT (MAKES IT CALL RUN METHOD)
  85.     private void start() {
  86.         if (t == null) {
  87.             t = new Thread(this);
  88.         }
  89.         t.start();
  90.         running = true;
  91.     }
  92.  
  93.     // MAIN GAME LOOP (NEEDS REVISING)
  94.     public void run() {
  95.         while (running) {
  96.             updateLogic();
  97.             repaint();
  98.  
  99.             try {
  100.                 Thread.sleep(2);
  101.             } catch (InterruptedException e) {
  102.                 e.printStackTrace();
  103.             }
  104.         }
  105.     }
  106.  
  107.     //UPDATES ALL OBJECT POSITIONS AND USED TO DECIDE GAME STATE
  108.     public void updateLogic() {
  109.         if (menu) {
  110.             if (left) {
  111.                 onePlayer = true;
  112.                 menu = false;
  113.                 game = true;
  114.             }
  115.             if (down) {
  116.                 onePlayer = false;
  117.                 twoPlayer = true;
  118.                 menu = false;
  119.                 game = true;
  120.             }
  121.             if (right) {
  122.                 onePlayer = false;
  123.                 noPlayer = true;
  124.                 menu = false;
  125.                 game = true;
  126.             }
  127.         } else if (game) {
  128.             // X DIRECTION MOVEMENT PLAYER ONE
  129.             if (right)
  130.                 playerOne.xSpeed = 3;
  131.             else if (left)
  132.                 playerOne.xSpeed = -3;
  133.             else
  134.                 playerOne.xSpeed = 0;
  135.  
  136.             // Y DIRECTION MOVEMENT PLAYER ONE
  137.             if (down)
  138.                 playerOne.ySpeed = 3;
  139.             else if (up)
  140.                 playerOne.ySpeed = -3;
  141.             else
  142.                 playerOne.ySpeed = 0;
  143.  
  144.             // X DIRECTION MOVEMENT PLAYER TWO
  145.             if (rightTwo)
  146.                 playerTwo.xSpeed = 3;
  147.             else if (leftTwo)
  148.                 playerTwo.xSpeed = -3;
  149.             else
  150.                 playerTwo.xSpeed = 0;
  151.  
  152.             // Y DIRECTION MOVEMENT PLAYER TWO
  153.             if (downTwo)
  154.                 playerTwo.ySpeed = 3;
  155.             else if (upTwo)
  156.                 playerTwo.ySpeed = -3;
  157.             else
  158.                 playerTwo.ySpeed = 0;
  159.  
  160.             if (ball.isColliding(playerOne)) {
  161.                 ball.x = 15;
  162.                 ball.xSpeed--;
  163.                 ball.xSpeed = -ball.xSpeed;
  164.             }
  165.             if (ball.isColliding(playerTwo)) {
  166.                 ball.x = GAME_WIDTH - 21 - ball.WIDTH;
  167.                 ball.xSpeed++;
  168.                 ball.xSpeed = -ball.xSpeed;
  169.             }
  170.  
  171.             // UPDATE OBJECT LOGIC
  172.             playerOne.update();
  173.             playerTwo.update();
  174.             ball.update();
  175.             scoreOneString = "Player One: " + scoreOne;
  176.             scoreTwoString = "Player Two: " + scoreTwo;
  177.         }
  178.     }
  179.    
  180.      public void drawCenteredString(String s, int w, int h, Graphics g) {
  181.             FontMetrics fm = g.getFontMetrics();
  182.             int x = (w - fm.stringWidth(s)) / 2;
  183.             int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
  184.             g.drawString(s, x, y);
  185.      }
  186.  
  187.     // METHOD OF JPANEL TO BE USED TO SHOW OBJECTS
  188.     @Override
  189.     public void paintComponent(Graphics g) {
  190.         super.paintComponent(g);
  191.         g.setFont(new Font("Arial", Font.BOLD, 18));
  192.         g.setColor(Color.BLACK);
  193.         g.fillRect(0, 0, Game.GAME_WIDTH, Game.GAME_HEIGHT);
  194.         g.setColor(Color.WHITE);
  195.         if (menu) {
  196.             for (int i = 0; i < instructions.length; i++) {
  197.                 drawCenteredString(instructions[i], Game.GAME_WIDTH, Game.GAME_HEIGHT + (i * 45) - 150, g);
  198.             }
  199.         } else if (game) {
  200.            
  201.             g.drawString(scoreOneString, (Game.GAME_WIDTH / 4) - (scoreOneString.length() * 8 / 2),
  202.                     Game.GAME_HEIGHT - 40);
  203.             g.drawString(scoreTwoString, (Game.GAME_WIDTH / 4 * 3) - (scoreTwoString.length() * 8 / 2),
  204.                     Game.GAME_HEIGHT - 40);
  205.             playerOne.render(g);
  206.             playerTwo.render(g);
  207.             ball.render(g);
  208.         }
  209.     }
  210.  
  211.     public void waitForStart() {
  212.         while (!space) {
  213.  
  214.         }
  215.         if (startDirection) {
  216.             ball.xSpeed = 1;
  217.             ball.ySpeed = -1;
  218.         } else {
  219.             ball.xSpeed = -1;
  220.             ball.ySpeed = 1;
  221.         }
  222.     }
  223.  
  224.     // KEYBOARD INPUT EVENTS TRIGGERED BY OS
  225.     @Override
  226.     public void keyPressed(KeyEvent e) {
  227.         int key = e.getKeyCode();
  228.  
  229.         if(key == KeyEvent.VK_ESCAPE){
  230.             System.exit(0);
  231.         }
  232.         if (onePlayer) {
  233.             if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) {
  234.                 right = true;
  235.             }
  236.  
  237.             if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) {
  238.                 left = true;
  239.             }
  240.  
  241.             if (key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) {
  242.                 down = true;
  243.             }
  244.  
  245.             if (key == KeyEvent.VK_UP || key == KeyEvent.VK_W) {
  246.                 up = true;
  247.             }
  248.         } else if (twoPlayer) {
  249.             if (key == KeyEvent.VK_D) {
  250.                 right = true;
  251.             }
  252.  
  253.             if (key == KeyEvent.VK_A) {
  254.                 left = true;
  255.             }
  256.  
  257.             if (key == KeyEvent.VK_S) {
  258.                 down = true;
  259.             }
  260.  
  261.             if (key == KeyEvent.VK_W) {
  262.                 up = true;
  263.             }
  264.  
  265.             if (key == KeyEvent.VK_RIGHT) {
  266.                 rightTwo = true;
  267.             }
  268.  
  269.             if (key == KeyEvent.VK_LEFT) {
  270.                 leftTwo = true;
  271.             }
  272.  
  273.             if (key == KeyEvent.VK_DOWN) {
  274.                 downTwo = true;
  275.             }
  276.  
  277.             if (key == KeyEvent.VK_UP) {
  278.                 upTwo = true;
  279.             }
  280.         }
  281.  
  282.         if (key == KeyEvent.VK_SPACE) {
  283.             space = true;
  284.             ball.reset();
  285.             waitForStart();
  286.         }
  287.     }
  288.  
  289.     @Override
  290.     public void keyReleased(KeyEvent e) {
  291.         int key = e.getKeyCode();
  292.  
  293.         if (onePlayer) {
  294.             if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) {
  295.                 right = false;
  296.             }
  297.  
  298.             if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) {
  299.                 left = false;
  300.             }
  301.  
  302.             if (key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) {
  303.                 down = false;
  304.             }
  305.  
  306.             if (key == KeyEvent.VK_UP || key == KeyEvent.VK_W) {
  307.                 up = false;
  308.             }
  309.         } else if (twoPlayer) {
  310.             if (key == KeyEvent.VK_D) {
  311.                 right = false;
  312.             }
  313.  
  314.             if (key == KeyEvent.VK_A) {
  315.                 left = false;
  316.             }
  317.  
  318.             if (key == KeyEvent.VK_S) {
  319.                 down = false;
  320.             }
  321.  
  322.             if (key == KeyEvent.VK_W) {
  323.                 up = false;
  324.             }
  325.  
  326.             if (key == KeyEvent.VK_RIGHT) {
  327.                 rightTwo = false;
  328.             }
  329.  
  330.             if (key == KeyEvent.VK_LEFT) {
  331.                 leftTwo = false;
  332.             }
  333.  
  334.             if (key == KeyEvent.VK_DOWN) {
  335.                 downTwo = false;
  336.             }
  337.  
  338.             if (key == KeyEvent.VK_UP) {
  339.                 upTwo = false;
  340.             }
  341.         }
  342.  
  343.     }
  344.  
  345.     // UNUSED METHOD REQUIRED BY KEYLISTENER INTERFACE
  346.     @Override
  347.     public void keyTyped(KeyEvent e) {
  348.     }
  349.  
  350.     // STARTING POINT
  351.     public static void main(String[] args) {
  352.         Window window = new Window("Pong", GAME_WIDTH, GAME_HEIGHT);
  353.         Game game = new Game();
  354.  
  355.         window.addGameInstance(game);
  356.         window.addListener(game);
  357.         game.start();
  358.     }
  359. }
  360.  
  361.  
  362. Entity.java:
  363.  
  364. package com.beckwith.main;
  365.  
  366. import java.awt.Graphics;
  367.  
  368. public abstract class Entity {
  369.     protected int x;
  370.     protected int y;
  371.     protected final int WIDTH;
  372.     protected final int HEIGHT;
  373.     protected int xSpeed;
  374.     protected int ySpeed;
  375.     protected ID id;
  376.  
  377.     public Entity(int x, int y, int width, int height, ID id) {
  378.         this.x = x;
  379.         this.y = y;
  380.         this.WIDTH = width;
  381.         this.HEIGHT = height;
  382.         this.id = id;
  383.     }
  384.  
  385.     public String toString() {
  386.         return "\nCoordinates: (" + this.x + ", " + this.y + ") \nxSpeed = " + this.xSpeed + "\nySpeed = " + this.ySpeed
  387.                 + "\nID = " + this.id;
  388.     }
  389.  
  390.     public void update() {
  391.     }
  392.  
  393.     public void render(Graphics g) {
  394.     }
  395. }
  396.  
  397.  
  398. Player.java:
  399.  
  400. package com.beckwith.main;
  401.  
  402. import java.awt.Color;
  403. import java.awt.Graphics;
  404.  
  405. public class Player extends Entity {
  406.  
  407.     public Player(int x, int y, int width, int height, ID id) {
  408.         super(x, y, width, height, id);
  409.     }
  410.  
  411.     @Override
  412.     public void update() {
  413.         if (this.id == ID.PLAYER_ONE) {
  414.             if (this.x < 0) {
  415.                 this.x = 0;
  416.             }
  417.             if (this.x + this.WIDTH > this.WIDTH) {
  418.                 this.x = 0;
  419.             }
  420.             if (this.y < 0) {
  421.                 this.y = 0;
  422.             }
  423.             if (this.y + this.HEIGHT + 30 > Game.GAME_HEIGHT) {
  424.                 this.y = Game.GAME_HEIGHT - this.HEIGHT - 30;
  425.             }
  426.  
  427.             this.x += this.xSpeed;
  428.             this.y += this.ySpeed;
  429.         }
  430.  
  431.         if (this.id == ID.PLAYER_TWO) {
  432.  
  433.             if (this.y < 0) {
  434.                 this.y = 0;
  435.             }
  436.             if (this.y + this.HEIGHT + 30 > Game.GAME_HEIGHT) {
  437.                 this.y = Game.GAME_HEIGHT - this.HEIGHT - 30;
  438.             }
  439.  
  440.             this.x = Game.GAME_WIDTH - 21;
  441.             this.y += this.ySpeed;
  442.         }
  443.     }
  444.  
  445.     @Override
  446.     public void render(Graphics g) {
  447.         g.setColor(Color.RED);
  448.         g.fillRect(this.x, this.y, this.WIDTH, this.HEIGHT);
  449.     }
  450.  
  451. }
  452.  
  453.  
  454. Ball.java:
  455.  
  456. package com.beckwith.main;
  457.  
  458. import java.awt.Color;
  459. import java.awt.Graphics;
  460.  
  461. public class Ball extends Entity {
  462.  
  463.     public Ball(int x, int y, int width, int height, ID id) {
  464.         super(x, y, width, height, id);
  465.         this.x = Game.GAME_WIDTH / 2 - (this.WIDTH / 2);
  466.         this.y = Game.GAME_HEIGHT / 2 - (this.HEIGHT / 2);
  467.     }
  468.  
  469.     public boolean isColliding(Player player) {
  470.         if (player.id == ID.PLAYER_ONE)
  471.             return (this.x < player.WIDTH && ((this.y >= player.y && this.y < player.y + player.HEIGHT)
  472.                     || (this.y + this.HEIGHT >= player.y && this.y + this.HEIGHT < player.y + player.HEIGHT)));
  473.         else
  474.             return (this.x + this.WIDTH > player.x && ((this.y >= player.y && this.y < player.y + player.HEIGHT)
  475.                     || (this.y + this.HEIGHT >= player.y && this.y + this.HEIGHT < player.y + player.HEIGHT)));
  476.     }
  477.  
  478.     public void reset() {
  479.         this.x = Game.GAME_WIDTH / 2 - (this.WIDTH / 2);
  480.         this.y = Game.GAME_HEIGHT / 2 - (this.HEIGHT / 2);
  481.         this.xSpeed = 0;
  482.         this.ySpeed = 0;
  483.     }
  484.  
  485.     @Override
  486.     public void update() {
  487.         if ((this.x + this.WIDTH) > Game.GAME_WIDTH) {
  488.             Game.scoreOne++;
  489.             Game.startDirection = true;
  490.             reset();
  491.         }
  492.         if (this.x < 0) {
  493.             Game.scoreTwo++;
  494.             Game.startDirection = false;
  495.             reset();
  496.         }
  497.         if (this.y + this.HEIGHT + 30 > Game.GAME_HEIGHT) {
  498.             this.ySpeed = -this.ySpeed;
  499.         }
  500.         if (this.y < 0) {
  501.             this.ySpeed = -this.ySpeed;
  502.         }
  503.         this.x += this.xSpeed;
  504.         this.y += this.ySpeed;
  505.  
  506.     }
  507.  
  508.     @Override
  509.     public void render(Graphics g) {
  510.         g.setColor(Color.RED);
  511.         g.fillRect(this.x, this.y, this.WIDTH, this.HEIGHT);
  512.     }
  513. }
  514.  
  515.  
  516. ID.java:
  517.  
  518. package com.beckwith.main;
  519.  
  520. public enum ID {
  521.     BALL, PLAYER_ONE, PLAYER_TWO, COMPUTER
  522. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement