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

    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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(windowWidth, windowHeight);
            this.setResizable(false);
            this.setLocation(100, 100);
            this.setVisible(true);
     
            //Creates a buffer strategy with 2 buffers. Gets rid of screen flickering that would
            //occur without
            this.createBufferStrategy(2);
     
            initGame();
     
            while(true) {
                long start = System.currentTimeMillis();
                gameLoop();
                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;
            
            drawFrame();
        }
     
        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.setColor(Color.BLACK);
                graphics.fillRect(0, 0, windowWidth, windowHeight);
                drawBall(graphics);
            } finally {
                //Destroys graphics object not being used now
            	graphics.dispose();
            }
     
            //Switches to display the back buffer
            buffer.show();
     
            //This tells the system to synchronize the state of the graphics so that the
            //display is up to date
            Toolkit.getDefaultToolkit().sync();
        }
        
        //Draws ball on the screen at the middle
        public void drawBall(Graphics graphics)
        {
        	graphics.setColor(Color.RED);
        	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
    	
    	//Speed
    	public int speedX;
    	public int speedY;
    	
    	//Start position
    	public int posX;
    	public int posY;
    	
    	//Size
    	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
    Posts
    28
    Rep Power
    0

    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
    Posts
    28
    Rep Power
    0

    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);
    to

    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: 3
    Last Post: 04-01-2009, 11:07 PM
  4. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09: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
  •