C语言实现贪吃蛇游戏

时间:2023-03-09 18:56:09
C语言实现贪吃蛇游戏
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/times.h>
#include<sys/types.h>
#include<unistd.h>
#include<ncurses.h> #define TBool int
#define True 1
#define False 0
#define SHAPE_FOOD '@'
#define SHAPE_SNAKE '#'
#define GAMEWIN_YLEN 20
#define GAMEWIN_XLEN 60
#define LOGWIN_YLEN 7
#define LOGWIN_XLEN (GAMEWIN_XLEN)
#define LOGBUF_NUM (LOGWIN_YLEN - 2)
#define LOGBUF_LEN (GAMEWIN_XLEN - 2)
#define MAXLEVEL 12 #define GetSnakeTail(s) ((s)->head->front) WINDOW *logwin; //declare a windows for displaying message
#define INITRUNLOG() logwin = newlogw()
#define RUNLOG(str) runlog(logwin, str)
#define DESTROYRUNLOG() delwin(logwin) int g_level; //level of player enum TDirection
{
DIR_UP,
DIR_DOWN,
DIR_LEFT,
DIR_RIGHT
}; struct TFood
{
int x;
int y;
}; struct TSnakeNode
{
int y;
int x;
struct TSnakeNode *front;
}; struct TSnake
{
int length;
struct TSnakeNode *head;
enum TDirection dir;
}; int refreshgamew(WINDOW *win, struct TSnake *psnake);
void movesnake(struct TSnake *psnake);
int checksnake(struct TFood *pfood, struct TSnake *psnake);
void snakegrowup(struct TFood *pfood, struct TSnake *psnake);
void gameover(WINDOW *win, char *str);
struct TSnakeNode *newsnakenode(struct TSnakeNode **ppsnode, int y, int x);
struct TSnake *initsnake();
void destroysnake(struct TSnake *psnake);
void drawsnakew(WINDOW *win, struct TSnake *psnake);
void drawfoodw(WINDOW *win, struct TFood *pfood, struct TSnake *psnake);
TBool checkfood(struct TFood *pfood, struct TSnake *psnake);
WINDOW* newgamew();
WINDOW* newlogw();
void runlog(WINDOW* win, char *str);
void cleanline(WINDOW *win, int y, int x); int main()
{
initscr();
raw();
noecho();
keypad(stdscr, TRUE);
curs_set();
refresh(); g_level = ;
INITRUNLOG(); RUNLOG(" press 'q' or 'Q' to quit.");
RUNLOG(" press 'w/s/a/d' or 'W/S/A/D' to move the snake.");
RUNLOG("info:"); WINDOW *gwin = newgamew(); struct TSnake *psnake = initsnake();
drawsnakew(gwin, psnake); while(refreshgamew(gwin, psnake) >= )
; getch();
destroysnake(psnake);
delwin(gwin);
DESTROYRUNLOG();
endwin(); return ;
} WINDOW* newlogw()
{
WINDOW *win = newwin(LOGWIN_YLEN, LOGWIN_XLEN, GAMEWIN_YLEN + , );
box(win, , );
mvwprintw(win, , , " LOG ");
wrefresh(win); return win;
} WINDOW *newgamew()
{
WINDOW *win = newwin(GAMEWIN_YLEN, GAMEWIN_XLEN, , );
box(win, , );
mvwprintw(win, , , " GAME ");
mvwprintw(win, GAMEWIN_YLEN - , , " Level: %d ", g_level);
mvwprintw(win, GAMEWIN_YLEN - , , " Speed: %d ", (int)(g_level/));
wrefresh(win);
return win;
} void runlog(WINDOW *win, char *str)
{
static char logbuf[LOGBUF_NUM][LOGBUF_LEN] = {};
static int index = ;
strcpy(logbuf[index], str); int i = ; for (; i < LOGBUF_NUM; ++i)
{
cleanline(win, i+, );
mvwprintw(win, i+, , logbuf[(index + i) % LOGBUF_NUM]);
wrefresh(win);
}
index = (index + LOGBUF_NUM - ) % LOGBUF_NUM;
} //将窗口win的坐标(x,y)清空
void cleanline(WINDOW *win, int y, int x)
{
char EMPTYLINE[LOGBUF_LEN] = {}; //LOGBUF_LEN = 57 //置空数组 0-56个位置
memset(EMPTYLINE, ' ', LOGBUF_LEN-); //将光标移动到窗口win(y,x)然后打印字符串EMPTYLINE
mvwprintw(win, y, x, EMPTYLINE); //在指定窗口上显示内容
wrefresh(win);
}
int refreshgamew(WINDOW *win, struct TSnake *psnake)
{
static TBool ffood = False;
struct TFood pfood; if (!ffood)
{
drawfoodw(win, &pfood, psnake);
ffood = True;
} int key = -; fd_set set;
FD_ZERO(&set);
FD_SET(, &set); struct timeval timeout;
timeout.tv_sec = ;
timeout.tv_usec = ( - (int)(g_level/)) * * ; if (select(, &set, NULL, NULL, &timeout) < )
{
return -;
} if (FD_ISSET(, &set))
{
while ((key = getch()) == -);
switch(key)
{
case 'w':
case 'W':
(psnake->dir == DIR_DOWN) ? : (psnake->dir = DIR_UP);
break;
case 's':
case 'S':
(psnake->dir == DIR_UP) ? : (psnake->dir = DIR_DOWN);
break; case 'a':
case 'A':
(psnake->dir == DIR_RIGHT) ? : (psnake->dir = DIR_LEFT);
break; case 'd':
case 'D':
(psnake->dir == DIR_LEFT) ? : (psnake->dir = DIR_RIGHT);
break; case 'q':
case 'Q':
RUNLOG("Quit Game!");
gameover(win, "Quit Game!");
return -; default:
break;
}
} movesnake(psnake);
drawsnakew(win, psnake);
switch(checksnake(&pfood, psnake))
{
case :
break; case :
ffood = False;
if (++g_level > MAXLEVEL)
{
RUNLOG("Win!!!");
gameover(win, "Win!!!");
return -;
} mvwprintw(win, GAMEWIN_YLEN-, , " Level: %d ", g_level);
mvwprintw(win, GAMEWIN_YLEN-, , " Speed: %d ", (int)(g_level/));
wrefresh(win);
RUNLOG("Level UP!");
snakegrowup(&pfood, psnake);
break; default:
RUNLOG("Game over!");
gameover(win, "Game over!");
return -;
}
return ;
} /**
* stuct TSnake是一个倒置的首尾相连的链表,head->front指向snake的尾部
* 如: [a]<-[b]<-[c]<-[d] a为head
* | ^ snake移动的时候,只用head指向d,
* `--------------' 并且修改d的(y,x)为snake头移动到的位置.
*/
void movesnake(struct TSnake *psnake)
{
int hy = psnake->head->y;
int hx = psnake->head->x; psnake->head = GetSnakeTail(psnake);
switch(psnake->dir)
{
case DIR_UP:
psnake->head->y = hy - ;
psnake->head->x = hx;
break; case DIR_DOWN:
psnake->head->y = hy + ;
psnake->head->x = hx;
break; case DIR_LEFT:
psnake->head->y = hy;
psnake->head->x = hx - ;
break; case DIR_RIGHT:
psnake->head->y = hy;
psnake->head->x = hx + ;
break; default:
break;
}
} int checksnake(struct TFood *pfood, struct TSnake *psnake)
{
int hy = psnake->head->y;
int hx = psnake->head->x;
if (hy <= || hy >= GAMEWIN_YLEN || hx <= || hx >= GAMEWIN_XLEN)
{
return -;
} struct TSnakeNode *pnode = GetSnakeTail(psnake);
int i = ;
for (; i < psnake->length - ; ++i, pnode = pnode->front)
{
if (hy == pnode->y && hx == pnode->x)
{
return -;
} }
if (hy == pfood->y && hx == pfood->x)
{
return ;
}
return ;
} void snakegrowup(struct TFood *pfood, struct TSnake *psnake)
{
struct TSnakeNode *pnode = (struct TSnakeNode *)malloc(sizeof(struct TSnakeNode)); switch(psnake->dir)
{
case DIR_UP:
pnode->y = psnake->head->y - ;
pnode->x = psnake->head->x;
break; case DIR_DOWN:
pnode->y = psnake->head->y + ;
pnode->x = psnake->head->x;
break; case DIR_LEFT:
pnode->y = psnake->head->y ;
pnode->x = psnake->head->x - ;
break; case DIR_RIGHT:
pnode->y = psnake->head->y;
pnode->x = psnake->head->x + ;
break; default:
break;
} pnode->front = GetSnakeTail(psnake);
psnake->head->front = pnode;
psnake->head = pnode;
++psnake->length;
} void gameover(WINDOW *win, char *str)
{
mvwprintw(win, (int)(GAMEWIN_YLEN/), (GAMEWIN_XLEN/ - strlen(str)/), str);
mvwprintw(win, (int)(GAMEWIN_YLEN/ + ), , "Please any key to quit...");
wrefresh(win);
} struct TSnakeNode *newsnakenode(struct TSnakeNode **ppsnode, int y, int x)
{
*ppsnode = (struct TSnakeNode *)malloc(sizeof(struct TSnakeNode));
(*ppsnode)->y = y;
(*ppsnode)->x = x;
(*ppsnode)->front = NULL; return *ppsnode;
} struct TSnake *initsnake()
{
struct TSnake *psnake = (struct TSnake *)malloc(sizeof(struct TSnake)); psnake->dir = DIR_LEFT;
psnake->length = ; newsnakenode(&newsnakenode(&newsnakenode(&newsnakenode(&psnake->head, , )->front,
, )->front, , )->front,
, )->front = psnake->head; return psnake;
} void destroysnake(struct TSnake *psnake)
{
struct TSnakeNode *psnode = GetSnakeTail(psnake);
struct TSnakeNode *ptmp = NULL; int i = ;
for (; i < psnake->length; ++i)
{
ptmp = psnode;
psnode = psnode->front;
free(ptmp);
} free(psnake);
psnake = NULL;
} void drawsnakew(WINDOW *win, struct TSnake *psnake)
{
static int taily = ;
static int tailx = ;
if (taily != && tailx != )
{
//将光标移动到指定窗口的指定位置(taily, tailx)然后输出字符
mvwaddch(win, taily, tailx, ' ');
} struct TSnakeNode *psnode = GetSnakeTail(psnake); int i = ;
for (; i < psnake->length; ++i)
{
mvwaddch(win, psnode->y, psnode->x, SHAPE_SNAKE);
psnode = psnode->front;
} taily = GetSnakeTail(psnake)->y;
tailx = GetSnakeTail(psnake)->x; wrefresh(win);
} void drawfoodw(WINDOW *win, struct TFood *pfood, struct TSnake *psnake)
{
do
{
pfood->y = random() % (GAMEWIN_YLEN - ) + ;
pfood->x = random() % (GAMEWIN_XLEN - ) + ;
}while(False == checkfood(pfood, psnake)); checkfood(pfood, psnake);
mvwaddch(win, pfood->y, pfood->x, SHAPE_FOOD);
wrefresh(win);
} //检查food出现的位置不能在snake上
TBool checkfood(struct TFood *pfood, struct TSnake *psnake)
{
struct TSnakeNode *pnode = GetSnakeTail(psnake); int i = ;
for (; i < psnake->length; ++i, pnode = pnode->front)
{
if (pfood->y == pnode->y && pfood->x == pnode->x)
{
return false;
}
} return true;
}

两个重点:

1.整个窗口的坐标以(0,0)为原点,x轴,y轴建立坐标点,x轴正向和y轴负向作为数值增大的方向,所以坐标移动时向下y轴坐标+1,反之-1

2.蛇移动时,每次只移动一个单位距离,所以只需要将最后一个节点拿走,放到要移动的那个位置上就整体上有移动的感觉了,至于移动的位置会有函数检测是否合法。

注意:

程序用到了ncurses库,如果没有的话需要下载:

# 更新软件源中的所有软件列表
$ sudo apt-get update # 安装 `curses` 库
$ sudo apt-get install libncurses5-dev

编译时最后要加上-lncurses