SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Part of Eclipse
  2. //Package name
  3. package pong;
  4. //Import required classes
  5.  
  6. import java.awt.BasicStroke;
  7. import java.awt.Color;
  8. import java.awt.Font;
  9. import java.awt.Graphics2D;
  10. import java.awt.RenderingHints;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.KeyEvent;
  14. import java.awt.event.KeyListener;
  15. import java.util.Random;
  16.  
  17. import javax.swing.JFrame;
  18. import javax.swing.Timer;
  19.  
  20. public class Pong implements ActionListener, KeyListener
  21. {
  22.  
  23.  
  24.     public static Pong pong;
  25.  
  26.  
  27.     Paddle player1;
  28.     Paddle player2;
  29.     Ball ball;
  30.  
  31.  
  32.     //Movement controls (if key is pressed, var = true_
  33.     boolean w, s, up, down;
  34.     /*
  35.      * Game State of Program
  36.      * Integer to tell the program which screen the program should be showing
  37.      * 0 = Menu
  38.      * 1 = Pause Screen
  39.      * 2 = PLaying
  40.      * 3 = End Screen
  41.      * 4 = How To Play
  42.      */
  43.     int gameStatus = 0;
  44.  
  45.     //Score Limit for game (possible to change lateR)
  46.     int scoreLimit =  1;
  47.  
  48.     //Which player wins
  49.     int playerWon;
  50.  
  51.     //Window Parameters
  52.     int  width =  1280;
  53.     int height = 720;
  54.  
  55.     public Random random;
  56.  
  57.     public JFrame jframe;
  58.     //GUI Method
  59.     public GUI GUI;
  60.  
  61.     public Pong()
  62.     {
  63.         //Timer so that the window refreshes
  64.         Timer timer = new Timer(20, this);
  65.         random = new Random();
  66.  
  67.         //Name of window
  68.         jframe = new JFrame("Pong");
  69.  
  70.         GUI = new GUI();
  71.         //jFrame setsize command to set window  size
  72.         jframe.setSize(width, height);
  73.         //User must resize window for some reason
  74.         jframe.setResizable(true);
  75.         //Set the frame to visible
  76.         jframe.setVisible(true);
  77.         //Terminite program when the user clicks close
  78.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79.  
  80.         jframe.add(GUI);
  81.         //Key listener
  82.         jframe.addKeyListener(this);
  83.  
  84.         timer.start();
  85.     }
  86.     //Start Method (Things that happen when the game starts)
  87.  
  88.     public void start()
  89.     {
  90.         gameStatus = 2;
  91.         //Inheritance
  92.         //Takes info from Paddle and makes users
  93.         player1 = new Paddle(this, 1);
  94.         player2 = new Paddle(this, 2);
  95.         ball = new Ball(this);
  96.     }
  97.  
  98.     //Method for both player movement info and player score check
  99.     public void update()
  100.     {
  101.         //If player 1 reaches score limit, player 1 wins, game status changes to 3 **IT IS GREATER THAN OR EQUAL IN CASE THE USER CHANGES THE SCORE LIMIT MID GAME
  102.         if (player1.score >= scoreLimit)
  103.         {
  104.             playerWon = 1;
  105.             gameStatus = 3;
  106.         }
  107.         //If player 2 reaches score limit, player 2 wins, game status changes to 3 **IT IS GREATER THAN OR EQUAL IN CASE THE USER CHANGES THE SCORE LIMIT MID GAME
  108.  
  109.         if (player2.score >= scoreLimit)
  110.         {
  111.             gameStatus = 3;
  112.             playerWon = 2;
  113.         }
  114.         //If player 1 presses w, it outputs true so player1.move making the player move up
  115.         if (w)
  116.         {
  117.             player1.move(true);
  118.         }
  119.  
  120.         //If player 1 presses s, it outputs false so player1.move making the player move down
  121.         if (s)
  122.         {
  123.             player1.move(false);
  124.         }
  125.  
  126.  
  127.  
  128.         //If player 2 presses the up  arrow, it outputs true so player2.move making the player move up
  129.         if (up)
  130.         {
  131.             player2.move(true);
  132.         }
  133.         //If player 2 presses the down arrow, it outputs false so player2.move making the player move down
  134.  
  135.         if (down)
  136.         {
  137.             player2.move(false);
  138.         }
  139.  
  140.  
  141.         //This tells the ball to move
  142.         //Player 1 and Player 2 are parameters for collision
  143.         ball.update(player1, player2);
  144.     }
  145.  
  146.     //Method for GUI
  147.     //This is the rendering method
  148.     public void render(Graphics2D g)
  149.     {
  150.  
  151.  
  152.         //Set the colour to Black
  153.         g.setColor(Color.BLACK);
  154.         //Fill the background black
  155.         g.fillRect(0, 0, width, height);
  156.  
  157.         /*
  158.          * Different if else statements
  159.          * Each statement is a different game mode...
  160.          * Game modes are different  "Screens" that the program can display
  161.          * The the user presses a key or if the game ends etc...
  162.          * The game mode changes which shows a new screen
  163.          */
  164.  
  165.         //Menu Game Screen
  166.         if (gameStatus == 0)
  167.         {
  168.             //Set the colour to White
  169.             g.setColor(Color.WHITE);
  170.             //Change the font to Arial
  171.             g.setFont(new Font("Arial", 1, 50));
  172.             //Draw string (Text)
  173.             //Parameters include the location, width and height are used for organization
  174.             g.drawString("PONG!", width / 2 - 75, 50);
  175.             g.drawString("Press Space to Play", width / 2 - 150, height / 2 + -75);
  176.             g.drawString("Press C for Controls", width / 2 - 150, height / 2 -25);
  177.             g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  178.  
  179.         }
  180.  
  181.  
  182.         //Menu Game Screen
  183.         if (gameStatus == 1)
  184.         {
  185.             g.setColor(Color.WHITE);
  186.             g.setFont(new Font("Arial", 1, 50));
  187.             g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  188.         }
  189.  
  190.  
  191.         //InGame Screen
  192.         //If statement.. Score is only shown if user is playing
  193.         if (gameStatus == 1 || gameStatus == 2)
  194.         {
  195.             g.setColor(Color.WHITE);
  196.  
  197.             g.setStroke(new BasicStroke(5f));
  198.  
  199.             g.drawLine(width / 2, 0, width / 2, height);
  200.  
  201.             g.setStroke(new BasicStroke(2f));
  202.  
  203.             g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  204.  
  205.             g.setFont(new Font("Arial", 1, 50));
  206.  
  207.             //Display string of player score
  208.             g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  209.             g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  210.  
  211.             //Continue to render player 1, player 2, and the ball (but not update)
  212.             player1.render(g);
  213.             player2.render(g);
  214.             ball.render(g);
  215.         }
  216.  
  217.         //End Screen
  218.         if (gameStatus == 3)
  219.         {
  220.             g.setColor(Color.WHITE);
  221.             g.setFont(new Font("Arial", 1, 50));
  222.  
  223.             g.drawString("PONG", width / 2 - 75, 50);
  224.  
  225.             //When the game ends, a variable of the winning player is set, this string displays that variable
  226.             g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  227.  
  228.  
  229.             g.drawString("Press Space to Play Again", width / 2 - 165, height / 2 - 25);
  230.             g.drawString("Press ESC for Menu", width / 2 - 165, height / 2 + 25);
  231.         }
  232.  
  233.         //Controls screen
  234.         if (gameStatus == 4)
  235.         {
  236.             g.setColor(Color.WHITE);
  237.             g.setFont(new Font("Arial", 1, 50));
  238.             g.drawString("How to Play", width / 2 - 75, 50);
  239.  
  240.  
  241.             g.setFont(new Font("Arial", 1, 30));
  242.  
  243.             g.drawString("ESC = Pause", width / 2 - 150, height / 2 - 25);
  244.             g.drawString("W/S = Player 1 Controls", width / 2 - 150, height / 2 - 50);
  245.             g.drawString("Up/Down = Player 2 Controls", width / 2 - 150, height / 2 - -75);
  246.             g.drawString("Press ESC for Menu", width / 2 - 165, height / 2 +100);
  247.  
  248.  
  249.  
  250.         }
  251.     }
  252.  
  253.     //@Overide was automatically generated
  254.     @Override
  255.     //Methods
  256.  
  257.  
  258.     //Update while the game status is 2 (Playing)
  259.     public void actionPerformed(ActionEvent e)
  260.     {
  261.         if (gameStatus == 2)
  262.         {
  263.             update();
  264.         }
  265.  
  266.         //Required to paint frames
  267.         GUI.repaint();
  268.     }
  269.  
  270.     //var pong = pong (Taken from this class)
  271.     public static void main(String[] args)
  272.     {
  273.         pong = new Pong();
  274.     }
  275.  
  276.     //Automatically generated
  277.     //Method for when key is pressed
  278.     @Override
  279.     public void keyPressed(KeyEvent e)
  280.     {
  281.         // TODO Auto-generated method stub
  282.  
  283.         //KEYCODES
  284.         /*
  285.          * If a player presses a key, something happens
  286.          * VK_KEY**
  287.          *
  288.          */
  289.         //int id = the keycode that the user presses
  290.         int id = e.getKeyCode();
  291.  
  292.         //If player presses W
  293.         if (id == KeyEvent.VK_W)
  294.         {
  295.             w = true;
  296.         }
  297.         //If player presses S
  298.         else if (id == KeyEvent.VK_S)
  299.         {
  300.             s = true;
  301.         }
  302.         //If player presses Up arrow
  303.         else if (id == KeyEvent.VK_UP)
  304.         {
  305.             up = true;
  306.         }
  307.         //If player presses down arrow
  308.         else if (id == KeyEvent.VK_DOWN)
  309.         {
  310.             down = true;
  311.         }
  312.         //If player presses right arrow
  313.         else if (id == KeyEvent.VK_RIGHT)
  314.         {
  315.  
  316.             //Actions are only triggered in certain game modes
  317.             if (gameStatus == 0)
  318.             {
  319.                 scoreLimit++;
  320.             }
  321.         }
  322.         //Action is only allowed if the scorelimit is above 1 as a 0 score limit is not possible
  323.         else if (id == KeyEvent.VK_LEFT)
  324.         {
  325.  
  326.             if (gameStatus == 0 && scoreLimit > 1)
  327.             {
  328.                 scoreLimit--;
  329.             }
  330.         }
  331.  
  332.         //If player presses ESC //Only triggerable in certain game modes
  333.         else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3 || gameStatus == 4))
  334.         {
  335.             gameStatus = 0;
  336.         }
  337.  
  338.         //If player presses C
  339.         else if (id == KeyEvent.VK_C)
  340.         {
  341.  
  342.             gameStatus = 4;
  343.  
  344.         }
  345.  
  346.  
  347.         //If player presses Space bar
  348.         else if (id == KeyEvent.VK_SPACE)
  349.         {
  350.             //If in the menu or end screen
  351.             //Game start
  352.             if (gameStatus == 0 || gameStatus == 3)
  353.             {
  354.                 start();
  355.             }
  356.             //If playing, game pause
  357.             else if (gameStatus == 1)
  358.             {
  359.                 gameStatus = 2;
  360.             }
  361.             //If game paused, game play
  362.             else if (gameStatus == 2)
  363.             {
  364.                 gameStatus = 1;
  365.             }
  366.         }
  367.     }
  368.  
  369.     //Generated Automatically
  370.     //Method for when key is released
  371.     @Override
  372.     public void keyReleased(KeyEvent e)
  373.     {
  374.         // TODO Auto-generated method stub
  375.  
  376.         int id = e.getKeyCode();
  377.  
  378.         if (id == KeyEvent.VK_W)
  379.         {
  380.             w = false;
  381.         }
  382.         else if (id == KeyEvent.VK_S)
  383.         {
  384.             s = false;
  385.         }
  386.         else if (id == KeyEvent.VK_UP)
  387.         {
  388.             up = false;
  389.         }
  390.         else if (id == KeyEvent.VK_DOWN)
  391.         {
  392.             down = false;
  393.         }
  394.     }
  395.  
  396.     //Generated Automatically
  397.     //Method is not needed but is required for app to run
  398.     @Override
  399.     public void keyTyped(KeyEvent e) {
  400.         // TODO Auto-generated method stub
  401.  
  402.     }
  403.  
  404.  
  405. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top