Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Rep Power

    Default Pong! But missing ball...

    Hey all so I'm making a pong game for my semester project. Currently, I'm just trying to get the basic engine up and running for it. I have no experience with graphics or windows programming before this so its all news to me. Well, I was going along, made my ball class and created an object and drew it on the screen. It displayed (even though not in the spot it should have), and then I went along adding some collision detection and ball movement, and now my ball disappeared! I tried commenting out the new code to no avail, not sure what happened but I will post my code and you can look at it!

    Java Code:
    package Pong;
    import java.awt.*;
    import java.awt.image.BufferStrategy;
    import javax.swing.JFrame;
    public class Game extends JFrame {
    	 * 1 player Pong-type game programmed by
    	 * Jared Ready and Jake Saile.
    	//Variable and object declaration
    	//windowWidth and windowHeight are used for the game window resolution
        private int windowWidth = 800;
        private int windowHeight = 600;
        private Ball ball;
        public static void main(String[] args) {
            new Game();
        //Constructor for class Game. Sets up attributes for the game window.
        public Game() {
            this.setSize(windowWidth, windowHeight);
            this.setLocation(100, 100);
            //Creates a buffer strategy with 2 buffers. Gets rid of screen flickering that would
            //occur without
            while(true) {
                long start = System.currentTimeMillis();
                while(System.currentTimeMillis()-start < 5) {
                     //do nothing
        //This initializes all variables and objects needed for Game class
        private void initGame() {
            ball = new Ball(windowWidth/2, windowHeight/2, 1,-1, 40);
        //This is the method that handles the logic of the game
        private void gameLoop() {
        	//We need to start with moving the ball to its new position
        	ball.posX = ball.posX + ball.speedX;
        	ball.posY = ball.posY + ball.speedY;
        	//Checks for wall collision, and changes movement of ball if need be
        	if(ball.posX <= 0 || ball.posX >= windowWidth - ball.ballSize)
        		ball.speedX = -ball.speedX;
        	if(ball.posY <= 0)
        		ball.speedY = -ball.speedY;
        private void drawFrame() {
            //draws the updated frame
            BufferStrategy buffer = this.getBufferStrategy();
            Graphics graphics = null;
            try {
                graphics = buffer.getDrawGraphics();
                //Clears the back buffer by just making it black
                graphics.fillRect(0, 0, windowWidth, windowHeight);
            } finally {
                //Destroys graphics object not being used now
            //Switches to display the back buffer
            //This tells the system to synchronize the state of the graphics so that the
            //display is up to date
        //Draws ball on the screen at the middle
        public void drawBall(Graphics graphics)
        	graphics.fillOval(ball.posX, ball.posY, ball.ballSize, ball.ballSize);
    Java Code:
    package Pong;
    public class Ball {
    	 * Class for holding our Ball object.
    	 * Just holds the dimensions and speed of the ball
    	//Using public variables currently for ease of use
    	//when drawing the ball
    	public int speedX;
    	public int speedY;
    	//Start position
    	public int posX;
    	public int posY;
    	public int ballSize;
    	//Contructor for the Ball class.
    	//Parameters (position x, position y, speed on x axis, speed on y axis, size of ball)
    	public Ball(int posX, int posY, int speedX, int speedY, int ballSize)
    		posX = this.posX;
    		posY = this.posY;
    		speedX = this.speedX;
    		speedY = this.speedY;
    		ballSize = this.ballSize;

  2. #2
    Join Date
    Nov 2011
    Rep Power

    Default Re: Pong! But missing ball...

    Update! Under my drawFrame() method, I did add a drawString() method in there to track the coordinates of my ball, and it always stays at (0,0)
    Not sure where my issue is with this, but I think I'm going to bed. I look forward to your help in the morning!
    Thank you all in advance, this is my first post here

  3. #3
    Join Date
    Nov 2011
    Rep Power

    Default Re: Pong! But missing ball...

    Ugh one more update. I decided to change
    Java Code:
    graphics.fillOval(ball.posX, ball.posY, ball.ballSize, ball.ballSize);

    Java Code:
    graphics.fillOval(ball.posX, ball.posY, 40, 40);
    And my ball went back to showing up. But at the wrong spot again. (Should be showing up in the middle of the screen, roughly, but it is showing up at the top left side). Leads me to believe something with my Ball class is faulty, not sure what though because there isnt much to it.

Similar Threads

  1. ping pong
    By ahmed_ijsh in forum Java Gaming
    Replies: 0
    Last Post: 10-31-2010, 03:10 AM
  2. The bug in the Pong game
    By Phoeenix in forum New To Java
    Replies: 4
    Last Post: 07-21-2009, 07:13 PM
  3. Pong Paddle Not Stopping Ball At Certain Speed
    By JDCAce in forum Java Applets
    Replies: 2
    Last Post: 10-12-2008, 05:15 AM
  4. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10:08 PM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts