| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6227 人关注过本帖, 1 人收藏
标题:c语言贪吃蛇
只看楼主 加入收藏
mumu1596321
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-6-6
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:11 
c语言贪吃蛇
还需图形化界面处理 大家谈论讨论
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>


#define LEN 30
#define WID 25

int Snake[LEN][WID] = {0};
char Sna_Hea_Dir = 'a';//记录蛇头的移动方向
int Sna_Hea_X, Sna_Hea_Y;//记录蛇头的位置
int Snake_Len = 3;//记录蛇的长度
clock_t Now_Time;//记录当前时间,以便自动移动
int Wait_Time = 300;//记录自动移动的时间间隔
int Eat_Apple = 1;//吃到苹果表示为1
int Level = 1;
int All_Score = -1;
int Apple_Num = -1;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

void gotoxy(int x, int y)//设置光标位置
{
    COORD pos = {x,y};
   
    SetConsoleCursorPosition(hConsole, pos);

}

void Hide_Cursor()//隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(hConsole, &cursor_info);
}

void SetColor(int color)//设置颜色
{
    SetConsoleTextAttribute(hConsole, color);
}

void Print_Snake()//打印蛇头和蛇的脖子和蛇尾
{
    int iy, ix, color;
    for(iy = 0; iy < WID; ++iy)
        for(ix = 0; ix < LEN; ++ix)
        {

            if(Snake[ix][iy] == 1)//蛇头
            {
                SetColor(0xf);            
                gotoxy(ix*2, iy);
                printf("※");
            }
            if(Snake[ix][iy] == 2)//蛇的脖子
            {
                color = rand()%15 + 1;
                if(color == 14)
                    color -= rand() % 13 +1;
                SetColor(color);
                gotoxy(ix*2, iy);
                printf("■");
            }
            if(Snake[ix][iy] == Snake_Len)
            {
                gotoxy(ix*2, iy);
                SetColor(0xe);
                printf("≈");
            }
        }
}

 void Clear_Snake()//擦除贪吃蛇
{
    int iy, ix;
    for(iy = 0; iy < WID; ++iy)
        for(ix = 0; ix < LEN; ++ix)
        {
            gotoxy(ix*2, iy);
            if(Snake[ix][iy] == Snake_Len)
                printf("  ");
        }
}

void Rand_Apple()//随机产生苹果
{
    int ix, iy;

    do
    {
        ix = rand() % LEN;
        iy = rand() % WID;
    }while(Snake[ix][iy]);

    Snake[ix][iy] = -1;
    gotoxy(ix*2, iy);
    printf("⊙");
    Eat_Apple = 0;
}

void Game_Over()//蛇死掉了
{
    gotoxy(30, 10);
    printf("Game Over");
    Sleep(3000);
    system("pause > nul");
    exit(0);
}

void Move_Snake()//让蛇动起来
{
    int ix, iy;
   
    for(ix = 0; ix < LEN; ++ix)//先标记蛇头
        for(iy = 0; iy < WID; ++iy)
            if(Snake[ix][iy] == 1)
            {
               
                switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头
                {
                    case 'w':
                        if(iy == 0)
                            Game_Over();
                        else
                            Sna_Hea_Y = iy - 1;
                        Sna_Hea_X = ix;
                     
                        break;
                    case 's':
                        if(iy == (WID -1))
                                Game_Over();
                        else
                            Sna_Hea_Y = iy + 1;
                        Sna_Hea_X = ix;
                    
                        break;
                    case 'a':
                        if(ix == 0)
                                Game_Over();
                        else
                            Sna_Hea_X = ix - 1;
                        Sna_Hea_Y = iy;
                    
                        break;
                    case 'd':
                        if(ix == (LEN - 1))
                                Game_Over();
                        else
                            Sna_Hea_X = ix + 1;
                        Sna_Hea_Y = iy;
               
                        break;
                    default:
                        break;
                }
            }
     
    if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
        Game_Over();

    if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到苹果
    {
        ++Snake_Len;
        Eat_Apple = 1;
    }
    for(ix = 0; ix < LEN; ++ix)//处理蛇尾
        for(iy = 0; iy < WID; ++iy)
        {
            if(Snake[ix][iy] > 0)
            {
                if(Snake[ix][iy] != Snake_Len)
                    Snake[ix][iy] += 1;
                else
                    Snake[ix][iy] = 0;
            }
        }
   

    Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头   
}

void Get_Input()//控制蛇的移动方向
{
    if(kbhit())
    {
        switch(getch())
        {
        case 87:
            
                Sna_Hea_Dir = 'w';
            break;
        case 83:
            
            Sna_Hea_Dir = 's';
            break;
        case 65:
            
            Sna_Hea_Dir = 'a';
            break;
        case 68:
            
            Sna_Hea_Dir = 'd';
            break;
        default:
            break;
        }
    }
   
    if(clock() - Now_Time >= Wait_Time)//蛇到时间自动行走
    {
        Clear_Snake();
        Move_Snake();
        Print_Snake();
        Now_Time = clock();
    }
}

void Init()//初始化
{
    system("title 贪吃毛毛蛇");
    system("mode con: cols=80 lines=25");
    Hide_Cursor();

    gotoxy(61, 4);
    printf("You Score:");
    gotoxy(61, 6);
    printf("You Level:");
    gotoxy(61, 8);
    printf("The Lenght:");
    gotoxy(61, 10);
    printf("The Speed:");
    gotoxy(61, 12);
    printf("Apple Num:");

    int i;
    for(i = 0; i < Snake_Len; ++i)//生成蛇
        Snake[10+i][15] = i+1;

    int iy, ix;//打印蛇
    for(iy = 0; iy < WID; ++iy)
        for(ix = 0; ix < LEN; ++ix)
        {
            if(Snake[ix][iy])
            {
                SetColor(Snake[ix][iy]);            
                gotoxy(ix*2, iy);
                printf("■");
            }
        }
}

void Pri_News()//打印信息
{
    SetColor(0xe);
    gotoxy(73,4);
    All_Score += Level;
    printf("%3d", All_Score);
    gotoxy(73, 6);
    printf("%3d", Level);
    gotoxy(73, 8);
    printf("%3d",Snake_Len);
    gotoxy(73, 10);
    printf("0.%3ds", Wait_Time/10);
    gotoxy(73, 12);
    printf("%d", Apple_Num);
}

void Lev_Sys()//等级系统
{
    if(((Apple_Num-1) / 10) == Level)
    {
        ++Level;
        if(Wait_Time > 50)
            Wait_Time -= 50;
        else
            if(Wait_Time > 10)
                Wait_Time -= 10;
            else
                Wait_Time -= 1;
    }   
}

int main(void)
{
    Init();
    srand((unsigned)time(NULL));//设置随机数的种子
    Now_Time = clock();

    while(1)
    {
        if(Eat_Apple)
        {
            ++Apple_Num;
            Rand_Apple();
            Lev_Sys();
            Pri_News();
        }
        Get_Input();
        Sleep(10);
    }
    return 0;
}
搜索更多相关主题的帖子: 记录 include 贪吃蛇 
2012-06-20 06:49
jokerskill
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:392
专家分:554
注 册:2012-3-4
收藏
得分:4 
#include <stdio.h>
int main(void)
{
int a,b;
char c;
for ( a = 0; a <= 10000; a += 1 ) {
scanf("%d",&b);
printf("你输入的整数是:%d\n\n",b);
switch( b ) {
default: printf("\ndefault  b = %d",b);
case 1: printf("\ncase 1  \t\tb = %d\n",b);
case 2: printf("\ncase 2  \t\tb = %d\n",b);
case 3: printf("\ncase 3  \t\tb = %d\n",b);
case 4: printf("\ncase 4  \t\tb = %d\n",b);
case 5: printf("\ncase 5  \t\tb = %d\n",b);
case 6: printf("\ncase 6  \t\tb = %d\n\n\n",b);
}
printf("\n\n\t\t你是否想要此程序继续运行?\n");
printf("\n\n\t\t继续执行请输入字符: y .\n\t\t退出请按字符: n .\n");
scanf("%c",&c);
printf("\n\t\t你输入的字符是:%c\n\n",c);
if ( c == 'y' ) printf("\t\t程序继续执行...请输入一个整数:\n");
else if ( c == 'n' ) {
printf("\n\t\t\t退出程序,程序已经退出!");
a = 1000000;
}
}
return 0;
}
2012-06-20 14:26
zhangqi_gsts
Rank: 6Rank: 6
来 自:甘肃天水
等 级:侠之大者
威 望:1
帖 子:227
专家分:457
注 册:2011-3-27
收藏
得分:4 
学到连个知识点,1、隐藏光标,2、设置标题
2012-06-23 23:38
星112
Rank: 5Rank: 5
来 自:山西朔州
等 级:职业侠客
威 望:2
帖 子:94
专家分:342
注 册:2012-6-22
收藏
得分:4 
写的真不错,牛人啊
2012-06-23 23:51
ly2222
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:217
专家分:618
注 册:2012-6-15
收藏
得分:4 
佩服佩服
2012-06-24 10:59
迷茫流浪
Rank: 1
等 级:新手上路
帖 子:1
专家分:4
注 册:2012-6-24
收藏
得分:4 
回复 楼主 mumu1596321
能留个QQ吗?想问几个问题
2012-06-24 23:48
mumu1596321
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-6-6
收藏
得分:0 
QQ:821132684 最近在忙写点其他的程序
2012-07-17 15:18
mumu1596321
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-6-6
收藏
得分:0 
回复 6楼 迷茫流浪
qq:821132684 欢迎交流
2012-07-17 15:18
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1933
专家分:3012
注 册:2009-12-22
收藏
得分:0 
VC  WIN-TC 上测试都存在错误。

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2012-07-17 18:20
WeekZhou
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-17
收藏
得分:0 
回复 楼主 mumu1596321
其实贪吃蛇可以用更加简单的代码来写出来。
2012-07-17 19:37
快速回复:c语言贪吃蛇
数据加载中...
 
   



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

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