• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Manhattanisgr8

Code Review ::Pong Clone::

10 posts in this topic

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

2

Share this post


Link to post
Share on other sites
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).

    For example, instead of:
    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)
    {
    	PlayerIndex player = paddle.Player;
    	KeyboardState keyboard = Keyboard.GetState(player)
    	if (keyboard.IsKeyDown(up))
    	{
    		paddle.Up();
    	}
    	if (keyboard.IsKeyDown(down)
    	{
    		paddle.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.
2

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.  

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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?

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0