#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <mem.h>
#include <time.h>
#include <math.h>
#include <alloc.h>
#define MAX_STAR 80
#define MAX_OBJ 30
#define MAXY 169
typedef unsigned long ulong;
char far *VideoBuf=(char far*)0xa0000000L, *MemBuf;
char keys[128];
const KEY_UP=72,KEY_DOWN=80,KEY_LEFT=75,KEY_RIGHT=77,KEY_ESC=1,KEY_CTRL=29;
void interrupt far (*OldInt9)(void);
void interrupt NewInt9(void);
char GameStart(void);
struct TStars { int x,y,v; } Stars[MAX_STAR];
struct TObj { int mode,type,index,next,x,y; } Objs[MAX_OBJ];
long GameScore=0;
unsigned char fly_pic[260]={
16,0,16,0,0,0,0,29,25,0,0,0,0,25,29,196,0,0,0,0,0,0,0,25,25,0,0,0,0,25,0,196,0,
0,0,0,112,112,0,25,196,40,40,29,112,112,40,112,0,112,112,112,40,40,112,112,40,40,
25,25,112,40,40,112,112,112,112,112,112,40,112,25,40,40,112,112,112,40,40,25,
40,112,112,0,0,112,112,25,40,40,40,112,40,40,40,25,40,112,0,0,0,0,112,25,40,
112,40,112,112,112,40,25,112,0,0,0,0,0,0,29,112,0,40,0,112,0,112,29,0,0,0,0,
0,0,0,29,112,0,40,54,112,0,112,29,0,0,0,0,0,0,0,29,0,0,40,54,112,0,0,29,0,0,
0,0,0,0,0,0,0,40,112,25,112,112,0,0,0,0,0,0,0,0,0,0,0,40,112,25,112,112,0,0,
0,0,0,0,0,0,0,0,0,40,112,112,112,112,0,0,0,0,0,0,0,0,0,0,0,0,40,112,112,0,0,
0,0,0,0,0,0,0,0,0,0,0,40,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,112,0,0,0,0,0,0,0,
0};
void copyright(void)
{ printf("Game Over. your score %ld/n",GameScore);
printf("Thank you for play it, if you have any question please call me/n");
printf("(0871)7167710 or [email protected], Lin Wei/n");
}
int game_init(void);
int game_restore(void);
void main(void)
{
if (!game_init()) return;
printf(" - STAR WAR -/r");
GameStart();
game_restore();
copyright();
}
int game_init(void)
{
long i;
union REGS regs;
MemBuf=(char far*)farmalloc(320L*(MAXY+1));
if (!MemBuf) {
printf("not enough memory/n"); return 0;
}
memset(MemBuf,0,320L*(MAXY+1));
randomize();
for (i=0;i<128;i++) keys[i]=0;
for (i=0;i<MAX_STAR;i++) {
Stars[i].x=random(320);
Stars[i].y=-20+random(250);
Stars[i].v=1;
if (i<MAX_STAR/3) Stars[i].v++;
}
for (i=0;i<MAX_OBJ;i++) Objs[i].mode=0;
OldInt9=getvect(9);
disable();
setvect(9,NewInt9);
enable();
regs.x.ax=0x13;
int86(0x10,®s,®s);
return 1;
}
int game_restore(void)
{
union REGS regs;
disable();
setvect(9,OldInt9);
enable();
regs.x.ax=3;
int86(0x10,®s,®s);
farfree(MemBuf);
return 0;
}
void interrupt NewInt9(void)
{
unsigned char key;
key=inportb(0x60);
if (key<0x80) keys[key]=1;
else keys[key&0x7f]=0;
key=inportb(0x61); key|=0x80; outportb(0x61,key);
outportb(0x61,key&0x7f);
outportb(0x20,0x20);
}
void pixel(unsigned x,unsigned y,char c)
{
if (x>=320||y>MAXY) return;
MemBuf[(y<<8)+(y<<6)+x]=c;
}
void show(void)
{
int offset=(199-MAXY)*160;
memcpy(VideoBuf+offset,MemBuf,320L*(MAXY+1));
}
void clear(void)
{
memset(MemBuf,0,320L*(MAXY+1));
}
void putimage(int x,int y,char *b,int mode)
{
int len, wid, i, j;
len = b[0] + (int)b[1] * 256;
wid = b[2] + (int)b[3] * 256;
x-=len/2; y-=wid/2;
for (j=0,b=b+4;j<wid;j++) for (i=0;i<len;i++,b++)
{ if (*b&&!mode) pixel(x+i,y+j,*b);
if (*b&&mode) pixel(x+i,y+wid-j-1,*b);
}
}
void drawfire(int x,int y)
{ int i,j;
for (i=-6;i<6;i++) { pixel(x-5,y+i,9); pixel(x+3,y+i,9); }
}
unsigned long timepass;
int GameOver, Sound=0;
int fly_x=160,fly_y=MAXY*2/3,fly_flag=1,fire_flag=1;
void drivers(void);
void control(void);
int AllocObj(void)
{ int i=0;
while (i<MAX_OBJ&&Objs[i].mode) i++;
if (i>=MAX_OBJ) i=MAX_OBJ-1;
return i;
}
int CheckHit(int x1,int y1,int x2,int y2,int r)
{
if (abs(x1-x2)<=r&&abs(y1-y2)<=r) return 1;
return 0;
}
ulong fclock(void)
{
ulong t;
disable(); outportb(0x43,0); t=inportb(0x40);
t+=(inportb(0x40)<<8); t=0xffff^t; enable();
return (clock()<<16)+t;
}
char GameStart(void)
{
ulong start=0;
timepass=0;
while (!GameOver)
{
while (fclock()-start<=45000L); start=fclock();
clear();
timepass++;
control();
drivers();
show();
if (--Sound<=0) nosound();
if ((timepass&3)==0) printf("%d/r",GameScore);
if (keys[KEY_ESC]) GameOver=1;
}
sound(105); delay(700); nosound();
return 1;
}
void control(void)
{
int i,j,x,y, ok;
for (i=0;i<MAX_STAR;i++) {
Stars[i].y+=Stars[i].v;
if (Stars[i].y>MAXY) {
Stars[i].x=random(320);
Stars[i].y=-random(60);
}
if (Stars[i].v==1) pixel(Stars[i].x,Stars[i].y,23);
else pixel(Stars[i].x,Stars[i].y,28);
}
for (i=0;i<MAX_OBJ;i++)
{ x=Objs[i].x; y=Objs[i].y; ok=1;
switch (Objs[i].mode)
{
case 1: if (Objs[i].index==0) {
switch (Objs[i].type)
{ case 0: if (!random(30)) Objs[i].type=1; break;
case 1: if (x<fly_x) x++; if (x>fly_x) x--;
if (!random(40)) Objs[i].type=2; break;
case 2:
default:if (!random(3)) y+=3;
break;
}
if (CheckHit(x,y,fly_x,fly_y,15)) fly_flag=0;
putimage(x,y,fly_pic,0);
} else {
if ((Objs[i].index&3)==0) putimage(x,y,fly_pic,0);
if (++Objs[i].index>50) Objs[i].mode=0;
}
y++; if (y>MAXY) Objs[i].mode=0;
break;
case 2: y-=4; if (y<-20) Objs[i].mode=0; drawfire(x,y);
for (j=0;j<MAX_OBJ;j++)
if (Objs[j].mode==1&&CheckHit(x,y,Objs[j].x,Objs[j].y,15)&&
!Objs[j].index&&ok)
{
Objs[j].index=1; ok=0; Objs[i].mode=0;
sound(220); Sound=10; GameScore+=10;
}
break;
}
Objs[i].x=x; Objs[i].y=y;
}
if (keys[KEY_UP]) if (--fly_y<0) fly_y=0;
if (keys[KEY_DOWN]) if (++fly_y>MAXY) fly_y=MAXY;
if (keys[KEY_LEFT]) if (--fly_x<0) fly_x=0;
if (keys[KEY_RIGHT]) if (++fly_x>319) fly_x=319;
if (keys[KEY_CTRL]&&fire_flag) {
i=AllocObj(); Objs[i].x=fly_x,
Objs[i].y=fly_y-10, Objs[i].mode=2; fire_flag=0;
}
if (!keys[KEY_CTRL]) fire_flag=1;
putimage(fly_x,fly_y,fly_pic,1);
if (!fly_flag) GameOver=1;
}
void drivers(void)
{
if (random(30)==0)
{ int i=AllocObj(); Objs[i].x=random(320); Objs[i].y=-random(20);
Objs[i].mode=1; Objs[i].type=Objs[i].index=0;
if (!random(20)) Objs[i].type=2;
}
}