| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 508 人关注过本帖
标题:贪吃蛇部分代码,吃了食物不能就不行了,求教大神!!!!!!!!!!!! ...
只看楼主 加入收藏
surface
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2013-8-8
结帖率:82.35%
收藏
已结贴  问题点数:10 回复次数:4 
贪吃蛇部分代码,吃了食物不能就不行了,求教大神!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define S 100
#define HEIGHT 20
#define WIDTH 80
int m,n;
void gotoxy(int x ,int y)
{
    COORD pos={x,y};
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut,pos);

}

void block()
{
    int i=0,j=0;
    gotoxy(i,j);
    while (i<WIDTH)
    {
        printf("@");
        i++;
    }
    i=0;j=1;
     while (j<HEIGHT-1)
    {
        gotoxy(i,j);
        printf("@");
        gotoxy(i+WIDTH-1,j);
        printf("@");
        j++;
    }
    while (i<WIDTH)
    {
        gotoxy(i,j);
        printf("@");
        i++;
    }
    }

 food()
{
   
    int x=(int)(rand()%79)+1;
    int y=(int)(rand()%19)+1;
    m=x;n=y;
    gotoxy(x,y);
    printf("*");
   
}

void start()
{
    int i,j;
    int count=0;
    char ch;
    int a[WIDTH*HEIGHT]={5,6,7,8};
    int b[WIDTH*HEIGHT]={5,5,5,5};
    food();
    for (i=0;;i++)
    {
        
        gotoxy(a[i],b[i]);
        printf(" ");
        for (j=1;j<count+3;j++)
        {
            gotoxy(a[i+j],b[i+j]);
            printf("*");
            
        }
        gotoxy(a[i+j],b[i+j]);
        printf("#");
        if (kbhit())
        {
            ch=getch();
            if (ch=='W'||ch=='w')
            {
                a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]-1;
            }
            else if (ch=='S'||ch=='s')
            {
                a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]+1;
            }
            else if (ch=='A'||ch=='a')
            {
                a[i+j+1]=a[i+j]-1;b[i+j+1]=b[i+j];
            }
             else if (ch=='D'||ch=='d')
            {
                a[i+j+1]=a[i+j]+1;b[i+j+1]=b[i+j];
            }
        }
        else if (b[i+j]==b[i+j-1]-1)
        {
            a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]-1;
        }
        else if (b[i+j]==b[i+j-1]+1)
        {
            a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]+1;
        }
        else if (a[i+j]==a[i+j-1]-1)
        {
            a[i+j+1]=a[i+j]-1;b[i+j+1]=b[i+j];
        }
        else if (a[i+j]==a[i+j-1]+1)
        {
            a[i+j+1]=a[i+j]+1;b[i+j+1]=b[i+j];
        }
        if (m==a[i+j+1]&&n==b[i+j+1])
        {
            count++;
            food();
        }
        Sleep(S);
    }
        
}
   
int main()
{
    srand((unsigned)time(NULL));
    block();
    start();
   
}
搜索更多相关主题的帖子: include 贪吃蛇 
2014-04-16 21:11
xiaozi2013
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:四川成都
等 级:蒙面侠
威 望:6
帖 子:830
专家分:4331
注 册:2013-12-13
收藏
得分:5 
感叹号!!

--整天敲那些破代码能找着媳妇儿吗。。。---
2014-04-16 21:35
surface
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2013-8-8
收藏
得分:0 
哪里错了啊,大神们???
2014-04-16 21:39
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:5 
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define S 100
#define HEIGHT 20
#define WIDTH 80
int m,n;
void gotoxy(int x ,int y)
{
    COORD pos={x,y};
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut,pos);

}

void block()
{
    int i=0,j=0;
    gotoxy(i,j);
    while (i<WIDTH)
    {
        printf("@");
        i++;
    }
    i=0;j=1;
    while (j<HEIGHT-1)
    {
        gotoxy(i,j);
        printf("@");
        gotoxy(i+WIDTH-1,j);
        printf("@");
        j++;
    }
    while (i<WIDTH)
    {
        gotoxy(i,j);
        printf("@");
        i++;
    }
}

void food()
{

    int x=(int)(rand()%79)+1;
    int y=(int)(rand()%19)+1;
    m=x;n=y;
    gotoxy(x,y);
    printf("*");

}

void start()
{
    int i,j;
    int count=0;
    char ch;
    int a[WIDTH*HEIGHT]={5,6,7,8};
    int b[WIDTH*HEIGHT]={5,5,5,5};
    food();
    for (i=0;;i++)
    {

        gotoxy(a[i],b[i]);
        printf(" ");
        for (j=1;j<count+3;j++)
        {
            gotoxy(a[i+j],b[i+j]);
            printf("*");

        }
        gotoxy(a[i+j],b[i+j]);
        printf("#");
        if (kbhit())
        {
            fflush(stdin);
            ch=getch();
            if (ch=='W'||ch=='w')
            {
                a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]-1;
            }
            else if (ch=='S'||ch=='s')
            {
                a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]+1;
            }
            else if (ch=='A'||ch=='a')
            {
                a[i+j+1]=a[i+j]-1;b[i+j+1]=b[i+j];
            }
            else if (ch=='D'||ch=='d')
            {
                a[i+j+1]=a[i+j]+1;b[i+j+1]=b[i+j];
            }
        }
        else if (b[i+j]==b[i+j-1]-1)
        {
            a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]-1;
        }
        else if (b[i+j]==b[i+j-1]+1)
        {
            a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]+1;
        }
        else if (a[i+j]==a[i+j-1]-1)
        {
            a[i+j+1]=a[i+j]-1;b[i+j+1]=b[i+j];
        }
        else if (a[i+j]==a[i+j-1]+1)
        {
            a[i+j+1]=a[i+j]+1;b[i+j+1]=b[i+j];
        }
        else if(m==a[i+j+1]&&n==b[i+j+1])
        {
            count++;
            food();
        }
        else;
        Sleep(S);
    }

}

int main()
{
    srand((unsigned)time(NULL));
    block();
    start();

}

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-04-17 01:17
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
另外提一点小意见吧

最好对错误的输入(非asde)也要有正确的反应

碰壁的时候反应有点不太对,建议楼主再自己仔细看看


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-04-17 01:18
快速回复:贪吃蛇部分代码,吃了食物不能就不行了,求教大神!!!!!!!!!! ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019526 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved