BlueBear icon

pong.java

BlueBear | PRO | 05/27/13 05:17:16 PM UTC | 0 ⭐ | 356 👁️ | Never ⏰ | []
Java |

10.22 KB

|

None

|

0 👍

/

0 👎

package pong;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class pong {
    boolean DEBUG = true;
    // Player 1
    float x = 50;
    float y = 300;
    // Player2
    float x1 = 750;
    float y1 = 300;
    // Ballz
    float ballX = 400;
    float ballY = 300;
    float directionX = -3;
    float directionY = 0;
    float speed = 2.0f;
 
    float rotation = 0;
    long lastFrame;
    int fps;
    long lastFPS;
 
    public void start() {
 
        try {
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
 
        initGL(); // init OpenGL
        getDelta();
        lastFPS = getTime();
 
        // Update code
        while (!Display.isCloseRequested()) {
            int delta = getDelta();
            ballMovement(delta);
            updatePlayer(delta);
 
            renderWalls();
            renderGL();
            renderBall();
            Display.update();
            Display.sync(60); // cap fps to 60fps
        }
 
        Display.destroy();
    }
 
    public void ballMovement(int delta) {
 
        if (DEBUG == true)
            if (ballY >= 580)
                directionY = directionY - 2 * directionY;
 
        if (ballY <= 20)
            directionY = directionY - 2 * directionY;
 
        if (ballX >= 800) {
            ballX = 200;
            ballY = 300;
            speed = 2.0f;
            directionX = 2;
            directionY = 0;
        }
        if (ballX <= 0) {
            ballX = 600;
            ballY = 300;
            speed = 2.0f;
            directionX = -2;
            directionY = 0;
        }
 
        /* ************************************************************ */
        /* ************************Platform 1**************************** */
        /* ************************************************************ */
        if (ballX > 10 && ballX < 65 && ballY < y + 50 && ballY > y - 50
                && directionX < 0) {
            if (directionY > 0) {
                if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
                    ballX = ballX + directionX;
                    directionY = directionY + 1;
                    directionX = directionX - (2 * directionX) - 1;
                    if (directionX == 0)
                        directionX++;
                }
                if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                    ballX = ballX + directionX;
                    directionY = directionY - 1;
                    directionX = directionX - (2 * directionX) + 1;
                    if (directionX == 0)
                        directionX++;
                }
                if (!Keyboard.isKeyDown(Keyboard.KEY_Q)
                        && !Keyboard.isKeyDown(Keyboard.KEY_A)) {
                    ballX = ballX + directionX;
                    directionX = directionX - (2 * directionX);
                }
            }
            if (directionY < 0) {
                if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
                    ballX = ballX + directionX;
                    directionY = directionY + 1;
                    directionX = directionX - (2 * directionX) + 1;
                    if (directionX == 0)
                        directionX++;
                }
                if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                    ballX = ballX + directionX;
                    directionY = directionY - 1;
                    directionX = directionX - (2 * directionX) - 1;
                    if (directionX == 0)
                        directionX++;
                }
                if (!Keyboard.isKeyDown(Keyboard.KEY_Q)
                        && !Keyboard.isKeyDown(Keyboard.KEY_A)) {
                    ballX = ballX + directionX;
                    directionX = directionX - (2 * directionX);
                }
            }
            if (directionY == 0) {
                if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
                    ballX = ballX + directionX;
                    directionY = directionY + 1;
                    directionX = directionX - (2 * directionX) - 1;
                    if (directionX == 0)
                        directionX++;
                }
                if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                    ballX = ballX + directionX;
                    directionY = directionY - 1;
                    directionX = directionX - (2 * directionX) - 1;
                    if (directionX == 0)
                        directionX++;
                }
                if (!Keyboard.isKeyDown(Keyboard.KEY_Q)
                        && !Keyboard.isKeyDown(Keyboard.KEY_A)) {
                    ballX = ballX + directionX;
                    directionX = directionX - (2 * directionX);
                }
            }
        }
 
        /* ************************************************************ */
        /* ************************Platform 2**************************** */
        /* ************************************************************ */
 
        if (ballX > 735 && ballX < 790 && ballY < y1 + 50 && ballY > y1 - 50
                && directionX > 0) {
            if (directionY > 0) {
                if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                    ballX = ballX - 2 * directionX;
                    directionY = directionY + 1;
                    directionX = directionX - 2 * directionX + 1;
                    if (directionX == 0)
                        directionX--;
                }
                if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                    ballX = ballX - 2 * directionX;
                    directionY = directionY - 1;
                    directionX = directionX - 2 * directionX - 1;
                    if (directionX == 0)
                        directionX--;
                }
                if (!Keyboard.isKeyDown(Keyboard.KEY_UP)
                        && !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                    ballX = ballX - 2 * directionX;
                    directionX = directionX - 2 * directionX;
                }
            }
            if (directionY < 0) {
                if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                    ballX = ballX - 2 * directionX;
                    directionY = directionY + 1;
                    directionX = directionX - 2 * directionX - 1;
                    if (directionX == 0)
                        directionX--;
                }
                if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                    ballX = ballX - 2 * directionX;
                    directionY = directionY - 1;
                    directionX = directionX - 2 * directionX + 1;
                    if (directionX == 0)
                        directionX--;
                }
                if (!Keyboard.isKeyDown(Keyboard.KEY_UP)
                        && !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                    ballX = ballX - 2 * directionX;
                    directionX = directionX - 2 * directionX;
                }
            }
            if (directionY == 0) {
                if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                    ballX = ballX - 2 * directionX;
                    directionY = directionY + 1;
                    directionX = directionX - 2 * directionX + 1;
                    if (directionX == 0)
                        directionX--;
                }
                if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                    ballX = ballX - 2 * directionX;
                    directionY = directionY - 1;
                    directionX = directionX - 2 * directionX + 1;
                    if (directionX == 0)
                        directionX--;
                }
                if (!Keyboard.isKeyDown(Keyboard.KEY_UP)
                        && !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                    ballX = ballX - 2 * directionX;
                    directionX = directionX - 2 * directionX;
                }
            }
        }
        if (directionX < -3)
            directionX = -3;
        if (directionX > 3)
            directionX = 3;
 
        if (directionY < -3)
            directionY = -3;
        if (directionY > 3)
            directionY = 3;
 
        speed += 0.001;
 
        ballX = ballX + directionX * speed;
        ballY = ballY + directionY * speed;
 
        if (DEBUG == false) {
 
        }
 
    }
 
    public void renderWalls() {
 
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
 
        GL11.glColor3f(0.0f, 1.0f, 0.0f);
 
        GL11.glPushMatrix();
        /*
         * GL11.glTranslatef(x, y, 0); GL11.glRotatef(rotation, 0f, 0f, 1f);
         * GL11.glTranslatef(-x, -y, 0);
         */
 
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(800, 600);
        GL11.glVertex2f(0, 600);
        GL11.glVertex2f(0, 590);
        GL11.glVertex2f(800, 590);
        GL11.glEnd();
 
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(0, 0);
        GL11.glVertex2f(0, 10);
        GL11.glVertex2f(800, 10);
        GL11.glVertex2f(800, 0);
        GL11.glEnd();
 
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(0, 0);
        GL11.glVertex2f(0, 20);
        GL11.glVertex2f(800, 40);
        GL11.glVertex2f(800, 20);
        GL11.glPopMatrix();
 
    }
 
    public void updatePlayer(int delta) {
        /* ************************************************************ */
        /* ************************Player 1**************************** */
        /* ************************************************************ */
        if (Keyboard.isKeyDown(Keyboard.KEY_Q))
            y += 0.35f * delta;
        if (Keyboard.isKeyDown(Keyboard.KEY_A))
            y -= 0.35f * delta;
 
        /* ************************************************************ */
        /* ************************Player 2**************************** */
        /* ************************************************************ */
 
        if (Keyboard.isKeyDown(Keyboard.KEY_UP))
            y1 += 0.35f * delta;
        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
            y1 -= 0.35f * delta;
 
        // keep paddle on the screen
        if (y < 60)
            y = 60;
        if (y > 540)
            y = 540;
 
        if (y1 < 60)
            y1 = 60;
        if (y1 > 540)
            y1 = 540;
 
        updateFPS();
    }
 
    public int getDelta() {
        long time = getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;
 
        return delta;
    }
 
    public long getTime() {
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }
 
    public void updateFPS() {
        if (getTime() - lastFPS > 1000) {
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFPS += 1000;
        }
        fps++;
    }
 
    public void initGL() {
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 0, 600, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }
 
    public void renderBall() {
        GL11.glEnd();
        GL11.glBegin(GL11.GL_POLYGON);
        GL11.glColor3f(1f, 1.5f, 1f);
        for (double angle = 0; angle <= Math.PI * 2; angle = angle + 0.01) { // Circumference
                                             // of
                                             // circle
                                             // =
                                             // 2PI
                                             // *
                                             // r
            GL11.glVertex2d(ballX + Math.sin(angle) * 5,
                    ballY + Math.cos(angle) * 5);
        }
        GL11.glEnd();
 
    }
 
    public void renderGL() {
 
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
 
        GL11.glPushMatrix();
        GL11.glTranslatef(x, y, 0);
        GL11.glRotatef(rotation, 0f, 0f, 1f);
        GL11.glTranslatef(-x, -y, 0);
 
        GL11.glBegin(GL11.GL_QUADS);
 
        /* ************************************************************ */
        /* ************************Player 1**************************** */
        /* ************************************************************ */
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex2f(x - 10, y - 50);
        GL11.glVertex2f(x + 10, y - 50);
        GL11.glVertex2f(x + 10, y + 50);
        GL11.glVertex2f(x - 10, y + 50);
 
        /* ************************************************************ */
        /* ************************Player 2**************************** */
        /* ************************************************************ */
        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glVertex2f(x1 - 10, y1 - 50);
        GL11.glVertex2f(x1 + 10, y1 - 50);
        GL11.glVertex2f(x1 + 10, y1 + 50);
        GL11.glVertex2f(x1 - 10, y1 + 50);
 
        GL11.glEnd();
        GL11.glPopMatrix();
    }
 
    public static void main(String[] argv) {
        pong app = new pong();
        app.start();
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  • Syutrsz icon
    03/31/26 10:27:06 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.@xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400
     Remove the @ 
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎