View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Code Review ::Pong Clone::

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.

10 replies to this topic

### #1Manhattanisgr8  Members

Posted 06 April 2013 - 06:27 PM

I just finished my first game in XNA 4.0 and would love some feedback on my code.

Game1:

Spoiler

Ball Class:

Spoiler

Spoiler

Button Class:

Spoiler

Spoiler

ParallaxingBackground Class:

Spoiler

CreditsScreenState Class:

Spoiler

Cpl Alt, Travis A

USMC

### #2RAZORUNREAL  Members

Posted 07 April 2013 - 03:11 AM

I haven't read the whole thing, but based on the ball class my main piece of advice would be this:

Try to reduce the number of conditions. The more similar code paths you have, the more opportunities there are for mistakes, particularly copy&paste errors. For example, rather than your 4 "moving..." booleans use a single velocity Vector2. You can add that to your position every update, avoiding 4 if statements. Then to bounce off the walls you just do velocity.x = -velocity.x

___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!

### #3rip-off  Moderators

Posted 07 April 2013 - 11:25 AM

First off, finishing any game, no matter how small, is an accomplishment, so congratulations!

These are some things that jumped out at me when reviewing your code:
• You've hard coded the width and height of the screen, and the playable area, in a number of locations. Consider having a single named constant for each of these values.
• Why not create a class for the Bumpers?
• You can probably move all the menu related stuff into the menu state class.
• You're not making optimum use of constructors. Instead of code like this:
someButton = new Button();
someButton.active = true;

someGameObject = new GameObject();
someGameObject.position.x = /* ... */;
someGameObject.position.y = /* ... */;
// And so on...

Consider writing a constructor that will allow you to specify this stuff:
someButton = new Button(Button.State.Active); // Enums are easier than booleans to understand sometimes

someGameObject = new GameObject(positionX, positionY, /* ... */);

• You're making extensive use of direct access to public fields. This might be manageable for smaller games, but it will quickly spiral out of control when you start trying to write larger programs. The idea of object oriented design is to delegate object specific logic to the objects in question, by calling methods (or property accessors).

private void PlayerControls()
{
// Player 1
if (Keyboard.GetState(p1.pNumber).IsKeyDown(Keys.W))
{
p1.position.Y -= p1.speed;
}
if (Keyboard.GetState(p1.pNumber).IsKeyDown(Keys.S))
{
p1.position.Y += p1.speed;
}

// Player 2
if (Keyboard.GetState(p2.pNumber).IsKeyDown(Keys.Up))
{
p2.position.Y -= p2.speed;
}
if (Keyboard.GetState(p2.pNumber).IsKeyDown(Keys.Down))
{
p2.position.Y += p2.speed;
}
}

Perhaps something like this:
private void UpdatePlayer(Paddle paddle, Keys up, Keys down)
{
KeyboardState keyboard = Keyboard.GetState(player)
if (keyboard.IsKeyDown(up))
{
}
if (keyboard.IsKeyDown(down)
{
}
}

private void PlayerControls()
{
UpdatePlayer(p1, Keys.W, Keys.S);
UpdatePlayer(p2, Keys.Up, Keys.Down);
}

The "Up" and "Down" methods would apply the (private) speed to the position,
• The ball "is a" game?
class Ball : Microsoft.Xna.Framework.Game

• I don't think the ball should be responsible for keeping track of the player's score. This logic can be pushed to the calling code.
• I notice that the members previousBallSpeed and increaseBallSpeed for the Ball are not actually speeds, they are timing related. Likewise, your "timer" is not in time units, it is actually used to remember the state you want to reset from.

When dealing with primitives, it can be an easy mistake to accidentally use mix two values that are expressed in different units together. One way to guard against this is to be explicit and consistent in your naming of the variables.
• I agree with RAZORUNREAL, having a single velocity will be easier to write and understand.

### #4stitchs  Members

Posted 07 April 2013 - 04:51 PM

I had a lot of these similar issues when I posted my code for review. In fact, it was @rip-off who listed me things I needed to change. What I would recommend doing is copying this list into word, and deciding the easiest things to do first, do them, then highlight the bullet point. It shows you that you are making steady progress towards your goal, and keeps giving you that feeling of achievement.

Regards,

Stitchs.

### #5Michael Tanczos  Staff Emeritus

Posted 07 April 2013 - 08:47 PM

With velocity it's going to be much easier.. you already have a time delta between updates given by "gameTime" in your update methods.  So updating a Vector2 location with a velocity is a matter of taking the elapsed number of seconds as a float (elapsed) and doing this:

ballposition += velocity * elapsed

You need to used the elapsed time with a velocity to ensure that the ball speed is consistent across systems.

### #6Manhattanisgr8  Members

Posted 07 April 2013 - 09:23 PM

Thank you all for the different pointers. I honestly believe you can't get better unless you have constructive criticism. I really do appreciate all of you for looking through my code.

Cpl Alt, Travis A

USMC

### #7Manhattanisgr8  Members

Posted 07 April 2013 - 09:28 PM

You've hard coded the width and height of the screen, and the playable area, in a number of locations. Consider having a single named constant for each of these values.

Would that be in a separate class on it's own and other classes inherit from that class that I need the height and widths?

Cpl Alt, Travis A

USMC

### #8rip-off  Moderators

Posted 08 April 2013 - 01:48 AM

No, inheritance would not be used for this. You can either make them public named constants in whichever class makes the most sense to you (e.g. Game1). If none of your existing classes suit, you can make a separate "Constants" class containing just these.

In addition, try to compute things from as few constants as possible. For instance, your three buttons are spaced 35 pixels apart, at 500, 535 and 570. Let use say you want to move the button list a bit. You must change each constant. If you wanted to increase this spacing to 40 pixels, you now have to modify the last two values.

However, you could also express this in terms of just two constants, TopButtonLocation and ButtonSpacing. So the first button is located at TopButtonLocation, the second at TopButtonLocation + ButtonSpacing and the last at TopButtonLocation + (2 * ButtonSpacing) (you could also express the second as playButton.position.X + ButtonSpacing.

Now, to shift the entire menu, just change the TopButtonLocation. To spread out the menu items, just change the ButtonSpacing.

### #9Andy474  Members

Posted 08 April 2013 - 01:58 AM

You've hard coded the width and height of the screen, and the playable area, in a number of locations. Consider having a single named constant for each of these values.

Would that be in a separate class on it's own and other classes inherit from that class that I need the height and widths?

I usually have a "Class" (more of a file) called Globals.cs which looks like:

public static class Globals
{
public static int SCREEN_WIDTH = 800;
public static int SCREEN_HEIGHT = 600;
}


which allow you to access this anywhere by doing Globals.SCREEN_WIDTH = 1024; etc.

### #10Manhattanisgr8  Members

Posted 08 April 2013 - 11:27 AM

Thank you guys so much for the advise. I am going to start working on a Worm clone using all of your suggestions.....hopefully soon.....wife just bought me SCII: HotS... so it might be a few days before I start.

Edited by Manhattanisgr8, 08 April 2013 - 11:30 AM.

Cpl Alt, Travis A

USMC

Posted 08 April 2013 - 02:39 PM

Thank you all for the different pointers. I honestly believe you can't get better unless you have constructive criticism. I really do appreciate all of you for looking through my code.

Also your code isn't bad for a beginner. You're also doing a lot of things right. One thing you're doing right is asking for constructive critsism. That is a good sign for a programmer. Though in terms of your code in general it isn't bad and you're doing a lot of things right.

Also on your next game, just as a good lesson, try to write down the game. Meaning write down all the elements you need in that game. Now pick those elements apart. What do those elements need? What do each of those elements have in common? Before ya know it you'll start seeing where you can use inhertience and be core you know it you would have ended up creating a basic framework you can use for any game you do.

Also on your next game don't be worried to come back here and ask for a code review it. Their are a lot of people here that love doing code reviews and helping people out.

Before you know it you'll understanding and applying popular design models without even realizing it!

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.