Sunday 25 August 2013

A Plane Shooting. ASCII Game Simulation.

2 Posts down, the asterisk pyramid thing got me thinking about space war games on the NES and ATARI systems I played as a kid.

This program draws a plane (many would disagree that it is a plane). The Spacebar key will "shoot", Left-Right keys will move the plane. Escape will end the program.

Since there are no nifty sounds that can be created, I had to use the Beep() function, which I recently picked up. Making the sound slows the program down. If you want, comment out the "Beep(100000-l,25);" line and un-comment the loop before it. The "bomb" will move faster. You can control its speed by increasing or decreasing the 5000000 in there.

The screen will be re-sized in the beginning, if it does not fit your screen, right click on the head of the program screen (The DOS screen), choose properties, go to the font tab. You can re-size your screen (which does not affect the display) there. 8X8 with Raster Font will work on most PCs and Laptops.

As with any program that has gotoxy; this program also needs myconstdwin.h be added,

SAMPLE:




Code:
#include <myconstdwin.h>

void print_lame_plane(int x,int y);
void erase_lame_plane(int x,int y);

void shoot_missile(int x,int y);

void main()
{
    system("mode 158,77");
    int key,x=80,y=73;

    print_lame_plane(x,y);
    while(1)
    {
        key=get_key();
        if(key==587 && x>2) //left
        {
            erase_lame_plane(x,y);
            x--;
            print_lame_plane(x,y);
        }
        else if(key==589 && x<154) //left
        {
            erase_lame_plane(x,y);
            x++;
            print_lame_plane(x,y);
        }
        else if(key==32)
        {
            shoot_missile(x,y);
        }
        else if(key==27)
        {
            system("mode 80,25");
            break;
        }
    }
    return 0;
}

void shoot_missile(int x,int y)
{
    printf("\a");
    int loop,l=1;
    for(;y>1;y--)
    {
        //for(loop=0;loop<5000000;loop++);
        Beep(100000-l,25);
        printf("");
        gotoxy(x,y-1);
        printf("%c",153);
        gotoxy(x,y);
        if(y!=73)
        {
            printf("  ");
        }
        l+=100;
        if(y==2)
        {
            gotoxy(x,y-1);
            printf("  ");
            gotoxy(x,y-1);
            printf("BOOM!");
        }
    }
}

void print_lame_plane(int x,int y)
{
    gotoxy(x,y);
    printf("%c",219);
    gotoxy(x-2,y+1);
    printf("%c%c%c%c%c",223,223,219,223,223);
    gotoxy(x,y+2);
    printf("%c",219);
    gotoxy(x-1,y+3);
    printf("%c%c%c",223,219,223);
}

void erase_lame_plane(int x,int y)
{
    gotoxy(x,y);
    printf("  ");
    gotoxy(x-2,y+1);
    printf("      ");
    gotoxy(x,y+2);
    printf("  ");
    gotoxy(x-1,y+3);
    printf("     ");
}

int get_key()
{
    int c = getch();
    switch (c)
    {
      case 0:   return getch()+256;
      case 224: return getch()+512;
    }
    return c;
}

No comments:

Post a Comment