Hello folks
Could someone please explain or tell me why my ball for my pong game is drawing at 0, 0 instead of the middle of the screen like I want it to.
Ball.java
[spoiler]
package com.spectregames.enities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
import com.spectregames.core.Game;
public class Ball extends Enity implements Runnable {
// Global variables
int xPosition, yPosition, xDirection, yDirection;
Rectangle ballCollisionBox;
public final static int BALL_SIZE = 10;
public final static int HALF_BALL_SIZE = BALL_SIZE / 2;
public Ball(int x, int y) {
this.xPosition = x;
this.yPosition = y;
// Set ball moving in a random direction.
Random r = new Random();
int rXDir = r.nextInt(1);
if (rXDir == 0) {
rXDir--;
}
setTheXDirection(rXDir);
int rYDir = r.nextInt(1);
if (rYDir == 0) {
rYDir--;
}
setTheYDirection(rYDir);
// Let's create the ball's collision box.
ballCollisionBox = new Rectangle(this.x, this.y, BALL_SIZE, BALL_SIZE);
}
public void setTheXDirection(int xDir) {
this.xDirection = xDir;
}
public void setTheYDirection(int yDir) {
this.yDirection = yDir;
}
public void update(Game game) {
}
public void render(Graphics g) {
// Let's draw our game ball.
g.setColor(Color.WHITE);
g.fillOval(ballCollisionBox.x, ballCollisionBox.y,
ballCollisionBox.width, ballCollisionBox.height);
}
public void run() {
try {
while (true) {
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
[/spoiler]
Game.java
[spoiler]
package com.spectregames.core;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import com.spectregames.enities.Ball;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 9157166091463961420L;
private boolean gameIsRunning = false;
// Ball objects.
Ball ball = new Ball(GameScreen.GAME_WIDTH / 2 - Ball.HALF_BALL_SIZE,
GameScreen.GAME_HEIGHT / 2 - Ball.HALF_BALL_SIZE);
// Our game threads.
private Thread gameThread;
public Game() {
// End of the Game Constructor.
}
public synchronized void start() {
if (gameIsRunning)
return;
gameIsRunning = true;
gameThread = new Thread(this);
gameThread.start();
// End of the start method.
}
public synchronized void stop() {
gameIsRunning = false;
try {
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(0);
// End of the stop method.
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (gameIsRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames + " UPS: " + updates);
frames = 0;
updates = 0;
}
}
stop();
// End of the run method.
}
private void update() {
// End of the update method.
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
// ********************************
// Render code goes here for the game.
// Draws the black background.
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
// Draws the ball.
ball.render(g);
// *******************************
g.dispose();
bs.show();
// End of the render method.
}
public static void main(String args[]) {
new GameScreen(GameScreen.GAME_WIDTH, GameScreen.GAME_HEIGHT,
GameScreen.GAME_TITLE, new Game());
// End of the main method.
}
}
[/spoiler]
my GameScreen.GAME_HEIGHT = 600;
and my GameScreen.GAME_WIDTH = GAME_HEIGHT / 9 * 16;
Thanks in advance.
Richard