![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
a question about making this stop
ok, i'm almost done with my space invaders program, just one big detail i need to iron out. i want the "I" in my program (which is the weapon) to stop when it hits an alien. in the following code, you'll see as of yet I don't have any aliens, but i've tried many things. I've tried putting the aliens in an array, and then changing the for loop to say "locationybullet<=2||locationybullet==alien[0]" for example, but that didn't work. then i realized that i need to give the alien an xy location on the screen, but i dont know how to do this. can anyone help me on what i should do?
#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <iostream.h>
int main()
{
char firstchoice,a,dir,begin,length;
int locationx,locationy,same,score,locationxbullet,locationybullet;
score=0;
_setcursortype(_NOCURSOR);
textcolor(YELLOW);
textbackground(BLUE);
locationx=40;
locationy=24;
locationxbullet=40;
locationybullet=23;
{
clrscr();
gotoxy(19,12);
cout << "Welcome to Steve's Really Crappy Space Invaders Clone!";
gotoxy(19,13);
cout << "Press H for help.";
gotoxy(19,14);
cout << "Otherwise, Press any other key to continue.";
firstchoice=getch();
}
if(firstchoice == 'h')
{
clrscr();
cout << "You're a rogue space pilot on a mission to destroy other ASCII characters.\nYour score goes up by one by just shooting!\nThis game is impossible to lose.\nUse 4 to move left and 6 to move right.\nUse 6 to shoot.\nYou win when all the other ASCII characters are dead.\nGOOD LUCK!";
getch();
}
else
{
clrscr();
for(a=1;a<=79;a++)
{
gotoxy(a,1);printf("X");
gotoxy(a,25);printf("X");
}
for(a=2;a<=24;a++)
{
gotoxy(1,a);printf("X");
gotoxy(79,a);printf("X");
}
{
gotoxy(10,1);
printf("Score:0");
}
do
{
if(kbhit() !=0)
{
dir=getch();
if(dir=='4')
{
gotoxy(locationx,locationy);
printf(" ^_^ ");
locationx--;
locationxbullet--;
}
if(dir=='6')
{
gotoxy(locationx,locationy);
printf(" ^_^ ");
locationx++;
locationxbullet++;
}
if(dir=='8')
{
for(locationybullet=22;locationybullet>=2;locationybullet--)
{
delay(100);
gotoxy(locationxbullet,locationybullet);
printf(" I ");
gotoxy(locationxbullet,locationybullet+1);
printf(" ");
}
{
score++;
gotoxy(10,1);
cout << "Score:" << score;
}
}
if(score==50)
{
clrscr();
gotoxy(10,10);
printf("YOU WIN!!! NICE FREAKING JOB.");
gotoxy(10,11);
printf("Press X to exit.");
getch();
}
}
}while(dir!='x');
}
}BTW, you guys have been a real help to me. my computer teacher is impressed on how fast im learning this stuff. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
One way would be to create a structure for the alien info. Something like:
struct Alien
{
int x;
int y;
};Alien alien; I don't know if you have done structures yet. If not, you could also just have int alienX; int alienY; Then you can check for a hit using something like: if (locationybullet==alien.y && locationxbullet==alien.x) |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
._. sadly, i have yet to do structures, but this intrigues me. can you go further into detail about what i should do? maybe ill get it
edit: ok, i think i understand now. but how do i declare mutliple aliens? i know i need to use an array, but how exactly do I declare that? |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
If you are using the Alien structure, the you can declare an array of aliens using
Alien aliens[10]; aliens[5].x = 40; The part inside the square brackets tells which alien to acces. This starts from zero. The numbers in this case go from 0 to 9. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
i notice you use just an x point in your example. i would declare the y point also in the exact same way, correct?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|