Ball in Pong stops moving

Started by
2 comments, last by Kaptein 10 years, 8 months ago

This is my Play class, I have only been working on Java here for about 2 weeks, I'm unsure of why my ball will move for about half a second, and then just stop

The Code
[spoiler]package pongGame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState{
boolean quit = false;
Image paddle;
Image ball;
int paddleXStart = 250;
int paddleYStart = 330;
int ballStartX = 60;
int ballStartY = 140;




public Play(int state){
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
paddle = new Image("res/paddle.png");
ball = new Image("res/pongBall.png");

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
g.drawImage(paddle, paddleXStart, paddleYStart);
g.drawImage(ball, ballStartX, ballStartY);
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();




if((ballStartX >=1) && (ballStartX <=300))
ballStartX += 1 ;

if((ballStartX <=1) && (ballStartX >=300))
ballStartX -= 1;

if((ballStartY >=1) && (ballStartY <=250))
ballStartY += 1;

if((ballStartY <=1) && (ballStartY >=250))
ballStartY -= 1;

if(input.isKeyDown(Input.KEY_A)){
paddleXStart -= 1 ;
if(paddleXStart < 0){
paddleXStart =+ 1 ;
}
}
if(input.isKeyDown(Input.KEY_D)){
paddleXStart += 1 ;
if(paddleXStart > 439){
paddleXStart -= 1;
}
}
}

public int getID(){
return 1;
}
}[/spoiler]

I'm not sure how to fix this >.<

Advertisement

I haven't run your code but I'm guessing your ball will move to one edge of the screen then stop? I added some comments to problem areas of your code. I'll expand on them after the code.


// Only the update code here. It's where the problem is. The rest of the class looks fine.
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    Input input = gc.getInput();
 
    if((ballStartX >=1) && (ballStartX <=300))
        ballStartX += 1 ;
 
    if((ballStartX <= 1) && (ballStartX >= 300)) // This will never return true. ballStartX cannot be both less than 1 and greater than 300.
        ballStartX -= 1;
 
    if((ballStartY >=1) && (ballStartY <=250))
        ballStartY += 1;
 
    if((ballStartY <=1) && (ballStartY >=250)) // Same as above. Never true.
        ballStartY -= 1;
 
    if(input.isKeyDown(Input.KEY_A)){
        paddleXStart -= 1 ;
        if(paddleXStart < 0){
           paddleXStart =+ 1 ; // paddleXStart += 1; May want to change to if (paddleXStart < 0) { paddleXStart = 0; }
        }
    }
    if(input.isKeyDown(Input.KEY_D)){
        paddleXStart += 1 ;
        if(paddleXStart > 439){ // Same as above. Might be better as if (paddleXStart > 439) { paddleXStart = 439; }
            paddleXStart -= 1;
        }
    }
}

Edit: Lost half my post.

As you can see, the ballwill never move left or I think up. I can't remember how Slick's coordinate system is. ballStartX will never be both less than 1 and greater than 300. You'll want to change


if((ballStartX <=1) && (ballStartX >=300))

to


if (ballStartX < 1) || ballStartX > 300)

The same goes for vertical movement.


if(paddleXStart < 0){
   paddleXStart =+ 1 ; // paddleXStart += 1; May want to change to if (paddleXStart < 0) { paddleXStart = 0; }
}

The above will not always exhibit the same behavior. If paddleStartX is located at (-0.5, 50) it will be set to (0.5, 50) while a paddle at (-1, 50) will go to (0, 50). Always setting its postion to 0 will fix this.

Your code is framerate-dependent right now. You may want to look at Slick's documentation and wiki and read up on delta time and framerate-independent motion.

ballStartX, ballStartY, paddleStartX, and paddleStartY sound like they store the initial positions of the paddle and ball so they can be returned to their initial configuration when a player scores. Personally I would rename them. However, it's more important that you understand your code since you're the one having to work with it.

You said you're just starting in Java so I'll let you know you may want to be consistent in your bracket usage. Some of your if statements use brackets while others do not. I can understand having single-line ifs not use brackets but some of yours do and some don't. Standardizing this may help readability.

Edit 2: Your ball is only moving one direction when it's on-screen.

if((ballStartX >=1) && (ballStartX <=300))
        ballStartX += 1 ;

If you want to change this behavior, which I assume you do, use a velocity variable and do something like

private float ballVelocity = 1f;
 
// update method
if (ballStartX < 1 || ballStartX > 300) {
    ballVelocity *= -1;
}
 
ballStartX += ballVeocity

Yeah, I tweaked a LOT of the current code, I've gotten the ball to move around the screen left right up and down now. I ended up deleting almost everything I've posted here.

Yeah, I tweaked a LOT of the current code, I've gotten the ball to move around the screen left right up and down now. I ended up deleting almost everything I've posted here.

That's a success story. Welcome to programming :)

The REALLY good days, are when you find your problem, remove the bad parts, and replace it with something that works :)

The bad days are when you can't for the life of you find out what goes wrong, where or even why :)

This topic is closed to new replies.

Advertisement