Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.jnk.pongTEST;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.util.Random;
- import javax.swing.JPanel;
- public class Elements extends JPanel {
- private static final long serialVersionUID = 1L;
- //DO NOT MODIFY. (Higher Number creates slower movement)
- private static final int THROTTLE = 2;
- private static int mousePos = 100;
- private static int computerPos = 100;
- private static int ballPosX = 380;
- private static int ballPosY = 270;
- //0 = Left; 1 = Right
- private static int ballXDir = 0;
- //-1 = Up; 0 = None; 1 = Down
- private static int ballYDir = 0;
- private static int ballSpeed = 1;
- private static int bounceCount = 0;
- private static int paddleSpeed = 1;
- private static int posToSet = 0;
- private static int scorePlyr = 0;
- private static int scoreComp = 0;
- @Override
- public void paint(Graphics g) {
- g.setColor(Color.DARK_GRAY);
- g.fillRect(0, 0, Pong.WIN_DIM_X, Pong.WIN_DIM_Y);
- //Drawing Border
- g.setColor(Color.GREEN);
- g.fillRect((Pong.WIN_DIM_X / 2) - 15, 50, 5, Pong.WIN_DIM_Y - 100);
- //Drawing Paddles
- //Player Paddle
- g.fillRect(10, 300 - mousePos, 20, 150);
- //Computer Paddle
- g.fillRect(770, 300 - computerPos, 20, 150);
- //Ball
- g.fillRect(ballPosX, ballPosY, 20, 20);
- g.setColor(Color.WHITE);
- g.setFont(new Font("Arial", Font.PLAIN, 40));
- g.drawString("Player: " + scorePlyr, 5, 37);
- g.drawString("Computer: " + scoreComp, Pong.WIN_DIM_X - 270, 37);
- g.setFont(new Font("Arial", Font.BOLD, 16));
- g.drawString("Pong: Created by Jnk1296. This software is free and open-source.", 3, Pong.WIN_DIM_Y - 5);
- //AI Call
- update();
- try {
- Thread.sleep(THROTTLE);
- } catch (InterruptedException e) { }
- repaint();
- }
- public static void update() {
- //Ball Logic
- ballMove();
- //Computer Paddle Logic
- computerMove();
- }
- //Logic for ball movement
- public static void ballMove() {
- //Collision Detection
- if (ballPosX > 750) {
- if (ballPosY > 300 - computerPos - 20 && ballPosY < (300 - computerPos) + 150) {
- ballXDir = 0;
- ballYDir = getYDirComp();
- bounceCount += 1;
- } else {
- reset();
- }
- } else if (ballPosX < 30) {
- if (ballPosY > 300 - mousePos - 20 && ballPosY < (300 - mousePos) + 150) {
- ballXDir = 1;
- ballYDir = getYDirMouse();
- bounceCount += 1;
- } else {
- reset();
- }
- }
- if (ballPosY < 50) {
- ballYDir = 1;
- } else if (ballPosY > Pong.WIN_DIM_Y - 70) {
- ballYDir = -1;
- }
- //Setting X and Y Directions for ball
- if (ballXDir == 1) ballPosX += ballSpeed;
- if (ballXDir == 0) ballPosX -= ballSpeed;
- if (ballYDir == 1) ballPosY += ballSpeed;
- if (ballYDir == -1) ballPosY -= ballSpeed;
- //Incrementing speed of movement for ball by one every five bounces of the ball
- if (bounceCount == 5) {
- ballSpeed += 1;
- bounceCount = 0;
- //Computer Paddle Speed Update
- if (new Random().nextInt(2) == 1) {
- paddleSpeed = ballSpeed;
- }
- }
- }
- //Computer Paddle AI
- public static void computerMove() {
- if (ballYDir == -1) {
- if (computerPos >= -100 && computerPos <= 255) {
- computerPos += paddleSpeed;
- } else if (computerPos < -100) {
- computerPos = -100;
- } else if (computerPos > 255) {
- computerPos = 255;
- }
- } else if (ballYDir == 1) {
- if (computerPos >= -100 && computerPos <= 255) {
- computerPos -= paddleSpeed;
- } else if (computerPos < -100) {
- computerPos = -100;
- } else if (computerPos > 255) {
- computerPos = 255;
- }
- }
- }
- //Ball Angle of Return for Player Paddle
- public static int getYDirMouse() {
- int YDir = 0;
- if (ballPosY > (300 - mousePos) - 20 && ballPosY < (300 - mousePos) + 50) {
- YDir = -1;
- } else if (ballPosY > (400 - mousePos) && ballPosY < (300 - mousePos) + 150) {
- YDir = 1;
- }
- return YDir;
- }
- //Ball Angle of Return for Computer Paddle
- public static int getYDirComp() {
- int YDir = 0;
- if (ballPosY > (300 - computerPos) - 20 && ballPosY < (300 - computerPos) + 50) {
- YDir = -1;
- } else if (ballPosY > (400 - computerPos) && ballPosY < (300 - computerPos) + 150) {
- YDir = 1;
- }
- return YDir;
- }
- //Returns a position value for the player paddle based on the location of the cursor
- public static void getMousePos(int pos) {
- posToSet = (pos * -1) + 380;
- if (posToSet >= -100 && posToSet <= 255) mousePos = posToSet;
- }
- //When called, resets all variables to their default values (aka reset the field)
- public static void reset() {
- if (ballXDir == 1) scorePlyr++;
- if (ballXDir == 0) scoreComp++;
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) { }
- mousePos = 100;
- computerPos = 100;
- ballPosX = 380;
- ballPosY = 270;
- ballXDir = 0;
- ballYDir = 0;
- ballSpeed = 1;
- paddleSpeed = 1;
- bounceCount = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement