Critique my code please

Started by
6 comments, last by mmakrzem 10 years, 8 months ago

Good evening everyone,

Would anyone mind please critiquing my code? Any help would be very appreciated.

Background:

I'm in school for computer science and am casually playing around with various languages coding different things. I decided to program a Pong clone in Java as a learning experience. This experiment has less to do with making an actual game and more to do with designing code really well. I designed my code with a lot of forward thought to make future changes and UI settings very easy to do later on. With that said, my code is probably a lot more complicated than it needs to be. But I did it for practice and learning.

I still have to add a point system and UI to change settings later on. I see those as more trivial things at this point though.

I'm putting in two links to the code. One is to my Github account where the files are located. I am also putting a link to my personal blog page (not intending on plugging here) because I put all the code on a single page. I figured it might be easier to follow back and fourth between the various classes that way.

So, if anyone wouldn't mind, please tear into my code. I can't learn more if I have no idea what I messed up or what I could be doing better. I appreciate any opinions any person might have.

GitHub

Personal Blog Page

Advertisement

In some of your boolean logic, change this:


public boolean isBallGreaterThanWindowWidth(){
    if (xPosition > (windowWidth - (xRadius * 1.5)))
      return true;
    else 
      return false;
  }

to this:


public boolean isBallGreaterThanWindowWidth(){
    return(xPosition > (windowWidth - (xRadius * 1.5)));
}
It makes no sense for an abstract game object like the Ball class to be aware of characteristics of the program window.

If you like, it could have functions that return the edge coordinates of the ball, which can then be compared to window coordinates elsewhere.

Thank you both very much. I'll have a few things to do the next few days.

  1. Every time you add a new item, you have to change the GameWindow.paint method.
  2. You have a lot of hardcoded magic numbers. Like 16666666..., (userInput == 38), (userInput == 40), etc.
  3. Some objects are not encapsulated very well. Ball is just a bag of data which doesn't do anything, just holds values. It also holds values not specific to ball, like window measurements.
  4. What is the purpose of GameLoopTimer? It looks like it could be used to make movement framerate independent, but it is not used for that. Is the frequency of GameWindow.paint calls fixed in Java, or does it vary with the speed of the machine?
  5. What is the purpose of the isHard boolean?
  6. GameWindow has too many responsibilities - drawing, input detection and response, general initialization, object creation, object movement. It should delegate a lot more to other classes.

mepis, I've sumitted a couple of pull requests that address one or two of the points I mentioned above.

mepis, I've sumitted a couple of pull requests that address one or two of the points I mentioned above.

Awesome. Thank you!

I've taken note of everything everyone is saying. For my purposes, I think it might be best to scrap what I have and start over again. It'll be a better learning experience that way.

I always figured the GameWindow class did to much. But I wasn't sure how to break it down and make it be in better form. But this is all practice and learning so it work out. It makes me go back and look at it again.

The whole point of the ball class was only to make it conceptually easier to see what was going on. I can see how I messed that up though. Correct me if I am wrong, but it would be alright for that class just to hold number pertaining to the ball itself but the rest is completely not needed and better handled somewhere else?

Part of the problem with the GameWindow class doing to much is that I'm still getting used to the Java GUI classes (Eg. Swing). I'm still learning and figuring out how to handle all of those classes. I have a hard time understanding the Java classes because there are just so bloody freaking many of them. It seems like half of them are capable of doing the same thing as another class. But I'm still learning. That's the way these things go.

Thanks a lot guys. I'm going to hold out for a couple of days longer and see if anyone else has any other input. Then I'm going to start over so I can figure out a way to better handle that GameWindow class.

Instead of explicitly creating two variables to represent the X and Y value of something, use a class like Point (http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Point2D.html) that contains both in one object.

This topic is closed to new replies.

Advertisement