21xrx.com
2024-12-23 01:17:23 Monday
登录
文章检索 我的文章 写文章
DevC++小游戏代码
2023-07-05 03:48:10 深夜i     --     --
DevC++ 小游戏 代码

DevC++ 是一款非常受欢迎的编程软件,很多程序员都在使用它开发各种应用程序、小工具和游戏,今天我们来分享一款使用 DevC++ 编写的小游戏代码。

这款小游戏是一款经典的贪吃蛇游戏,玩家通过控制一条蛇来吃掉屏幕上的食物,每吃掉一块食物,蛇的长度就会增加一些。同时,玩家需要避开蛇头碰到屏幕边缘或者蛇的身体,否则游戏就会结束。

以下是该游戏的代码实现:


#include<graphics.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#define UP 72

#define DOWN 80

#define LEFT 75

#define RIGHT 77

struct SnakeNode

y;

;

struct Snake

{

    SnakeNode snakeBody[400];

    int length;

    int direction;

};

void GameOver();

void InitGame();

void DrawSnake(Snake *snake);

void DrawFood(int x,int y);

void MoveSnake(Snake *snake);

bool EatFood(Snake *snake,int x,int y);

int main()

{

    InitGame();

    setbkcolor(EGERGB(0,0,0));

    cleardevice();

    Snake *snake=new Snake;

    DrawSnake(snake);

    srand((unsigned)time(NULL));

    int x=rand()%60*10+30,y=rand()%40*10+30;

    DrawFood(x,y);

    while(1)

    {

        if(EatFood(snake,x,y))

        {

            snake->length++;

            x=rand()%60*10+30;

            y=rand()%40*10+30;

            DrawFood(x,y);

        }

        MoveSnake(snake);

        if(snake->snakeBody[0].x<20||snake->snakeBody[0].x>630||snake->snakeBody[0].y<20||snake->snakeBody[0].y>430)

            GameOver();

        for(int i=1;i<snake->length;i++)

            if(snake->snakeBody[0].x==snake->snakeBody[i].x&&snake->snakeBody[0].y==snake->snakeBody[i].y)

                GameOver();

        cleardevice();

        DrawSnake(snake);

        DrawFood(x,y);

        delay(200);

        if(kbhit())

            switch(getch())

            {

            case UP:

                if(snake->direction!=DOWN)snake->direction=UP;

                break;

            case DOWN:

                if(snake->direction!=UP)snake->direction=DOWN;

                break;

            case LEFT:

                if(snake->direction!=RIGHT)snake->direction=LEFT;

                break;

            case RIGHT:

                if(snake->direction!=LEFT)snake->direction=RIGHT;

                break;

            case 27:

                delete []snake;

                closegraph();

                exit(0);

                break;

            }

    }

}

void InitGame()

{

    initgraph(640,480);

    setbkcolor(EGERGB(0,0,0));

    setcolor(EGERGB(255,255,255));

    settextstyle(18,0,"Arial");

    outtextxy(260,200,"Press Any Key To Start!");

    getch();

}

void GameOver()

{

    setbkcolor(EGERGB(0,0,0));

    setcolor(EGERGB(255,255,255));

    settextstyle(18,0,"Arial");

    outtextxy(270,220,"Game Over!");

    getch();

    delete []snake;

    closegraph();

    exit(0);

}

void DrawSnake(Snake *snake)

{

    snake->snakeBody[0].x=320;

    snake->snakeBody[0].y=240;

    snake->length=3;

    snake->direction=RIGHT;

    for(int i=1;i<snake->length;i++)

    {

        snake->snakeBody[i].x=snake->snakeBody[0].x-i*10;

        snake->snakeBody[i].y=snake->snakeBody[0].y;

    }

    for(int i=0;i<snake->length;i++)

    {

        setfillcolor(EGERGB(255,255,255));

        fillcircle(snake->snakeBody[i].x,snake->snakeBody[i].y,5);

    }

}

void DrawFood(int x,int y)

{

    setfillcolor(EGERGB(255,0,0));

    solidcircle(x,y,5);

}

void MoveSnake(Snake *snake)

{

    for(int i=snake->length-1;i>0;i--)

    {

        snake->snakeBody[i].x=snake->snakeBody[i-1].x;

        snake->snakeBody[i].y=snake->snakeBody[i-1].y;

    }

    switch(snake->direction)

    {

    case UP:

        snake->snakeBody[0].y-=10;break;

    case DOWN:

        snake->snakeBody[0].y+=10;break;

    case LEFT:

        snake->snakeBody[0].x-=10;break;

    case RIGHT:

        snake->snakeBody[0].x+=10;break;

    }

}

bool EatFood(Snake *snake,int x,int y)

{

    if(snake->snakeBody[0].x-10<=x&&snake->snakeBody[0].x+10>=x&&snake->snakeBody[0].y-10<=y&&snake->snakeBody[0].y+10>=y)

        return true;

    else

        return false;

}

以上代码非常详细地实现了贪吃蛇游戏的核心逻辑,包括初始化游戏、绘制蛇身和食物、移动蛇身、检测吃掉食物、检测游戏结束等功能。如果你也想开发一个自己的小游戏,可以通过对这个代码进行理解和修改来实现自己的创意游戏。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复