| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1298 人关注过本帖, 1 人收藏
标题:贪吃蛇源代码
只看楼主 加入收藏
菜鸟成长记11
Rank: 2
等 级:论坛游民
帖 子:22
专家分:21
注 册:2012-12-11
结帖率:66.67%
收藏(1)
已结贴  问题点数:20 回复次数:9 
贪吃蛇源代码
有的话共享一下,谢谢。能在VC6.0下编译运行的
搜索更多相关主题的帖子: 源代码 谢谢 贪吃蛇 
2013-04-11 16:30
tyf19938
Rank: 2
等 级:论坛游民
帖 子:47
专家分:24
注 册:2013-1-6
收藏
得分:0 
需要vc6.0  和easy x 的图形库 函数
还有注意的是 以 .cpp编译  而不是建立 .c 文件
可以百度 easy x  下载图形库
已经亲测 可以运行
程序代码:
//贪吃蛇Game
//模拟板
//环境 :  vc 6.0  和 Easy x 图形函数
#include<stdio.h>
#include<graphics.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#define N 500//蛇长度
#define UP 'w'//方向键控制WASD
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
#define ESC 27
#define BKCO BLACK//background
#define SNCO YELLOW//snake
#define QCO DARKGRAY//RED//墙
#define FOCO GREEN//food
int i,score=0,key,s_time=300;
struct Food//食物结构体
{
    int x,y;
    int yes;
}food;
struct Snake//蛇的结构体
{
    int x[N],y[N];
    int node;//蛇长度
    int direction;//前进方向
    int life;//是否存活
}snake;
void initgraph(void)//初始化图像界面
{
    initgraph(840,580);
    setbkcolor(BKCO);
    cleardevice();
}
void drawq(void)//画墙
{
    setfillstyle(QCO);
    
    bar(50,50,550,60);
    bar(50,540,550,550);
    bar(50,50,60,550);
    bar(540,50,550,550);
    outtextxy(600,150,"按键控制:");
    outtextxy(600,170,"w上 a左 s下 d右");
    outtextxy(600,190,"Esc 退出游戏");
    outtextxy(600,210,"p 暂停游戏");
    outtextxy(600,390,"欢迎找出BUG和提出改进");
    outtextxy(600,410,"E-mail:tyf19938@");
}
void print_grade(void)
{
    char str1[20],str2[20];
    sprintf(str1,"YOUR LENGTH : %d",score/10+2);
    sprintf(str2,"YOUR SPEED : %5.2f",1000.0/s_time);
    setfillstyle(DARKGRAY,INTERLEAVE_FILL);
    bar(560,280,770,360);
    setcolor(YELLOW);
    outtextxy(600,300,str1);
    outtextxy(600,330,str2);
}
void print_score(void)//成绩输出函数
{
    char str[20];
    sprintf(str,"SCORE: %d ",score);//数字转化为字符数组
    setfillstyle(BROWN,INTERLEAVE_FILL);
    bar(50,20,550,50);
    setcolor(GREEN);
    outtextxy(50,20,str);
    setcolor(YELLOW);
    outtextxy(200,30,"贪吃蛇----- --------(C语言版)");
}
void gameover(void)
{
    cleardevice();
    print_score();
    print_grade();
    outtextxy(300,250,"GAME OVER");
    getch();
}
void playgame(void)//玩游戏
{
    int pd=1;
    snake.x[0]=100;
    snake.y[0]=100;
    snake.node=2;
    snake.direction=3; 
    snake.life=0;
    food.yes=1;    
    print_score();
    print_grade();
    outtextxy(600,100,"按任意键开始...");
    system("pause");
    outtextxy(600,100,"正在游戏中....."); 
    while(1)
    {
        srand(time(0));
        while(!kbhit() ||pd)//无键盘输入时 执行部分
        {
            if(food.yes==1)
            {
                int flag=1;
                while(flag)//循环使食物与蛇身不重合
                {
                food.x=rand()%470+60;
                food.y=rand()%470+60;
                while(food.x%10!=0)//使食物处在整10坐标上
                    food.x++;
                while(food.y%10!=0)
                    food.y++;
                flag=0;
                for(i=0;i<snake.node;i++)
                    if(food.x==snake.x[i] && food.y==snake.y[i])
                    {
                        flag=1;break;
                    }
                }
                food.yes=0;
            }    
            if(food.yes==0)//画食物
            {
                setfillstyle(FOCO);
                bar(food.x,food.y,food.x+10,food.y+10);
            }
            for(i=snake.node-1;i>0;i--)//仅移动时除第一节以外的其他坐标变化
            {
                snake.x[i]=snake.x[i-1];
                snake.y[i]=snake.y[i-1];
            }
            switch(snake.direction)//第一节根据方向变化
            {
            case 1:snake.x[0]-=10;break;//left
            case 2:snake.y[0]-=10;break;//up
            case 3:snake.x[0]+=10;break;//right
            case 4:snake.y[0]+=10;break;//down
            } 
            for(i=3;i<snake.node;i++)//是否撞自身
            {
                if(snake.x[0]==snake.x[i] && snake.y[0]==snake.y[i])
                {
                    snake.life=1;
                    break;
                }
            }
            if(snake.x[0]<60 || snake.x[0]>530 ||snake.y[0]<60 ||snake.y[0]>530)//是否撞墙
                snake.life=1;
            if(snake.life==1)//撞到退出while(!kbhit())循环
            {
                putchar('\a');
                gameover();
                break;
            }
            if(snake.x[0]==food.x&&snake.y[0]==food.y)//是否吃到食物
            {
                setfillstyle(BKCO);
                bar(food.x,food.y,food.x+10,food.y+10);//消食物
                food.yes=1;
                snake.node++;
                score+=10;
                print_score();
            }
            for(i=0;i<snake.node;i++)//画蛇
            {
                if(i==0)//蛇头颜色亮青
                {
                    setfillstyle(LIGHTCYAN);
                    bar(snake.x[0],snake.y[0],snake.x[0]+10,snake.y[0]+10);
                }
                else//蛇身颜色浅灰
                {
                    setfillstyle(LIGHTGRAY);
                    bar(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
                }
            }
            if(score<=50)
                s_time=300;
            else if(score<100)
                s_time=280;
            else if(score<200)
                s_time=250;
            else if(score<300)
                s_time=210;
            else if(score<400)
                s_time=180;
            else if(score<450)
                s_time=160;
            else s_time=160-(score-450)/10;
            print_grade();
            Sleep(s_time);//游戏短暂停息
            setfillstyle(BKCO);
            bar(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]+10);
            pd=0;    
        }//end  while(!kbhit())
        if(snake.life==1)
            break;
        if(kbhit())
        {
            pd=1;
            key=getch();
        if(key==ESC)
            break;
        else if(key==LEFT && snake.direction!=3)
        snake.direction=1;
        else if(key==UP && snake.direction!=4)
            snake.direction=2;
        else if(key==RIGHT && snake.direction!=1)
            snake.direction=3;
        else if(key==DOWN && snake.direction!=2)//can not use "else"
            snake.direction=4;
        else if(key=='p')
        {
            outtextxy(600,100,"已暂停,任意键继续"); 
            system("pause"); 
            outtextxy(600,100,"正在游戏中...................."); 
        }
        }
    }// end  while(1)
}
int main()
{
    initgraph();
    drawq();
    playgame();
    getch();
    getch();
    closegraph();
    return 0;
}


2013-04-11 17:03
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:10 
好长好长

人生是一场错过 愿你别蹉跎
2013-04-11 17:09
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
源码你可以到百度去搜一下,那上面很多的,不过你要有一定的数据结构知识才能看懂,数据结构是这类题的基础,所以你要好好学习数据结构

Maybe
2013-04-11 19:15
我叫沃恩
Rank: 12Rank: 12Rank: 12
来 自:Asia
等 级:贵宾
威 望:10
帖 子:1234
专家分:3865
注 册:2013-3-29
收藏
得分:0 
佩服佩服!

因为我是菜鸟,所以应该被骂! 细节+坚持=成功!
2013-04-11 22:49
ly371031846
Rank: 2
等 级:论坛游民
帖 子:40
专家分:79
注 册:2013-4-9
收藏
得分:10 
/***snake.c***/
#include <stdio.h>
#include <malloc.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <termio.h>
#include <fcntl.h>
#define SNAKE_INITX 5
#define SNAKE_INITY 5
#define SNAKE_SHAPE '*'
#define SNAKE_INITLEN 8
#define WIN_X1 1
#define WIN_X2 80
#define WIN_Y1 1
#define WIN_Y2 24
#define MAX_LEVEL 20
#define MAX_INTER 200000
#define MIN_INTER 0
#define MAX_RICH 10
#define DEVRATE 5
#define OVER "Game Over!!!"
struct stNode
{
        int x;
        int y;
        char shape;
        struct stNode *next;
};
struct stFood
{
        int x;
        int y;
};
struct stNode *gpstHead,*gpstTail;
struct stFood gastFood[MAX_RICH];
int giLevel=1;
int giRich=1;
int giScore=0;
int giLen=0;
void settty(int iFlag)
{
        int fd;
        struct termio stTerm;
        if((fd = open(ttyname(1),O_RDWR))==-1)        return;
        if(iFlag == 1)
        {
                ioctl(fd,TCGETA,&stTerm);
                stTerm.c_lflag &= ~ICANON;
                stTerm.c_lflag &= ~ECHO;
                stTerm.c_cc[4] = 1;
                stTerm.c_cc[5] = 0;
                stTerm.c_iflag &= ~ISTRIP;
                stTerm.c_cflag |= CS8;
                stTerm.c_cflag &= ~PARENB;
                ioctl(fd,TCSETA,&stTerm);
        }
        else
        {
                ioctl(fd,TCGETA,&stTerm);
                stTerm.c_lflag |= ICANON;
                stTerm.c_lflag |= ECHO;
                stTerm.c_cc[4] = 4;
                stTerm.c_cc[5] = 5;
                stTerm.c_iflag &= ~ISTRIP;
                stTerm.c_cflag |= CS8;
                stTerm.c_cflag &= ~PARENB;
                ioctl(fd,TCSETA,&stTerm);
        }
        close(fd);
}
void vDrawOneNode(struct stNode *pstNode,int iFlag)
{
        printf("\033[%dm\033[40;%dm\033[%d;%d;H%c",
                iFlag,iFlag*3+30,pstNode->y,pstNode->x,pstNode->shape);
        fflush(stdout);
}
void vDrawOneFood(int x,int y)
{
        printf("\033[1m\033[40;36m\033[%d;%d;H%c",y,x,'@');
        fflush(stdout);
}
int iGetDir(int iOriDir)
{
        fd_set rset;
        struct        timeval        hTmo;
        int iRet,iFlag=0;
        char cCh;
        FD_ZERO(&rset);
        FD_SET(0,&rset);
        hTmo.tv_sec=0;
        hTmo.tv_usec=MAX_INTER-(MAX_INTER-MIN_INTER)/MAX_LEVEL*giLevel;
        iRet=select(1,&rset,NULL,NULL,&hTmo);
        if(iRet<=0)
        {
                return(iOriDir);
        }
        for(;;)
        {
                cCh=getchar();
                if(cCh != -1)
                {
                        switch(cCh)
                        {        
                        case 27  :
                        case 91  :
                                iFlag++;
                                break;
                        case 65  ://UP
                        case 66 ://DOWN
                        case 67  ://RIGHT
                        case 68 ://LEFT
                        if(iFlag==2)
                                return((!((cCh-0x41)^iOriDir^1))^(cCh-0x41));
                        default  :
                                return(iOriDir);
                        }
                }
        }
}
void vInitScreen()
{
        settty(1);
        printf("\033[?25l\033[2J");
}
void vRestoreScreen()
{
        printf("\033[24;1H\033[1m\033[40;34m\033[?25h");
        settty(0);
}
void vDrawScope()
{
        int i,j;
      
        for(j=WIN_Y1;j<=WIN_Y2;j+=WIN_Y2-WIN_Y1)
        {
                printf("\033[%d;%dH+",j,WIN_X1);
                for(i=WIN_X1+1;i<WIN_X2;i++)
                        printf("-");
                printf("+");
        }
        for(i=WIN_Y1+1;i<WIN_Y2;i++)
                printf("\033[%d;%dH|%*c|\n",i,WIN_X1,WIN_X2-WIN_X1-1,' ');
}
void vCreateSnake()
{
        struct stNode *pstNew;
        int i;
        gpstHead=(struct stNode*)malloc(sizeof(struct stNode));
        gpstHead->x=SNAKE_INITX;      
        gpstHead->y=SNAKE_INITY;
        gpstHead->shape=SNAKE_SHAPE;
        gpstHead->next=NULL;
        vDrawOneNode(gpstHead,1);
        gpstTail=gpstHead;
        for(i=1;i<SNAKE_INITLEN;i++)
        {
                pstNew=(struct stNode*)malloc(sizeof(struct stNode));
                pstNew->x=gpstHead->x+1;      
                pstNew->y=gpstHead->y;
                pstNew->shape=SNAKE_SHAPE;
                pstNew->next=NULL;
                vDrawOneNode(pstNew,1);
                gpstHead->next=pstNew;
                gpstHead=pstNew;
        }
        return;
}
void vKillSnake()
{
        struct stNode *pstNode;
        for(pstNode=gpstTail;gpstTail!=NULL;)
        {
                gpstTail=pstNode->next;
                free(pstNode);
                pstNode=gpstTail;
        }
}
void vGenFood(int iIdx)
{
        struct stNode *pstNode;
        int i,iFound=0;
      
        for(;!iFound;)
        {
                iFound=1;
                gastFood[iIdx].x=rand()%(WIN_X2-WIN_X1-1)+WIN_X1+1;
                gastFood[iIdx].y=rand()%(WIN_Y2-WIN_Y1-1)+WIN_Y1+1;
                for(i=0;i<giRich;i++)
                {
                        if(i!=iIdx && gastFood[iIdx].x==gastFood[i].x &&
                                gastFood[iIdx].y==gastFood[i].y)
                        {
                                iFound=0;
                                break;
                        }
                }
                if(!iFound) continue;
                for(pstNode=gpstTail;pstNode!=NULL;pstNode=pstNode->next)
                {
                        if(gastFood[iIdx].x==pstNode->x &&
                                gastFood[iIdx].y==pstNode->y)
                        {
                                iFound=0;
                                break;
                        }
                }
                if(!iFound) continue;
        }
        vDrawOneFood(gastFood[iIdx].x,gastFood[iIdx].y);
}
void vInitFood()
{
        int i;
      
        srand(getpid());
        for(i=0;i<giRich;i++)        vGenFood(i);
}
      
int iIsValid(int x,int y)
{
        struct stNode *pstNode;
        if(x<=WIN_X1 || x>=WIN_X2 || y<=WIN_Y1 || y>=WIN_Y2)
                return(0);
        pstNode=gpstTail;
        for(;pstNode!=NULL;)
        {
                if(x==pstNode->x && y==pstNode->y)
                        return(0);
                pstNode=pstNode->next;
        }
        return(1);
}
int iEat(int x,int y)
{
        int i,j;
      
        for(i=0;i<giRich;i++)
        {
                if(x==gastFood[i].x && y==gastFood[i].y)
                {
                        vGenFood(i);
                        giScore+=giLevel*10;
                        giLen++;
                        if(giLevel<MAX_LEVEL)
                                if(giLen%DEVRATE==0)
                                        giLevel++;
                        return(1);
                }
        }
        return(0);
}
main()
{
        int iDir=2,iNextX,iNextY;
        struct stNode *pstNew;
        char sPrompt[80];
      
        vInitScreen();
        vDrawScope();
        vCreateSnake();
        vInitFood();
        for(;;)
        {
                iDir=iGetDir(iDir);
                iNextX=gpstHead->x+(iDir>>1)*(5-(iDir<<1));
                iNextY=gpstHead->y-(!(iDir>>1))*(1-(iDir<<1));
                if(!iIsValid(iNextX,iNextY))
                {
                        printf("\033[%d;%dH\033[1m\033[40;34m%s\033[0m",
                                WIN_Y2-1,(WIN_X1+WIN_X2)/2-strlen(OVER)/2,OVER);
                        break;
                }
                pstNew=(struct stNode*)malloc(sizeof(struct stNode));
                pstNew->x=iNextX;
                pstNew->y=iNextY;
                pstNew->shape=SNAKE_SHAPE;
                pstNew->next=NULL;
                gpstHead->next=pstNew;
                gpstHead=pstNew;
                vDrawOneNode(gpstHead,1);
                if(!iEat(iNextX,iNextY))
                {
                        vDrawOneNode(gpstHead,1);
                        vDrawOneNode(gpstTail,0);
                        pstNew=gpstTail;
                        gpstTail=pstNew->next;
                        free(pstNew);
                }
                sprintf(sPrompt,"Score:%7d Level:%2d",giScore,giLevel);
                printf("\033[%d;%dH\033[1m\033[40;34m%s\033[0m",
                        WIN_Y2,(WIN_X1+WIN_X2)/2-strlen(sPrompt)/2,sPrompt);
        }
        vKillSnake();
        vRestoreScreen();
}
2013-04-12 14:57
风绝致
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-10-28
收藏
得分:0 
pd有什么用?
貌似没什么用啊?
2014-07-05 10:31
列车永不停息
Rank: 2
等 级:论坛游民
帖 子:76
专家分:48
注 册:2015-7-31
收藏
得分:0 
回复 2楼 tyf19938
我怎么运行不了,会报错啊。
图片附件: 游客没有浏览图片的权限,请 登录注册
2015-08-10 11:37
hyq加油加油
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-10-26
收藏
得分:0 
感觉这个代码有错误,不能运行。
2015-10-26 22:02
万一呢
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-3-29
收藏
得分:0 
回复 8楼 列车永不停息
setlinestyle(0,0,2); 是 TC 的写法,在 vc 下,功能做了添加,参数略有不同,需要修改一下。你想画什么样的线呢?如果没有要求,试试 setlinestyle(PS_SOLID);
2016-03-29 22:11
快速回复:贪吃蛇源代码
数据加载中...
 
   



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

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