• Advertisement
Sign in to follow this  

Pong in C# w/ XNA

This topic is 2698 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

I feel I've made too many topics about this project, so I'm going to put down my list of goals for this game and try to keep it in a single topic:


Scoring System. I want to display the player scores onscreen. I figure the score would be an array, though I haven't had much experience with those.

A Win condition: I want to have a win condition of 10 points before the game ends, and it announces the winner, etc, with a free play (no end score) option as well. I also want the option of a 'Sudden Death' mode, where the first one to miss loses. For this I'll also need to handle the next feature.

Menus: I want to have a simple title screen, with a menu where the player can choose to play with the AI, or another human player, and be able to select which point system (as mentioned earlier) they want to play under. I'll need to know how to actually make title screens/menus and how to transition between the screens, as well as how to allow the player to choose their options (I figure this would be centered around Boolean.)

Sound: I want simple sounds for the various actions that occur in the game, and possibly title music. I was also thinking about allowing the player to adjust the audio levels, but I don't know what that would require, and I'd also want the player to be able to save this option so they don't have to do it every time.

I was also thinking about multiple levels of AI difficulty (in regards to how the second paddle moves), though I don't know if the Code I currently use for AI will work with this.

Edit:

Game Background: I want to be able allow the player to choose the background color (or possibly a background image) during the game. It would be in an option menu. I'd like to know how to implement this.

Extras: I wanted to have one background color be available through typing the color name on the keyboard. I don't know how to read keyboard input outside of checking for individual presses, and only if they're held down.

I'd like like to look into custom paddle colors for players to choose from.

I've gotten the collision, and the AI to work correctly, and now I'm onto the next step, which is scoring.


Some of this seems a bit excessive for simply pong, but I want to learn how to do these things now, so I can apply them to later projects once this is complete.

Any assistance on how to do any of these things is highly appreciated.

[Edited by - Slateboard on September 7, 2010 2:51:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
I can't comment on the XNA-specific aspects of your question, but:
Quote:
Scoring System. I want to display the player scores onscreen. I figure the score would be an array, though I haven't had much experience with those.
Typically you would use an integer for the numerical representation of the score, and a string for the textual representation.

In C#, you can easily convert an integer to a string using the ToString() function. Although I can't tell you how to do it off the top of my head, I'm sure you can also format the string to your liking as well (e.g. by adding leading zeros to create a 'fixed-width' string).

I imagine when you mention the use of arrays, you're thinking of an array where each entry is one digit of the score's numerical value. Although you could do it this way, I'd go with using an integer and a string instead, as it's more straightforward (IMO).

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk

In C#, you can easily convert an integer to a string using the ToString() function. Although I can't tell you how to do it off the top of my head,



In .net all types have a ToString() function.

Share this post


Link to post
Share on other sites
Quote:
In .net all types have a ToString() function.
Hm, if you're pointing out that the way to convert an integer to a string is to use ToString(), then yes, that's exactly what I said. If you're pointing out that all types derived from System.Object (not just integer types) have a ToString() method, then yes, that is of course true, but it doesn't contradict my statement in any way.

Anyway, sorry if I'm misunderstanding your post (if I am though, feel free to clarify).

Share this post


Link to post
Share on other sites
I'm new to xna myself but hopefully i can be of some help.

Scoring: exactly what jyk said, just use an int variable to hold the score and use it with a Spritebatch drawstring as below to draw it on screen:

spriteBatch.DrawString(mySpriteFont, "Player 1 Score: " + player1Score, new Vector2(50, 10), Color.Yellow);


As for the win condition, could have a Win() method which returns a boolean value, true if the score has reached 10 false otherwise, and have the main game loop have a test for checking if win is false then keep doing what its doing, otherwise show gameover screen or something like that.

Menus im actually looking into this myself, look at the xna website it actualy has an example of how to make some basic menus, i think the example is called GameStateManagement (i think).

Sound, with xna it comes with the tool XACT, its straightforward to use to get sounds in a game ( look up riemers xna tutorial, has a specific bit to do with sound in xna)

http://www.riemers.net/eng/Tutorials/XNA/Csharp/series2d.php

but not sure how you would control the volume in game.

Im sure some other people will be able to give better and more detailed answers, im still getting used to XNA. Good luck with the game its good that your wanting to do a finished product rather than just get the basics working and then move on to something else, it will help so much for future projects.


Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
In .net all types have a ToString() function.
Hm, if you're pointing out that the way to convert an integer to a string is to use ToString(), then yes, that's exactly what I said. If you're pointing out that all types derived from System.Object (not just integer types) have a ToString() method, then yes, that is of course true, but it doesn't contradict my statement in any way.

Anyway, sorry if I'm misunderstanding your post (if I am though, feel free to clarify).
You wrote "Although I can't tell you how to do it off the top of my head" and I'm pretty sure that guy misunderstood that to mean you were talking about ToString. I was a little confused as well at first, but I kept reading and realized you were pre-emptively saying you couldn't tell how to format a string.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dr1fter
I'm new to xna myself but hopefully i can be of some help.


Im sure some other people will be able to give better and more detailed answers, im still getting used to XNA. Good luck with the game its good that your wanting to do a finished product rather than just get the basics working and then move on to something else, it will help so much for future projects.


In my mind, if I can incorporate these features into Pong, then I'll be able to add them to future projects much easier due to previous experience.

And there's the possibility of the code being re-usable.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
I can't comment on the XNA-specific aspects of your question, but:
Quote:
Scoring System. I want to display the player scores onscreen. I figure the score would be an array, though I haven't had much experience with those.
Typically you would use an integer for the numerical representation of the score, and a string for the textual representation.

In C#, you can easily convert an integer to a string using the ToString() function. Although I can't tell you how to do it off the top of my head, I'm sure you can also format the string to your liking as well (e.g. by adding leading zeros to create a 'fixed-width' string).

I imagine when you mention the use of arrays, you're thinking of an array where each entry is one digit of the score's numerical value. Although you could do it this way, I'd go with using an integer and a string instead, as it's more straightforward (IMO).


I wasn't aware that I could use an integer and string. Would you mind elaborating on it?

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
You wrote "Although I can't tell you how to do it off the top of my head" and I'm pretty sure that guy misunderstood that to mean you were talking about ToString. I was a little confused as well at first, but I kept reading and realized you were pre-emptively saying you couldn't tell how to format a string.
Even if Fl4sh misread my statement as you described, how would the statement:
Quote:
In .net all types have a ToString() function.
Be relevant? Still doesn't really make much sense to me :|
Quote:
Original post by Slateboard
I wasn't aware that I could use an integer and string. Would you mind elaborating on it?
There's really not much more to it than what was described previously: store the score as an integer, and then convert it to a string for display.

Can you specify what you need clarification on?

Share this post


Link to post
Share on other sites
Quote:
Well, I'm not really familiar with the process itself. So an example would be appreciated.
You would just have an integer 'score' variable that you would adjust as necessary. As needed, you'd then convert the score to string form as follows (can't guarantee the syntax will be right, but it should be something like this):
string scoreText = score.ToString();
I'm not familiar with text rendering in XNA, but it looks like Dr1fter provided an example of how to do this earlier (I'm guessing that in his example, player1Score would correspond to scoreText in my example).

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
In C#, you can easily convert an integer to a string using the ToString() function. Although I can't tell you how to do it off the top of my head, I'm sure you can also format the string to your liking as well (e.g. by adding leading zeros to create a 'fixed-width' string).


It's actually pretty simple.

http://www.csharp-examples.net/string-format-double/
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Share this post


Link to post
Share on other sites
I have my scoring done.

With the info posted in this topic, in addition to this - http://msdn.microsoft.com/en-us/library/bb447673.aspx

Thanks for those who helped me.




Next step is the win condition, and Menu screens, etc.

Share this post


Link to post
Share on other sites
Ok, I think I know how to create a win condition.

But I don't know how it should affect the game itself.

Like, is there a way I can stop everything, and display a win message?

Also, how can I have my pong ball spawn in the middle of the screen, regardless of screen size?

Share this post


Link to post
Share on other sites
To spawn the pong ball at the center of the screen you need to store the current screen res...

so something like

int screen_W = 1680;
int screen_H = 1050;


then have your spawn function do this.

SpawnBall( ballImg , ( screen_W / 2 ) , ( screen_H / 2 ) );


And of course whenever the screen_w and screen_h change you will now draw in the middle of the screen.... Or really close.

Share this post


Link to post
Share on other sites
(Please bare in mind that I am still fairly new to XNA, so if there is a better / more efficient way to accomplish this, please disregard my advice ;) )

To elaborate more on what RanBlade described:

instead of creating two new integers, XNA already stores the size of the screen for you inside "graphics.GraphicsDevice.Viewport.Width" and "graphics.GraphicsDevice.Viewport.Height"

As for the win condition:

create a bool variable called hasWon, and a bool method called DetermineVictory(). Inside, have it test whether or not either players score is equal to the score To Win. Set hasWon to true or false based on the result, and have the method return true if either player has the score to win.

Then, inside your Update(), have a do/while loop that checks if DetermineVictory() is true or false. the loop will then continue until either condition is met. Afterwards, just display the appropriate message based on the outcome. in pseudo-code terms:


//game loop
do
{
//this is where your game logic will go
} while (DetermineVictory() == false)

if (hasWon == true)
{
//display victory screen
}
else if (hasWon == false)
{
//display defeat screen
}

Share this post


Link to post
Share on other sites
I want everything to stop once a player wins/loses and display a message in the middle.

How can I do that?

Share this post


Link to post
Share on other sites
Thats a state switch, I usually do stuff like that with function pointers, but thats in c, not c#.

you could do it like this tho->

if(gamedone==true)
{
draw game over on the screen
if(mouse clicked on play again) gamedone=false; //itll revert back to the game
//but youll probably have to reset variables.
}
else
{
play game
}

Share this post


Link to post
Share on other sites
Quote:
Original post by RanBlade
To spawn the pong ball at the center of the screen you need to store the current screen res...

so something like

int screen_W = 1680;
int screen_H = 1050;


then have your spawn function do this.

SpawnBall( ballImg , ( screen_W / 2 ) , ( screen_H / 2 ) );


And of course whenever the screen_w and screen_h change you will now draw in the middle of the screen.... Or really close.


This is probably a little more complex than needed. According to the XNA Best Practices you should set the screen resolution to 1280x720 as that will work on TV's and Windows. One thing you need to be conscious of though is making sure to draw in the Viewport.TitleSafeArea especially on a TV. I've used this practice and my game magically renders properly on monitors and SD/HD TV's (however an SD tv will show a black bar on top and bottom due to the screen ratio).

Typically what I've done is stored those in global constants so if I need to access them somewhere (in some kind of Render or Display class) I can access PongGame.SCREEN_SAFE and instantly have access to the rectangle used to draw into. Then center would be simple as PongGame.SCREEN_SAFE.Center.

You can set the resolution in your Initialize() method by doing:

graphics.PreferredBackBufferHeight = 720;
graphics.PreferredBackBufferWidth = 1280;
graphics.ApplyChanges();

Share this post


Link to post
Share on other sites
I changed the size, and I got the ball and paddle Y positions to be in the middle.

But I can't seem to do the same for the X Position of the paddles.

Is there a way to have the paddles positioned a specific distance away from the edges of the screen in percentages? Like having them set at a spot 1/10 of the distance from the left and right edges respectively?

Share this post


Link to post
Share on other sites
Quote:
Original post by Slateboard
I changed the size, and I got the ball and paddle Y positions to be in the middle.

But I can't seem to do the same for the X Position of the paddles.

Is there a way to have the paddles positioned a specific distance away from the edges of the screen in percentages? Like having them set at a spot 1/10 of the distance from the left and right edges respectively?


If you have access to the screen safe area you could do:

// For Left + 1/10
Paddle.Position.X = MyGame.SCREEN_SAFE.Left+(MyGame.SCREEN_SAFE.Width/10);

For right you would do:

// For Right - 1/10
Paddle.Position.X = MyGame.SCREEN_SAFE.Right-(MyGame.SCREEN_SAFE.Width/10);

Share this post


Link to post
Share on other sites
I downloaded this - http://creators.xna.com/en-us/samples/gamestatemanagement

I don't really know where to start, and what parts relate to my current goal.

Share this post


Link to post
Share on other sites
Quote:
Original post by Slateboard
It's starting to make a bit more sense, but I still find it confusing.


That's pretty complicated code for a Pong game. Instead of using their method, you could probably just use an enum to define a bunch of states, and use a switch statement to control which state you're currently in.

I.E.

enum GameStates
{
Playing,
Menu,
HighScore
}

switch ( gamestate )
{
case Menu:
//Do Menu Stuff

case Playing:
//Play the game!

case HighScore:
//View the high score screen
}

My syntax is probably wrong because I've gotten rusty on C#, but you get the idea.

Share this post


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

  • Advertisement