| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 31867 人关注过本帖, 8 人收藏
标题:写个贪吃蛇代码来玩玩~
只看楼主 加入收藏
Alien_Lee
Rank: 8Rank: 8
来 自:Linux帝国
等 级:蝙蝠侠
威 望:7
帖 子:149
专家分:739
注 册:2016-7-19
收藏
得分:9 
//这里有个不用graphic库的贪吃蛇游戏,可以参考
#include<stdio.h>//基本库
#include<stdlib.h>//系统库
#include<windows.h>//光标定位、字符颜色函数库
#include<time.h>//时间函数库
#include<conio.h>//键值读入函数库
#define width 60
#define height 25
struct ssnake
{
int f;//蛇身有效标志
int x;
int y;//蛇身坐标
};
void color(int b) //颜色函数
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole,b) ;
}
void HideCursor()//隐藏光标
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(hOut,&cursor_info);
}

void gotoxy(int x,int y)//设置字符显示位置
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD loc={x,y};
SetConsoleCursorPosition(hOut,loc);
}
void drawmap(char *mp,char *omp,ssnake *snp)
{//画场景
char pel[]=" ◆■●";
int i,j,k;
for(i=0;snp[i].f>0;i++)
{
j=4;//蛇身
if(i==0)j=2;//蛇头
mp[snp[i].x*height+snp[i].y]=j;
}
for(k=0;k<width*height;k++)
{
if(mp[k]!=omp[k])
{
i=k/height;j=k%height;
color(15);
if(mp[k]==6)color(13);
omp[k]=mp[k];
gotoxy(i,j);
printf("%c",pel[mp[k]]);
printf("%c",pel[mp[k]+1]);
}
}
}
void rndapple(ssnake *snp,int *px,int *py)
{//产生苹果随机位置
bool f=false;
int i,j,k=0,z[1500][2]={0};
char map[width][height]={0};
*px=-1;*py=-1;
for(i=0;snp[i].f>0;i++)map[snp[i].x][snp[i].y]=1;
for(i=0;i<width;i+=2)
for(j=0;j<height;j++)
{
if(map[i][j]==0)
{
z[k][0]=i;
z[k][1]=j;
k++;
}
}
if(k>0)
{
i=rand()%k;
*px=z[i][0];
*py=z[i][1];
}
}

int main(void)
{
char map[width][height]={0},oldmap[width][height]={0};//场景 0:空 2:蛇头 4:蛇身 6:苹果
int mx=0,my=0;//蛇头移动方向
int apx=-1,apy=-1;//苹果位置
int i,j,sc=0,ef=0,sp=210;
long t1,nt;//计时
char a;
bool pf=false;//暂停标志
ssnake sn[1500]={0};//蛇,最长1500个关节,关节数据结构为ssnake,
nt=clock();
t1=nt;
system("mode con cols=80 lines=25");
HideCursor();
for(i=0;i<height;i++){gotoxy(width,i);printf("‖");}
color(14);
gotoxy(width+4,12);printf("控制:←↑→↓");
gotoxy(width+4,14);printf("空格:暂停");
gotoxy(width+4,16);printf("ESC :退出");
gotoxy(width+4,18);printf("得分:%d",sc);
sn[0].f=1;sn[0].x=30;sn[0].y=10;sn[1].f=0;//蛇头位置
srand((unsigned)time(NULL));
while(1)
{
nt=clock();
if(kbhit())
{
a=getch();
if(a<0)
{
a=getch();
if(a=='K'&&mx!=2){mx=-2;my=0;}
if(a=='M'&&mx!=-2){mx=2;my=0;}//左右移动,由于用汉字符号表示,所以步长为2
if(a=='H'&&my!=1){my=-1;mx=0;}
if(a=='P'&&my!=-1){my=1;mx=0;}//上下移动
}
else
{
if(a==27){ef=1;break;}//人为退出
if(a==32)pf=!pf;
}
}
if(!pf&&nt-t1>sp)
{
t1=nt;
for(i=0;sn[i].f>0;i++);
for(j=i;j>0;j--){sn[j].x=sn[j-1].x;sn[j].y=sn[j-1].y;}//蛇身前进
sn[0].x+=mx;sn[0].y+=my;//蛇头前进
if(sn[0].x<0||sn[0].x>=width||sn[0].y<0||sn[0].y>=height){ef=2;break;}//越界
for(j=1;sn[j].f>0;j++){if(sn[j].x==sn[0].x&&sn[j].y==sn[0].y)break;}
if(sn[j].f!=0){ef=3;break;}//判断是否自己吃自己
if(sn[0].x==apx&&sn[0].y==apy)//吃了苹果
{
sn[i].f=1;sn[i+1].f=0;apx=-1;apy=-1;sp--;
sc=sc+10+i;//成绩累加
gotoxy(64,18);printf("得分:%d",sc);
}
}
for(i=0;i<width;i++)
for(j=0;j<height;j++)
map[i][j]=0;
if(apx<0&&apy<0)rndapple(sn,&apx,&apy);//获取苹果位置
if(apx>=0&&apy>=0)
map[apx][apy]=6;//设置苹果
else
{//已经没有空位画苹果位置了,说明蛇足够长,这是不可能的
gotoxy(62,24);
printf("厉害,满分!");
break;
}
drawmap(&map[0][0],&oldmap[0][0],sn);
}
gotoxy(25,23);
color(15);
if(ef==1)printf("退出!Game Over!\n");
if(ef==2)printf("越界!Game Over!\n");
if(ef==3)printf("自杀了!Game Over!\n");
}

  DEBUG的过程就是进步的过程,每一个小错误都是大问题!...
2017-03-18 17:02
as1069
Rank: 2
来 自:河北
等 级:论坛游民
威 望:1
帖 子:46
专家分:31
注 册:2014-6-10
收藏
得分:9 
我自己也写了一个
虽然很烂,但玩还是可以的
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <conio.h>
#include<time.h>

#define WX 50
#define WY 23

void gotoxy(int x, int y);


typedef struct snake{
int x;
int y;
int length;
struct snake *next;
}snake, *Snake;

typedef struct food{
int x;
int y;
struct food *next;
}food, *Food;

void clean(Snake head);
void test(Snake head);
int getvalue(Snake o, int n);
int getvaluey(Snake o, int n);
void changevalue(Snake n, int i, int s);
void changevaluey(Snake n, int i, int s);
void control(Snake a);
void auto_move(Snake head,char direction, Food k);
void *show_food(Snake head);
int getlength(Snake k);
void the_end(Snake head);
int f = 0;//是否有食物

//创建动态链表,在链表末尾添加一个x,y坐标
void createL(Snake head, int x, int y)
{
    Snake swap, newl;
    swap = head;
    while(swap->next != NULL)
    {
        swap = swap->next;
    }

    newl = (Snake) malloc(sizeof(snake));
    if(newl == NULL )
    {
        printf("error\n");
    }
    newl->x = x;
    newl->y = y;
    newl ->length = swap ->length + 1;
    swap->next = newl;
    newl->next = NULL;

}
void* _snake()//初始化蛇
{
    int i;
    Snake head;
    head =(Snake) malloc(sizeof(snake));
    head->next = NULL;
    head->x = 8;
    head->y = 3;
    head ->length = 1;

    for(i = 0; i < 3; i++)
    {
        createL(head, 7 - i, 3);
    }
    return (head);

}


void draw_snake(Snake head)
{
    gotoxy(head->x, head->y);
    putchar('@');//这是脑袋
    head = head->next;
    for( ;head->next != NULL;head = head->next)
    {
        gotoxy(head->x, head->y);
        putchar('*');//这是身体
    }
    gotoxy(head->x, head->y);
    putchar('*');//这是尾巴
}

int getlength(Snake head)
{
    Snake swap = head;
    int n = 0;
     for( ; swap->next != NULL ;swap = swap ->next)
         {
             n = swap->length;
             /** 不包括蛇头 n 是蛇身的长度
             */
         }
         return n;
}
void *show_food(Snake head)
{
    srand(time(NULL));
    Food apple;

    apple = (Food)malloc(sizeof(food));
    apple ->next = NULL;
    while(1)
    {
        apple -> x = rand() % (WX - 1) + 1;
        apple -> y = rand() % (WY - 1) + 1;
        while(head ->next != NULL)
        {
            if(head->x == apple->x || head->y == apple->y)
                break;
            head = head ->next;
        }
        break;
    }

    gotoxy(apple -> x, apple -> y);
    putchar('#');

    return (apple);
}
int getvalue(Snake swap, int n)
{
    while(n--)
    {
        swap = swap ->next;
    }
    return swap->x;
}

int getvaluey(Snake swap, int n)
{
    while(n--)
    {
        swap = swap ->next;
    }
    return swap->y;
}

void changevalue(Snake swap, int i, int n)
{
   while(n--)
   {
       swap = swap ->next;
   }
   swap ->x = i;
}
void changevaluey(Snake swap, int i, int n)
{
   while(n--)
   {
       swap = swap ->next;
   }
   swap ->y = i;
}

void auto_move(Snake head,char direction,Food apple)
{
    Snake swap;
    int x = 0, y = 0, n = 0;
    while(!kbhit())
   {

       swap = head;//初始化不要忘了


        if(head ->x == apple->x && head ->y == apple->y)
        {
            f = 0;
            n = getlength(head);
            x = getvalue(swap,n);
            y = getvaluey(swap,n);/**不懂为啥n+ 1不对,还不太明白*/
            createL(head,x , y);
        }
        clean(head);//我先清理的所以刚开始蛇身只有两节
        /**
         *  把链表倒数第二个值赋值给倒数第一个
         *  这样就不会把值覆盖了
            最后一个链表存储当前蛇的长度
         */
         n = getlength(head);

        while(n--)
        {
            x = getvalue(swap,n);//得到第n个链表的值
            y = getvaluey(swap,n);
            changevalue(swap, x, n+1);//把第n个链表的值写入第n+1个链表
            changevaluey(swap, y, n+1);
        }
        switch(direction)
        {
            case 'w': head ->y -= 1;break;
            case 'a': head ->x -= 1;break;
            case 's': head ->y += 1;break;
            case 'd': head ->x += 1;break;
        }



        Sleep(160);
        draw_snake(head);
        the_end(head);
   }
}

void control(Snake head)
{
    char c, o = 0;
    Food apple;
    while(1)
    {
        if(!f)//判断是否有食物
        {
            f = 1;
            apple = show_food(head);
        }

       c = getch();
        if(c == 'w' || c == 'a' || c == 's' || c == 'd' )
        {
            if((c == 'w' && o != 's') || (c == 'a' && o != 'd') || (c == 'd' && o != 'a') || (c == 's' && o != 'w'))
                {
                    o = c;
                    auto_move(head, c, apple);
                }

        }

    }
}
void test(Snake head)
{
    for(; head->next != NULL;head = head->next )
    {
        printf("%d,%d\n",head->x,head->y);
    }
    printf("%d,%d\n",head->x,head->y);
}
void clean(Snake head)
{
    Snake swa;
    swa = head;
    while(swa->next != NULL)
    {
        swa = swa->next;
    }
    gotoxy(swa->x, swa->y);
    putchar(' ');
}

void the_end(Snake head)
{
    Snake swap = head;
    if(getlength(head) > 50)
    {
        system("cls");
        gotoxy(10,20);
        printf("YOU WIN!!");
        Sleep(2000);
        exit(0);
    }
    while(swap ->next != NULL)
    {
        swap = swap ->next;
        if(head ->x == swap ->x && head ->y == swap->y)
        {
            system("cls");
            gotoxy(10,20);
            printf("YOU DEATH!!");
            Sleep(2000);
            exit(0);
        }

    }
    if(head->x >= WX || head->y >= WY || head->x == 0 || head->y == 0)
    {
        system("cls");
        gotoxy(10,20);
        printf("YOU DEATH!!");
        Sleep(2000);
        exit(0);
    }
}
void gotoxy(int x, int y)//定义光标位置
{
    COORD wei;
    wei.X = x;
    wei.Y = y;
    //Windows±à3ì
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), wei);
}
void wall()
{
    int x,y;

    for(x = 0; x <= WX; x++)
    {
        if(x == 0 || x == WX)
        {
            for(y =0 ;y <= WY; y++)
            {
                gotoxy(x , y);
                printf("+");
            }
        }
        gotoxy(x, 0);
        printf("+");
        gotoxy(x, WY);
        printf("+");

    }

}
int main()
{
    Snake p;//链表头指针p

    wall();
     p = _snake();
    draw_snake(p);
    control(p);
    system("pause");
    return 0;
}

我已入坑
2017-03-19 11:20
xzlxzlxzl
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖北
等 级:贵宾
威 望:125
帖 子:1091
专家分:5825
注 册:2014-5-3
收藏
得分:9 
回复 5楼 ehszt
这代码原始出处应该是这里:https://bbs.bccn.net/thread-442229-1-1.html
我也做了收藏的,看样子,做这个小游戏是一代一代码农成长、培养兴趣的必经之路啊!
2017-03-19 13:17
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:0 
回复 13楼 xzlxzlxzl
哦,原贴己编辑。多谢!
2017-03-19 13:32
王俊svip
Rank: 2
等 级:论坛游民
帖 子:2
专家分:14
注 册:2017-2-26
收藏
得分:9 
太厉害了。 为什么在xcode里不能运行
2017-03-19 20:16
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:9 
同样的功能代码越短肯定越好啊。楼主能够较短的代码实现肯定是好事噻。我写过win32版的,代码太冗余不好意思展示了
        设计了减少长度的功能能增加更多的可玩性。佩服
2017-03-20 13:33
张乾
Rank: 2
等 级:论坛游民
帖 子:6
专家分:16
注 册:2017-2-22
收藏
得分:9 
回复 8楼 bjut_Allen
文件下好了,不知道放哪里怎么办,求帮助
2017-03-20 18:42
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
回复 17楼 张乾
可以试试放到Include的目录下~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-03-20 18:48
张乾
Rank: 2
等 级:论坛游民
帖 子:6
专家分:16
注 册:2017-2-22
收藏
得分:0 
回复 18楼 九转星河
版主,我用的c-free4.0,有没有VC的安装包,我win7的,谢谢。
2017-03-20 19:12
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
回复 19楼 张乾
在九九的下载频道有~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-03-20 19:22
快速回复:写个贪吃蛇代码来玩玩~
数据加载中...
 
   



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

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