| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 999 人关注过本帖, 1 人收藏
标题:用数组和循环实现蛇形移动,贪吃蛇的初版,不能吃豆子,问题虽然还有很多, ...
只看楼主 加入收藏
卜算子
Rank: 1
等 级:新手上路
帖 子:86
专家分:8
注 册:2016-9-18
结帖率:82.35%
收藏(1)
 问题点数:0 回复次数:0 
用数组和循环实现蛇形移动,贪吃蛇的初版,不能吃豆子,问题虽然还有很多,对我来说这是一个成功!!我才学C一个月....我都被这个蛇骚扰两天了...
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define N 23
#define M 80
main()
{
int a[N][M];
int i, j, x, y,q,k,u,p,t,e;
char c;
    x=10;
    y=2;
q=x;
k=y-1;
u=x;
p=y-2;
    while(1)
    {
        system("cls");
        for(i = 0; i <N ; i++)
        {
            for(j = 0; j <M ; j++)
            {
                a[i][j]=0;
            }
        }
         a[x][y]=1;
         a[q][k]=2;
         a[u][p]=2;
        for(i = 0; i < N; i++)
        {
            for(j = 0; j < M; j++)

            {
                if(i==0||j==79||j==0||i==22)
                printf("-");
                else if(a[i][j]==0)
                    printf(" ");
                if(a[i][j]==1||a[i][j]==2)
                    printf("*");
            }
            printf("\n");
        }
        c=getch();
        if(c=='w')
            {   
            if(k==t&&q>x)
             {
                 q-=1;
             }
        if(p==t&&u>q)
        {
            u-=1;
        }                 
            x=x-1;
             t=y;
             if(k<t)
             {k+=1;
             }
             else if(k>t)
             {
                 k-=1;
             }
         if(p<t)
             {
                 p+=1;
             }
         else if(p>t)
         {
             p-=1;
         }
                   if(x==0)
                   {
                       printf("游戏结束\n");
                       Sleep(1000);
                   break;}
            
}
            else
            
                if(c=='a')
            {   
                if(k>=t&&q>x)
                {
                    q-=1;
                }
                else if(k>=t&&q<x)
                {
                    q+=1;
                }
                 if(q==x&&k>y)
                { k-=1;
                }
                if(p>=k&&u>q)
                {
                    u-=1;
                }
                else if(p>=k&&u<q)
                {
                    u+=1;
                }
                 if(u==x&&p>k)
                {

                    p-=1;
                }
                else if(u!=x&&p<k)
                {
                    p+=1;
                }
                    t=y;
                    y=y-1;

                 if(y==0)
                   {
                       printf("游戏结束\n");
                       Sleep(1000);
                   break;
                 }
            }
            
            else
                if(c=='s')
            {
                if(k==t&&q<x)
                {
                    q+=1;
                }
                if(p==t&&u<q)
                {
                    u+=1;
                }
                    x=x+1;
                t=y;
                if(k<t)
                {
                    k+=1;
                }
                else if(k>t)
                {
                    k-=1;
                }
                if(p<k)
                {
                    p+=1;
                }
                else if(p>k)
                {
                    p+=1;
                }
                 if(x==22)
                   {
                       printf("游戏结束\n");
                       Sleep(1000);
                   break;
                 }
            }
            
            else  
                if(c=='d')
            {
                    if(k<=t&&q>x)
                    {
                        q-=1;
                    }
                    else if(k<=t&&q<x)
                    {
                        q+=1;
                    }
                    else if(q==x&&k<y)
                    {
                        k+=1;
                    }
                    if(p<=k&&u>x)
                    {
                        u-=1;
                    }
                    else if(p<=k&&u<x)
                    {
                        u+=1;
                    }

                    else if(u==q&&p<k)
                    {
                        p+=1;
                    }
   
                    t=y;
                    y=y+1;
               if(y==79)
                   {
                       printf("游戏结束\n");
                       Sleep(1000);
                   break;}
           
               
                }
        if(c=='t') break;
     
       Sleep(500);
    }
}
搜索更多相关主题的帖子: include 贪吃蛇 system 
2016-11-20 19:34
快速回复:用数组和循环实现蛇形移动,贪吃蛇的初版,不能吃豆子,问题虽然还有很 ...
数据加载中...
 
   



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

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