WEB开发网
开发学院软件开发C++ 简单按键游戏 阅读

简单按键游戏

 2008-03-08 12:27:57 来源:WEB开发网   
核心提示:想编个贪吃蛇游戏,但是在编写这个游戏之前想先练练兵,所以就编了这个按键游戏,没有用多少色彩,而且很菜,都挤在一起了,请大家多多指教.#include "conio.h"#include "stdlib.h"#include "graphics.h" main(
想编个贪吃蛇游戏,但是在编写这个游戏之前想先练练兵,所以就编了这个按键游戏,没有用多少色彩,而且很菜,都挤在一起了,请大家多多指教. #include "conio.h"
#include "stdlib.h"
#include "graphics.h"
 main()
{char a[36];
 int i,j,x,key,t,score=0;
 int gd=DETECT,gr;
 initgraph(&gd,&gr,"");
 cleardevice();
 for(i=0;i<10;i++)
 a[i]=48+i;
 for(i=10;i<36;i++)
 a[i]=55+i;
 a[i]=0;
 for(i=1;i<81;i++) /*游戏初始化*/
 
 gotoxy(5,1);
 PRintf("score:");
 settextstyle(1,0,8);
 outtextxy(100,350,"TYPE GAME"); /*初始化结束*/
 sleep(2);
 randomize();
 for(;;)
 {
 gotoxy(12,1);
 printf("   ");
 gotoxy(12,1);
 printf("%d",score);
  i=rand()%80; /*随机赋值*/
  if(i==0)
  i=1;
  x=rand()%36;
  t=0;
  for(j=4;!kbhit()&&j!=18;j++) /*自动移动*/
  {
  gotoxy(i,j);
  printf("%c",a[x]);
  gotoxy(i,j);
  if(score<10) /*难度设定*/
  delay(10000);
  else if(score<20)
  delay(7000);
  else if(score<30)
  delay(5000);
  else if(score<40)
  delay(4000);
  if(score==40) /*游戏胜利*/
  {
  clrscr();
  textmode(C40);
  delay(100000);
  gotoxy(18,12);
  printf("YOU WIN!");
  delay(100000);
  exit(1);
  }
  gotoxy(i,j);
  printf(" ");
  gotoxy(i,j);
  }  /*自动移动结束一个循环*/
  if(j==18)
  {
  t=1;
  score--;
  }
  if(t==1)
  continue;
 key=bioskey(0);
  if(key==0x011b)
  break;
  else if(key==0x231&&a[x]==49) /*判定按键,其实用宏定义可以节省行数*/
  {
  score++;
  continue;
  }
  else if(key==0x332&&a[x]==50)
  {
  score++;
  continue;
  }
  else if(key==0x433&&a[x]==51)
  {
  score++;
  continue;
  }
  else if(key==0x534&&a[x]==52)
  {
  score++;
  continue;
  }
  else if(key==0x635&&a[x]==53)
  {
  score++;
  continue;
  }
  else if(key==0x736&&a[x]==54)
  {
  score++;
  continue;
  }
  else if(key==0x837&&a[x]==55)
  {
  score++;
  continue;
  }
  else if(key==0x938&&a[x]==56)
  {
  score++;
  continue;
  }
  else if(key==0x939&&a[x]==57)
  {
  score++;
  continue;
  }
  else if(key==0xb30&&a[x]==48)
  {
  score++;
  continue;
  }
  else if(key==0x1e61&&a[x]==65)
  {
  score++;
  continue;
  }
  else if(key==0x3062&&a[x]==66)
  {
  score++;
  continue;
  }
  else if(key==0x2e63&&a[x]==67)
  {
  score++;
  continue;
  }
  else if(key==0x2064&&a[x]==68)
  {
  score++;
  continue;
  }
  else if(key==0x1265&&a[x]==69)
  {
  score++;
  continue;
  }
  else if(key==0x2166&&a[x]==70)
  {
  score++;
  continue;
  }
  else if(key==0x2267&&a[x]==71)
  {
  score++;
  continue;
  }
  else if(key==0x2368&&a[x]==72)
  {
  score++;
  continue;
  }
  else if(key==0x1769&&a[x]==73)
  {
  score++;
  continue;
  }
  else if(key==0x246a&&a[x]==74)
  {
  score++;
  continue;
  }
  else if(key==0x256b&&a[x]==75)
  {
  score++;
  continue;
  }
  else if(key==0x266c&&a[x]==76)
  {
  score++;
  continue;
  }
  else if(key==0x326d&&a[x]==77)
  {
  score++;
  continue;
  }
  else if(key==0x316e&&a[x]==78)
  {
  score++;
  continue;
  }
  else if(key==0x186f&&a[x]==79)
  {
  score++;
  continue;
  }
  else if(key==0x1970&&a[x]==80)
  {
  score++;
  continue;
  }
  else if(key==0x1071&&a[x]==81)
  {
  score++;
  continue;
  }
  else if(key==0x1372&&a[x]==82)
  {
  score++;
  continue;
  }
  else if(key==0x1f73&&a[x]==83)
  {
  score++;
  continue;
  }
  else if(key==0x1474&&a[x]==84)
  {
  score++;
  continue;
  }
  else if(key==0x1675&&a[x]==85)
  {
  score++;
  continue;
  }
  else if(key==0x2f76&&a[x]==86)
  {
  score++;
  continue;
  }
  else if(key==0x1177&&a[x]==87)
  {
  score++;
  continue;
  }
  else if(key==0x2d78&&a[x]==88)
  {
  score++;
  continue;
  }
  else if(key==0x1579&&a[x]==89)
  {
  score++;
  continue;
  }
  else if(key==0x2c7a&&a[x]==90)
  {
  score++;
  continue;
  }
  else
  {
  score--;
  continue;
  }
 }
 cleardevice();
 outtextxy(100,200,"GAME OVER");  /*假如异常退出表示失败*/
 getch();
 closegraph();
}


Tags:简单 按键 游戏

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接