Code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#define MAX 27
#define yFIRST 10
#define yLAST 30
#define VRT 179 //vertical bar
#define HRZ 196 //horizontal bar
#define TLC 218 //top left corner
#define TRC 191 //top right corner
#define BLC 192 //bottom left corner
#define BRC 217 //bottom right corner
#define LHC 180 //left handed T connector
#define RHC 195 //right handed T connector
#define UHC 193 //upward handed T connector
#define MINI 254 //mini box
#define NORM 220 //normal low box
#define PAUSE 2 //pause between descending loops
//each hun[] element, is made up of this struct
struct invaders {
char wrd[20];
int x;
int y;
};
void menu(char words[MAX][20]);
void skyline(void);
int iplay(char words[MAX][20]);struct invaders huns[20];
void clearView(void);
int main() //my system requires int main, not void main
{
int i,len;
char words[MAX][20];
FILE *fp;
fp=fopen("wordList.txt","r");
if(fp == NULL) {
printf("Error. File did not open.\n");
return 1;
}
for(i=0;i<MAX;i++) {
fgets(words[i],20,fp);
len=strlen(words[i]) -1;
if(words[i][len]=='\n')
words[i][len]='\0';
//printf("%s\n",words[i]);
}
fclose(fp);
menu(words);
printf("\npress any key to continue\n");
//_getch();
return 0;
}
void menu(char words[MAX][20])
{
int a,gameOn;
char pname[25]={"al"}; //'\0'};
do {
//clrscr();
gameOn=0;
_gotoxy(34,4);
//textcolor(RED); textbackground(WHITE);
printf("Welcome to ");
_gotoxy(30,6);
printf("*******************");
_gotoxy(30,8);
printf("* Letter Invaders *");
_gotoxy(30,10);
printf("*******************");
/*
_gotoxy(15,14);
if(pname[0]=='\0') {
printf("Enter Player Name: ");
scanf("%s",pname);
}
_gotoxy(15,16);
printf("%s, What would you like to do? ",pname);
_gotoxy(18,18);
printf("1.Play ");
_gotoxy(18,20);
printf("2.Quit");
_gotoxy(15,22);
printf("Enter your choice:");
scanf("%d",&a);
*/
a=1;
clearView();
if(a==1) {
gameOn=1;
skyline();
iplay(words);
}
else {
//clrscr();
gameOn=0;
_gotoxy(25,22);
printf("Game Over. Thanks for playing.");
}
gameOn=0;
}while(gameOn==1);
}
int iplay(char words[MAX][20]) {
struct invaders huns[MAX];
int i,j;
/* sets the initial x and y values for all the words that will be displayed */
j=yFIRST;
for(i=0;i<MAX;i+=3) { //words are grouped in sets, three words per row
strcpy(huns[i].wrd,words[i]);
strcpy(huns[i+1].wrd,words[i+1]);
strcpy(huns[i+2].wrd,words[i+2]);
huns[i].x=8;
huns[i].y=j;
huns[i+1].x=40- (strlen(huns[i+1].wrd)/2);
huns[i+1].y=j;
huns[i+2].x=72- strlen(huns[i+2].wrd);
huns[i+2].y=j;
++j;
}
for(j=0;j<MAX;j++) { //displays the words at their highest position on-screen
_gotoxy(huns[j].x,huns[j].y);
printf("%s",huns[j].wrd);
}
do { //causes the words to descend, row by row
for(i=MAX-3;i>=0;i-=3) { //clears the current row of words
_gotoxy(huns[i].x,huns[i].y);
_clreol();
for(j=i;j<i+3;j++) { //prints the current row of words
huns[j].y++; //and updates the words y value
_gotoxy(huns[j].x,huns[j].y);
printf("%s",huns[j].wrd);
}
_sleep(1); //slows it down so you can play
}
}while(huns[j].y < yLAST);
return 0;
}
void clearView(void) {
int i=1;
_gotoxy(1,i);
for(i=1;i<30;i++) {
//do not change the length of the next print line, (1 to 78 columns are cleared)
printf(" ");
}
}
void skyline(void) {
int i,j,x=1,y=yLAST+1;
unsigned char s[9][79]=
{
{" M|M "},
{" MNNNM | "},
{" 1---2 | "},
{" 1--2 1--2 |MMM| 1--2 1-C-2 "},
{" |MM| 1---2 1--BMM| |MMM| |MM| 1-2 1--2 |MMM| 1----2 12 "},
{" D--B 1--BMMM| |MM|MM| 1---BMMM| |MM| |M| 1-BMM| 1-2 |MMM| |MMMM| |42 "},
{" |MM| |MM|MMM| |MM|MM| |MMM|MMM| |MM| |M| 1-3M|MM| |MD-3MMM| 42MM13 |M| "},
{" |MM| |MM|MMM| |MM|MM| |MMM|MMM| |MM| |M| |MMM|MM| |M|MMMMM| |MM| |M| "},
{"-3MM4-3MM|MMM4--3MM|MM4--3MMM|MMM4-3MM4-3M4--3MMM|MM4--3M|MMMMM4---3MM4--3M4--"}
};
for(i=0;i<9;i++) {
for(j=0;j<78;j++) {
switch(s[i][j]) {
case '1': s[i][j]=TLC; break;
case '2': s[i][j]=TRC; break;
case '3': s[i][j]=BRC; break;
case '4': s[i][j]=BLC; break;
case 'B': s[i][j]=LHC; break;
case 'C': s[i][j]=UHC; break;
case 'D': s[i][j]=RHC; break;
case '-': s[i][j]=HRZ; break;
case '|': s[i][j]=VRT; break;
case 'M': s[i][j]=MINI; break;
case 'N': s[i][j]=NORM; break;
}
}
}
_gotoxy(x,y);
for(i=0;i<9;i++) {
printf(" %s\n",s[i]);
}
}