计算机二级C++辅导:简单打飞碟游戏
来源:优易学  2010-1-14 19:16:47   【优易学:中国教育考试门户网】   资料下载   IT书店

  同样是最初步的模拟,有待再完善下去.飞碟速度上的问题还是希望大家自己调整gamespeed的速度.
  空格是发射,发射的具体样子我没写.
  #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)
  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)/*画瞄准器*/
  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;

[1] [2] 下一页

责任编辑:cyth

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训