Pong in C# w/ XNA

Started by
28 comments, last by Bromordra 13 years, 6 months ago
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).
Advertisement
Thanks. I understand it now.

I've also edited my original post.
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
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.
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?
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.

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

(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 loopdo{//this is where your game logic will go} while (DetermineVictory() == false)if (hasWon == true){//display victory screen}else if (hasWon == false){//display defeat screen}
I want everything to stop once a player wins/loses and display a message in the middle.

How can I do that?
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
}
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();

=============================RhinoXNA - Easily start building 2D games in XNA!Projects

This topic is closed to new replies.

Advertisement