Pongclone - constructive criticism please

Hi all
I’ve been lurking around for quite a while whilst learning the basics of java and have just finished thenewbostons java game dev tuts and made a pong clone earlier today. Attached is both the runnable .jar, which starts in fullscreen, and the source.
Any constructive criticism or pointers would be much appreciated.
Known issue: pressing space while ball is moving will re-randomize ball velocity(direction).

Fullscreen Mode: http://www.mediafire.com/?98qobp777zf6jrb,22qohsmvefvnif8

EDIT: Windowed mode version(working): http://www.mediafire.com/?c7sg1cvft9qe47j,d2yeaa1lqihxfhs

enjoy,
Ado

You did this in one day? This is fast for a first game :wink:

Fullscreen works perfectly…

But it somehow looks non-smooth.
I downloaded the source,
looked at the game loop: Seems okey, checked.
let the code print the fps, compiled and started: FPS is a constant 62.

I guess your problem lies in the famous spikes.
Spikes are some frames which just accedently take much more time than they should. Then it appears like it would be slower, but it isn’t.
looks at code again
It seems like your “draw()” method in mainPong.java is “synchronized”. Remove this keyword, it propably doesn’t does what you want it to do.

One more thing:
To answer your question about random ball-launching:
Replace this:


        //launch ball
        if(keyCode == KeyEvent.VK_SPACE){
        	ball.setVelocityX(3f);
            ball.setVelocityY(3f);
            startMsg = false;
        }

With this:


        //launch ball
        if(keyCode == KeyEvent.VK_SPACE){
	    // We create a "rand" instance for generating random stuff.
	    // It would be a better practice, if you store the rand
	    // in this class's fields.
	    Random rand = new Random();
	    // rand.nextBoolean() returns randomly either true or false
	    if (rand.nextBoolean() == true) {
		// If it's true, then the X-Velocity is positive, else it's negative.
		ball.setVelocityX(3f);
	    } else {
		ball.setVelocityX(-3f);
	    }
	    if (rand.nextBoolean() == true) {
		ball.setVelocityY(3f);
	    } else {
		ball.setVelocityY(-3f);
	    }
            startMsg = false;
        }

I think I don’t have to explain it, as it should already be done in the comments.

I think you should either put in AI or make it clearer that it is two player. :smiley:

@master oops. I forgot to mention that. Just made the actual main pong class today and that’s right off the end of bucky’s tutorials, so I don’t know enough to implement Ai and I don’t want the 2nd paddle to just follow the ball exactly. Might as well not even have it :stuck_out_tongue:

@matheus I made the mainPong class today and used the other classes from bucky’s tutorials, with some modification(which is where that sync in draw comes from, wasn’t sure if I should keep or discard it. From what I understand it has to do with letting a particular thread do its thing before allowing others to run(?)).
I have started reading killer game programming in java and used neither its nor bucky’s loops, instead using the variable timestep loop from the game loops post in the articles section of this forum.
Thanks for that random function. I think I will use that as I didn’t know it could randomize booleans.

Just an update.
I have been working today to add the option to switch between full screen and windowed mode. I think I am almost there as I can successfully switch between the two(via windowedMode boolean in ScreenManager, not ingame yet though I will bind setWindowedMode() to F3 or something later) but I am having issues with updating the canvas. I am not sure if this is an issue with my buffer strategy but I think I have narrowed it down to somewhere in getGraphics() or the ScreenManager update() method.
I have a feeling that Canvas buffering doesn’t work the same way as Window buffering but maybe I’m wrong, I don’t know a whole lot about Canvas or even Window at this point.
If someone would be so kind as to have a look at my code and help me out it would be much appreciated.
The new link is in the OP.

@master: not sure if you are being serious or not but just to clarify I was saying ‘oops’ as in ‘oh no I made a mistake’.

On another note, I have fixed switching between windowed and fullscreen. Embasassing mistake where I didn’t rename some constructors in the draw method. It was still trying to get the fullscreen window height/width and not the canvas ::slight_smile:
Will upload the new source later.

I’m going to compile your code compiling code. It appears you don’t need the


import java.awt.Graphics;
import java.awt.Graphics2D;

inports. Your ball dropping at the start wasn’t even random. I could place my paddle in exactly the same place, and the paddle would hit it. You should include a random class between maybe 1 and 36 (for 360 degrees).

I hadn’t uploaded the new source at that time. new source is up for windowed mode. I have implemented matheus’s random code but thats about as much work I’m going to do on the randomness of the ball.
The last thing I’m doing with this before I move onto a new project is to bind the window switching to the F3 key which has been giving me troubles. I’m having trouble cleanly killing the windowed canvas and launching fullscreen during the game. I’m trying to do this by just toggling the windowedMode boolean but I have a feeling it’s either not switching fast enough, which doesn’t sound likely, or there’s something else happening but I’m just too tired and aren’t picking up on it.

Shouldn’t it be posted on WIP section?

Possibly but as I have just finished working through learning the basics of java and some other tutorials I was more after advice on code structure, anything that could be done better or more efficently, getting help problem solving etc etc. Newbie stuff.
I have no plans for this beyond adding in the ability to toggle between window modes and thought some other newbies might like to check out the code. I have been collecting any other java game source code other people have put out to learn from and either couldn’t find much in the way of basic(pong, mario, tetris, pacman) beginner level game source available or had to do alot of digging to find it.