ASCII space invaders

Hi i am very new to c++ and and this forum and i am trying to make an ASCII space invaders game while i learn. i have got a main menu function and am able to create a grid and the player but i cant get the bullet to work properly.can any one help please?

it is on windpows xp and compiled in dev c++.

here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <conio.h>
using namespace std;

void printgrid(char grid[][30],int h,int w);
void play();
void ship();
void instructions();
void pshoot();

     char grid[20][30];
     char mov;
     int  ypos = 17;
     int  xpos = 7;

int main()                               //main() function
{
    char menuopt;
    
    do
    {
    system("cls");
    
    cout << "                                 :[B]EGIN:   " <<endl;
    cout << "                                           " <<endl;
    cout << "                              :[H]IGH SCORES:" <<endl;
    cout << "                                           " <<endl;
    cout << "                             :[I]NSTRUCTIONS:" <<endl;
    cout << "                                           " <<endl;
    cout << "                                 :[E]xit:    " <<endl;
    
    menuopt =getch();
    switch(menuopt)
    {
                   case 'b':
                        {
                            play();
                        }
                   break;
                   case 'i':
                        {
                            instructions();
                        }
                   break;
                   case 'e':
                   break;
    } 
    }
    while(menuopt != 'e');  

    return 0;
}

void play()                              //play() function          
{
    do
    {   
    
    system("cls");

    for(int i = 0;i < 20;i++)           
    {
            for(int j = 0;j < 30;j++)
            {
                    grid[i][j] = ' ';
            }
    }
        
    ship();
    if(mov == 's')                         //shoot       
    {
           pshoot();
           system("cls");
    };
    printgrid(grid,20,30);                 //create grid

    mov = ' ';                             //move player
    mov =getch();
    grid [ypos][xpos] = 0;
    
    switch(mov)
    {
               
               case 'a':
                    if(xpos > 0)
                    {
                            xpos--;
                    }
               break;
               case 's':
                    {
                    }
               break;
               case 'd':
                    if(xpos < 27)
                    {
                            xpos++;
                    }
               break;
    };
    
    
  
    }
    while(mov != 'e');
}

void ship()                                  //create ship   
{
     int texture = '#';
     
     grid [ypos+1][xpos]   = texture;
     grid [ypos+1][xpos+1] = texture;
     grid [ypos+1][xpos+2] = texture;
     grid [ypos+2][xpos]   = texture;
     grid [ypos+2][xpos+1] = texture; 
     grid [ypos+2][xpos+2] = texture;
     grid [ypos]  [xpos+1] = texture;
     grid [ypos+2][xpos+1] = texture;
     
}

void pshoot()                                 //shoot
{
     int pbullet = '.';
     int bpos;

     for(bpos = 1;bpos > 0;bpos--)
     {
              for(int x = 1;x < 16;x++)
              {
                      grid [ypos-x][xpos+1] = pbullet; 
                      printgrid(grid,20,30);    
                      system("cls");                                   
              }
                  
     }
          
}

void instructions()                            //output instructions
{
     system("cls");
     cout <<"     :INSTRUCTIONS:     " <<endl;
     cout <<"press 'a' to move left"  <<endl;
     cout <<"press 'd' to move right" <<endl;
     cout <<"press 's' to shoot"  <<endl;    
     
     system("pause"); 
}

void printgrid(char grid[][30],int h,int w)    //create grid
{
     for(int i = 0;i < h;i++)
     {
             for(int j = 0;j < w;j++)
             {
                     cout << grid[i][j] << ' ';
             }
     cout <<endl;
     }
}


Yup. Streams aren't designed for games. They are designed to input and output data in a manner as generic as possible. That's why there are no functions to position the cursor on the console. The console's presence is incidental. Output could be going to a file.
Anyway, the library you need is ncurses or PDcurses:
http://www.gnu.org/software/ncurses/ (GPLed)
http://pdcurses.sourceforge.net/ (public domain)
If you have something against weird bearded men telling you what to do with your source code, you might want to try out PDcurses first. I know I will.
hi thanks for fast responce. i have downloaded pdcurses, could you point me to a tutorial on how to use it please.
It appears as though there is documentation on the website...

http://pdcurses.sourceforge.net/doc/index.html
it looks to complicated for me i have only been programing for 3 weeks is there any other tutorial or any easier way to sort my program(im not trying to sound lazy).
My suggestion, and I'm not trying to be rude, but I would start out with an easier program until you get a bit more fluent... Those library definitions can be pretty tough to decipher, even if you've been coding for 20 years...
ok, thank you for helping :)
Sorry I couldn't help you any more than that. Keep working at it though, you'll get there. :)
It does look scary at first, but you can do it without too much difficulty. Take a read through the http://www.faqs.org/docs/Linux-HOWTO/NCURSES-Programming-HOWTO.html

The main tricks are 1. initialization, 2. refresh, 3. finalization.

1. Initialization. You probably want the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  // Start curses
  initscr();

  // Direct key input
  raw();
  (void)noecho();
  nonl();
  intrflush( stdscr, FALSE );
  (void)keypad( stdscr, TRUE );

  // I want colors!
  // (You may actually want to check has_color() first)
  start_color();

  // Make the cursor invisible (hopefully)
  curs_set( 0 );


2. Refresh. Every time you want do display changes you must refresh the display.
1
2
3
4
5
6
7
8
9
10
11
12
  // Main event loop
  while (...)
    {
    // check for and act on user input
    ...

    // draw the current display (using mvaddch() and the like)
    ...

    // show the changes
    wrefresh( stdscr );
    }


3. Finalization. Always do this before terminating your program.
 
  endwin();


Hope this helps.
thanks for the advice i will take a look at them and keep you informed if i make any progress :)

when i try to compile the code it comes up with error messages saying
curses.h: no such file or directory ?
Last edited on
hi i cant get the pdcurses library working but i have solved my problem by adding this:
1
2
3
grid [ypos-x][xpos+1] = ' '; 
printgrid(grid,40,40);    
system("cls");          

at the end of the second for loop within pshoot();
thanks for the help :}
Hi, I just looked at your question and would like to offer this little tidbit to your pshoot function:

void pshoot() //shoot
{
int pbullet = '.';
int bpos;

for(int x = 1;x < 16;x++)
{
grid [ypos-x][xpos+1] = pbullet;
if (x > 1)
grid [ypos-x+1][xpos+1] = ' ';
printgrid(grid,20,30);
system("cls");
}
}

Nice little program :)
> when i try to compile the code it comes up with error messages saying
> curses.h: no such file or directory ?

> system("cls");

To use NCurses on Windows you need to get and install PDCurses
http://pdcurses.sourceforge.net/

If you are using MinGW, you can download and unzip the pdcurses file directly into your C:\MinGW directory and you are all set. For other's you might as well just download the source archive (it is small) and execute the makefile appropriate for your compiler. For example, if you are using MS VC++, change to the ~\win32 directory and execute make -f vcwin32.mak from the command prompt (or load and execute it directly from the IDE).

Using system("cls") is really a bad idea, for a lot of reasons. Unfortunately, I've tired of explaining them at the moment.

Good luck!
thanks for your help.
i know system("cls") isnt good but i cant find any other way
please could you post a link to a better way thanks :}

i have got pdcurses working it compiles correctly but when i run the program that was created it says :
this application has failed to start because SDL.dll was not found.
Last edited on
What? Impossible. You didn't leave any .libs attached to the default configuration of the compiler, did you?
You can download SDL.dll from http://www.libsdl.org/
thanks its working now :}
You got the program to run correctly? You should consider posting the code for it if you did. I would like to play this :)
i haven't fineshd it yet, i have to learn alot of new stuff with pdcurses
but when i finish i'll post the code :}

edited:

i have put the code so far here just scroll down
i will update the code when i have fineshed writing it;
http://crazykid-14.piczo.com/cppprograming?cr=3&linkvar=000044
Last edited on
i cnat figure out how to kill the bad guys;
when they get shot they just re appear and cary on moving
can somone help;
this is my bad guy function;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void enemy1()                                //enemys
{
                int etexture = 153;
                e1x++;
                if(ei == 0)
                {
                      grid [e1y][e1x] = etexture;  
                      grid [e1y+1][e1x+1] = etexture;
                      grid [e1y+1][e1x-1] = etexture; 
                      ei++;
                }
                else
                if(ei == 1)
                {
                      grid [e1y][e1x] = etexture;  
                      grid [e1y+1][e1x] = etexture;
                      ei--;
                }
     
}     
Last edited on
Topic archived. No new replies allowed.