Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Critique my code please


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 mepis   Members   -  Reputation: 230

Like
1Likes
Like

Posted 14 August 2013 - 05:31 PM

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



Sponsor:

#2 Cosmic314   Members   -  Reputation: 1243

Like
0Likes
Like

Posted 14 August 2013 - 06:30 PM

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

Edited by Cosmic314, 14 August 2013 - 06:36 PM.


#3 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
0Likes
Like

Posted 15 August 2013 - 03:29 AM

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.

#4 mepis   Members   -  Reputation: 230

Like
0Likes
Like

Posted 15 August 2013 - 06:04 AM

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



#5 zdlr   Members   -  Reputation: 266

Like
1Likes
Like

Posted 15 August 2013 - 07:34 AM

  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. 


#6 zdlr   Members   -  Reputation: 266

Like
1Likes
Like

Posted 16 August 2013 - 07:20 AM

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



#7 mepis   Members   -  Reputation: 230

Like
0Likes
Like

Posted 17 August 2013 - 02:49 PM

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.



#8 MarekKnows.com   Members   -  Reputation: 668

Like
0Likes
Like

Posted 21 August 2013 - 11:03 AM

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.


---
Free C++, OpenGL, and Game Development Video Tutorials @
www.MarekKnows.com
Play my free games: Ghost Toast, Zing, Jewel Thief





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS