-
Pong - Simple question
I am trying to write a text based Pong game (I know this sounds very stupid, but...)
Here's what I came up with so far :
PHP Code:
#include <stdio.h>
#include <conio.h>
struct gamecomponent
{
int bufx, bufy;
int oldx, oldy;
int newx, newy;
int speed;
void resetcoords();
} ball, paddle;
void gamecomponent :: resetcoords()
{
oldx = bufx;
oldy = bufy;
bufx = newx;
bufy = newy;
}
void changecoordball()
{
int xadd, yadd;
if (ball.newx >= 80)
{
xadd = -ball.speed;
}
if (ball.newx <= 0)
{
xadd = ball.speed;
}
if (ball.newy >= 25)
{
yadd = -ball.speed;
}
if (ball.newy <= 0)
{
yadd = ball.speed;
}
ball.newx = ball.newx + xadd;
ball.newy = ball.newy + yadd;
}
void changecoordpaddle(int direction)
{
int xadd;
if (direction == 1)
{
xadd = ball.speed;
}
else
{
xadd = -ball.speed;
}
}
void initialize()
{
ball.bufx = 1;
ball.bufy = 1;
ball.newx = 1;
ball.newy = 1;
ball.oldx = 1;
ball.oldy = 1;
ball.speed = 1;
paddle.bufx = 1;
paddle.bufy = 25;
paddle.newx = 1;
paddle.newy = 25;
paddle.oldx = 1;
paddle.oldy = 25;
paddle.speed = 1;
}
void placeball()
{
gotoxy(ball.oldx, ball.oldy);
printf(" ");
gotoxy(ball.newx, ball.newy);
printf("*");
}
void placepaddle()
{
gotoxy(paddle.oldx, paddle.oldy)
printf(" ");
gotoxy(paddle.newx, paddle.newy)
printf("=======");
}
int checkbounce()
{
int bounceval = 1;
if (ball.newy == paddle.newy)
{
if (ball.newx < paddle.newx || ball.newx > (paddle.newx + 7))
{
bounceval = 0;
}
}
return bounceval;
}
main()
{
clrscr();
initialize();
while (!kbhit())
{
}
}
My question is :
How do I write a function in the main() which does the
following :
1. Run checkbounce function (when an 'if' condition occurs) I know how to do this one.
2. Run changecoordball() function every 1/100th of a second
3. Run Placeball function
4. Sense the input from the user (arrow keys) and
pass 1 to the changecoordpaddle() function when he/she presses right arrow key and 0, if he/she presses left arrow key
5. Run placepaddle function
I want all these 5 steps to be taken care of by 1 function.
Can anyone help me out ? I am new to this.
HOMEPAGE @ www.akilla.tk
-
oh
oh, and also, changecoordpaddle function should be:
PHP Code:
void changecoordpaddle(int direction)
{
int delx;
if (direction == 1)
{
delx = paddle.speed;
}
else
{
delx = -paddle.speed;
}
paddle.newx = paddle.newx + delx;
}
Also, the 5 steps function should also run
ball.resetcoords() and paddle.resetcoords()
So there is a total of 7 things that the function should do.
Please help me as to how I can write this function.
HOMEPAGE @ www.akilla.tk
-
k i'll try to help you out since no one else has tried...
1. you already know how to do...
2. maybe sleep function sleep(time in milliseconds);
3. just run it (not sure what the problem is?)
4. check out the link in my sig for that one look at the code for dungeons of moria, more specifically look at my_getch() function (in .c file) and look at keys.h file.
try these for now and if you need more help post your questions in this thread, i'll try to help you out the best i can.
-
here..
This is the latest code:
PHP Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
struct gamecomponent
{
int bufx, bufy;
int oldx, oldy;
int newx, newy;
int xspeed, yspeed;
void resetcoords();
} ball, paddle;
void gamecomponent :: resetcoords()
{
oldx = bufx;
oldy = bufy;
bufx = newx;
bufy = newy;
}
void changecoordball()
{
if (ball.newy == paddle.newy)
{
if (ball.newx >= paddle.newx && ball.newx <= (paddle.newx + 3))
{
if (ball.xspeed > 0)
{
ball.xspeed = -ball.xspeed;
}
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed;
}
}
if (ball.newx > (paddle.newx + 3) && ball.newx <= (paddle.newx + 7))
{
if (ball.xspeed < 0)
{
ball.xspeed = -ball.xspeed;
}
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed;
}
}
}
if (ball.newx >= 80)
{
if (ball.xspeed > 0)
{
ball.xspeed = -ball.xspeed;
}
}
if (ball.newx <= 0)
{
if (ball.xspeed < 0)
{
ball.xspeed = -ball.xspeed;
}
}
if (ball.newy >= 25)
{
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed;
}
}
if (ball.newy <= 0)
{
if (ball.yspeed < 0)
{
ball.yspeed = -ball.yspeed;
}
}
ball.newx = ball.newx + ball.xspeed;
ball.newy = ball.newy + ball.yspeed;
}
void changecoordpaddle(int direction)
{
int delx;
if (direction == 1)
{
delx = paddle.xspeed;
}
else
{
delx = -paddle.xspeed;
}
paddle.newx = paddle.newx + delx;
}
void initialize()
{
ball.bufx = 1;
ball.bufy = 1;
ball.newx = 1;
ball.newy = 1;
ball.oldx = 1;
ball.oldy = 1;
ball.xspeed = 1;
ball.yspeed = 1;
paddle.bufx = 1;
paddle.bufy = 25;
paddle.newx = 1;
paddle.newy = 25;
paddle.oldx = 1;
paddle.oldy = 25;
paddle.xspeed = 1;
paddle.yspeed = 1;
}
void placeball()
{
gotoxy(ball.oldx, ball.oldy);
printf(" ");
gotoxy(ball.newx, ball.newy);
printf("*");
}
void placepaddle()
{
gotoxy(paddle.oldx, paddle.oldy);
printf(" ");
gotoxy(paddle.newx, paddle.newy);
printf("=======");
}
int checkbounce()
{
int bounceval = 1;
if (ball.newy == paddle.newy)
{
if (ball.newx < paddle.newx || ball.newx > (paddle.newx + 7))
{
bounceval = 0;
}
}
return bounceval;
}
main()
{
clrscr();
initialize();
while (1 == 1)
{
changecoordball();
ball.resetcoords();
placeball();
delay(100);
while (kbhit())
{
return 0;
}
}
}
However, it is leaving trails at the left and the top of the screen.
It has something to do with if conditions (for left and top boundaries) but I couldn't find where.
The movement of the ball is a little unnatural when at the time of bouncing on the borderes.
I want to make the cursor dissappear.
-
k change the bounds for the ball from 80 and 25 to 79 and 24 because when the ball is printed on the edge it goes to a new line hence you see extra balls printed, that should fix the bounds.
to make the cursor disapear i only know how with the windows.h header add this function
Code:
void cursor (int yn)
{
CONSOLE_CURSOR_INFO c;
c.dwSize = 1;
c.bVisible = yn;
SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );
}
and call cursor(0); in main to make it disappear, use a 1 to make it appear again.
lastly main must be declared int main its the proper style.
-
Problem
Your solution worked fine.
I have TC++ 3 and Borland C++ 5.5
I dont have windows.h in TC++ 3, so I used
borland c++ 5.5 to compile it.
It doesn't recognize the "delay" command.
But when I used "sleep" it is going either very
fast (for numbers below 1) and very slow (for numbers
over 1)
Is there another delay comand in borland c++ 5.5 ?
here's my code as of now:
PHP Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <dos.h>
struct gamecomponent
{
int bufx, bufy;
int oldx, oldy;
int newx, newy;
int xspeed, yspeed;
void resetcoords();
} ball, paddle;
void gamecomponent :: resetcoords()
{
oldx = bufx;
oldy = bufy;
bufx = newx;
bufy = newy;
}
void changecoordball()
{
if (ball.newy == paddle.newy)
{
if (ball.newx >= paddle.newx && ball.newx <= (paddle.newx + 3))
{
if (ball.xspeed > 0)
{
ball.xspeed = -ball.xspeed;
}
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed;
}
}
if (ball.newx > (paddle.newx + 3) && ball.newx <= (paddle.newx + 7))
{
if (ball.xspeed < 0)
{
ball.xspeed = -ball.xspeed;
}
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed;
}
}
}
if (ball.newx >= 79)
{
if (ball.xspeed > 0)
{
ball.xspeed = -ball.xspeed;
}
}
if (ball.newx <= 1)
{
if (ball.xspeed < 0)
{
ball.xspeed = -ball.xspeed;
}
}
if (ball.newy >= 24)
{
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed;
}
}
if (ball.newy <= 1)
{
if (ball.yspeed < 0)
{
ball.yspeed = -ball.yspeed;
}
}
ball.newx = ball.newx + ball.xspeed;
ball.newy = ball.newy + ball.yspeed;
}
void changecoordpaddle(int direction)
{
int delx;
if (direction == 1)
{
delx = paddle.xspeed;
}
else
{
delx = -paddle.xspeed;
}
paddle.newx = paddle.newx + delx;
}
void initialize()
{
ball.bufx = 1;
ball.bufy = 1;
ball.newx = 1;
ball.newy = 1;
ball.oldx = 1;
ball.oldy = 1;
ball.xspeed = 1;
ball.yspeed = 1;
paddle.bufx = 1;
paddle.bufy = 25;
paddle.newx = 1;
paddle.newy = 25;
paddle.oldx = 1;
paddle.oldy = 25;
paddle.xspeed = 1;
paddle.yspeed = 1;
}
void placeball()
{
gotoxy(ball.oldx, ball.oldy);
printf(" ");
gotoxy(ball.newx, ball.newy);
printf("*");
}
void placepaddle()
{
gotoxy(paddle.oldx, paddle.oldy);
printf(" ");
gotoxy(paddle.newx, paddle.newy);
printf("=======");
}
int checkbounce()
{
int bounceval = 1;
if (ball.newy == paddle.newy)
{
if (ball.newx < paddle.newx || ball.newx > (paddle.newx + 7))
{
bounceval = 0;
}
}
return bounceval;
}
void cursor (int yn)
{
CONSOLE_CURSOR_INFO c;
c.dwSize = 1;
c.bVisible = yn;
SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );
}
main()
{
clrscr();
initialize();
cursor(0);
while (1 == 1)
{
changecoordball();
ball.resetcoords();
placeball();
sleep(.5);
while (kbhit())
{
cursor(1);
return 0;
}
}
}
Also, I want to do the following
When user presses right or left arrow, it should run
changecoordpaddle, and pass 1 to the function if it is
right arrow and 0 if it is left arrow.
it should run paddle.resetcoords() immediately after that.
Thanks for your help!
SEE MY HOMEPAGE @ www.akilla.tk
-
Code:
void my_delay ( int milli )
{
time_t end, now = clock();
for ( end = now + milli; now < end; now = clock() )
continue;
}
try that
and for the keys check out my game (dungeons of moria) www.geocities.com/golsafidi i can't give you all the answers :D
-
Thanks a lot
Thanks a lot for the delay command..
I've seen your game...
but i only got a slight picture of what was happening.
how does KEY_LEFT work directly ? is it defined anywhere ?
Is there a simpler code that recognizes 'ESC' and arrow keys ?
-
look in keys.h and at my_getch() function thats all you need.
basically call like this
input=my_getch();
if(input==K_LEFT)
0;
else if (input==K_RIGHT)
1;
dont forget to look in keys.h! :p
-
hey
Hey..
THANKS VERY MUCH!!!!
I figured out a way to use kbhit and getch functions
to get the values..
I had several problems with it and i could fix it by kbhit..
I pasted all the code in C++ forum.
Please play it and post your comments there..
Once again, thanks very much for your patience.
MY HOMEPAGE @ www.akilla.tk