| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1266 人关注过本帖
标题:【分享】贪吃蛇游戏代码VISUSLC++6.0
取消只看楼主 加入收藏
寒夏
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-12-4
结帖率:0
收藏
 问题点数:0 回复次数:2 
【分享】贪吃蛇游戏代码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
寒夏
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-12-4
收藏
得分:0 
回复 7楼 御坂美琴
嗯 是有点闪  因为每动一次屏幕自动刷新一次了~
2010-12-04 22:13
快速回复:【分享】贪吃蛇游戏代码VISUSLC++6.0
数据加载中...
 
   



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

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