I'm a beginner at java and am trying to code a basic pong game java applet.
The code has no errors in it but when i run it the .png's do not come up in the window until its moved off screen.
when the .png's are shown they do not update whatsoever.
i thought the problem could be with the key-listener but even the ball (which moves independently of user input) does not move.
I've placed all the code from the 5 classes below.
there's some unnecessary code in the background class but that's because I'm thinking of making the background scroll later on.
package main; import java.applet.Applet; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.net.URL; public class MainClass extends Applet implements Runnable, KeyListener { private Player1 p1; private Player2 p2; private Ball ball; private Image image, paddle1, paddle2, Ball, backTile; private Graphics second; private URL base; private static Background bg; @Override public void init() { setSize(960, 720); setBackground(Color.DARK_GRAY); setVisible(true); setFocusable(true); addKeyListener(this); Frame frame = (Frame) this.getParent().getParent(); frame.setTitle("Pong"); try { base = getDocumentBase(); } catch (Exception e) { // TODO:handle exception } // Image Setups paddle1 = getImage(base, "resource/paddle1.png"); paddle2 = getImage(base, "resource/paddle2.png"); backTile = getImage(base, "resource/backTile.png"); Ball = getImage(base, "resource/Ball.png"); } @Override public void start() { bg = new Background(0, 0); p1 = new Player1(); p2 = new Player2(); ball = new Ball(); Thread thread = new Thread(this); thread.start(); } @Override public void stop() { } @Override public void destroy() { } @Override public void run() { while (true) { p1.update1(); p2.update2(); ball.update3(); bg.update4(); repaint(); try { Thread.sleep(17); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void update(Graphics g) { if (image == null) { image = createImage(this.getWidth(), this.getHeight()); second = image.getGraphics(); } second.setColor(getBackground()); second.fillRect(0, 0, getWidth(), getHeight()); second.setColor(getForeground()); paint(second); g.drawImage(image, 0, 0, this); } @Override public void paint(Graphics g) { g.drawImage(backTile, bg.getBgX(), bg.getBgY(), this); g.drawImage(paddle1, p1.getCenterX1()-16, p1.getCenterY1()-63, this); g.drawImage(paddle2, p2.getCenterX2()-16, p2.getCenterY2()-63, this); g.drawImage(Ball, ball.getCenterX3()-16, ball.getCenterY3()-16, this); } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_W: p1.moveUp1(); p1.setMovingUP1(true); break; case KeyEvent.VK_S: p1.moveDown1(); p1.setMovingDown1(true); break; case KeyEvent.VK_UP: p2.moveUp2(); p2.setMovingUP2(true); break; case KeyEvent.VK_DOWN: p2.moveDown2(); p2.setMovingDown2(true); break; } } @Override public void keyReleased(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_W: p1.stop(); break; case KeyEvent.VK_S: p1.stop(); break; case KeyEvent.VK_UP: p2.stop(); break; case KeyEvent.VK_DOWN: p2.stop(); break; } } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }
package main; public class Player1 { final int SPEED = 5; final int CEILING = 71; final int GROUND = 649; public int centerX1 = 40; public int centerY1 = GROUND; private boolean movingUp1 = false; private boolean movingDown1 = false; private int speedY1 = 0; public void update1() { if(speedY1 > 0 && centerY1 > CEILING) { centerY1 += speedY1; } if(speedY1 < 0 && centerY1 < GROUND){ centerY1 += speedY1; } } public void moveUp1() { speedY1 = SPEED; } public void moveDown1() { speedY1 = -SPEED; } public void stop() { if(isMovingUp1() == false && isMovingDown1() == false) { speedY1 = 0; } if (isMovingUp1() == false && isMovingDown1() == true) { moveDown1(); } if (isMovingUp1() == true && isMovingDown1() == false) { moveUp1(); } } public int getCenterY1() { return centerY1; } public int getSpeedY1() { return speedY1; } public void setCenterY1(int centerY1) { this.centerY1 = centerY1; } public void setSpeedY1(int speedY1) { this.speedY1 = speedY1; } public int getCenterX1() { return centerX1; } public void setCenterX1(int centerX1) { this.centerX1 = centerX1; } public boolean isMovingUp1() { return movingUp1; } public void setMovingUP1(boolean movingup) { this.movingUp1 = movingUp1; } public boolean isMovingDown1() { return movingDown1; } public void setMovingDown1(boolean movingdown) { this.movingDown1 = movingDown1; } }
package main; public class Player2 { final int SPEED = 5; final int CEILING = 71; final int GROUND = 649; public int centerX2 = 920; public int centerY2 = GROUND; private boolean movingUp2 = false; private boolean movingDown2 = false; private int speedY2 = 0; public void update2() { if(speedY2 > 0 && centerY2 > CEILING) { centerY2 += speedY2; } if(speedY2 < 0 && centerY2 < GROUND){ centerY2 += speedY2; } } public void moveUp2() { speedY2 = SPEED; } public void moveDown2() { speedY2 = -SPEED; } public void stop() { if(isMovingUp2() == false && isMovingDown2() == false) { speedY2 = 0; } if (isMovingUp2() == false && isMovingDown2() == true) { moveDown2(); } if (isMovingUp2() == true && isMovingDown2() == false) { moveUp2(); } } public int getCenterY2() { return centerY2; } public int getSpeedY2() { return speedY2; } public void setCenterY2(int centerY2) { this.centerY2 = centerY2; } public void setSpeedY2(int speedY2) { this.speedY2 = speedY2; } public int getCenterX2() { return centerX2; } public void setCenterX2(int centerX2) { this.centerX2 = centerX2; } public boolean isMovingUp2() { return movingUp2; } public void setMovingUP2(boolean movingup) { this.movingUp2 = movingUp2; } public boolean isMovingDown2() { return movingDown2; } public void setMovingDown2(boolean movingdown) { this.movingDown2 = movingDown2; } }
package main; import java.util.Random; public class Ball { private Player1 p1; private Player2 p2; final int SPEED = 8; final int CEILING = 71; final int GROUND = 649; private int centerX3 = 480; private int centerY3 = 360; private int speedX3 = SPEED; private int speedY3 = SPEED; Random rand = new Random(); public int dir = rand.nextInt(5) + 1; public void update3() { if(centerX3-16 == p1.centerX1+16 && centerY3 >= p1.centerY1-63 || centerX3-16 == p1.centerX1+16 && centerY3 <= p1.centerY1+63 ) { dir = rand.nextInt(2)+1; moveBall2(); } else if(centerX3+16 == p2.centerX2-16 && centerY3 >= p2.centerY2-63 || centerX3-16 == p2.centerX2+16 && centerY3 <= p2.centerY2+63) { dir = rand.nextInt(2)+1; moveBall4(); } else if(centerY3 == CEILING) { dir = rand.nextInt(2)+1; moveBall3(); } else if(centerY3 == GROUND) { dir = rand.nextInt(2)+1; moveBall1(); } else { move(); } } public int getCenterX3() { return centerX3; } public void setCenterX3(int centerX3) { this.centerX3 = centerX3; } public int getCenterY3() { return centerY3; } public void setCenterY3(int centerY3) { this.centerY3 = centerY3; } public void move() { centerX3 += speedX3; centerY3 += speedY3; } public void moveBall1() { switch(dir) { case 1: speedX3 = -SPEED; speedY3 = -SPEED; move(); break; case 2: speedX3 = SPEED; speedY3 = -SPEED; move(); break; } } public void moveBall2() { switch(dir) { case 1: speedX3 = SPEED; speedY3 = -SPEED; move(); break; case 2: speedX3 = SPEED; speedY3 = SPEED; move(); break; } } public void moveBall3() { switch(dir) { case 1: speedX3 = -SPEED; speedY3 = SPEED; move(); break; case 2: speedX3 = SPEED; speedY3 = SPEED; move(); break; } } public void moveBall4() { switch(dir) { case 1: speedX3 = -SPEED; speedY3 = -SPEED; move(); break; case 2: speedX3 = -SPEED; speedY3 = SPEED; move(); break; } } }
package main; public class Background { private int bgX, bgY; public Background(int x, int y){ bgX = 0; bgY = 0; } public void update4() { } public int getBgX() { return bgX; } public int getBgY() { return bgY; } public void setBgX(int bgX) { this.bgX = bgX; } public void setBgY(int bgY) { this.bgY = bgY; } }