Archived

This topic is now archived and is closed to further replies.

C#: text-based pong

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

So I''m sitting here in C#, right? I''m minding my own business. Okay here''s the deal: Listen, I want to make a text-based pong game. I have my own ideas about the paddles and user input, but I am unsure about ball movement. I want to set up a (invisible to the user) grid/room for the ball to move in. Let''s say the room is 10x10 "units" in size. First of all, I don''t even know what a "unit" could be; in other words, how to represent this grid in memory, in relation to the context of the game. Any ideas? Secondly, when the ball hits a wall (four total), I want it to bounce off of it in a physically realistic way, as it would in real life. Any ideas on how to make a ball move in a virtual environment that the user cannot see? In all cases, if a ball hits a surface, will it reflect off at the same angle it hit, or no? I imagine I will need the computer to keep track of the position of the ball, the speed, and the direction... does that seem right? (A vector is a direction, correct?) Based on this data, I want the ball to move in the invisible arena. Then I want the computer to output data that will help the players decide how to move... in short. So, all in all... how might I do this without the user actually seeing anything but text? No graphics, just realistic math. How might you do it? I just want a few going ideas to run on. Thanks all for your help.

Share this post


Link to post
Share on other sites
One part of this i have an idea for would be to have a much more detailed virtual playing field for the ball compared to the one u would display to the screen. That way u could accurately work out the maths behind the ball movement. Then u would have to plot the ball form the virtual grid to the character that it wouls lie nearest on the screen.



regards,

Ace

/*
~ Programming is creating problems then solving them ~

Level :- Beginner Intermediate
Currently Doing :- Background work on smart pointers, will then update 3D MineSweeper.
*/

Share this post


Link to post
Share on other sites
Say you can only display 80 characters x 40 characters. This is your "screen space".

You represent the ball movement in a "game space" of 160 x 80 units, maybe for convenience so you don''t have fractional movement velocities or something.

Well, if this was the case it would be obvious how the translation from game space to screen space would be - divide by 2. Apply this to whatever you decide and you are good to go.

technic

Share this post


Link to post
Share on other sites
How am I going to create my virtual arena? An array of sorts? A 10x10 array... of what... or what type? This was my main source of confusion.

How will I deal with timing?
> I would enjoy knowing about high precision timing in C#.

[edited by - jim bob on May 20, 2004 6:41:43 PM]

Share this post


Link to post
Share on other sites
This is kind of ridiculous. I fail to see how no one in the world has the slightest idea of what they could say to point me in the right direction.

More replies go to less serious crap than my dumb question. It''s kind of sad. Sorry..

But thanks if anyone can help, again.

Share this post


Link to post
Share on other sites
I fail to see the point of making a text based pong game, other than to say "I made a text based pong game".

I am sure c# has an easy to use graphics libary or way to draw simple objects on the screen.

I saw a text based pong clone made in assembly but I dont have to source or the link with me right now.

I really feel it would be better to learn another api to put graphics on the screen.

But in the end its up to you I guess.




gdipong

oglpong Online (43% complete)


If you got any requests or suggestions post here

Share this post


Link to post
Share on other sites
If you use the console, how are you going to read user input in real-time and redraw the screen? By printing 40 lines or whatever every time you wish to repaint? I don't see how you can do it without using some special DOS API, which defeats your original purpose of making the program simple. Try GDI, it should take only a few lines to draw the things you want.

Also, you should not use any sort of grid representation for the field; just keep track of the ball location (x,y) using, say, floats, and of the players' paddle locations, and then cast to ints to get the row/column location of the character/pixel where to draw the ball.

To update the ball location and take care of bounces, do the following (assuming x,y is the ball position and vx, vy is the ball velocity):

x += vx;
if(x < 0) { // hit left wall

x = 0; vx = -vx;
}
else if(x > fieldWidth) { // hit right wall

x = fieldWidth; vx = -vx;
}

y += vy;
if(y < 0) { // hit top wall

y = 0; vy = -vy;
}
else if(y > fieldHeight) { // hit bottom wall

y = fieldHeight; vy = -vy;
}

Note that this does perfectly accurate math, since the walls are vertical and horizontal. Try drawing a diagram to see how negating vx and vy causes the ball to bounce off the horizontal and vertical walls respectively.

Of course, you will have to be chaning vx and vy at some times to simulate the physics; whenever the ball hits a paddle, change vx to -vx (assuming your paddles are horizontal; change vy if they're vertical), to make the ball bounce off the paddle. You can also slow down the ball to simulate friction by decreasing vx and vy towards 0 slightly each frame, and by increasing them when a paddle is hit to simulate force from the user. (You can even include the velocity of the paddle in determining the new velocity of the ball).

[edited by - Matei on May 21, 2004 4:57:45 PM]

[edited by - Matei on May 21, 2004 4:58:18 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Matei


That post helped greatly, thank you for your time!

I know this sounds silly, but I wanted to try this more as a pong simulation, not a actually user-based interactive game. My ideas are odd and hard to explain, sorry.

Anyways, thank you much! I appreciate it.

[edited by - jim bob on May 21, 2004 5:35:00 PM]

Share this post


Link to post
Share on other sites