#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#define key_esc 0x01
#define key_space 0x39
#define key_up 0x48
#define key_left 0x4b
#define key_right 0x4d
#define key_down 0x50
int gamespeed=1200;/*自己改游戏的速度*/
int speed;/*飞碟移动速度*/
int col;/*中心坐标*/
int score=0;/*得分*/
char key_state[128],key_pressed[128];/*键盘操作用的变量*/
void init();/*初始*/
void close();/*关闭*/
void playgame();/*游戏过程*/
void prscore();/*输出成绩*/
void drawm(int x,int y,int color);/*画瞄准器*/
void win();/*输出最后结果*/
int getkey(int scancode);/*这里开始都是按键函数*/
void interrupt far (*oldint9handler)();
void far interrupt newint9();
void installkeyboard();
void shutdownkeyboard();
drawfly(int x,int y,int color);/*画飞碟*/
void main(void)
{
init();
playgame();
close();
}
void prscore()/*输出成绩*/
{
char s[10];
setfillstyle(solid_fill,black);
bar(30,0,100,50);
setcolor(6);
settextstyle(0,0,2);
sprintf(s,"%d",score);
outtextxy(30,20,s);
}
void drawm(int x,int y,int color)/*画瞄准器*/
{
setcolor(color);
rectangle(x-10,y-10,x+10,y+10);
line(x-8,y,x+8,y);
line(x,y-8,x,y+8);
}
void win()/*输出最后结果*/
{
settextstyle(0,0,4);
setcolor(red);
if(score>18)
outtextxy(200,200,"very good");
else if(score>10)
outtextxy(250,200,"good");
else
outtextxy(250,200,"~@_@~");
}
void playgame()/*游戏过程*/
{
float x,y;/*飞碟的坐标*/
int mx,my;
int i,num=0;
for(i=40;i<640;i+=30)
drawfly(i,65,white);
mx=my=300;
setcolor(15);
line(0,80,640,80);
randomize();
while(num<20)
{
prscore();/*输出成绩*/
col=random(10);/*中心坐标随机*/
col=col*20+200;
speed=2+random(2);/*飞碟速度随机*/
for(x=-250;x<=250;x+=speed)/*飞碟移动全过程*/
{
y=pow((x/10),2)+200;/*求y坐标*/
drawfly(x,y,white);
drawm(mx,my,yellow);
delay(gamespeed);/*间隔*/
drawm(mx,my,black);
drawfly(x,y,black);
if(getkey(key_esc))/*结束游戏*/
break;
if(getkey(key_left))
mx-=4;
if(getkey(key_right))
mx+=4;
if(getkey(key_down))
my+=4;
if(getkey(key_up)&&my>100)
my-=4;
if(getkey(key_space))/*发子弹*/
{
if(((x+col+10)>=(mx-2)&&x<=(mx+2))&&(y>=(my-2)&&y<=(my+2)))/*这里控制精确度*/
{
score++;
drawfly(x,y,black);
drawm(mx,my,yellow);
prscore();
drawm(mx,my,black);
break;
}
}
if(y>490&&col+x>col)/*自动消失*/
break;
}
if(y<490)
{
setcolor(red);
line(40+num*30-10,55,40+num*30+10,75);
line(40+num*30-10,75,40+num*30+10,55);
}
num++;
if(getkey(key_esc))/*结束游戏*/
break;
}
win();
while(1)
{
if(getkey(key_esc))
break;
}
}
void init()/*初始*/
{ int gd=detect,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
installkeyboard();
}
void close()/*关闭*/
{
shutdownkeyboard();
closegraph();
}
drawfly(int x,int y,int color)/*画飞碟*/
{
setcolor(black);
setfillstyle(solid_fill,color);
fillellipse(col+x,y,10,6);
}
void far interrupt newint9(void)
{
unsigned char scancode,temp;
scancode=inportb(0x60);
temp=inportb(0x61);
outportb(0x61,temp | 0x80);
outportb(0x61,temp & 0x7f);
if(scancode&0x80)
{
scancode&=0x7f;
key_state[scancode]=0;
}
else
{
key_state[scancode]=1;
key_pressed[scancode]=1;
}
outportb(0x20,0x20);
}
void installkeyboard(void)
{
int i;
for(i=0;i<128;i++)
key_state=key_pressed=0;
oldint9handler=getvect(9); /*中断向量值*/
setvect(9,newint9); /*中断程序newint9地址存入指定的中断向量表中int 09h*/
}
void shutdownkeyboard(void)
{
setvect(9,oldint9handler);
}
int getkey(int scancode)
{
int res;
res=key_state[scancode]|key_pressed[scancode];
key_pressed[scancode]=0;
return res;
}
在 turboc2 下测试通过
作者: c闲人 转自 百度 |