| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1306 人关注过本帖
标题:贪吃蛇
只看楼主 加入收藏
brokenheart
Rank: 2
等 级:论坛游民
帖 子:64
专家分:25
注 册:2015-11-7
结帖率:76.92%
收藏
已结贴  问题点数:20 回复次数:2 
贪吃蛇
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#define MAX_JOINTS  200
#define LEFT  0x4b00
#define RIGHT 0x4d00
#define DOWN  0x5000
#define UP    0x4800
#define ESC   0x011b
#define MV_RIGHT  1
#define MV_LEFT   2
#define MV_UP     3
#define MV_DOWN   4
void InitGraph(void);
void DrawFence(void);
void GameOver(int score);
void GamePlay(void);
void PrScore(int score);
struct Food
{
    int x;
    int y;
    int addFood;
};
struct Snake
{
    int x[MAX_JOINTS];
    int y[MAX_JOINTS];
    int joint;
    int direction;
     int life;
};
main()
{
    InitGraph();
    DrawFence();
    GamePlay();
    dlosegraph();
}
void InitGraph(void)
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"");
    sleardevice();
    setbkcolor(BLUE);
    cleardevice();
    setcolor(WHITE);
    settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
    outtextxy(170,150,"Greedy Snake");
    outtextxy(219,254,"Ready?");
    setcolor(BLUE);
    sleardevice();
}
void DrawFence(viod)
{
    int i;
    setbkcolor(LIGHTGREEN);
    setcolor(11);
    setlinestyle(SOLID_LINE,0,THICK_WIDTH);
    for(i=50,i<=600;i+=10)
    {
        rectangle(i,40,i+10,49);
        rectanhle(i,451,i+10,460);
    }
    for (i=40;i<=450,i+=10)
    {
        rectanhle(50,i,59,i+10);
        rectanhle(601,i,610,i+10);
    }
}
void GamePlay(void)
{
    int i,key;
    int gamespeed=22000;
    int score=0;
    struct Food food;
    struct Snake snake;
    food.addFood=1;
    snake.life=0;
    snake.direction=MV_RIGHT;
    snake.x[0]=100;snake.y[0]=100;
    snake.x[1]=110;snake.y[1]=100;
    snake.joint=2;
    PrScore(score);
    srand(time(NULL));
    while(1)
    {
        while(!kbhit())
        {
            if(food.addFood==1)
            {
                food.x=rand()%400+60;
                food.y=rand()%350+60;
                while(food.x%10!=0)
                {
                    food.x++;
                }
                while(food.y%10!=0)
                {
                    food.y++;
                }
                food.addFood=0;
            }
            if(food.addFood==0)
            {
                setcolor(GREEN);
                rectangle(food.x,food.y,food.x+10,food.y-10);
            }
            for(i=snake.joint-1;i>0;i--)
            {
                snake.x[i]=snake.x[i-1];
                snake.y[i]=snake.y[i-1];
            }
            switch(snake.direction)
            {
                case MV_RIGHT:snake.x[0]+=10;break;
                case MV_LEFT:snake.x[0]-=10;break;
                case MV_UP:snake.y[0]-=10;break;
                case MV_DOWN:snake.y[0]+=10;break;
            }
            for(i=3;i<snake.joint;i++)
            {
                if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
                {
                    GameOver(score);
                    snake.life=1;
                    break;
                }
            }
            if(snake.x[0]<55||snake.x[0]>595
               ||snake.y[0]<55||snake.u[0])
            {
                GameOver(score);
                snake.life=1;
            }
            if(snake.life==1)   break;
            if(snake.x[0]==food.x&&snake.y[0]==food.y)
            {
                setcolor(0);
                rectangle(food.x,food.y,food.x+10,food.y-10)
                snake.x[snake.joint]=-20;snake.y[snake.joint]=-20;
                snake.joint++;
                food.addFood=1;
                score+=10;
                PrScore(score);
            }
            setcolor(4);
            for(i=0;i<snake.joint;i++)
            {
                rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
            }
            delay(gamespeed);
            setcolor(0);
            rectangle(snake.x[snake.joint-1],snake.y[snake.joint-1],snake.x[snake.joint-1]+10,snake.y[snake.joint-1]-10);
        }
        if(key==ESC)   break;
        else if(key==UP&&snake.direction!=4)
            snake.direction=MV_UP;
        else if(key==RIGHT&&snake.direction!=MV_LEFT)
            snake.direction=MV_RIGHT;
        else if(key==LEFT&&snake.direction!=MV_RIGHT)
            snake.direction=MV_LEFT;
        else if(key==DOWN&&snake.direction!=MV_UP)
             snake.direction=MV_DOWN;
    }
}
void GameOver(int score)
{
    cleardevice();
    PrScore(score);
    setcolor(RED);
    serrextstle(0,0,4);
    outtextxy(200,200,"GAME OVER");
    getch();
}
void PrScore(int score)
{
    char str[10];
    setfillstyle(SOLID_FILL,YELLOW);
    bar(50,15,220,35);
    setcolor(6);
    settextstyle(0,0,2);
    sprintf(str,"score:%d",score);
    outtextxy(55,20,str);
}
照着书上打的代码,但是一编译就显示#include<graphics.h>这个库文件有问题,我的编译环境是codeblocks,大神帮忙看看该如何改动好
搜索更多相关主题的帖子: 贪吃蛇 include 
2015-12-18 13:34
newdos
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:6
帖 子:251
专家分:1169
注 册:2012-8-13
收藏
得分:20 
很古老的BGI代码了,看到你的代码,照着也打错了比如:dlosegraph(); sleardevice();rectanhle()...这些

你可以搜索一个叫ege, easyx的库来安装到你的codeblocks里,或许支持这种代码编译,在编译的时候,你需要在工程Build选项里的linker setting 链结处 add添加上graphics.a库才可以链结成功。
2015-12-18 14:19
brokenheart
Rank: 2
等 级:论坛游民
帖 子:64
专家分:25
注 册:2015-11-7
收藏
得分:0 
回复 2楼 newdos
看起来好像挺复杂似的
2015-12-18 17:31
快速回复:贪吃蛇
数据加载中...
 
   



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

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