Jump to content

  • Log In with Google      Sign In   
  • Create Account


First Pong Game - Code review


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
4 replies to this topic

#1 Pyrotemplar   Members   -  Reputation: 127

Like
1Likes
Like

Posted 23 March 2013 - 02:41 PM

Hello everyone,

 

it would be nice if some of you guys can review my code for my pong game. this is the first game i have ever written, i used this tutorialbut i adapted it to pong instead,  so there will be a lot of code that is similar if not the same to the tutorial but i fully understand the code. i also found a FPS class online and used that with a some minor changes.

 

thank you

 

p.s. if the images dont display, add them to the Pong\bin\com\pyro\sprite\

 

Attached File  Pyro_pong.zip   21.87KB   73 downloads

 

 



Sponsor:

#2 CryoGenesis   Members   -  Reputation: 484

Like
1Likes
Like

Posted 23 March 2013 - 04:19 PM

Yeah I imported your project into eclipse and I found a few problems at the start.

On starting, it gave me an error with loading the images from the project. I found that this was because you were loading the image by reading from a URL instead of the folder that you're running from (might be because it's an applet but I'm not sure). Anyway, I found a bunch load of junk-code when you're importing images. I'm just going to make the assumption that this isn't meant to be an applet (because if it isn't then you don't need ALL the image load code that you have).

 

Ok so I can't remember what your image loading code was but it was in the SpriteStore class in the getSprite function.

The entire block can either be shortened by one of two ways.

1.

		try{
			System.out.println("Importing Image:" + ref);
			Image image = ImageIO.read(new File(ref));
			
			Sprite sprite = new Sprite(image);
			sprites.put(ref, sprite);

			return sprite;
		}catch(Exception e){
			System.out.println(ref + " does not exist");
		}
		
		System.exit(0);
		return null;

 

or if you want just 3 lines:

 

2.


Sprite sprite = new Sprite(new ImageIcon(ref).getImage());
sprites.put(ref, sprite);
return sprite;

 

 

 

Also, Ball.png was corrupt when I downloaded it. Might just be my WinRar but I thought I should tell you just in case.

 

All in all, I think the code is pretty good and well commented. It's very easy to see what's going on in the code which is a very good thing.

 

My tips for future games:

I see that you have all of your game logic code, window code and window animation code in one class. My tip for the future would be to write the game logic in its own class. This class could extend an abstract class called State which has 3 abstract methods init(), update(), and render(Graphics g). Then you could have an application holder class which would be created by passing an instance of a class extends from the State class. Sooo for example:


EDIT: this isn't supposed to be a quote.

public class Pong extends State{

     @Override 
     public void init(){
              //Initialize crap in here
     }
     @Override 
     public void update(){
           //All of the update logic EG checking for collisions and resolving
     }
     @Override 
     public void render(Graphics G){
           //Do all your drawing code here
     }

}
public class AppHolder extends JPanel implements Runnable {

     State state;

     public AppHolder(State state){
          state.init();
          this.state = state;
          Thread thread = new Thread(this);
          thead.start();
     }

     public void run(){
          while(true){
                  state.update();
                  repaint();
                  thread.sleep(1000/FPSYOUWANT)
          }
     }
  
     @Override
     public void paint(Graphics g){
             state.render(g);
     }
}

 

Then you can just initialize a JFrame and add a new instance of AppHolder and it'll do the crap.

The reason that this is better (not necessarily this but the general structure) is because you'll have to re-write a bunch of crap-ass initialization code if you want something like a main screen. Or, if you just put it all in the Game class then the game class will begin to be cluttered and ugly. Having this structure means you can just access the AppHolder class and set the state object to a new one such as MainScreen that extends state and do all the mainscreen logic and drawing in there. 

After you get more advanced you can start using the more complicated, flexible game architecture types. This is just one that will make the work easier in the long run.

 

Anyway, I'm bored of writing, hope this helps!

Also, sorry if I made any mistakes, it's pretty late.


Edited by CryoGenesis, 23 March 2013 - 04:44 PM.


#3 CryoGenesis   Members   -  Reputation: 484

Like
0Likes
Like

Posted 23 March 2013 - 04:43 PM

Also, I checked your other post about where to go from here. 

I see that you're an experienced coder so you're pretty much sorted. There is just one thing you'll probably need to get your head over though. Coding games (or simulations in general) is pretty much coding your own universe, that's how I see it anyway.

To make your game more realistic you'll need to learn physics (laws of motion, etc). To add functionality you'll need to learn mathematics (trigonometry, pythag, basically the stuff you learn in high school). For instance, if you wanted to draw a simple line from point A to point B, pixel by pixel, you'll either need to know trigonometry or how to work out the gradient of a line to do so. You'd actually be surprised how much trig and geometry stuff you use in game programming.

Anyway, I would recommend you to go get some game physics books then use the things you learn from the books to enhance your games to make them more interesting. For instance, about 2 years ago I learnt the equation to gravity and made an asteroids game where all the asteroids would start chasing after you because they were being pulled towards you due to their gravity.

Good luck with your game devving!



#4 tufflax   Members   -  Reputation: 473

Like
0Likes
Like

Posted 24 March 2013 - 04:40 AM

Why do you have Player1Bar and Player2Bar when they are the same except for the name? Having the same code in more than one place is very bad.



#5 Pyrotemplar   Members   -  Reputation: 127

Like
0Likes
Like

Posted 25 March 2013 - 07:10 AM

thank you all

 

CryoGenesis - thanks, i will look into replacing the sprite loading code, and separating the code a bit more. i will also looking into getting a book on game programming math and physics.

tufflax - yes i do realize that they do pretty much the same stuff - will change it.
 

thank you both.


Edited by Pyrotemplar, 25 March 2013 - 02:12 PM.





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