| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1335 人关注过本帖, 1 人收藏
标题:我的c语言版贪吃蛇 大家可以看看 讨论讨论 给些意见 谢谢啦[em04]
取消只看楼主 加入收藏
wk2008520
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-4-17
结帖率:0
收藏(1)
已结贴  问题点数:10 回复次数:0 
我的c语言版贪吃蛇 大家可以看看 讨论讨论 给些意见 谢谢啦[em04]
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

int  snake_len=1;//蛇的长度
int  snake_loc[50][2]={31,12};//整条蛇的位置,最长为50
int  snake_head[2]={31,12};//蛇头位置,初始值为11,12;
int  food[2];//食物位置
char snake_direction='s';
int  delay=200; //蛇每delay个时间走一步
int  eat_flag=0;//1表示吃了食物,0表示未吃
int  liv_stat=0;//1表示死了,游戏该结束了;0表示还活着

void gotoxy(int x, int y)//定位光标,x为行坐标,y为列坐标
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}

void hidden()//隐藏光标
{
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=0;//赋1为显示,赋0为隐藏
    SetConsoleCursorInfo(hOut,&cci);
}
void init()//初始化
{
    int i;
    snake_len=1;//蛇的长度
    snake_loc[0][0]=31;//整条蛇的位置
    snake_loc[0][1]=12;
    snake_head[0]=31;//蛇头位置,初始值为11,12;
    snake_head[1]=12;
    snake_direction='s';
    delay=200;
    eat_flag=0;
    liv_stat=0;
    for(i=1;i<50;i++)
    {
        snake_loc[i][0]=0;//整条蛇的位置
        snake_loc[i][1]=0;
    }
   
}


void create_window()//创建窗口
{
    gotoxy(0,0);
    printf("********************************************************************************");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *   分数:1     *");
    printf("*                                                              *   按键说明:  *");
    printf("*                                                              *   上:w       *");
    printf("*                                                              *   下:s       *");
    printf("*                                                              *   左:a       *");
    printf("*                                                              *   右:d       *");
    printf("*                                                              *   暂停:空格  *");
    printf("*                                                              *   退出:Esc键 *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("*                                                              *               *");
    printf("********************************************************************************");
}
void update_score()//更新分数
{
    gotoxy(73,3);
    printf("%2d",snake_len);
}

void create_food()//产生食物的位置
{
    time_t t;
    srand(time(&t));
    while(1)
    {
        food[0]=rand()%62+1;//生成1~62之间的随机数,其中random函数生成0~77之间的随机数
        food[1]=rand()%22+1;//生成1~22之间的随机数,其中random函数生成0~17之间的随机数
        if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;
    }
    gotoxy(food[0],food[1]);
    printf("*");
}

void direction()
{
    char keyhit=0,i;
    while(kbhit()!=0)keyhit=getch();
    if( ((keyhit=='a') || (keyhit=='d') || (keyhit=='w') || (keyhit=='s')) && (abs(snake_direction/16-keyhit/16)==1) )snake_direction=keyhit;
    else if(keyhit==' ')
    {
        gotoxy(30,24);
        system("pause");
        gotoxy(30,24);
        for(i=0;i<19;i++)printf(" ");
    }
    else if(keyhit==27)exit(0);
}
void state()//判定蛇死没死
{
    if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)liv_stat=1;
}

void eat()//判定蛇吃没吃上,并对根据方向对蛇头位置进行更新
{
    switch(snake_direction)
        {
            case 'w':    snake_head[1]--;break;
            case 's':    snake_head[1]++;break;
            case 'a':    snake_head[0]--;break;
            case 'd':    snake_head[0]++;break;
        }
    if((food[0]==snake_head[0]) && (food[1]==snake_head[1]) )
    {
        eat_flag=1;
        switch(snake_direction)
        {
            case 'w':    snake_head[1]--;break;
            case 's':    snake_head[1]++;break;
            case 'a':    snake_head[0]--;break;
            case 'd':    snake_head[0]++;break;
        }
    }
        
}

void show_snake()//更新蛇在屏幕中的位置
{
        gotoxy(snake_head[0],snake_head[1]);
        printf("*");
        gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);
        printf(" ");
}

void update_maxtrix()//更新存储蛇位置的数组
{
    int i;
    if(eat_flag!=1)
    {
        for(i=snake_len-1;i>0;i--)
        {
            snake_loc[i][0]=snake_loc[i-1][0];
            snake_loc[i][1]=snake_loc[i-1][1];
        }
    }
    else
    {
        snake_len++;
        if(snake_len>3 && delay>100)delay-=30;
        for(i=snake_len-1;i>1;i--)
        {
            snake_loc[i][0]=snake_loc[i-2][0];
            snake_loc[i][1]=snake_loc[i-2][1];
        }
        snake_loc[1][0]=food[0];
        snake_loc[1][1]=food[1];
        eat_flag=0;
        create_food();
        update_score();
    }
    snake_loc[0][0]=snake_head[0];
    snake_loc[0][1]=snake_head[1];
}

void main()
{
    LOOP:   
            system("cls");
                        system("color 9");
            init();
            create_window();
            hidden();
            create_food();
    while(1)
    {
        int i;
        Sleep(delay);
        direction();
        eat();
        show_snake();
        update_maxtrix();
        state();
        if(liv_stat==1)
        {   
            for(i=1;i<snake_len;i++)
            {
                gotoxy(snake_loc[i][0],snake_loc[i][1]);
                printf(" ");
            }
            gotoxy(food[0],food[1]);
            printf(" ");
            gotoxy(30,12);
            printf("Game over!\n");
            gotoxy(25,13);
            printf("继续请按y,退出请按n");
            while(1)
            {
                i=getch();
                if(i=='y')goto LOOP;
                else if(i=='n')break;
            }
            break;
        }
    }   
}
搜索更多相关主题的帖子: 贪吃蛇 include 
2013-04-17 22:01
快速回复:我的c语言版贪吃蛇 大家可以看看 讨论讨论 给些意见 谢谢啦[em04]
数据加载中...
 
   



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

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