| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 672 人关注过本帖
标题:贪吃蛇行走问题
只看楼主 加入收藏
正人君子
Rank: 2
等 级:论坛游民
帖 子:10
专家分:28
注 册:2013-4-20
结帖率:0
收藏
 问题点数:0 回复次数:2 
贪吃蛇行走问题
#include <windows.h>
#include <iostream>
#include<conio.h>
using namespace std;
int touhang=5,toulie=4,mohang=5,molie=0,weizhi[20][50];
void move(char ch);
int main()
{
    char ch='d';
    int i=0,j=0,k=0;
    for(i=0;i<20;i++)
        for(j=0;j<50;j++)
            weizhi[i][j]=0;
    for(j=0;j<5;j++)
       weizhi[5][j]=1;
    weizhi[5][4]=2;//对数组赋值
        for(;;)
        {
            for(i=0;i<20;i++)
            {
               for(j=0;j<50;j++)
               {
                   if(weizhi[i][j]==0)
                      cout<<" ";
                   if(weizhi[i][j]==2)
                      cout<<"#";   //蛇头
                   if(weizhi[i][j]==1)
                      cout<<"*";
               }
                cout<<endl;
            }
         move(ch);
          if(kbhit())
            {
              ch=getch();
              move(ch);
            }
              Sleep(100);
              system("CLS");
        }
        return 0;
}
         void move(char ch)
         {
             if(ch=='a')
              {
                 if(toulie<49)
                 {  weizhi[touhang][toulie-1]=2;
                  weizhi[touhang][toulie]=1;
                  toulie=toulie-1;            //对头的操作
                 weizhi[mohang][molie]=0;
                 if(weizhi[mohang][molie-1]==1)
                    molie=molie-1;
                 if(weizhi[mohang][molie+1]==1)
                   molie=molie+1;
                 if(weizhi[mohang-1][molie]==1)
                    mohang=mohang-1;
                 if(weizhi[mohang+1][molie]==1)
                    mohang=mohang+1;                 //对尾巴的操作
                 }
              }
               if(ch=='d')
               {
                   if(toulie<49)
                   {  weizhi[touhang][toulie+1]=2;
                    weizhi[touhang][toulie]=1;
                    toulie=toulie+1;
                   weizhi[mohang][molie]=0;
                 if(weizhi[mohang][molie-1]==1)
                    molie=molie-1;
                 if(weizhi[mohang][molie+1]==1)
                    molie=molie+1;
                 if(weizhi[mohang-1][molie]==1)
                    mohang=mohang-1;
                 if(weizhi[mohang+1][molie]==1)
                    mohang=mohang+1;
                    }
               }
               if(ch=='w')
               {
                   weizhi[touhang-1][toulie]=2;
                    weizhi[touhang][toulie]=1;
                    touhang=touhang-1;
                   weizhi[mohang][molie]=0;
                 if(weizhi[mohang][molie-1]==1)
                    molie=molie-1;
                 if(weizhi[mohang][molie+1]==1)
                    molie=molie+1;
                 if(weizhi[mohang-1][molie]==1)
                    mohang=mohang-1;
                 if(weizhi[mohang+1][molie]==1)
                    mohang=mohang+1;
               }
             if(ch=='s')
             {  weizhi[touhang+1][toulie]=2;
                    weizhi[touhang][toulie]=1;
                    touhang=touhang+1;
                   weizhi[mohang][molie]=0;
                 if(weizhi[mohang][molie-1]==1)
                    molie=molie-1;
                 if(weizhi[mohang][molie+1]==1)
                    molie=molie+1;
                 if(weizhi[mohang-1][molie]==1)
                    mohang=mohang-1;
                 if(weizhi[mohang+1][molie]==1)
                    mohang=mohang+1;
             }
         }
像先让蛇走起来结果往下走时出问题了,找不到问题,求高手回复

搜索更多相关主题的帖子: include 贪吃蛇 
2013-06-18 21:25
蛋蛋的忧桑
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-9-23
收藏
得分:0 
同求
2013-10-01 11:18
Hover_shang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-10-10
收藏
得分:0 
我也遇到麻烦了,求助。谢谢!
2013-10-10 11:43
快速回复:贪吃蛇行走问题
数据加载中...
 
   



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

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