Pong in C# w/ XNA

Started by
28 comments, last by Bromordra 13 years, 6 months ago
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?

Advertisement
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);

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

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.
It's starting to make a bit more sense, but I still find it confusing.
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.

Quote:Original post by Kalnos
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.


So where exactly would I implement this into my code? Would it go in the Game.cs file, or somewhere else?
I usually put it in my Game1.cs however I consider it like my 'controller' class that basically controls what needs to get called and when based on the current state. Otherwise I would suggest a seperate controller class that has all that implementation logic in it but I haven't done this yet so I'm curious how you would tie it together with the Game1.cs cleanly.

Either way, whichever class you put it in, you aren't going to want any other code in there. Usually only a few states can even lead to quite a large class using switch...cases for each state.

I also do a switch...case even though I'm not a big fan of them. Usually I have 2 seperate switch statements, one in the Update() and one in the Render() as I typically need my state to control what's updated and what's rendered.

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

Tiny Update. Finally getting around to putting this all together. So far it's easier than I originally assumed.

Gonna update the first post with new stuff.
Is it at all viable to reorganize my code mid-project?

I'm sure everything would be a lot easier to manage if I divided it all into classes, but I'm not too good with that sort of thing. Perhaps someone can direct me to an article or tutorial on it?
Just wanted to let you know that I've actually done exactly all of the things you covered(except backgrounds) in my Pong game and it's all excessively easy actually, especially in XNA. You should be more than capable of handling it.

And yeah, Classes are basically mandatory for C# as that's kind of the foundational paradigm for the language. Also, everything becomes much more clear and relationships between data become much more obvious and manageable once you have your code split up as such. Also what the guy said about enums for your game states is exactly what I did, though I used If statements instead of a switch, though the switch makes more sense now that I think about it, and would just look much cleaner.

Good luck with your project.

This topic is closed to new replies.

Advertisement