Piczo

Log in!
Stay Signed In
Do you want to access your site more quickly on this computer? Check this box, and your username and password will be remembered for two weeks. Click logout to turn this off.

Stay Safe
Do not check this box if you are using a public computer. You don't want anyone seeing your personal info or messing with your site.
Ok, I got it
Back To Home Page
C++ programing
here is a good place to learn



This one asks user for input and creates a grid according to the number s the user types
here i will be posting some of my code it wont be verry good cus i only just started programming if u wnt 2 ask questions u can in the box at the side i will try to get back 2 u.
497 hits
<?xml version="1.0" encoding="iso-8859-1"?> spaceinvaders.htm
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;

void printgrid(char grid[][40],int h,int w);
void play();
void ship();
void enemy1();
void instructions();
void pshoot();
int  highscores();
int  mkhighscores(int);

     char grid[40][40];
     char mov;
     int  ypos = 37;
     int  xpos = 7;
     int  e1y = 1;
     int  e1x = 1;
     long score = 0;
     int  e1stats = 1;
     int  ei = 0;

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 'h':
                        {
                           highscores();
                        }
                   break;
                   case 'e':
                   break;
    } 
    }
    while(menuopt != 'e');  

    return 0;
}

void play()                              //play() function          
{
    e1stats = 1;
    score = 0;

    do
    {
    system("cls");
    cout <<score;

    for(int i = 0;i < 40;i++)           
    {
            for(int j = 0;j < 40;j++)
            {
                    grid[i][j] = ' ';
            }
    }

    if(e1stats == 1)
    {
               enemy1();
    }

    ship();
    if(mov == 's')                         //shoot       
    {
           pshoot();
           system("cls");
    };
    printgrid(grid,40,40);                 //create grid

    mov = ' ';                             //move player
    mov =getch();
    grid [ypos][xpos] = 0;

    switch(mov)
    {
               case 't':
                    {
                        e1stats = 0;
                    }
               break;
               case 'a':
                    if(xpos > 0)
                    {
                            xpos--;
                    }
               break;
               case 's':
                    {
                    }
               break;
               case 'd':
                    if(xpos < 37)
                    {
                            xpos++;
                    }
               break;
    };



    }
    while(mov != 'e');
    //mkhighscores...
}

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 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--;
                }

                if(e1x == 40)
                {
                       e1y++;
                }

}     

void pshoot()                                 //shoot nearly fineshed
{
     int pbullet = '.';
     int bpos;
     int z;
     int bposx = xpos;
     int bposy = ypos+1;

     for(bpos = 1;bpos > 0;bpos--)
     {
              for(int x = 1;x < 38;x++)
              {
                      if((bposy && bposx+1) == (e1y && e1x))
                      {
                           e1stats = 0;
                           score =+ 20;
                      }

                      if(x % 2 == 0)
                      {

                           grid [bposy-x][bposx+1] = pbullet; 
                           printgrid(grid,40,40);    
                           system("cls");

                           grid [bposy-x][bposx+1] = ' ';
                           system("cls");
                      }
              }


     }


}

void instructions()                            //output instructions
{
     system("cls");
     cout <<"\t\t\t\t:INSTRUCTIONS:" <<endl;
     cout <<"\t\t\t\t``````````````" <<endl;
     cout <<"\t\t\tpress 'a' to move left"  <<endl;
     cout <<"\t\t\tpress 'd' to move right" <<endl;
     cout <<"\t\t\tpress 's' to shoot"  <<endl;    

     getch();
}

void printgrid(char grid[][40],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;
     }
}

int highscores()
{
    int v;
    string hs;

    ifstream hscores("highscores.txt");

    system("cls");

    if(! hscores)
    {
         cout <<"Error geting HIGH SCORES" <<endl;
         return -1;
    }

    for(v = 0;! hscores.eof();v++)
    {
          getline(hscores,hs);
          cout <<"\t\t\t" << hs <<endl;
    }
    cout <<"press any key to go back..." <<endl;
    cout <<"wheres the 'any' key" <<endl;
    getch();         
}

int mkhighscores(int scr)
{

}