这是贪吃蛇的代码,但是蛇不会动,求解答

qq_39289874 2017-06-24 10:51:58
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
char gamemap[20][40];//游戏地图大小20*40;
int score;//记录当前分数;
//*****记录蛇的节点;
int x[800];//记录节点的行编号;
int y[800];//记录节点的列编号;
int len=0;//蛇的长度;
//*****记录食物信息
int fx=0;//食物的横坐标;
int fy=0;//食物的纵坐标;
int fcount=0;//食物的数目;
//*****主要函数操作
void createfood();//生成食物;
void PrintgameMap(int x[],int y[]);//画游戏地图;
void move(int x[],int y[]);//移动蛇

int main()
{
srand(time(NULL));//初始化蛇头和身体的位置,默认刚开始蛇长为2;
x[len]=9;
y[len]=9;
len++;
x[len]=9;
y[len]=8;
len++;

createfood();
PrintgameMap(x,y);
move(x,y);
return 0;
}

void createfood()
{
if(fcount==0)
{
int tfx=rand()%18+1;
int tfy=rand()%38+1;
int i,j;
int have=0;//为0表示食物不是食物的一部分;
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
if(x[i]==fx&&y[j]==fy)
{
have=1;
break;
}
else
{
have=0;
}
}
if(have==1)//若为蛇的一部分,执行下一次循环
{
continue;
}
else//否则生成心的食物
{
fcount++;
fx=tfx;
fy=tfy;
break;
}
}
}
}
//游戏地图;
void PrintgameMap(int x[],int y[])
{
int snake=0,food=0;

int i,j;
//画游戏地图,并画出蛇的初始位置;
for(i<0;i<20;i++)
{
for(j<0;j<40;j++)
{
if(i==0&&j>=1&&j<=38)
{
gamemap[i][j]='=';
}
else if(i==19&&j>=1&&j<=38)
{
gamemap[i][j]='=';
}
else if(j==0||j==39)
{
gamemap[i][j]='#';
}
else
{
gamemap[i][j]=' ';
}
//判断蛇是否在当前位置
int k;
for(k=0;k<len;k++)
{
if(i==x[k]&&j==y[k])
{
snake=1;
break;
}
else
{
snake=0;
}
}
{
if(fcount&&fx==i&&fy==j)
{
food=1;
}
else
{
food=0;
}
}
//若蛇在当前位置
if(snake==1)
{
printf("*");
}
else if(food==1)
{
printf("f");
}
//若蛇不在当前位置,并且当前位置没有食物;
else
{
printf("%c",gamemap[i][j]);
}
}
printf("\n");
}
}
//移动
void move(int x[],int y[])
{
char s;
s=getch();
int move=0,beat=0;
while(1)
{
int cx[800];
int cy[800];
memcpy(cx,x,sizeof(int)*len);
memcpy(cy,y,sizeof(int)*len);
//头
if(s=='w')
{
x[0]--;
move=1;
if(x[0]<=0)
{
printf("Game over\n");
break;
}
}
else if(s=='s')
{
x[0]++;
move=1;
if(x[0]>=19)
{
printf("Game over\n");
break;
}
}
else if(s=='a')
{
y[0]--;
move=1;
if(y[0]<=0)
{
printf("Game over\n");
break;
}
}
else if(s=='d')
{
y[0]++;
move=1;
if(y[0]>=39)
{
printf("Game over\n");
break;
}
}
//身体;
int i;
for(i=1;i<len;i++)
{
x[i]=cx[i-1];
y[i]=cy[i-1];
}
for(i=1;i<len;i++)//要是咬了自己
{
if(x[0]==x[i]&&y[0]==y[i])
{
beat=1;
}
else
{
beat=0;
}
}
if(beat==1)
{
printf("Game over\n");
break;
}
if(move==1)
{
if(fcount&&x[0]==fx&&y[0]==fy)//如果吃到了食物
{
//拷贝当前蛇头地址到第二个结点
memcpy(x+1,cx,sizeof(int)*len);
memcpy(y+1,cy,sizeof(int)*len);
len++;
fcount--;
fx=0;
fy=0;
score++;
createfood();
}
Sleep(70);
system("cls");
PrintgameMap(x,y);
}
else
continue;
if(kbhit())//判断是否按下按键
{
s=getch();
}
}
}
...全文
974 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-08-16
  • 打赏
  • 举报
回复
仅供参考:
#include <conio.h>
#include <windows.h>

void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
   DWORD count;
   COORD coord = {x, y};
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hStdOut, coord);
   WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}
void HideTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = FALSE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

void ShowTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = TRUE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}
void GetWH(int *w,int *h) {
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (GetConsoleScreenBufferInfo(hStdOut, &csbi)) {
        *w=csbi.srWindow.Right;
        *h=csbi.srWindow.Bottom;
    } else {
        *w=80;
        *h=25;
    }
}
void ClearConsole()
{
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //   not used but we need to capture this since it will be
   //   written anyway (passing NULL causes an access violation).
   DWORD count;
   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

int main() {
    unsigned short k;
    int x,y,w,h;
    char d;

    SetConsoleOutputCP(437);
    ClearConsole();
    GetWH(&w,&h);
    x=w/2;y=h/2;
    HideTheCursor();
    ConPrintAt(x,y,"O",1);
    d='o';
    while (1) {
        Sleep(50);
        if (kbhit()) {
            k=getch();
            if (27==k) break;//按Esc键退出
            if (0==k||0xe0==k) k|=getch()<<8;//非字符键
            switch (k) {
                case 0x48e0:case 0x04800://上
                    d='u';
                    if (y>0) {
                        ConPrintAt(x,y," ",1);
                        y--;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
                case 0x50e0:case 0x05000://下
                    d='d';
                    if (y<h) {
                        ConPrintAt(x,y," ",1);
                        y++;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
                case 0x4be0:case 0x04b00://左
                    d='l';
                    if (x>0) {
                        ConPrintAt(x,y," ",1);
                        x--;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
                case 0x4de0:case 0x04d00://右
                    d='r';
                    if (x<w-1) {
                        ConPrintAt(x,y," ",1);
                        x++;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
            }
//          cprintf("%04x pressed.\r\n",k);
        } else {
            switch (d) {
                case 'u':
                    if (y>0) {
                        ConPrintAt(x,y," ",1);
                        y--;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
                case 'd':
                    if (y<h) {
                        ConPrintAt(x,y," ",1);
                        y++;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
                case 'l':
                    if (x>0) {
                        ConPrintAt(x,y," ",1);
                        x--;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
                case 'r':
                    if (x<w-1) {
                        ConPrintAt(x,y," ",1);
                        x++;
                        ConPrintAt(x,y,"O",1);
                    }
                break;
            }
        }
    }
    ClearConsole();
    ShowTheCursor();
    return 0;
}
saibotanren 2017-07-21
  • 打赏
  • 举报
回复
这绝对是条死蛇

21,459

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧