回 帖 发 新 帖 刷新版面

主题:打飞碟游戏








最初步的模型以及算法.空格是发射,发射的具体效果没写,呵呵~~我个人觉得还有以下地方可以改:
1.飞碟的路径可以变化大些
2.飞碟出现的方向
3.每次飞碟出现后有规定子弹
4.闯关
本程序的算放十分简单,主要是练抛物线.
#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[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;
}
不好意思,是下载的,有兴趣的朋友研究一下啊!·

回复列表 (共10个回复)

沙发

太厉害了,请问你是在哪个网站下的?

板凳

[email]http://pchc.vicp.net/pchc/default.asp[/email]

3 楼

在什么下面编译通过啊,

4 楼

真棒!!!

5 楼

   篇幅好长

6 楼

很是费体力,不费脑袋

7 楼

求救!!!我的显示器是液晶的,就是不能显示这个程序的运行结果,编译倒是没有什么错误啊,是不是液晶显示器的显示方式有什么问题啊!!!请高手指教啊!

8 楼

不错啊,我一直不明白怎么用tc控制键盘,原来是用dos中断啊。
谢谢了。

9 楼

兄弟 ,我怎么一运行,就说缺少<graphics.h>头文件?

10 楼

请说是用什么键,表示“打击”。

我来回复

您尚未登录,请登录后再回复。点此登录或注册