Thread: Space Combat Ready

  1. #1
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    Space Combat Ready

    Here's the Space Combat game..

    (This is not a fancy multi chance game... it's just
    to test myself whether i can write programs that
    suck or not )

    there's a small glitch with the missile hitting the target
    but no matter how many times i look at the code, it
    looks innocent.

    tell me your commons/opnions.

    CONTROLS :

    1) ARROW KEYS -- LEFT/RIGHT
    2) SPACE BAR -- SHOOT
    3) ESC -- QUIT

    (in one way, this game is similar to Galaga/Galaxian... but there's a lot of differnce)

    EDIT: Latest file uploaded
    EDIT: Game Renamed to "Dragon War"
    Last edited by moonwalker; 07-29-2002 at 06:37 PM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    that's a cool game!

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Hey, I gotta admit... that's pretty cool for being text!

    I say continue to develope it!

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    yeah

    i am changing this to a multi dragon, multi chance game...

    but i still can't figure out that small problem with missile
    hitting the target..

    but so far, is it really interesting ?

  5. #5
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    that's a pretty good game for being text. good job. you should make it that you can shoot multiple bullets at the sametime, though, instead of only being able to have one bullet shot at a time. for that you would see if each bullet exists and use for statements.

    Code:
    //when the space button is pressed this is to determine if a bullet and what bullet should be created
    if(key == space)   //if the space bar is pressed
     {
      for(int i = 0; i < max_bullets; i++)  //run the until the max amount of bullets has been reached
       {
         if(bullets[i].exists == 0)  //if a bullet doesn't exists then create a new one
          {
           bullets[i].exists = 1;  //this is iniitialzing the bullet
           bullet[i].x = ship.x;   //this is setting the bullets equal to the ships x and y coordinates
           bullet[i].y = ship.y;
           break;  //this is ending the for loop so no more bullets are created
          }
        }
       }
    
    //this is for redrawing the bullets
    for(int i = 0; i < max_bullets; i++)
     {
       if(bullet[i].exists == 1)
        {
         draw_bullet;  //this is where you would insert all of the code for drawing your bullets...the cout and finding the coordinates and such...
        }
      }
    
    //basically you're going to use the same routines as before though
    And if I knew what the glitch was for the missle hitting the target I might be able to help you. Is the glitch where the missle and target doesn't disappear when you hit it? Not to mention that I would need to see your source code for it.

  6. #6
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    yeah..

    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.
    Last edited by moonwalker; 07-28-2002 at 01:20 PM.

  7. #7
    I never seen anybody do that with ASCII, pretty nice.

    It would be neat if it used the whole screen, and it had an AI. Also more enemies.

  8. #8
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    thanks

    oh thanks, i am flattered

    it's my 2nd game (after Text Pong) and it's a celebration
    of 2 months of learning in C

  9. #9
    no offence, but it does look like a two month learned program

  10. #10
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Well, I can't narrow the problem down to one exact thing, but I think I might have an idea as to what the problem is.

    When the missle is moving up it's changin it's coordinates and it equals the same as the target but then the target changes it's coordinates. So what's happening is that the missle and target equal each others coordinates at a certain point in time but it's not the time when the coordinates are being checked to see if they actually do each other. I'm not sure if this is even possible with your code (I didn't look at it extensively).

    Code:
    //missle up
    missle_y--;
    //missle_y = 25;
    
    //missle_y == target_y
    
    //target_y down
    targe_y++;
    //target_y = 26;
    
    check_for_hit()
     {
      if(target_y == missle_y)
      //targe_y != missle_y anymore
      }
    I doubt that is the problem, but it's worth looking into.

  11. #11
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Good job, I could not do that after two months.

  12. #12
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    FINISHED!

    When the missle is moving up it's changin it's coordinates and it equals the same as the target but then the target changes it's coordinates. So what's happening is that the missle and target equal each others coordinates at a certain point in time but it's not the time when the coordinates are being checked to see if they actually do each other.
    You're right. I doubted this yesterday. But I couldn't find the right place to put the checktargethit() function...

    I placed it in placemissile() function itself and now it works.
    I am uploading the latest version..

    I am also going to increase the number of missiles allowed to 2 at a time... and i will include the lives option.
    no offence, but it does look like a two month learned program
    very encouraging comment.
    Last edited by moonwalker; 07-29-2002 at 06:37 PM.

  13. #13
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    keep working on it; it looks pretty good right now
    Away.

  14. #14
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    upgrading problem

    i've got some technichal .......... going on with my code right now

    I just realised that I should have put a scope for upgrading the the features..
    right now, the functions only work for the things that are in there... it cant be upgraded easily..

    so i am going to have to make structs, etc and some other functions that take care of more than 1 missile, dragon, etc..

    basically, i am going to change the game almost completely, except for those graphics and concepts that you've seen.

  15. #15
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    sounds like you'll be making some improvements to the game...but I must say that thinking about what you are going to do for the entire game and setting up your data structures before you start is a good way to avoid writing a bunch of code that you then delete.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. First phases complete for space combat
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-22-2009, 09:42 AM
  3. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-06-2009, 12:54 AM
  4. Space Combat (50% Completed Program)
    By moonwalker in forum Game Programming
    Replies: 2
    Last Post: 07-26-2002, 08:59 AM