Advertisement
Jnk1296

Pong Recreation - Class Elements

Feb 28th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. package net.jnk.pongTEST;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.util.Random;
  7.  
  8. import javax.swing.JPanel;
  9.  
  10. public class Elements extends JPanel {
  11.    
  12.     private static final long serialVersionUID = 1L;
  13.    
  14.     //DO NOT MODIFY. (Higher Number creates slower movement)
  15.     private static final int THROTTLE = 2;
  16.    
  17.     private static int mousePos = 100;
  18.     private static int computerPos = 100;
  19.    
  20.     private static int ballPosX = 380;
  21.     private static int ballPosY = 270;
  22.     //0 = Left; 1 = Right
  23.     private static int ballXDir = 0;
  24.     //-1 = Up; 0 = None; 1 = Down
  25.     private static int ballYDir = 0;
  26.     private static int ballSpeed = 1;
  27.     private static int bounceCount = 0;
  28.    
  29.     private static int paddleSpeed = 1;
  30.    
  31.     private static int posToSet = 0;
  32.    
  33.     private static int scorePlyr = 0;
  34.     private static int scoreComp = 0;
  35.    
  36.     @Override
  37.     public void paint(Graphics g) {
  38.         g.setColor(Color.DARK_GRAY);
  39.         g.fillRect(0, 0, Pong.WIN_DIM_X, Pong.WIN_DIM_Y);
  40.        
  41.         //Drawing Border
  42.         g.setColor(Color.GREEN);
  43.         g.fillRect((Pong.WIN_DIM_X / 2) - 15, 50, 5, Pong.WIN_DIM_Y - 100);
  44.        
  45.         //Drawing Paddles
  46.         //Player Paddle
  47.         g.fillRect(10, 300 - mousePos, 20, 150);
  48.         //Computer Paddle
  49.         g.fillRect(770, 300 - computerPos, 20, 150);
  50.         //Ball
  51.         g.fillRect(ballPosX, ballPosY, 20, 20);
  52.        
  53.         g.setColor(Color.WHITE);
  54.        
  55.         g.setFont(new Font("Arial", Font.PLAIN, 40));
  56.         g.drawString("Player: " + scorePlyr, 5, 37);
  57.         g.drawString("Computer: " + scoreComp, Pong.WIN_DIM_X - 270, 37);
  58.        
  59.         g.setFont(new Font("Arial", Font.BOLD, 16));
  60.         g.drawString("Pong: Created by Jnk1296. This software is free and open-source.", 3, Pong.WIN_DIM_Y - 5);
  61.        
  62.         //AI Call
  63.         update();
  64.        
  65.         try {
  66.             Thread.sleep(THROTTLE);
  67.         } catch (InterruptedException e) { }
  68.         repaint();
  69.     }
  70.    
  71.     public static void update() {
  72.         //Ball Logic
  73.         ballMove();
  74.        
  75.         //Computer Paddle Logic
  76.         computerMove();
  77.     }
  78.    
  79.     //Logic for ball movement
  80.     public static void ballMove() {
  81.        
  82.         //Collision Detection
  83.         if (ballPosX > 750) {
  84.             if (ballPosY > 300 - computerPos - 20 && ballPosY < (300 - computerPos) + 150) {
  85.                 ballXDir = 0;
  86.                 ballYDir = getYDirComp();
  87.                 bounceCount += 1;
  88.             } else {
  89.                 reset();
  90.             }
  91.         } else if (ballPosX < 30) {
  92.             if (ballPosY > 300 - mousePos - 20 && ballPosY < (300 - mousePos) + 150) {
  93.                 ballXDir = 1;
  94.                 ballYDir = getYDirMouse();
  95.                 bounceCount += 1;
  96.             } else {
  97.                 reset();
  98.             }
  99.         }
  100.        
  101.         if (ballPosY < 50) {
  102.             ballYDir = 1;
  103.         } else if (ballPosY > Pong.WIN_DIM_Y - 70) {
  104.             ballYDir = -1;
  105.         }
  106.        
  107.         //Setting X and Y Directions for ball
  108.         if (ballXDir == 1) ballPosX += ballSpeed;
  109.         if (ballXDir == 0) ballPosX -= ballSpeed;
  110.        
  111.         if (ballYDir == 1) ballPosY += ballSpeed;
  112.         if (ballYDir == -1) ballPosY -= ballSpeed;
  113.        
  114.         //Incrementing speed of movement for ball by one every five bounces of the ball
  115.         if (bounceCount == 5) {
  116.             ballSpeed += 1;
  117.             bounceCount = 0;
  118.            
  119.             //Computer Paddle Speed Update
  120.             if (new Random().nextInt(2) == 1) {
  121.                 paddleSpeed = ballSpeed;
  122.             }
  123.         }
  124.     }
  125.    
  126.     //Computer Paddle AI
  127.     public static void computerMove() {
  128.         if (ballYDir == -1) {
  129.             if (computerPos >= -100 && computerPos <= 255) {
  130.                 computerPos += paddleSpeed;
  131.             } else if (computerPos < -100) {
  132.                 computerPos = -100;
  133.             } else if (computerPos > 255) {
  134.                 computerPos = 255;
  135.             }
  136.         } else if (ballYDir == 1) {
  137.             if (computerPos >= -100 && computerPos <= 255) {
  138.                 computerPos -= paddleSpeed;
  139.             } else if (computerPos < -100) {
  140.                 computerPos = -100;
  141.             } else if (computerPos > 255) {
  142.                 computerPos = 255;
  143.             }
  144.         }
  145.     }
  146.    
  147.     //Ball Angle of Return for Player Paddle
  148.     public static int getYDirMouse() {
  149.         int YDir = 0;
  150.        
  151.         if (ballPosY > (300 - mousePos) - 20 && ballPosY < (300 - mousePos) + 50) {
  152.             YDir = -1;
  153.         } else if (ballPosY > (400 - mousePos) && ballPosY < (300 - mousePos) + 150) {
  154.             YDir = 1;
  155.         }
  156.         return YDir;
  157.     }
  158.    
  159.     //Ball Angle of Return for Computer Paddle
  160.     public static int getYDirComp() {
  161.         int YDir = 0;
  162.        
  163.         if (ballPosY > (300 - computerPos) - 20 && ballPosY < (300 - computerPos) + 50) {
  164.             YDir = -1;
  165.         } else if (ballPosY > (400 - computerPos) && ballPosY < (300 - computerPos) + 150) {
  166.             YDir = 1;
  167.         }
  168.         return YDir;
  169.     }
  170.    
  171.     //Returns a position value for the player paddle based on the location of the cursor
  172.     public static void getMousePos(int pos) {
  173.         posToSet = (pos * -1) + 380;
  174.         if (posToSet >= -100 && posToSet <= 255) mousePos = posToSet;
  175.     }
  176.    
  177.     //When called, resets all variables to their default values (aka reset the field)
  178.     public static void reset() {
  179.         if (ballXDir == 1) scorePlyr++;
  180.         if (ballXDir == 0) scoreComp++;
  181.        
  182.         try {
  183.             Thread.sleep(2000);
  184.         } catch (InterruptedException e) { }
  185.        
  186.         mousePos = 100;
  187.         computerPos = 100;
  188.        
  189.         ballPosX = 380;
  190.         ballPosY = 270;
  191.         ballXDir = 0;
  192.         ballYDir = 0;
  193.         ballSpeed = 1;
  194.         paddleSpeed = 1;
  195.         bounceCount = 0;
  196.     }
  197.    
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement