| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1266 人关注过本帖
标题:【分享】贪吃蛇游戏代码VISUSLC++6.0
只看楼主 加入收藏
寒夏
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-12-4
结帖率:0
收藏
 问题点数:0 回复次数:13 
【分享】贪吃蛇游戏代码VISUSLC++6.0


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

struct Node
{
    int x;
    int y;
    struct Node *pre;
    struct Node *next;
};

struct Food
{
    int x;
    int y;
    char c;
};

void main()
{
    int a[15][15]={0};
    int i,j,t,flag=0;
    char c='d',c1='d';
    struct Food food={5,8,'A'};;
    int gameover=0;
    struct Node *head,*p,*rear,*pt;
   
    head=(struct Node *)malloc(sizeof(struct Node));
    head->x=5;
    head->y=8;
    head->pre=NULL;
    head->next=NULL;
    rear=head;

    srand((unsigned)time(NULL));
   
    while(1)
    {
        if(food.x==head->x && food.y==head->y)
        {
            p=(struct Node *)malloc(sizeof(struct Node));
            pt=head;
            while(pt->next!=NULL)
                pt=pt->next ;
            p->pre= pt;
            pt->next = p;
            p->next=NULL;
            rear=p;

            food.x=rand()%15;
            food.y=rand()%15;
            food.c=65+rand()%26;

            flag=1;
            t=0;
            while(flag==1)
            {
                if(t>5)
                    break;

                flag=0;

                pt=head;
                while(pt!=NULL)
                {
                    if(food.x==pt->x && food.y==pt->y)
                    {
                        flag=1;
                        food.x=rand()%15;
                        food.y=rand()%15;
                        break;
                    }
                    pt=pt->next;
                }
                t++;
            }
            if(t>5)
            {
                if(c=='d')
                {
                    food.x=head->x+1;
                    food.y=head->y;
                    if(food.x>=15)
                        food.x-=15;
                }
                else if(c=='a')
                {
                    food.x=head->x-1;
                    food.y=head->y;
                    if(food.x<0)
                        food.x+=15;
                }
                else if(c=='w')
                {
                    food.x=head->x;
                    food.y=head->y+1;
                    if(food.y>=15)
                        food.y-=15;
                }
                else if(c=='s')
                {
                    food.x=head->x;
                    food.y=head->y-1;
                    if(food.y<0)
                        food.y+=15;
                }
            }
        }

        if(kbhit())
        {
            c1=getch();
            if(c1==27)
                break;
            
            if(c!='d' && c1=='a')
                c=c1;
            else if(c!='a' && c1=='d')
                c=c1;
            else if(c!='w' && c1=='s')
                c=c1;
            else if(c!='s' && c1=='w')
                c=c1;
        }

        pt=rear;
        while(pt!=head )
        {
            pt->x=pt->pre->x;
            pt->y=pt->pre->y;
            pt=pt->pre;
        }

        if(c=='d')
        {
            head->y+=1;
            if(head->y>=15)
                head->y-=15;
        }
        else if(c=='a')
        {
            head->y-=1;
            if(head->y<0)
                head->y+=15;
        }
        else if(c=='w')
        {
            head->x-=1;
            if(head->x<0)
                head->x+=15;
        }
        else if(c=='s')
        {
            head->x+=1;
            if(head->x>=15)
                head->x-=15;
        }

        pt=head->next;
        while(pt!=NULL)
        {
            if(head->x==pt->x && head->y==pt->y)
            {
                gameover=1;
                break;
            }
            pt=pt->next ;
        }
        if(gameover==1)
            break;
        
        system("cls");
        printf("  ───────────────\n");
        for(i=0;i<15;i++)
        {
            printf("│");
            for(j=0;j<15;j++)
            {

                flag=0;
                pt=head;
                while(pt!=NULL)
                {
                    if(i==pt->x && j==pt->y)
                    {
                        if(pt==head)
                            printf("■");
                        else
                            printf("□");
                        flag=1;
                        break;
                    }
                    pt=pt->next;
                }


                if(flag==0)
                {
                    if(i==food.x && j==food.y)
                    {
                        putchar(food.c);
                        putchar(food.c);
                        continue;
                    }
                    printf("  ");
                }
            }
            printf("│");
            putchar('\n');
        }
        printf("  ───────────────\n");
        
        _sleep(200);   
    }

    if(gameover==1)
        puts("game over!\n");

    getch();
}
搜索更多相关主题的帖子: VISUSLC 贪吃 游戏 代码 分享 
2010-12-04 12:52
寒夏
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-12-4
收藏
得分:0 
比较简单,供大家娱乐了~
2010-12-04 12:52
li5683li
Rank: 2
等 级:论坛游民
帖 子:12
专家分:13
注 册:2010-4-12
收藏
得分:0 
试玩过了,有意思
2010-12-04 13:17
roar
Rank: 2
等 级:论坛游民
帖 子:47
专家分:30
注 册:2010-5-14
收藏
得分:0 
void main()
我改成int main(void)才编译成功,玩了下不错,

C语言 /  VB编程/MCS-51单片的菜鸟,大虾们请加QQ:89306026大家一起学习探讨。
2010-12-04 14:54
outsider_scu
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:430
专家分:1333
注 册:2010-10-21
收藏
得分:0 
不错。。

编程的道路上何其孤独!
2010-12-04 15:24
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:0 
今天回家去玩玩看看。。
2010-12-04 16:48
御坂美琴
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:魔術の禁書目錄
等 级:小飞侠
威 望:9
帖 子:952
专家分:2929
注 册:2010-8-18
收藏
得分:0 
这样写的话,屏幕会很闪

永远为正义而奋斗,锄强扶弱的Level 5 超能力者
とある魔術の禁書目錄インデックス__御み坂さか美み琴こと
http://bbs.bccn.net/space.php?action=threads&uid=483997
2010-12-04 16:49
a343637412
Rank: 7Rank: 7Rank: 7
来 自:そ ら
等 级:黑侠
帖 子:357
专家分:620
注 册:2010-9-26
收藏
得分:0 
以下是引用御坂美琴在2010-12-4 16:49:42的发言:

这样写的话,屏幕会很闪
有牛人拔掉这个瑕疵吗
2010-12-04 19:11
xx123321
Rank: 1
等 级:新手上路
帖 子:7
专家分:7
注 册:2010-12-1
收藏
得分:0 
有註釋就更好了~
2010-12-04 20:00
tang_ke
Rank: 2
来 自:西安电子科技大学
等 级:论坛游民
帖 子:45
专家分:46
注 册:2010-10-12
收藏
得分:0 
你很厉害。。。。。向你学习!!!!!
2010-12-04 21:03
快速回复:【分享】贪吃蛇游戏代码VISUSLC++6.0
数据加载中...
 
   



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

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