Jump to content
  • Advertisement
Sign in to follow this  

XNA - Learn your keyboard game

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, So, I have been doodling around with XNA and wrote this keyboard learning type thing. Anyhow, wonder if anyone has some time and would maybe offer some advice on the styling of the code and make any recommendations on better ways to handle stuff. Granted this is a small program, but I would rather break any bad coding habits from the start. One other thing if anyone has a look, my "random" characters for each key are not all that random it seems. Am I perhaps using the Random class incorrectly? Thanks for any suggestions/help! File download NOTE: Programmed with VSEE 2008 and XNA 3.0 Other notes: Guess I should add, this is by no means finished. No sound, no menu, not really an end to the game either :0 I know it is a silly little "game/app" but it is good practice for me at least. Maybe I can actually finish something I started... [Edited by - shawnre on December 22, 2008 6:48:03 PM]

Share this post

Link to post
Share on other sites
One thing I would have changed would be the draw function to something more like this:

public void Draw(SpriteBatch spriteBatch)
Vector2 letterOffset = new Vector2(
(position.X * offset) + (texture.Width * 0.5f),
position.Y + (texture.Height * 0.5f)) - (font.MeasureString(character.ToString()) * 0.5f);

spriteBatch.Draw(texture, new Vector2(position.X * offset, position.Y), Color.White);
spriteBatch.DrawString(font, character.ToString(), letterOffset, Color.White);

seemed a bit unneccasery to create a float for each component of a Vector2, then create the Vector2 afterwards anyway, only a small and seemingly important thing, but I guess is boils down to using the tools you have...

Also you can write

List<Key> keyList = new List<key>();

on one line instead of declaring and then defining in the constructor, that may be a personal preference tho, keeps things clean.

A little niggle for where you handle your timer, you say:

if (timer > nextKey)
timer = 0.0f;

Using this method would cause you problems later on down the line in timing critical sections, a better method perhaps would be to count down and incrememnt by the time:

if(timer <= 0)
timer += nextKey

This way it takes into consideration those extra few ms lost each time it ticks, not something overly important here but perhaps something to keep in mind when those few ms will make a difference.

One more thing and then ill leave and see if im able to criticize my own code this much:

if (key.Alive == false)

is a bit redundant, since if statements have a boolean result, simply testing the bool is enough, if key.Alive was equal to false that line would be saying "if false is equal to false" which is a bit silly really, if(!key.Alive) does the same thing and involves less typing, and is less prone to those annoying bugs where you wrote if(bob = true) instead of if(bob == true).

As for the random problem i couldnt quite see it myself, but i would suggest googling to find out how the random seeds itself, it may be that its seeding to the same value each time, a solution maybe would be to have your Key class hold the random as a property instead of creating a new one when needed.


Share this post

Link to post
Share on other sites
Excellent, thanks for taking the time and offering some suggestions. You make some good points that I will probably adopt.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!