im am trying to make pong by only using by y current knowledge , but now I'm at a halt , i want to move the ball , for that i need to change the variables BallX
and BallY
in a while loop bu the problem is that is i use the while(true){ }
to do something , my implementation of the GUI is stopped , eventually doing nothing and entering a infinite loop. How can i solve this problem.
this is the code , don't mind the unused variables
package pong;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class gamePanel extends JPanel implements ActionListener , KeyListener{
int fixPlayerYPos_one = 25;
int fixPlayerYPos_two = 930;
int playerPos_one = 200;
int playerPos_two = 200;
int PlayerDimenX = 25;
int PlayerDimenY = 200;
int BallXElement = 1;
int BallYElement = 2;
int ballSpeed = 10;
int ballX = 490;
int ballY = 340;
Timer timer = new Timer(5, this);
gameGraphics player_one = new gameGraphics(fixPlayerYPos_one, playerPos_one, PlayerDimenX,
PlayerDimenY, Color.cyan);
gameGraphics player_two = new gameGraphics(fixPlayerYPos_two, playerPos_two, PlayerDimenX,
PlayerDimenY, Color.pink);
gameGraphics Ball = new gameGraphics(ballX, ballY, 20, 20, Color.yellow);
public gamePanel() {
setSize(new Dimension(1000,700));
setLayout(null);
setOpaque(true);
setBackground(Color.DARK_GRAY);
//panel contructor
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
player_one.drawSquare(g);
player_two.drawSquare(g);
Ball.drawSquare(g);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
there is also a gameGraphics class but i dint think it is related to the context of this question