求c++贪吃蛇的代码

详细点用过了,看不大懂... 详细点
用过了,看不大懂
展开
 我来答
jhanker
2019-09-11 · TA获得超过1478个赞
知道小有建树答主
回答量:675
采纳率:73%
帮助的人:496万
展开全部
/*
贪吃蛇游戏,由于是C++源码 且 用到Windows API ,是控制台界面不是图形界面,需要用VC++6.0 或 VC++2010 在windows环境编译运行。如果符合上述条件一定可以编译运行zjlj,2015.3.16
*/
#define DEBUG 0 //当程序在调试阶段时 DEBUG为 1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
void readini(FILE **fphead, int *score, char *argv[]) //创建或打开一个和运行文件对应的ini文件,读取最高纪录
{
 char filename[200],*pfilename;
 int flag=-1,i;
     
    strcpy(filename,argv[0]);
    for(i=0;filename[i]!='\0';i++)
 {
  if ('.'==filename[i])flag=1;
 }
  
 if(1==flag)
 {
 filename[i-1]='i';
    filename[i-2]='n';
 filename[i-3]='i';
 }
 else
 {
  filename[i]='.';
 filename[i+1]='i';
 filename[i+2]='n';
    filename[i+3]='i';
    filename[i+4]='\0';
 }
 for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i];
    if ( (*fphead=fopen(pfilename, "rb+"))==NULL)
 {
        if ( (*fphead=fopen(pfilename, "wb+"))==NULL)
  {
    printf("无法创建或打开\"%s\"文件\n",pfilename);
    system("pause");
       exit(0);
  }
    }
 else
 {
  fread(score,sizeof(int),1,*fphead);
 }
}
void writeini(FILE **fphead, int *score, char *argv[])  //打开一个和运行文件对应的ini文件,写入最高纪录
{
 char filename[200],*pfilename;
 int flag=-1,i;
    
    strcpy(filename,argv[0]);
    for(i=0;filename[i]!='\0';i++)
 {
  if ('.'==filename[i])flag=1;
 }
  
 if(1==flag)
 {
 filename[i-1]='i';
    filename[i-2]='n';
 filename[i-3]='i';
 }
 else
 {
  filename[i]='.';
 filename[i+1]='i';
 filename[i+2]='n';
    filename[i+3]='i';
    filename[i+4]='\0';
 }
 for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i];
    if ( (*fphead=fopen(pfilename, "wb+"))==NULL)
 {
          printf("无法写入\"%s\"文件,磁盘写保护!\n",pfilename);
    system("pause");
       exit(0);
 }
 else
 {
  rewind(*fphead);
  fwrite(score,sizeof(int),1,*fphead);
  fclose(*fphead);
 }
}
void gotoxy(int x,int y)//光标定位,光标定位函数SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸
{
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
 
void Refresh(int q[][22], int grade, int gamespeed, int length,int score) //  输出贪吃蛇棋盘
{
 int i,j;
 for(i=0;i<22;i++)
 {
  for(j=0;j<22;j++)
  {
   if(q[i][j]==0)//输出棋盘空白
   {
    gotoxy(i,j);
    color(11);
    cout<<"■";
   }
   if(q[i][j]==1||q[i][j]==2)//输出棋盘墙壁
   {  
    gotoxy(i,j);
    color(11);
    cout<<"□";
   }
   if(q[i][j]==3)//输出蛇头
   {  
    gotoxy(i,j);
    color(14);
    cout<<"★";
   }
   if(q[i][j]==4)//输出蛇身
   {  
    gotoxy(i,j);
    color(12);
    cout<<"◆";
   }
     if(q[i][j]==5)//输出果子
   {  
    gotoxy(i,j);
    color(12);
    cout<<"●";
   }
  }
  if(i==0) cout << "\t***********************";
  if(i==1) cout << "\t等级为:" << grade;//显示等级
  if(i==3) cout << "\t自动前进时间";
  if(i==4) cout << "\t间隔为:" << gamespeed << "ms";//显示时间
     if(i==6) cout << "\t历史最高分为:" << score << "分";
  if(i==7) cout << "\t你现在得分为:" << (length+(grade-1)*8)*10 << "分";
  if(i==8) cout << "\t**********************";
     if(i==9) cout << "\t游戏说明:";
     if(i==10) cout << "\t(1)用小键盘方向键控制";
  if(i==11) cout << "\t蛇头运动方向;";
  if(i==12) cout << "\t(2)蛇每吃一个果子蛇身";
  if(i==13) cout << "\t增加一节;";
  if(i==14) cout << "\t(3)蛇咬到自己或碰到墙";
  if(i==15) cout << "\t壁游戏结束。";
  if(i==18) cout << "\t**********************";
     if(i==19) cout << "\tC/C++语言作业:";
     if(i==20) cout << "\tzjlj,2015.03.16 ";
 }
}
  
int main(int argc, char *argv[]){
    int tcsQipan[22][22];     //  贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
    int i,j,score,directiontemp;
 FILE  *fpini;//*fpini 信息文件
 readini(&fpini, &score, argv);//读取ini文件的最高纪录
 if (score<0)//最高成绩小于零设置为零,初建文件会是负数
  score=0;
 while(1)
 {
  for(i=1;i<=20;i++)
   for(j=1;j<=20;j++)
    tcsQipan[i][j]=0;    //贪吃蛇棋盘相应坐标标上中间空白部分的标志0
  for(i=0;i<=21;i++)
   tcsQipan[0][i] = tcsQipan[21][i] = 1;      //贪吃蛇棋盘相应坐标标上上下墙壁的标志1
  for(i=1;i<=20;i++)
   tcsQipan[i][0] = tcsQipan[i][21] = 2;      //贪吃蛇棋盘相应坐标标上左右墙壁的标志2
  int tcsZuobiao[2][500];     //蛇的坐标数组
  for(i=0; i<4; i++)
  {
   tcsZuobiao[0][i] = 1;//蛇身和蛇头的x坐标
   tcsZuobiao[1][i] = i + 1;//蛇身和蛇头的y坐标
  }
  int head = 3,tail = 0;//标示蛇头和蛇尾的数组偏移量
  for(i=1;i<=3;i++)
   tcsQipan[1][i]=4;    //蛇身
  tcsQipan[1][4]=3;       //蛇头
  int x1, y1;           // 随机出果子
  srand(time(0));//设置随机种子
  do
  {
   x1=rand()%20+1;
   y1=rand()%20+1;
  }
  while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子
  tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5
  color(12);
  cout<<"\n\n\t\t\t\t贪吃蛇游戏即将开始 !"<<endl;//准备开始
  long start,starttemp;
  int grade = 1, length = 4;  //设置初始等级和蛇的初始长度
  int gamespeed = 500;  //设置初始前进时间间隔
  for(i=3;i>=0;i--)
  {
   start=clock();
   while(clock()-start<=1000);
   system("cls");
   if(i>0)
    cout << "\n\n\t\t\t\t进入倒计时:" << i << endl;  //倒计时显示
   else
    Refresh(tcsQipan,grade,gamespeed,length,score);  //初始棋盘显示
  }
  int timeover=1,otherkey=1;//初始化超时时间和按键判断参数
  char direction = 77;  // 设置初始情况下,向右运动
  int x=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇头坐标到x,y变量
  while(1)//运行一局游戏
  {
   start = clock();
   while((timeover=((starttemp=clock())-start<=gamespeed))&&!kbhit());//如果有键按下或时间超过自动前进时间间隔则终止循环
   if(direction==72||direction==80||direction==75 ||direction==77)
   directiontemp=direction;//保留上一次方向按键
            //starttemp=gamespeed+start-starttemp;//保留停留时间
   if(timeover)
   {
    #if (DEBUG==1)
    direction = getch();//调试代码
             #else
    if((direction =getch())==-32)
     direction = getch();
       #endif
   }
             #if (DEBUG==1)//调试代码
       start=clock();
    while(clock()-start<=2000);
    gotoxy(24,4);
    cout << "\t按键ASCII代码"<<(int)direction<<"    "<<endl;
             #endif
    if(!(direction==72||direction==80||direction==75 ||direction==77))
    {   
     otherkey=0;//  按键非方向键,otherkey设置为0
    }
    else
    {
     otherkey=1;//  按键为方向键,otherkey设置为1
    }
             if(direction==72 && directiontemp==80)//忽略反方向按键
    {
        direction=32;
     otherkey=0;
                 //start = clock();
        //while(clock()-start<=starttemp);
    }
    else if(direction==80 && directiontemp==72)
   {
        direction=32;//设置按键为非方向键
      otherkey=0;//  按键为非方向键,otherkey设置为0
                // start = clock();
       //while(clock()-start<=starttemp);//补偿等待时间
    }
    else if(direction==75 && directiontemp==77)
    {
        direction=32;
     otherkey=0;
                 //start = clock();
        //while(clock()-start<=starttemp);
    }
    else if(direction==77 && directiontemp==75)
    {
        direction=32;
     otherkey=0;
                 //start = clock();
        //while(clock()-start<=starttemp);
    }
     
     
    switch(direction)//判断方向键
    {
     case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break;      // 向上
     case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break;      // 向下
     case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break;      // 向左
     case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;break;      // 向右
     default: break;
    }
    
  
    if(x==0 || x==21 ||y==0 || y==21)      // 蛇头碰到墙壁,结束本局游戏
    {  
     gotoxy(22,12);
     cout << "\t游戏已结束!" << endl;
     if(score>=(length+(grade-1)*8)*10)//判断是否破记录
     {
      gotoxy(10,7);
      color(12);
      cout << "闯关失败 加油耶!" << endl;
      fclose(fpini);//关闭ini文件
     }
     else
     {
      gotoxy(10,7);
      color(12);
      cout << "恭喜您打破记录" << endl;
      score=(length+(grade-1)*8)*10;
      writeini(&fpini, &score, argv);//写入ini文件的最高纪录
     }
     gotoxy(23,12);
        cout << "按回车键重新开始,按ESC退出游戏" << endl;//显示的提示
     break;//退出该局游戏
    }
    if(tcsQipan[x][y]!=0&&!(x==x1&&y==y1)&&tcsQipan[x][y]!=3) //   蛇头碰到蛇身,结束本局游戏
    {
     gotoxy(22,12);
     cout << "\t游戏已结束!" << endl;
     if(score>=(length+(grade-1)*8)*10)//判断是否破记录
     {
      gotoxy(10,7);
      color(12);
      cout << "闯关失败 加油耶!" << endl;
      fclose(fpini);//关闭ini文件
     }
     else
     {
      gotoxy(10,7);
      color(12);
      cout << "恭喜您打破记录" << endl;
      score=(length+(grade-1)*8)*10;
      writeini(&fpini, &score, argv);//写入ini文件的最高纪录
     }
     gotoxy(23,12);
     cout << "按回车键重新开始,按ESC退出游戏" << endl;//显示的提示
     break;//退出该局游戏
    }
    /*
    游戏运行时的核心算法开始
    */
    if(x==x1 && y==y1) //  吃果子,长度加1
    {   
     length ++;
     if(length>=8)//长度大于等于8重新计算长度,等级加1
     {
      length -= 8;//重新计算长度
      grade ++;//等级加1
      if(gamespeed>50)//控制最快速度为50
       gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
     }
     tcsQipan[x][y]= 3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3
     tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4
     head = (head+1)%400;//防止数组越界
     tcsZuobiao[0][head] = x;//蛇头的x坐标
     tcsZuobiao[1][head] = y;//蛇头的y坐标
     do//随机出果子
     {
      x1=rand()%20+1;
      y1=rand()%20+1;
     }
     while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子
     tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5
     gotoxy(22,12);
     cout << "\t游戏进行中!" << endl;
     Refresh(tcsQipan,grade,gamespeed,length,score);
    }
    else  //  不吃果子
    {  
     if(otherkey)
     {
      tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0;
      tail=(tail+1)%400;//防止数组越界
      tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;
      head=(head+1)%400;//防止数组越界
      tcsZuobiao[0][head]=x;//蛇头的x坐标
      tcsZuobiao[1][head]=y;//蛇头的y坐标
      tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3;
      gotoxy(22,12);
      cout << "\t游戏进行中!" << endl;
      Refresh(tcsQipan,grade,gamespeed,length,score);
     }
     else
     {
      gotoxy(22,12);
      cout << "\t游戏暂停中!" << endl;
     }
    }
    /*
    游戏运行时的核心算法结束
    */
       }
    while(1)
    {
     while(!kbhit());
     if((direction =getch())==13)//按回车键开始下一局
      break;
     if(direction ==27)//按ESC退出游戏
      exit(0);
    }
       system("cls");//清除屏幕重新开始
 }
 return 0;
}
兔兔蜜桃酱
推荐于2017-09-18 · TA获得超过779个赞
知道小有建树答主
回答量:278
采纳率:0%
帮助的人:228万
展开全部
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/

DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}

Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}

/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}

void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}

void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /*画出上边框*/
rectangle(i,451,i+10,460); /*画出下边框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*画出左边框*/
rectangle(601,i,610,i+10); /*画出右边框*/
}
}

void GamePlay(char ch)
{
randomize(); /*随机数发生器*/
food.yes=1; /*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判断食物是否出现在整格里*/
food.yes=0; /*现在有食物了*/
}
if(food.yes==0) /*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/
}

switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++) /*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循环*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/

snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移动后将后面的身体擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}

void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}

void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}

贪吃蛇
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
310934797
2009-05-31 · TA获得超过1508个赞
知道小有建树答主
回答量:452
采纳率:0%
帮助的人:250万
展开全部
我给你,但是具体的编译还要靠你,还有界面

#include "graphics.h"
#include "stdio.h"

#define MAX 200
#define MAXX 30
#define MAXY 30

#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#define ESC 283
#define ENTER 7181
#define PAGEUP 18688
#define PAGEDOWN 20736
#define KEY_U 5749
#define KEY_K 9579
#define CTRL_P 6512
#define TRUE 1
#define FALSE 0
#define GAMEINIT 1
#define GAMESTART 2
#define GAMEHAPPY 3
#define GAMEOVER 4

struct SPlace
{
int x;
int y;
int st;
} place[MAX];
int speed;
int count;
int score;
int control;
int head;
int tear;
int x,y;
int babyx,babyy;
int class;
int eat;
int game;
int gamedelay[]={5000,4000,3000,2000,1000,500,250,100};
int gamedelay2[]={1000,1};
static int hitme=TRUE,hit = TRUE;
void init(void);
void nextstatus(void);
void draw(void);

void init(void)
{
int i;
for(i=0;i<MAX;i++)
{
place[i].x = 0;
place[i].y = 0;
place[i].st = FALSE;
}
place[0].st = TRUE;
place[1].st = TRUE;
place[1].x = 1;
speed = 9;
count = 0;
score = 0;
control = 4;
head = 1;
tear = 0;
x = 1;
y = 0;
babyx = rand()%MAXX;
babyy = rand()%MAXY;
eat = FALSE;
game = GAMESTART;
}
void nextstatus(void)
{
int i;
int exit;
int xx,yy;
xx = x;
yy = y;
switch(control)
{
case 1: y--; yy = y-1; break;
case 2: y++; yy = y+1; break;
case 3: x--; xx = x-1; break;
case 4: x++; xx = x+1; break;
}
hit = TRUE;
if ( ((control == 1) || (control ==2 )) && ( (y < 1) ||(y >= MAXY-1)) ||
(((control == 3) || (control == 4)) && ((x < 1) ||(x >= MAXX-1) ) ) )
{
hit = FALSE;
}

if ( (y < 0) ||(y >= MAXY) ||
(x < 0) ||(x >= MAXX) )
{
game = GAMEOVER;
control = 0;
return;
}
for (i = 0; i < MAX; i++)
{
if ((place[i].st) &&
(x == place[i].x) &&
(y == place[i].y) )
{
game = GAMEOVER;
control = 0;
return;
}
if ((place[i].st) &&
(xx == place[i].x) &&
(yy == place[i].y) )
{
hit = FALSE;
goto OUT;
}

}
OUT:
if ( (x == babyx) && (y == babyy) )
{
eat = TRUE;
count ++;
score += (1+class) * 10;
}

head ++;
if (head >= MAX) head = 0;
place[head].x = x;
place[head].y = y;
place[head].st= TRUE;
if (eat == FALSE)
{
place[tear].st = FALSE;
tear ++;
if (tear >= MAX) tear = 0;
}
else
{
eat = FALSE;
exit = TRUE;
while(exit)
{
babyx = rand()%MAXX;
babyy = rand()%MAXY;
exit = FALSE;
for( i = 0; i< MAX; i++ )
if( (place[i].st)&&( place[i].x == babyx) && (place[i].y == babyy))
exit ++;
}

}
if (head == tear) game = GAMEHAPPY;

}
void draw(void)
{
char temp[50];
int i,j;
for (i = 0; i < MAX; i++ )
{
setfillstyle(1,9);
if (place[i].st)
bar(place[i].x*15+1,place[i].y*10+1,place[i].x*15+14,place[i].y*10+9);
}
setfillstyle(1,4);
bar(babyx*15+1,babyy*10+1,babyx*15+14,babyy*10+9);
setcolor(8);
setfillstyle(1,8);
bar(place[head].x*15+1,place[head].y*10+1,place[head].x*15+14,place[head].y*10+9);
/* for( i = 0; i <= MAXX; i++ )
line( i*15,0, i*15, 10*MAXY);
for( j = 0; j <= MAXY; j++ )
line( 0, j*10, 15*MAXX, j*10);
*/
rectangle(0,0,15*MAXX,10*MAXY);
sprintf(temp,"Count: %d",count);
settextstyle(1,0,2);
setcolor(8);
outtextxy(512,142,temp);
setcolor(11);
outtextxy(510,140,temp);
sprintf(temp,"1P: %d",score);
settextstyle(1,0,2);
setcolor(8);
outtextxy(512,102,temp);
setcolor(12);
outtextxy(510,100,temp);
sprintf(temp,"Class: %d",class);
setcolor(8);
outtextxy(512,182,temp);
setcolor(11);
outtextxy(510,180,temp);
}
main()
{
int pause = 0;
char temp[50];
int d,m;
int key;
int p;
static int keydown = FALSE;
int exit = FALSE;
int stchange = 0;
d = VGA;
m = VGAMED;
initgraph( &d, &m, "" );
setbkcolor(3);
class = 3;
init();
p = 1;
while(!exit)
{
if (kbhit())
{
key = bioskey(0);
switch(key)
{
case UP: if( (control != 2)&& !keydown)
control = 1;
keydown = TRUE;
break;
case DOWN: if( (control != 1)&& !keydown)
control = 2;
keydown = TRUE;
break;
case LEFT: if( (control != 4)&& !keydown)
control = 3;
keydown = TRUE;
break;
case RIGHT: if( (control != 3)&& !keydown)
control = 4;
keydown = TRUE;
break;
case ESC: exit = TRUE;break;
case ENTER: init();break;
case PAGEUP: class --; if (class<0) class = 0; break;
case PAGEDOWN: class ++;if (class>7) class = 7; break;
case KEY_U: if( ( (control ==1) ||(control ==2))&& !keydown)
control = 3;
else if(( (control == 3) || (control == 4))&& !keydown)
control = 1;
keydown = TRUE;
break;
case KEY_K: if( ( (control ==1) ||(control ==2))&& !keydown)
control = 4;
else if(( (control == 3) || (control == 4))&& !keydown)
control = 2;
keydown = TRUE;
break;
case CTRL_P:pause = 1 - pause; break;
}
}
stchange ++ ;
putpixel(0,0,0);
if (stchange > gamedelay[class] + gamedelay2[hit])
{
stchange = 0;
keydown = FALSE;
p = 1 - p;
setactivepage(p);
cleardevice();
if (!pause)
nextstatus();
else
{
settextstyle(1,0,4);
setcolor(12);
outtextxy(250,100,"PAUSE");
}
draw();

if(game==GAMEOVER)
{
settextstyle(0,0,6);
setcolor(8);
outtextxy(101,101,"GAME OVER");
setcolor(15);
outtextxy(99,99,"GAME OVER");
setcolor(12);
outtextxy(100,100,"GAME OVER");
sprintf(temp,"Last Count: %d",count);
settextstyle(0,0,2);
outtextxy(200,200,temp);
}

if(game==GAMEHAPPY)
{
settextstyle(0,0,6);
setcolor(12);
outtextxy(100,300,"YOU WIN");
sprintf(temp,"Last Count: %d",count);
settextstyle(0,0,2);
outtextxy(200,200,temp);
}
setvisualpage(p);
}
}
closegraph();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tsyj00000
2009-06-01 · TA获得超过213个赞
知道答主
回答量:144
采纳率:0%
帮助的人:107万
展开全部
#include
#include
#include
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/

DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}

Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}

/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}

void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}

void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /*画出上边框*/
rectangle(i,451,i+10,460); /*画出下边框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*画出左边框*/
rectangle(601,i,610,i+10); /*画出右边框*/
}
}

void GamePlay(char ch)
{
randomize(); /*随机数发生器*/
food.yes=1; /*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判断食物是否出现在整格里*/
food.yes=0; /*现在有食物了*/
}
if(food.yes==0) /*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/
}

switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++) /*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]595||snake.y[0]455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循环*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/

snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移动后将后面的身体擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}

void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}

void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
漆越矫天慧
2020-04-01 · TA获得超过3688个赞
知道大有可为答主
回答量:3122
采纳率:30%
帮助的人:166万
展开全部
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define
LEFT
0x4b00
#define
RIGHT
0x4d00
#define
DOWN
0x5000
#define
UP
0x4800
#define
ESC
0x011b
int
i,key;
int
score=0;
int
gamespeed=32000;
struct
Food
/*食物的结构体*/
{
int
x;
/*食物的横坐标*/
int
y;
/*食物的纵坐标*/
int
yes;
/*食物是否出现的变量*/
}food;
struct
Snack
/*蛇的结构体*/
{
int
x[N];
int
y[N];
int
node;
/*蛇的节数*/
int
direction;
/*蛇的方向*/
int
life;
/*蛇的生命,0活着,1死亡*/
}snake;
void
Init(void);
/*图形驱动*/
void
Close(void);
/*关闭游戏函数*/
void
DrawK(void);
/*画图函数*/
void
GameOver(void);/*输出失败函数*/
void
GamePlay();
/*游戏控制函数
主要程序*/
void
PrScore(void);
/*分数输出函数*/
DELAY(char
ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed);
/*delay是延迟函数*/
delay(gamespeed);
}
else
if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char
ch;
printf("Please
choose
the
gamespeed:\n");
printf("1-Fast
2-Normal
3-Slow\n");
printf("\nPlease
Press
The
numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void
main(void)
{
int
ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void
Init(void)
{
int
gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void
DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
/*画出上边框*/
rectangle(i,451,i+10,460);
/*画出下边框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10);
/*画出左边框*/
rectangle(601,i,610,i+10);
/*画出右边框*/
}
}
void
GamePlay(char
ch)
{
randomize();
/*随机数发生器*/
food.yes=1;
/*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1)
/*可以重复游戏*/
{
while(!kbhit())
/*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1)
/*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
/*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++;
/*判断食物是否出现在整格里*/
food.yes=0;
/*现在有食物了*/
}
if(food.yes==0)
/*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)
/*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
/*贪吃蛇的身体移动算法*/
}
switch(snake.direction)
/*贪吃蛇的头部移动算法,以此来控制移动*/
{
case
1:snake.x[0]+=10;break;
case
2:snake.x[0]-=10;break;
case
3:snake.y[0]-=10;break;
case
4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)
/*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1)
/*如果死亡就退出循环*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)
/*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10);
/*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*现把增加的一节放到看不到的地方去*/
snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4);
/*每次移动后将后面的身体擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0);
/*接受按键*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}
void
GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME
OVER");
getch();
}
void
PrScore(void)
{
char
str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void
Close(void)
{
getch();
closegraph();
}
贪吃蛇
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(6)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式