| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2944 人关注过本帖, 1 人收藏
标题:零基础自学快两个月了,昨天刚刚写小贪食蛇,分享下,以前写的小游戏就不发了! ...
取消只看楼主 加入收藏
hackrol
Rank: 4
来 自:世界和平组织
等 级:业余侠客
帖 子:62
专家分:267
注 册:2014-9-6
结帖率:0
收藏(1)
 问题点数:0 回复次数:3 
零基础自学快两个月了,昨天刚刚写小贪食蛇,分享下,以前写的小游戏就不发了! GCC 编译器 312 行
程序代码:
//左上角坐标 1,1   右上角坐标 30,1   左下角坐标 1,22   右下角坐标 30,22
//贪食蛇 gcc版 欢迎交流 QQ 87835836 -- Rol
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <Windows.h>

#define W 40  //控制台宽(40*2)
#define H 24  //控制台高
#define LEFT 75
#define UP 72
#define RIGHT 77 
#define DOWN 80
#define ESC 27
#define SPACE 32
#define ENTER 13

typedef struct 
{
    int x;

    int y;

} COORSNAKE;  //蛇坐标

typedef struct    SHE 
{
    COORSNAKE coorsnake;

    struct SHE *next;

} NODESNAKE;  //链表

NODESNAKE *head=NULL, *last=NULL;  //蛇头蛇尾指针

COORSNAKE food, new_coor, tmp_coor;  //坐标
static COORD food_position;      //坐标
int score=0;                     //分数
int level=1;                      //难度

void gotoxy(int x, int y, int color);
void map();
void huashe(int x,int y,int color);
char getkey();
void mksnake(NODESNAKE **plast);
void printfood(int x,int y,int color);
void mkfood(int *x,int *y);
void HideCursor();

int main(void)
{    
    HideCursor();
    int key,isfood,isself,dx,dy;                //key为取键值,isfood标记有没有吃到食物,isself标记有没有碰到自己,dx表示横向的方向,dy表示纵向方向;
    NODESNAKE *pcheck;                            //检查指针
    system ("title SNAKE   -- BY ROL");
    system ("cls");
    head=NULL;
    map();
    gotoxy(11,12,0x0b);
    printf("Enter键开始,ESC退出!"); 
    while((key=getkey())!=ESC||key!=ENTER)            //开始游戏
    {
        if(key==ESC) return 0;
        else break;
    }
    gotoxy(11,12,0x0b);
    printf("                     ");     //清屏
    mksnake(&last);
    suiji:
    srand((unsigned int)time(NULL));     
    last->coorsnake.x = rand()%((W-12)/2)+2; //现在的蛇头和蛇尾指针都指向蛇头 保证蛇头在左边
    last->coorsnake.y = rand()%(H-2)+1; 
    srand((unsigned int)time(NULL)); 
    food.x=rand()%(W-11)+2;
    food.y=rand()%(H-2)+1;
    if(food.x==head->coorsnake.x&&food.y==head->coorsnake.y) goto suiji;
    printfood(food.x,food.y,0x0f);
    huashe(last->coorsnake.x,last->coorsnake.y,0x0c);
    dx=1,dy=0;                                                //默认方向向右
    while(1)
    {
        isself=isfood=0;
        gotoxy(35,16,0x0d);
        printf("%-5d",score);
        gotoxy(35,12,0x0d);
        printf("%-3d",level);
        if (_kbhit())      //当吃到一个食物后才会执行
            {
                fflush(stdin);
                switch (key=getkey())
                {
                    case LEFT:     dx=-1; dy= 0;break;
                    case RIGHT: dx= 1; dy= 0;break;
                    case UP:    dx= 0; dy=-1;break;
                    case DOWN:     dx= 0; dy= 1;break;
                    case SPACE:    getch();break;                 
                }
            } 
        new_coor.x=head->coorsnake.x+dx;                            //新蛇头
        new_coor.y=head->coorsnake.y+dy;
        if (head->next != NULL && head->next->coorsnake.x == new_coor.x && head->next->coorsnake.y == new_coor.y) //如果按按键与原来方向一致,则还原新蛇头
            {
                new_coor.x = head->coorsnake.x - dx;

                new_coor.y = head->coorsnake.y - dy;
            }
        for(pcheck=head;pcheck->next!=NULL;pcheck=pcheck->next)        //判断是否撞到自身
            if(pcheck->coorsnake.x==new_coor.x&&pcheck->coorsnake.y==new_coor.y)
                {
                    isself=1;break;
                }
        if(isself==1||new_coor.x<1||new_coor.x>30||new_coor.y<1||new_coor.y>22)       //撞到自身或者越界,游戏结束!
            {
                gotoxy(11,12,0x0b);
                printf("Game Over! 按任意键退出");
                break;
            }
        if(new_coor.x==food.x&&new_coor.y==food.y)                    //吃到食物
            {
                isfood=1;
                score+=10;
                mksnake(&last);                                     //增加一节蛇
                mkfood(&food.x,&food.y);                            //创建新食物
                printfood(food.x,food.y,0x0f);                        //打印食物
            }
        if (isfood == 0)                                             //去掉尾巴
            {
                huashe(last->coorsnake.x, last->coorsnake.y, 0x00);    
            }
        for (pcheck=head; pcheck != NULL; pcheck = pcheck->next)      //蠕动的动作  tmp_coor为中间变量  
            {
                tmp_coor = pcheck->coorsnake;

                pcheck->coorsnake = new_coor;

                new_coor = tmp_coor;
            }
        for (pcheck=head; pcheck != NULL; pcheck = pcheck->next)       //打印蛇身各节
            {
                if (pcheck == head)
                {
                    huashe(pcheck->coorsnake.x, pcheck->coorsnake.y, 0x0c);
                }

                else
                {
                    huashe(pcheck->coorsnake.x, pcheck->coorsnake.y, 0x0f);
                }
            }         
        switch(score/50)                                            //分数用来控制等级和蛇移动速度
        {
            case 0:level = 1; Sleep(500); break;
            case 1:level = 2; Sleep(400); break;
            case 2:level = 3; Sleep(300); break;
            case 3:level = 4; Sleep(200); break;
            case 4:level = 5; Sleep(150); break;
            case 5:level = 6; Sleep(100); break;
            default:level = 7; Sleep(50);
        }
    }    
    getch();    
    return 0;
}

void gotoxy(int x,int y,int color)                        //移动光标
{
    static HANDLE hOut; 
    static COORD position;      //坐标
    position.X = x*2;
    position.Y = y;
    hOut==NULL?(hOut = GetStdHandle(STD_OUTPUT_HANDLE)):hOut; 
    SetConsoleCursorPosition(hOut, position);            
    SetConsoleTextAttribute(hOut, color);
}

void map()
{
    int i,j;
    gotoxy(0,0,0x0b);
    for(i=0;i<W;i++)
    {
        for(j=0;j<H;j++)
        if(i==0||j==0||i==W-1||j==H-1||i==W-9)
        huashe(i,j,0x0b);
        if (i == W - 1 && j == H)break;              //不打印最后一个换行
        printf("\n");
    }
    gotoxy(34,2,0x0b);
    printf ("贪吃蛇");
    gotoxy(35,3,0x0b);
    printf ("--by rol");
    gotoxy(33,5,0x0b);
    printf (" 操作方法:");
    gotoxy(33,7,0x0b);
    printf ("← ↑ ↓ →");
    gotoxy(33,8,0x0b);
    printf ("左 上 下 右");
    gotoxy(34,10,0x0b);
    printf ("等 级");
    gotoxy(34,14,0x0b);
    printf ("得 分");
    gotoxy(34,17,0x0b);
    printf ("帮 助");
    gotoxy(33,19,0x0b);
    printf ("空格键 暂停");
    gotoxy(33,21,0x0b);
    printf ("任意键 继续");    
}

void huashe(int x,int y,int color)                        //画蛇头或蛇身以及边界
{
    static HANDLE hOut;  

    static COORD position;      //坐标

    position.X = x*2;
    position.Y = y;

    hOut==NULL?(hOut = GetStdHandle(STD_OUTPUT_HANDLE)):hOut;      

    SetConsoleCursorPosition(hOut, position);            
    SetConsoleTextAttribute(hOut, color);
    printf("");
}

char getkey()                                            //取键盘按键值
{
    char ch1,ch2;
    ch1=getch();
    if(ch1==ESC||ch1==SPACE||ch1==ENTER)
    return ch1;
    else if(ch1==-32)
    {
        ch2=getch();
        return ch2;
    }
    else return 0;
}

void mksnake(NODESNAKE **plast)                            //创建蛇头或蛇身链表
{
    NODESNAKE *pnew;
    if ((pnew = (NODESNAKE *)malloc(sizeof(NODESNAKE))) == NULL)
    {
        gotoxy(11,12,0x0b);
        printf("分配内存失败!按任意键退出!"); 
        getch();
        exit(0);
    }
    if(head==NULL)head=(*plast)=pnew;                                //第一次创建蛇头
    else
    {
    (*plast)->next=pnew,(*plast)=pnew;
    }
    pnew->next=NULL;    
}

void printfood(int x,int y,int color)                    //打印食物
{
    static HANDLE hOut;  

    static COORD position;      //坐标

    position.X = x*2;
    position.Y = y;

    hOut==NULL?(hOut = GetStdHandle(STD_OUTPUT_HANDLE)):hOut;      

    SetConsoleCursorPosition(hOut, position);            
    SetConsoleTextAttribute(hOut, color);
    printf("");
}

void mkfood(int *x,int *y)                                //创建随机食物坐标
{
    int issnake, i;            //issnake  标记是否在蛇身上
    NODESNAKE *pd;
    srand((unsigned int)time(NULL));
    for (i=0; i<W*H; i++)                          //生成食物供执行40*25次(控制台:80/2 * 25)
    { 
        issnake = 0; 
        do
        {
            *x = 1+rand()%(W-11); 

            *y = 1+rand()%(H-3);

            food_position.X = *x*2;
            food_position.Y = *y;

        } while (food_position.X<2||food_position.Y<2);

        for (pd=head; pd != NULL; pd = pd->next)
        if (pd->coorsnake.x == *x && pd->coorsnake.y == *y)            //判断是不是属于蛇身 
        {
            issnake = 1;
            break;
        }
        if (issnake == 0)    return;                                     //如果不是蛇身就返回        
    }
    gotoxy(11,12,0x0b);
    printf("你是大神,已经没有位置可以放食物了!\n 按任意键结束游戏!");
    getch();
    exit(1);
}

void HideCursor()                                                        //隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
搜索更多相关主题的帖子: include 小游戏 编译器 贪食蛇 color 
2014-10-10 00:34
hackrol
Rank: 4
来 自:世界和平组织
等 级:业余侠客
帖 子:62
专家分:267
注 册:2014-9-6
收藏
得分:0 
回复 2 楼 thj0775
呵呵 ..努力 一点罢了.几乎每天都 要看到12点.
2014-10-11 15:34
hackrol
Rank: 4
来 自:世界和平组织
等 级:业余侠客
帖 子:62
专家分:267
注 册:2014-9-6
收藏
得分:0 
回复 12 楼 cazhxi
老谭的书入门的,之后先后看了C与指针,C陷阱与缺陷,目前正在看C专家编程。

虽然老谭书入门的,不过真心觉得不怎么样,当然是在看到了更好的书之后 才觉得
2014-10-12 16:24
hackrol
Rank: 4
来 自:世界和平组织
等 级:业余侠客
帖 子:62
专家分:267
注 册:2014-9-6
收藏
得分:0 
回复 22 楼 t495647533
前面我有回复过,你看看!!
2014-10-19 23:55
快速回复:零基础自学快两个月了,昨天刚刚写小贪食蛇,分享下,以前写的小游戏就不 ...
数据加载中...
 
   



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

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