Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Part of Eclipse
- //Package name
- package pong;
- //Import required classes
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.Timer;
- public class Pong implements ActionListener, KeyListener
- {
- public static Pong pong;
- Paddle player1;
- Paddle player2;
- Ball ball;
- //Movement controls (if key is pressed, var = true_
- boolean w, s, up, down;
- /*
- * Game State of Program
- * Integer to tell the program which screen the program should be showing
- * 0 = Menu
- * 1 = Pause Screen
- * 2 = PLaying
- * 3 = End Screen
- * 4 = How To Play
- */
- int gameStatus = 0;
- //Score Limit for game (possible to change lateR)
- int scoreLimit = 1;
- //Which player wins
- int playerWon;
- //Window Parameters
- int width = 1280;
- int height = 720;
- public Random random;
- public JFrame jframe;
- //GUI Method
- public GUI GUI;
- public Pong()
- {
- //Timer so that the window refreshes
- Timer timer = new Timer(20, this);
- random = new Random();
- //Name of window
- jframe = new JFrame("Pong");
- GUI = new GUI();
- //jFrame setsize command to set window size
- jframe.setSize(width, height);
- //User must resize window for some reason
- jframe.setResizable(true);
- //Set the frame to visible
- jframe.setVisible(true);
- //Terminite program when the user clicks close
- jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jframe.add(GUI);
- //Key listener
- jframe.addKeyListener(this);
- timer.start();
- }
- //Start Method (Things that happen when the game starts)
- public void start()
- {
- gameStatus = 2;
- //Inheritance
- //Takes info from Paddle and makes users
- player1 = new Paddle(this, 1);
- player2 = new Paddle(this, 2);
- ball = new Ball(this);
- }
- //Method for both player movement info and player score check
- public void update()
- {
- //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
- if (player1.score >= scoreLimit)
- {
- playerWon = 1;
- gameStatus = 3;
- }
- //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
- if (player2.score >= scoreLimit)
- {
- gameStatus = 3;
- playerWon = 2;
- }
- //If player 1 presses w, it outputs true so player1.move making the player move up
- if (w)
- {
- player1.move(true);
- }
- //If player 1 presses s, it outputs false so player1.move making the player move down
- if (s)
- {
- player1.move(false);
- }
- //If player 2 presses the up arrow, it outputs true so player2.move making the player move up
- if (up)
- {
- player2.move(true);
- }
- //If player 2 presses the down arrow, it outputs false so player2.move making the player move down
- if (down)
- {
- player2.move(false);
- }
- //This tells the ball to move
- //Player 1 and Player 2 are parameters for collision
- ball.update(player1, player2);
- }
- //Method for GUI
- //This is the rendering method
- public void render(Graphics2D g)
- {
- //Set the colour to Black
- g.setColor(Color.BLACK);
- //Fill the background black
- g.fillRect(0, 0, width, height);
- /*
- * Different if else statements
- * Each statement is a different game mode...
- * Game modes are different "Screens" that the program can display
- * The the user presses a key or if the game ends etc...
- * The game mode changes which shows a new screen
- */
- //Menu Game Screen
- if (gameStatus == 0)
- {
- //Set the colour to White
- g.setColor(Color.WHITE);
- //Change the font to Arial
- g.setFont(new Font("Arial", 1, 50));
- //Draw string (Text)
- //Parameters include the location, width and height are used for organization
- g.drawString("PONG!", width / 2 - 75, 50);
- g.drawString("Press Space to Play", width / 2 - 150, height / 2 + -75);
- g.drawString("Press C for Controls", width / 2 - 150, height / 2 -25);
- g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
- }
- //Menu Game Screen
- if (gameStatus == 1)
- {
- g.setColor(Color.WHITE);
- g.setFont(new Font("Arial", 1, 50));
- g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
- }
- //InGame Screen
- //If statement.. Score is only shown if user is playing
- if (gameStatus == 1 || gameStatus == 2)
- {
- g.setColor(Color.WHITE);
- g.setStroke(new BasicStroke(5f));
- g.drawLine(width / 2, 0, width / 2, height);
- g.setStroke(new BasicStroke(2f));
- g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
- g.setFont(new Font("Arial", 1, 50));
- //Display string of player score
- g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
- g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
- //Continue to render player 1, player 2, and the ball (but not update)
- player1.render(g);
- player2.render(g);
- ball.render(g);
- }
- //End Screen
- if (gameStatus == 3)
- {
- g.setColor(Color.WHITE);
- g.setFont(new Font("Arial", 1, 50));
- g.drawString("PONG", width / 2 - 75, 50);
- //When the game ends, a variable of the winning player is set, this string displays that variable
- g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
- g.drawString("Press Space to Play Again", width / 2 - 165, height / 2 - 25);
- g.drawString("Press ESC for Menu", width / 2 - 165, height / 2 + 25);
- }
- //Controls screen
- if (gameStatus == 4)
- {
- g.setColor(Color.WHITE);
- g.setFont(new Font("Arial", 1, 50));
- g.drawString("How to Play", width / 2 - 75, 50);
- g.setFont(new Font("Arial", 1, 30));
- g.drawString("ESC = Pause", width / 2 - 150, height / 2 - 25);
- g.drawString("W/S = Player 1 Controls", width / 2 - 150, height / 2 - 50);
- g.drawString("Up/Down = Player 2 Controls", width / 2 - 150, height / 2 - -75);
- g.drawString("Press ESC for Menu", width / 2 - 165, height / 2 +100);
- }
- }
- //@Overide was automatically generated
- @Override
- //Methods
- //Update while the game status is 2 (Playing)
- public void actionPerformed(ActionEvent e)
- {
- if (gameStatus == 2)
- {
- update();
- }
- //Required to paint frames
- GUI.repaint();
- }
- //var pong = pong (Taken from this class)
- public static void main(String[] args)
- {
- pong = new Pong();
- }
- //Automatically generated
- //Method for when key is pressed
- @Override
- public void keyPressed(KeyEvent e)
- {
- // TODO Auto-generated method stub
- //KEYCODES
- /*
- * If a player presses a key, something happens
- * VK_KEY**
- *
- */
- //int id = the keycode that the user presses
- int id = e.getKeyCode();
- //If player presses W
- if (id == KeyEvent.VK_W)
- {
- w = true;
- }
- //If player presses S
- else if (id == KeyEvent.VK_S)
- {
- s = true;
- }
- //If player presses Up arrow
- else if (id == KeyEvent.VK_UP)
- {
- up = true;
- }
- //If player presses down arrow
- else if (id == KeyEvent.VK_DOWN)
- {
- down = true;
- }
- //If player presses right arrow
- else if (id == KeyEvent.VK_RIGHT)
- {
- //Actions are only triggered in certain game modes
- if (gameStatus == 0)
- {
- scoreLimit++;
- }
- }
- //Action is only allowed if the scorelimit is above 1 as a 0 score limit is not possible
- else if (id == KeyEvent.VK_LEFT)
- {
- if (gameStatus == 0 && scoreLimit > 1)
- {
- scoreLimit--;
- }
- }
- //If player presses ESC //Only triggerable in certain game modes
- else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3 || gameStatus == 4))
- {
- gameStatus = 0;
- }
- //If player presses C
- else if (id == KeyEvent.VK_C)
- {
- gameStatus = 4;
- }
- //If player presses Space bar
- else if (id == KeyEvent.VK_SPACE)
- {
- //If in the menu or end screen
- //Game start
- if (gameStatus == 0 || gameStatus == 3)
- {
- start();
- }
- //If playing, game pause
- else if (gameStatus == 1)
- {
- gameStatus = 2;
- }
- //If game paused, game play
- else if (gameStatus == 2)
- {
- gameStatus = 1;
- }
- }
- }
- //Generated Automatically
- //Method for when key is released
- @Override
- public void keyReleased(KeyEvent e)
- {
- // TODO Auto-generated method stub
- int id = e.getKeyCode();
- if (id == KeyEvent.VK_W)
- {
- w = false;
- }
- else if (id == KeyEvent.VK_S)
- {
- s = false;
- }
- else if (id == KeyEvent.VK_UP)
- {
- up = false;
- }
- else if (id == KeyEvent.VK_DOWN)
- {
- down = false;
- }
- }
- //Generated Automatically
- //Method is not needed but is required for app to run
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement