page contents

C语言小游戏-贪吃蛇

//贪吃蛇 #include <graphics.h> #include <conio.h> #include <time.h> #include <stdlib.h> #include <stdio.h> #define SIZEMAX100 #define SNAKELEN1...

//贪吃蛇

#include <graphics.h>

#include <conio.h>

#include <time.h>

#include <stdlib.h>

#include <stdio.h>

#define SIZEMAX100

#define SNAKELEN10

#define SPEED100


typedef enum

{

left, right, up, down

}DIR;


typedef struct

{

int x;

int y;

}COOR;


struct FOOD

{

COOR crd;

int flag;

}food;


struct SNAKE

{

COOR crd[SIZEMAX];

int node;

DIR dir;

}snake;


int speed = SPEED;

void init();

void setfoodcrd();

void showfood();

void snakemove();

void judgeslod();

void judgesef();

void showsnake();

void changeskdir();

void gameover();


void main()

{

init();

while (1)

{

while (!_kbhit())

{

if (!food.flag)

{

setfoodcrd();

}

showfood();

judgeslod();

judgesef();

snakemove();

Sleep(speed);

}


changeskdir();

}

}


void init()

{

initgraph(640, 480);

srand(time(NULL));

food.flag = 0;

snake.crd[0].x = 0 + SNAKELEN;

snake.crd[0].y = 0;

snake.crd[1].x = 0;

snake.crd[1].y = 0;

snake.node = 2;

snake.dir = right;

}


void setfoodcrd()

{

food.crd.x = rand() % (600 / SNAKELEN) * SNAKELEN;

food.crd.y = rand() % (480 / SNAKELEN) * SNAKELEN;

food.flag = 1;

}


void showfood()

{

rectangle(food.crd.x, food.crd.y, food.crd.x + SNAKELEN, food.crd.y + SNAKELEN);

}

void snakemove()

{

int i; for (i = snake.node; i>0; i--)

{

snake.crd[i].x = snake.crd[i - 1].x;

snake.crd[i].y = snake.crd[i - 1].y;

}

switch (snake.dir)

{

case up:

snake.crd[0].y -= SNAKELEN;

break;

case down:

snake.crd[0].y += SNAKELEN;

break;

case left:

snake.crd[0].x -= SNAKELEN;

break;

case right:

snake.crd[0].x += SNAKELEN;

} showsnake();

}

void judgeslod()

{

int i;

if (snake.crd[0].x<0 || snake.crd[0].x >= 640 ||

snake.crd[0].y<0 || snake.crd[0].y >= 480)

{

gameover();

getchar();

exit(0);

}

for (i = snake.node - 1; i>0; i--)

{

if (snake.crd[0].x == snake.crd[i].x && snake.crd[0].y == snake.crd[i].y)

{

gameover();

getchar();

exit(0);

}

}

}

void judgesef()

{

if (snake.crd[0].x == food.crd.x && snake.crd[0].y == food.crd.y)

{

snake.node++;


setcolor(BLACK);

rectangle(food.crd.x, food.crd.y, food.crd.x + SNAKELEN, food.crd.y + SNAKELEN);

setcolor(WHITE);


food.flag = 0;

}

}

void showsnake()

{

int i;

for (i = snake.node - 1; i >= 0; i--)

{

rectangle(snake.crd[i].x, snake.crd[i].y, snake.crd[i].x + SNAKELEN, snake.crd[i].y + SNAKELEN);

}

setcolor(BLACK);

rectangle(snake.crd[snake.node].x, snake.crd[snake.node].y, snake.crd[snake.node].x + SNAKELEN, snake.crd[snake.node].y + SNAKELEN);

setcolor(WHITE);

rectangle(snake.crd[snake.node - 1].x, snake.crd[snake.node - 1].y, snake.crd[snake.node - 1].x + SNAKELEN, snake.crd[snake.node - 1].y + SNAKELEN);

}

void changeskdir()

{

char key;

key = _getch();

switch (key)

{

case 'w':

case 'W':

case 72:

if (snake.dir != down)

{

snake.dir = up;

}

break;

case 's':

case 'S':

case 80:

if (snake.dir != up)

{

snake.dir = down;

}

break;

case 'a':

case 'A':

case 75:

if (snake.dir != right)

{

snake.dir = left;

}

break;

case 'd':

case 'D':

case 77:

if (snake.dir != left)

{

snake.dir = right;

}

break;

}

}


void gameover()

{

closegraph();

printf("game over!\n");

}

  • 发表于 2021-05-07 16:22
  • 阅读 ( 672 )
  • 分类:C/C++开发

1 条评论

请先 登录 后评论
小威
小威

64 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1470 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章