/*简单潜艇大战
全局变量speed一开始需要调整,大家的速度不同
主要练习下这个游戏的基本算法,用简单的图形来模拟
----跳蚤侦察-------
*/
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
#define KEY_ESC 0x01
#define KEY_SPACE 0x39
#define KEY_LEFT 0x4b
#define KEY_RIGHT 0x4d
int speed=10000;
char key_state[128],key_pressed[128];/*键盘操作用的变量*/
int score=0;/*得分*/
char scorestr[10];/*输出分数的变量*/
struct Fire/*子弹的结构体*/
{
int x;
int y;
int start;/*是否发射炮弹*/
}amyfire[6];/*屏幕上最多有5个敌人可以发子弹*/
struct Play/*玩家的结构体*/
{
int x;
int y;
struct Fire fire[6];
life;
}play;
struct Amy/*敌人的结构体*/
{
int x;
int y;
int speed;/*敌人的速度*/
int color;
int direction;
life;
}amy[6];
void InitGraph(void);/*图形初始化*/
void CloseGraph(void);/*图形结束*/
void GamePlay(void);/*具体玩游戏*/
void DrawPlay(void);/*画玩家*/
void DrawAmy(int i);/*画敌人*/
void DrawFire(int x,int y,int n);/*画子弹,0去除,1显示*/
void DrawAmyFire(int x,int y,int n);/*画子弹,0去除,1显示*/
void DrawBlack(int x,int y,int color);/*去除原来地方的物体*/
void PrScore();/*输出得分*/
int GetKey(int ScanCode);/*这里开始都是按键函数*/
void interrupt far (*OldInt9Handler)();
void far interrupt NewInt9();
void InstallKeyboard();
void ShutDownKeyboard();
void main(void)
{
InitGraph();/*图形初始化*/
GamePlay();/*具体玩游戏*/
CloseGraph();/*图形结束*/
}
void InitGraph(void)/*图形初始化*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
InstallKeyboard();
}
void DrawPlay(void)/*画玩家*/
{
setfillstyle(SOLID_FILL,6);
bar(play.x-20,play.y-10,play.x+20,play.y);
setfillstyle(SOLID_FILL,4);
bar(play.x-20,play.y-10,play.x-10,play.y);
}
void DrawAmy(int i)/*画敌人*/
{
setfillstyle(SOLID_FILL,amy[i].color);
bar(amy[i].x-20,amy[i].y-10,amy[i].x+20,amy[i].y);
setfillstyle(SOLID_FILL,GREEN);
if(amy[i].direction==1)
bar(amy[i].x-20,amy[i].y-10,amy[i].x-10,amy[i].y);
else
bar(amy[i].x+10,amy[i].y-10,amy[i].x+20,amy[i].y);
}
void DrawBlack(int x,int y,int color)/*去除原来地方的物体*/
{
setfillstyle(SOLID_FILL,color);
bar(x-20,y-10,x+20,y);
}
void DrawFire(int x,int y,int n)/*画子弹,0去除,1显示*/
{
if(n==0)
setfillstyle(SOLID_FILL,BLUE);
else
setfillstyle(SOLID_FILL,0);
setcolor(BLUE);
fillellipse(x,y,5,5);
}
void DrawAmyFire(int x,int y,int n)/*画子弹,0去除,1显示*/
{
if(n==0)
setfillstyle(SOLID_FILL,BLUE);
else
setfillstyle(SOLID_FILL,YELLOW);/*敌人的炮弹是黄的*/
setcolor(BLUE);
fillellipse(x,y,3,3);
}
void PrScore()/*输出得分*/
{
setfillstyle(SOLID_FILL,8);/*把原来分数去除*/
bar(30,0,200,40);
setcolor(11);/*输出新得分*/
settextstyle(0,0,2);
sprintf(scorestr,"%d",score);
outtextxy(30,20,scorestr);
}
void GamePlay(void)/*具体玩游戏*/
{
int i,j;
setfillstyle(SOLID_FILL,BLUE);
bar(0,100,640,480);
setfillstyle(SOLID_FILL,8);
bar(0,0,640,99);
play.x=200;play.y=99;/*玩家初始位置*/
for(i=0;i<6;i++)
{
play.fire[i].start=0;/*一开始的子弹都为没发射状态*/
amyfire[i].start=0;
amy[i].life=0;
}
play.life=1;
DrawPlay();/*玩家初始位置*/
PrScore();/*输出得分*/
randomize();
while(1)
{
for(i=0;i<6;i++)/*查找玩家是否有发射的子弹*/
{
if(play.fire[i].start==1)/*发射的子弹*/
{
DrawFire(play.fire[i].x,play.fire[i].y,0);/*去除原来子弹位置*/
play.fire[i].y+=3;
DrawFire(play.fire[i].x,play.fire[i].y,1);/*显示新位置*/
for(j=0;j<6;j++)/*判定子弹是否击中敌人*/ if(play.fire[i].x>(amy[j].x-22)&&play.fire[i].x<(amy[j].x+22)&&play.fire[i].y>(amy[j].y-14)&&play.fire[i].y<amy[j].y+5)/*击中敌人*/
{
play.fire[i].start=0;/*子弹消失*/
amy[j].life=0;/*敌人生命结束*/
score+=10;/*得分增加*/
DrawFire(play.fire[i].x,play.fire[i].y,0);/*去除原来子弹位置*/
DrawBlack(amy[j].x,amy[j].y,BLUE);/*去除敌人*/
PrScore();
}
if(play.fire[i].y>480)/*子弹超出屏幕就等于消失*/
{
play.fire[i].start=0;
DrawFire(play.fire[i].x,play.fire[i].y,0);
}
}
}
for(i=0;i<6;i++)/*敌人炮弹的产生*/
{
if(amy[i].life==1&&amy[i].color<14&&amyfire[i].start==0)/*敌人发射炮弹的条件*/
{
amyfire[i].start=1;
amyfire[i].x=amy[i].x;
amyfire[i].y=amy[i].y-10;
}
}
for(i=0;i<6;i++)/*敌人炮弹移动以及移动*/
{
if(amyfire[i].start==1)/*子弹存在*/
{
DrawAmyFire(amyfire[i].x,amyfire[i].y,0);
amyfire[i].y-=2;
DrawAmyFire(amyfire[i].x,amyfire[i].y,1);/*画敌人炮弹新位置*/
}
if(amyfire[i].x>(play.x-14)&&amyfire[i].x<(play.x+14)&&(amyfire[i].y+3)<100)/*击中玩家*/
{
play.life=0;
amyfire[i].start=0;
DrawAmyFire(amyfire[i].x,amyfire[i].y,0);
DrawBlack(play.x,play.y,8);/*去除玩家*/
break;
}
if(amyfire[i].y<96)/*敌人炮弹超出海洋就消失*/
{
amyfire[i].start=0;
setfillstyle(SOLID_FILL,8);
setcolor(8);
fillellipse(amyfire[i].x,amyfire[i].y,3,3);
}
}
delay(speed);/*间隔时间可以自己定*/
if(play.life!=1)/*玩家被击中*/
break;
for(i=0;i<6;i++)
{
if(amy[i].life==0)/*出现一个敌人后就跳出循环*/
{
amy[i].life=1;
amy[i].color=rand()%4+10;/*敌人的颜色*/
amy[i].y=rand()%300+130;/*高度随机*/
amy[i].speed=2+rand()%10;/*敌人的移动速度随机*/
amy[i].direction=rand()%2;/*敌人的移动方向,0左边,1右边出来*/
if(amy[i].direction==0)
amy[i].x=-20;
else
amy[i].x=660;
break;
}
}
if(GetKey(KEY_ESC))/*退出键*/
break;
if(GetKey(KEY_SPACE))/*发射*/
{
for(i=0;i<6;i++)
if(play.fire[i].start==0)/*发射一颗子弹后跳出循环*/
{
play.fire[i].x=play.x;
play.fire[i].y=105;
pla
y.fire[i].start=1;
break;
}
}
if(GetKey(KEY_RIGHT)&&play.x<620)/*右键盘的控制*/
{
DrawBlack(play.x,play.y,8);
play.x+=10;
DrawPlay();
}
if(GetKey(KEY_LEFT)&&play.x>20)/*左键的控制*/
{
DrawBlack(play.x,play.y,8);
play.x-=10;
DrawPlay();
}
for(i=0;i<6;i++)/*显示敌人*/
{
if(amy[i].life==1)/*假如敌人存在*/
{
DrawBlack(amy[i].x,amy[i].y,BLUE);/*原来位置输出海洋颜色*/
if(amy[i].direction==0)
amy[i].x+=amy[i].speed;/*敌人横坐标的变化*/
else
amy[i].x-=amy[i].speed;
DrawAmy(i);/*画敌人*/
if(amy[i].x>660||amy[i].x<-20)/*敌人超出屏幕范围就消失*/
amy[i].life=0;
}
}
}
if(play.life==0)/*被击中失败的话*/
{
setcolor(6);
settextstyle(0,0,3);
outtextxy(250,40,"GAME OVER");
}
while(1)
{
if(GetKey(KEY_ESC))/*结束游戏按ESC*/
break;
}
}
void CloseGraph(void)/*图形结束*/
{
ShutDownKeyboard();
closegraph();
}
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[i]=key_pressed[i]=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;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |