Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Space Shooter!

Here are some projects of actual programs you can practice making.

Space Shooter!

Postby Alfaris on Sat Nov 15, 2008 12:35 am

Heyyy :)

OK so about a year ago I had started to learn programming on my own, but this year I signed up for 'Computer Programming 1A' at school and all I have to say is...I won't really have anything to do for the rest of the year (lol)

So anyways here's why that was important. With all the free time I've had in that class I've been messing around with C++ and making a couple of projects, two of which I called 'Space Shooter' and 'Base Converter' and I thought it would be fun to see different people's methods of making them. In this post I will explain 'Space Shooter' and provide a link to download my example.

I am not going to post my code for them because it is pretty long and messy, so I'd rather spare you guys the trouble of trying to understand my poor organization. If anyone wants to see it, though, I'd be glad to post it. Also, I'm sorry if I'm too vague...I'm reaallly introverted so I tend to assume everyone knows what I'm thinking...do not hesitate to ask me if you have any questions.

Now on to 'Space Shooter':

REQUIREMENTS:
Fundamentals of C++, at least until structs and classes I'd say. I've seen those in the videos by antiRTFM so watch them if you don't know. Also, being able to make your own custom hearer would help with organization, but it is not necessary at all.

HEADERS THAT WOULD BE USEFUL:
<iostream>
<conio.h>
// I do not know if <conio.h> has been covered in the videos...but basically all you *might* need from it is getch().
// Of course, it all depends on how you do this project so it is all up to you what to use.

GAME BOARD/ARENA SETUP:
1. The way I did it is I made a 25x80 matrix, which would be the standard dimensions for a console application.
2. HINT: You cannot visibly print out a null character ('') so making the space (' ') your default character for the matrix might help you.

MOVEMENT:
1. Space Shooter must allow the user to be able to move around the screen.
2. EXTRA: add diagonal movement (at one press of two keys)
3. ALTERNATIVE: add four extra keys for diagonal movement instead of a combination of two.


SHOOTING:
1. The player should be able to shoot 'bullets' out of his space ship (or whatever your vessel happens to be, mine was a smiley face).[/b]
2. HINT: It would help if you created two separate functions:
a) One function to set the shot.
b) One function to shoot the shot forward.
3. EXTRA: Turrets (automatic shooters) could be set up by the player. In my case they shot every time the player would shoot, but you
could make them shoot completely independent of the player.


ENEMY:
1. There should be enemies on the screen that you can shoot.
2. A score system would be ideal here.
3. EXTRA: give the enemy/enemies moving abilities.
4. EXTRA: program a 'super enemy' that has artificial intelligence and is able to dodge bullets.
5. EXTRA: give the enemy a 'search&destroy' attitude and/or the ability to shoot in special circumstances.



If you are new to C++ this will probably be an unbeatable challenge to you, unless you have a mentor that could help you every step of the way. For those that think they have an idea on how to do this, I encourage you to do so and I hope you will post it here when you are finished. For the others who are completely stumped, do not bother yourself with this and go practice a simpler project or learn more C++ until you think you are ready. I, myself, am still somewhat of a 'n00b' to C++ but I think this is a good idea for people to practice making a project larger than a simple program.

Here is the link to my example: http://rapidshare.com/files/163901238/Space_Shooter.rar

*Code not included unless requested. I'd rather you guys do this on your own though.
Alfaris
 
Posts: 4
Joined: Mon May 19, 2008 12:21 am

Re: Space Shooter!

Postby NOOB on Tue Feb 03, 2009 11:18 am

Code: Select all
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>

using namespace std;


void gotoxy (int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int GetRand(int low, int high)
{
    return (int)(rand() / (RAND_MAX + 1.0) * (high - low) + low);
}
class Field
{
   char Point[1920];

public:
   
   void Updat_Player();
   void Show_Field();
   void Change_Point(int Wich_Point,char To_What);
   void Move_Shots();
   char Give_Point(int Wich_Point);
}Area ;

class Player
{
   int Score;
   int Place;

public:

   void Move_Player(char Wich_Way);
   void Game_Over();
   void Add_Score();
   void Update_Player();
   void Set_Place();
   int  Give_Score();
   int  Give_Place();
}Gamer ;

class Enemy
{
public:
   void Spawn_Enemy();
   void Enemy_Shot();
   void Move_Enemy();
   void Kill_Enemy(int Wich_Enemy);
}Opponent ;

int Game_Control = 0;
int Main_Control = 0;
int main()
{
   srand((long)time(0));

   while(Main_Control == 0)
   {
      Game_Control = 0;
      cout <<"Hello, use W,A,S,D to move and C to shot"<<endl;
      int Helper_2 = 0;
      while(Helper_2<23)
      {
         int Helper = 0;
         while(Helper<80)
         {
            Helper++;
         }
         Helper_2++;
      }
      cout<<"So enjoi.";

      int AAA =0;
      while(AAA < 1920)
      {
         Area.Change_Point(AAA,' ');
         AAA++;
      }
      Opponent.Spawn_Enemy();
      Opponent.Spawn_Enemy();
      Area.Change_Point(0,'M');
      Gamer.Set_Place();
      

      while(Game_Control == 0)
      {
         if(kbhit())
         {
            char CC = getch();
            Gamer.Move_Player(CC);
         }
         gotoxy(0,0);
         Area.Move_Shots();
         Area.Show_Field();
         Opponent.Move_Enemy();
         Opponent.Enemy_Shot();
      }
   }
};



void Field::Change_Point(int Wich_Point, char To_What)
{
   int  A = Wich_Point;
   char B = To_What;

   Point[A] = B;
};

char Field::Give_Point(int Wich_Point)
{
   int A = Wich_Point;

   return Point[A];
};

void Field::Show_Field()
{
   cout << "Score:" << Gamer.Give_Score() << " Use W / A / S / D to move and C to shot." <<endl;
   int A = 0;
   while (A<1920)
   {
      cout << Point[A];
      A++;
   };
};
void Field::Move_Shots()
{
   int A = 0;
   while(A<1920)
   {
      char B = Point[A];
      if(B == '*')
      {
         Area.Change_Point(A,' ');
         if((A != 0) && (A != 80) && (A != 160) && (A != 240) && (A != 320) && (A != 400) && (A != 480) && (A != 560) && (A != 640) && (A != 720) && (A != 800) && (A != 880) && (A != 960) && (A != 1040) && (A != 1120) && (A != 1200) && (A != 1280) && (A != 1360) && (A != 1440) && (A != 1520) && (A != 1600) && (A != 1680) && (A != 1760) && (A != 1840))
         {
            if(Point[A-1] == 'M')
            {
               Gamer.Game_Over();
            }
            else
            {
               Area.Change_Point(A-1,'*');
            }
         }
      }
      A++;
   }
   int X = 1919;
   while(X>=0)
   {
      char B = Point[X];
      if(B =='~')
      {
         Area.Change_Point(X,' ');
         if((X != 79) && (X != 159) &&  (X != 239) && (X != 319) && (X != 399) && (X != 479) && (X != 559) && (X != 639) && (X != 719) && (X != 799) && (X != 879) && (X != 959) && (X != 1039) && (X != 1119) && (X != 1199) && (X != 1279) && (X != 1359) && (X != 1439) && (X != 1519) && (X != 1599) && (X != 1679) && (X != 1759) && (X != 1839) && (X != 1919))
         {
            if(Point[X+1] == '#')
            {
               Opponent.Kill_Enemy(X+1);
            }
            else
            {
               Area.Change_Point(X+1,'~');
            }
         }
      }
      X--;
   }
};
void Player::Move_Player(char Wich_Way)
{
   char A =  Wich_Way;

   if (A == 'w' || A == 'W')
   {
      if (Place >79)
      {
         Place = Place-80;
         Gamer.Update_Player();
         
         
         
         
         
         
      
      }
   }


   if (A == 'd' || A == 'D')
   {
      if ((Place != 79) && (Place != 159) &&  (Place != 239) && (Place != 319) && (Place != 399) && (Place != 479) && (Place != 559) && (Place != 639) && (Place != 719) && (Place != 799) && (Place != 879) && (Place != 959) && (Place != 1039) && (Place != 1119) && (Place != 1199) && (Place != 1279) && (Place != 1359) && (Place != 1439) && (Place != 1519) && (Place != 1599) && (Place != 1679) && (Place != 1759) && (Place != 1839) && (Place != 1919) )
      {
         
         Place = Place+1;
         Gamer.Update_Player();
         
      }
   }


   if (A == 's' || A == 'S')
   {
      if (Place <1840)
      {
         Place = Place+80;
         Gamer.Update_Player();
         
      }
   }



   if (A == 'a' || A == 'A')
   {
      if ((Place != 0) && (Place != 80) && (Place != 160) && (Place != 240) && (Place != 320) && (Place != 400) && (Place != 480) && (Place != 560) && (Place != 640) && (Place != 720) && (Place != 800) && (Place != 880) && (Place != 960) && (Place != 1040) && (Place != 1120) && (Place != 1200) && (Place != 1280) && (Place != 1360) && (Place != 1440) && (Place != 1520) && (Place != 1600) && (Place != 1680) && (Place != 1760) && (Place != 1840))
      {
         Place = Place-1;
         Gamer.Update_Player();
         
      }
   }


   if (A == 'c' || A == 'C')
   {
      if ((Place != 79) && (Place != 159) &&  (Place != 239) && (Place != 319) && (Place != 399) && (Place != 479) && (Place != 559) && (Place != 639) && (Place != 719) && (Place != 799) && (Place != 879) && (Place != 959) && (Place != 1039) && (Place != 1119) && (Place != 1199) && (Place != 1279) && (Place != 1359) && (Place != 1439) && (Place != 1519) && (Place != 1599) && (Place != 1679) && (Place != 1759) && (Place != 1839) && (Place != 1919) )
      {
         int B = Place+1;
         char A = Area.Give_Point(B);
         if(A == '#')
         {
            Opponent.Kill_Enemy(B);
         }
         else
         {
            Area.Change_Point(B,'~');
         }
      }
   }


};
void Player::Game_Over()
{
   Sleep(1000);
   gotoxy(0,0);
   
   cout << "You collected: " << Score <<" points                            "<<endl;
   cout << "Thx for playing"<<endl<<"Press n to replay,and any other key to close.";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                           ";
   cout << "                                                                          ";
   cout << "                                                                          ";
   cout << "                                                                          ";
   cout << "                                                                          ";
   cout << "                                                                         ";
   while(!kbhit)
   {
   }
   char G = getch();
   if((G == 'n')||(G == 'N'))
   {
      Game_Control = 1;
   }
   else
   {
      Game_Control = 1;
      Main_Control = 1;
   }

   
   
};
void Player::Add_Score()
{
   Score++;
};
int  Player::Give_Score()
{
   return Score;
};
int  Player::Give_Place()
{
   int a = 0;
   return Place;
};
void Player::Update_Player()
{
   char A;
   int B = 0;
   A = Area.Give_Point(Place);
   if(A == 'M')
   {
   }
   else
   {
      if((A=='#')||(A=='*'))
      {
         Gamer.Game_Over();
      }
      else
      {
         while(B<1920)
         {
            A = Area.Give_Point(B);
            if(A == 'M')
            {
               Area.Change_Point(B,' ');
               Area.Change_Point(Place,'M');
            }
            B++;
         }
      }
   }
};

void Player::Set_Place()
{
   Place = 0;
};
void Enemy::Spawn_Enemy()
{
   int A = GetRand(0,1919);

   Area.Change_Point(A,'#');
};
void Enemy::Enemy_Shot()
{
   int A = 0;
   char B;
   char D;
   char E;
   int Y = 0;
   int X = 0;

   while((A<1920)&&(Y == 0))
   {
      B = Area.Give_Point(A);

      if(B == 'M')
      {
         Y++;
         while((X== 0)&&(A != 79) && (A != 159) &&  (A != 239) && (A != 319) && (A != 399) && (A != 479) && (A != 559) && (A != 639) && (A != 719) && (A != 799) && (A != 879) && (A != 959) && (A != 1039) && (A != 1119) && (A != 1199) && (A != 1279) && (A != 1359) && (A != 1439) && (A != 1519) && (A != 1599) && (A != 1679) && (A != 1759) && (A != 1839) && (A != 1919))
         {A++;
         D = Area.Give_Point(A);
         if(D=='#')
         {
            X++;
            A--;
            E = Area.Give_Point(A);
            if(E=='M')
            {
               Gamer.Game_Over();
            }
            else
            {
               Area.Change_Point(A,'*');
            }
         }
         }
      }
      A++;
   }
};
void Enemy::Move_Enemy()
{
   int A = 0;
   char B;
   int C = 0;
   int D = 0;
   char E;

   while(A<1920)
   {
      
      B = Area.Give_Point(A);

      if(B == '#')
      { 
         C = GetRand(0,5);
         if((C==0) &&(A>79))
         {
            
            D = A-80;

            E = Area.Give_Point(D);

            if(E == ' ')
            {
               
               Area.Change_Point(A,' ');
               Area.Change_Point(D,'#');
            }

            else if(E == 'M')
            {
               Gamer.Game_Over();
            }

            else
            {}
         }
         else if((C==1)&&(A != 79) && (A != 159) &&  (A != 239) && (A != 319) && (A != 399) && (A != 479) && (A != 559) && (A != 639) && (A != 719) && (A != 799) && (A != 879) && (A != 959) && (A != 1039) && (A != 1119) && (A != 1199) && (A != 1279) && (A != 1359) && (A != 1439) && (A != 1519) && (A != 1599) && (A != 1679) && (A != 1759) && (A != 1839) && (A != 1919))
         {
            D = A+1;

            E = Area.Give_Point(D);

            if(E == ' ')
            {
               Area.Change_Point(A,' ');
               Area.Change_Point(D,'#');
            }

            else if(E == 'M')
            {
               Gamer.Game_Over();
            }

            else
            {}
         }
         else if((C==3) &&(A<1840))
         {
            D = A+80;

            E = Area.Give_Point(D);

            if(E == ' ')
            {
               Area.Change_Point(A,' ');
               Area.Change_Point(D,'#');
            }

            else if(E == 'M')
            {
               Gamer.Game_Over();
            }

            else
            {}
         }
         else if((C==4) && (A != 0) && (A != 80) && (A != 160) && (A != 240) && (A != 320) && (A != 400) && (A != 480) && (A != 560) && (A != 640) && (A != 720) && (A != 800) && (A != 880) && (A != 960) && (A != 1040) && (A != 1120) && (A != 1200) && (A != 1280) && (A != 1360) && (A != 1440) && (A != 1520) && (A != 1600) && (A != 1680) && (A != 1760) && (A != 1840))
         {
            D = A-1;

            E = Area.Give_Point(D);

            if(E == ' ')
            {
               Area.Change_Point(A,' ');
               Area.Change_Point(D,'#');
            }

            else if(E == 'M')
            {
               Gamer.Game_Over();
            }

            else
            {}
         }
      }
      A++;
   }
};





void Enemy::Kill_Enemy(int Wich_Enemy)
{
   int A = Wich_Enemy;
   Area.Change_Point(A,' ');
   Gamer.Add_Score();
   Opponent.Spawn_Enemy();
   Opponent.Spawn_Enemy();
   
   
   
   

   
};

NOOB
 
Posts: 59
Joined: Tue May 06, 2008 11:04 am

Re: Space Shooter!

Postby Dodi300 on Tue Apr 14, 2009 5:35 pm

Hey!
Both of those are great games! lol
Any chance one of you can make a tutorial? :D
Dodi300
 
Posts: 3
Joined: Fri Apr 10, 2009 12:43 pm

Re: Space Shooter!

Postby glinka57 on Sat Apr 25, 2009 12:23 am

Nice work Alfaris. HHAHAH HardMode LOl thats some nice dodging

LOL @ NOOB, you have a challenger to your great spaceshooter :p
(apearantly you both read it from the same book. LOL. you pretended you created it hahahaha :p )
User avatar
glinka57
 
Posts: 195
Joined: Fri Feb 27, 2009 7:32 pm

Re: Space Shooter!

Postby sawm on Sat Aug 29, 2009 5:56 pm

what book is it from? :O
sawm
 
Posts: 8
Joined: Fri Aug 28, 2009 3:24 pm

Re: Space Shooter!

Postby Ruddie on Wed Sep 23, 2009 11:14 am

looks like fun, but I prefer making this with SDL/allegro, that also seems a LOT easier. And also having a lil bit more functions =)

http://cpptutorials.freeforums.org/post2788.html#p2788

Their is my code! +)
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands


Return to Projects

Who is online

Users browsing this forum: No registered users and 1 guest