I've looked over it time and time again, but I can't spot any fault in it.
Le Game.java
public class Game extends JFrame {
// Double buffering implementations.
Image dbImage;
Graphics dbGraphics;
// Create the instance of the ball.
static Ball ball = new Ball(239, 175);
// Main game variables.
int
GWIDTH = 500,
GHEIGHT = 400;
// Game screen dimensions. (GWIDTH*GHEIGHT)
Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
// Create the game constructor.
public Game(){
this.setTitle("SurvivalPong - by GamerStudios");
this.setSize(screenSize);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.DARK_GRAY);
this.addKeyListener(new ActionL());
}
public static void main(String[] args) {
Game survpong = new Game();
// Create and start the threads.
Thread ballthread = new Thread(ball);
ballthread.start();
}
@Override
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbGraphics = dbImage.getGraphics();
draw(dbGraphics);
g.drawImage(dbImage, 0, 0, this);
}
public void draw(Graphics g) {
ball.draw(g);
repaint();
}
///////// ActionListener class. Controls key input. /////////
public class ActionL extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
///////// END OF ActionListener CLASS. /////////
}
Le Ball.java
public class Ball implements Runnable {
// Global variables.
int
xLocation, yLocation,
xDirection, yDirection;
Rectangle ball;
// Begin Ball constructor.
public Ball(int xLoc, int yLoc) {
// Set the location.
this.xLocation = xLoc;
this.yLocation = yLoc;
//// Set the ball moving randomly. ////
// First set the X direction.
Random randomdir = new Random();
int xrDir = randomdir.nextInt(2);
if(xrDir == 0) {
xrDir--;
setXDirection(xrDir);
}
// Now set the Y direction.
int yrDir = randomdir.nextInt(2);
if(yrDir == 0) {
yrDir--;
setYDirection(yrDir);
}
// Create the 'ball'.
ball = new Rectangle(this.xLocation, this.yLocation, 15, 15);
}
///////// Setting the X and Y directions for the ball. /////////
// Set the X-Direction of the ball.
public void setXDirection(int xDir) {
xDirection = xDir;
}
// Set the Y-Direction of the ball.
public void setYDirection(int yDir) {
yDirection = yDir;
}
///////// End of setting X and Y directions. /////////
// Draw the ball. (Technically a square.)
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(ball.x, ball.y, ball.width, ball.height);
}
public void move() {
xLocation += xDirection;
yLocation += yDirection;
// Collision detection for the edge of the panel.
if(xLocation <= 0) {
setXDirection(+1);
}
if(xLocation >= 485) {
setXDirection(-1);
}
if(yLocation <= 0) {
setYDirection(+1);
}
if(yLocation <= 385) {
setYDirection(-1);
}
}
@Override
public void run() {
try{
while(true) {
move();
Thread.sleep(4);
}
}catch(Exception e){System.err.println(e.getMessage());};
}
}
Thanks and cheers,
skymonkier

New Topic/Question
Reply




MultiQuote








|