thanks for the reply
well, I purposefully made sure that only 1 bullet is seen on the screen (I am going to increase it to 2 or 3 at max) .. but that's the next job.. i will also have to change the number of lives, etc which i am going to do later..
right now, the problem is with hitting the target ... it's detecting the hit only sometimes, but not always...
here's the code:
Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
struct gamecomponent
{
int bufx, bufy;
int oldx, oldy;
int newx, newy;
int xspeed, yspeed;
int type, animate;
int capture;
void resetcoords();
} dragon, spaceship, missile;
void gamecomponent :: resetcoords()
{
oldx = bufx;
oldy = bufy;
bufx = newx;
bufy = newy;
}
void cursor (int yn)
{
CONSOLE_CURSOR_INFO c;
c.dwSize = 1;
c.bVisible = yn;
SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );
}
void my_delay (int milliseconds)
{
time_t end, now = clock();
for ( end = now + milliseconds; now < end; now = clock() )
continue;
}
void waitforkeypress()
{
fflush(stdin);
while (!kbhit())
{
}
fflush(stdin);
}
void showtitle()
{
my_delay(1000);
clrscr();
gotoxy(1, 24);
printf("\nProgrammed by Mahurshi Akilla\nhttp://mahurshi.tripod.com\n");
waitforkeypress();
cursor(1);
exit(1);
}
void saycaptured()
{
gotoxy(30,13);
printf("Spaceship Captured");
gotoxy(35,14);
printf("Game Over");
cursor(0);
waitforkeypress();
showtitle();
}
void saydestroyed()
{
gotoxy(30,12);
printf("Mission Accomplished");
gotoxy(33,13);
printf("Return To Base");
cursor(0);
waitforkeypress();
showtitle();
}
void checktargethit()
{
if (missile.newy == dragon.newy)
{
if (missile.newx >= dragon.newx && missile.newx <= dragon.newx + 5)
{
saydestroyed();
}
}
}
void placedragon(int type)
{
gotoxy(dragon.oldx, dragon.oldy);
printf(" ");
gotoxy(dragon.newx, dragon.newy);
if (dragon.animate == 1)
{
if (type == 1)
{
printf("(^x^)");
}
else if (type == 2)
{
printf("/^x^\\");
}
}
else if (dragon.animate == 0)
{
clrscr();
gotoxy(dragon.newx, dragon.newy);
printf("/^x^\\");
gotoxy(dragon.newx, dragon.newy + 1);
printf("/=^=\\");
}
}
void changecoordmissile()
{
if (missile.newy >= 2 )
{
missile.newy = missile.newy - 1;
}
else if (missile.newy < 2)
{
missile.animate = 0;
}
}
void placespaceship()
{
gotoxy(spaceship.oldx, spaceship.oldy);
printf(" ");
gotoxy(spaceship.newx, spaceship.newy);
printf("/=^=\\");
}
void placemissile()
{
gotoxy(missile.oldx, missile.oldy);
printf(" ");
gotoxy(missile.newx, missile.newy);
printf("|");
if (missile.animate == 0)
{
gotoxy(missile.newx, missile.newy);
printf(" ");
}
}
void initialize()
{
int tempposition = 1 + rand()%40;
dragon.bufx = tempposition;
dragon.bufy = 1;
dragon.newx = tempposition;
dragon.newy = 1;
dragon.oldx = tempposition;
dragon.oldy = 1;
dragon.xspeed = 1;
dragon.yspeed = 1;
dragon.type = 1;
dragon.capture = 0;
dragon.animate = 1;
spaceship.bufx = 38;
spaceship.bufy = 25;
spaceship.newx = 38;
spaceship.newy = 25;
spaceship.oldx = 38;
spaceship.oldy = 25;
spaceship.xspeed = 2;
spaceship.yspeed = 1;
spaceship.type = 1;
spaceship.capture = 0;
spaceship.animate = 1;
missile.bufx = 38;
missile.bufy = 25;
missile.newx = 38;
missile.newy = 25;
missile.oldx = 38;
missile.oldy = 25;
missile.xspeed = 2;
missile.yspeed = 1;
missile.type = 1;
missile.capture = 0;
missile.animate = 0;
}
void missilelaunch()
{
missile.animate = 1;
missile.newx = spaceship.newx + 2;
missile.newy = spaceship.newy;
missile.oldx = missile.newx;
missile.oldy = missile.newy;
}
void changecoorddragon()
{
if (dragon.newx +5 >= 80)
{
if (dragon.xspeed > 0)
{
dragon.xspeed = -dragon.xspeed;
}
}
if (dragon.newx <= 1)
{
if (dragon.xspeed < 0)
{
dragon.xspeed = -dragon.xspeed;
}
}
if (dragon.newy <= 1)
{
if (dragon.yspeed < 0)
{
dragon.yspeed = -dragon.yspeed;
}
}
if (dragon.newy >= 25)
{
if (dragon.yspeed > 0)
{
dragon.yspeed = -dragon.yspeed;
}
}
dragon.newx = dragon.newx + dragon.xspeed;
dragon.newy = dragon.newy + dragon.yspeed;
if (spaceship.capture == 1)
{
spaceship.newy = dragon.newy + 1;
spaceship.newx = dragon.newx;
if (spaceship.newy == 2)
{
spaceship.newy = 1;
saycaptured();
}
}
}
void changecoordspaceship(int direction)
{
int delx;
if (direction == 1)
{
delx = spaceship.xspeed;
}
else
{
delx = -spaceship.xspeed;
}
if (spaceship.newx + delx >= 1 && spaceship.newx + delx + 5 <= 80)
{
spaceship.newx = spaceship.newx + delx;
}
}
void checkcapture()
{
if (dragon.newy == spaceship.newy - 1)
{
if ( (spaceship.newx + 5) >= dragon.newx && (spaceship.newx + 5) <= (dragon.newx + 5) )
{
spaceship.capture = 1;
dragon.animate = 0;
}
else if ( spaceship.newx >= dragon.newx && spaceship.newx <= (dragon.newx + 5) )
{
spaceship.capture = 1;
}
}
}
void animate()
{
my_delay(100);
placespaceship();
if (dragon.type == 1)
{
dragon.type = 2;
changecoorddragon();
dragon.resetcoords();
}
else if (dragon.type == 2)
{
dragon.type = 1;
changecoorddragon();
dragon.resetcoords();
}
gotoxy(dragon.newx, dragon.newy);
placedragon(dragon.type);
if (missile.animate == 1)
{
checktargethit();
changecoordmissile();
missile.resetcoords();
placemissile();
}
checkcapture();
}
int getpressedkey()
{
char c;
c = getch();
switch ((int)c)
{
case 27:
fflush(stdin);
return 2;
case 32:
fflush(stdin);
return 3;
case 75:
fflush(stdin);
return 0;
case 77:
fflush(stdin);
return 1;
}
return -1;
}
void checkkey()
{
char pressedkey;
while (kbhit() && spaceship.capture == 0)
{
pressedkey = getpressedkey();
if (pressedkey == 0 || pressedkey == 1)
{
changecoordspaceship(pressedkey);
spaceship.resetcoords();
placespaceship();
}
else if (pressedkey == 2)
{
waitforkeypress();
showtitle();
}
else if (pressedkey == 3)
{
if (missile.animate == 0)
{
missilelaunch();
}
}
}
}
main()
{
clrscr();
randomize();
initialize();
while (1)
{
animate();
checkkey();
}
}
i think the problem is probably with the checktargethit() function...
in animate() function, i said
if (missile.animate == 1) , then run checktargethit()
i.e., if the missile is alive (or 'exists,' then it should check if it hit the target)
the code looks pretty perfect to me... i ran over it a hundred times but i couldn't locate the culprit...
any help is greatly appreciated!
thanks for your time.