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