View Single Post
Old 10-05-2008, 12:18 PM   #430 (permalink)
[xubz]
"The Cake is a Lie!!"
 
[xubz]'s Avatar
 
Join Date: Oct 2006
Posts: 471
Default Re: Post ur C/C++ Programs Here

Heh! Made this as a challenge to a friend who started a debate about graphics.h.. Its a simple DOS Text-based alien shooter game.. The following code is released under Creative Commons Share Alike, Attribution License.

Compiles Obviously only on TC.. A Lame one.. I know :-\

Code:
/* DESTROY TEH ALIENS (c) [XUBZ] */
#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* Total no of Shots to give for shooting down 10 Aliens */
#define NUMSHOTS 11

char alien[80];
int score;
int shots;
int lastflag = 0;
int alienpos = 3;
int aliensound = 100;

void drawmain(int pos)
{
	int i;
	clrscr();

	/* Draw the Ground */
	for(i = 1; i <= 80; i++) {
		gotoxy(i, 24);
		textcolor(GREEN);
		cprintf("_");
		gotoxy(i, 7);
		textcolor(BLUE);
		cprintf("_");
		gotoxy(i, 24);
	}

	/* Draw the Cannon based on Current Position */
	textcolor(RED);
	gotoxy(pos, 23);
	cprintf(" || ");
	textcolor(GREEN);
	gotoxy(pos, 24);
	cprintf("|__|");
	textcolor(BLUE);
	gotoxy(pos+1, 7); /* For Shot Marker on the Top */
	cprintf(".");

	/* Draw the 'Quit' and 'New Game' Messages */
	gotoxy(2, 2);
	textcolor(RED);
	cprintf("Press Q to Exit");
	gotoxy(2, 3);
	cprintf("Press N for New Game");

	/* Draw the Current Cannon Position */
	gotoxy(66, 2);
	textcolor(YELLOW);
	cprintf("Position: %d", pos);

	/* Draw the Score */
	gotoxy(66, 5);
	textcolor(BLUE);
	cprintf("Score: %d", score);

	/* Draw No. of Shots */
	gotoxy(66, 6);
	textcolor(RED);
	cprintf("Shots Left: %d", shots);

	gotoxy(2, 6);
	textcolor(YELLOW);
	cprintf("DESTROY TEH ALIENS(tm) v0.1 (C) [XUBZ]");

	/* Draw the Aliens */
	if (!lastflag) {
		alienpos = (alienpos == 3) ? 0 : alienpos;
		aliensound = (aliensound == 250) ? 100 : aliensound;
		gotoxy(1+(alienpos++), 10);
	} else {
		gotoxy(alienpos, 10);
	}

	for (i = 0; i < 80; i++) {
		textcolor(WHITE);
		cprintf("%c", alien[i]);
	}

	if (!lastflag) {
		gotoxy(80, 25);
		sound(aliensound);
		delay(50);
		aliensound += 50;
		nosound();
	}

	/* If Score is 10; Show 'You Won' and Play Music ^_^ */
	if (score == 10) {
		gotoxy(36, 15);
		textcolor(YELLOW);
		cprintf("YOU WON!");
		gotoxy(30, 16);
		cprintf("Press N for New Game");
		if (!lastflag) {
			sound(250);
			delay(100);
			sound(450);
			delay(100);
			sound(350);
			delay(100);
			sound(450);
			delay(100);
			sound(350);
			delay(100);
			nosound();
			lastflag = 1;
		}
	}

	/* If No Shots left and score is NOT 10; Show 'Game Over' and Play Music */
	if (shots == 0 && score != 10) {
		gotoxy(35, 15);
		textcolor(RED);
		cprintf("GAME OVER");
		gotoxy(30, 16);
		cprintf("Press N for New Game");
		if (!lastflag) {
			sound(250);
			delay(100);
			sound(350);
			delay(100);
			sound(450);
			delay(100);
			nosound();
			lastflag = 1;
		}
	}

	/* Move the Cursor to End of the Screen for reducing color corruption :-\ */
	textcolor(BLUE);
	gotoxy(80, 25);
}

void newgame()
{
	int i;
	for(i = 0; i < 80; i++)
		alien[i] = '\0';

	/* Alien Positions */
	alien[6] = '0';
	alien[13] = '8';
	alien[18] = '0';
	alien[25] = '0';
	alien[65] = '0';
	alien[32] = '8';
	alien[45] = '0';
	alien[50] = '8';
	alien[58] = '0';
	alien[73] = '0';

	/* Set/Reset the No. of Shots, Score and Flag */
	score = 0;
	shots = NUMSHOTS;
	lastflag = 0;
	alienpos = 3;
	aliensound = 100;
}

void main()
{
	char c;
	int i, curpos = 38;
	clrscr();

	/* Draw Title Screen */
	newgame();
	gotoxy(30, 10);
	textcolor(GREEN);
	cprintf("DESTROY TEH ALIENS(tm)");
	gotoxy(33, 12);
	textcolor(YELLOW);
	cprintf("Press N to Start!");
	gotoxy(80, 25);
	textcolor(BLUE);

	while(c != 'q' && c != 'Q') {
		c = getch();

		/* Move the Cannon; \K is ASCII for Left Key and \M for Right */
		if (c == '\K' || c == '\M') {
			if (c == '\K') {
				if (curpos >= 3)
					curpos--;
			} else if (c == '\M') {
				if (curpos <= 75)
					curpos++;
			}
			drawmain(curpos);
		}

		/* Shoot the Alien and Register the Shots/Score */
		if (c == ' ') {
			if (!lastflag) {
				sound(400);
				delay(100);
			}
			if (curpos >= 2 && curpos <= 76)
				if (shots > 0) {

					if (alien[curpos-(alienpos-1)] == '0' || alien[curpos-(alienpos-1)] == '8') {
						alien[curpos-(alienpos-1)] = '*';
						sound(600);
						delay(100);
						score++;
					}
					shots--;
				}
			nosound();
			drawmain(curpos);
		}

		/* Reset the Game */
		if (c == 'n' || c == 'N') {
			newgame();
			curpos = 38;
			drawmain(curpos);
		}
	}
}
__________________
[xubz] ● http://xubz.com/
[steam_id] ● http://steamcommunity.com/id/xubz
[xubz] is offline