Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2006, 8:26 PM   #1
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 22
Rep Power: 0 lilmul123 is on a distinguished road
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.
lilmul123 is offline   Reply With Quote
Old Apr 27th, 2006, 9:06 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
One way would be to create a structure for the alien info. Something like:
struct Alien
{
  int x;
  int y;
};
and then declaring an Alien struct in your program :
Alien alien;
This will make it easier when you want to have more than one alien.

I don't know if you have done structures yet. If not, you could also just have
int alienX;
int alienY;
in your program, but that only allows for one alien and will get messy if you want to have more.

Then you can check for a hit using something like:
if (locationybullet==alien.y && locationxbullet==alien.x)
The Dark is offline   Reply With Quote
Old Apr 27th, 2006, 9:18 PM   #3
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 22
Rep Power: 0 lilmul123 is on a distinguished road
._. 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?
lilmul123 is offline   Reply With Quote
Old Apr 27th, 2006, 9:25 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
If you are using the Alien structure, the you can declare an array of aliens using
Alien aliens[10];
Where 10 gives the size of the array. You can then access the individual aliens using something like:
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.
The Dark is offline   Reply With Quote
Old Apr 27th, 2006, 9:27 PM   #5
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 22
Rep Power: 0 lilmul123 is on a distinguished road
i notice you use just an x point in your example. i would declare the y point also in the exact same way, correct?
lilmul123 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC