刚刚写的贪吃蛇
在linux下写的,刚刚写好,图形化用的是调用curses.h插个效果图
程序代码:
/************************************** 功能:贪吃蛇程序 ***************************************/ //======================================== //设置光标初始位置 #include <stdio.h> #include <curses.h> #include <pthread.h> #include <time.h> #include <stdlib.h> #define MAXY 25 #define MAXX 70 typedef struct dotS{ int x; int y; struct dotS *next; }dotSnake, *dotLink; int ch; //初始视窗 void initail() { initscr(); cbreak(); nonl(); noecho(); intrflush(stdscr,FALSE); keypad(stdscr,TRUE); } //初始化贪吃蛇 void initLink(dotLink *L) { dotSnake *p; int m, n; *L = (dotSnake *)malloc(sizeof(dotSnake)); (*L)->next = NULL; p = (dotSnake *)malloc(sizeof(dotSnake)); m = (rand() % (MAXX - 2)) + 1; n = (rand() % (MAXY - 2)) + 1; (*L)->next = p; p->x = m; p->y = n; p->next = NULL; } //判断是否死亡函数 int judgeDeath(dotLink L, WINDOW *win) { dotSnake *p, *q; p = L->next; q = p->next; if ( p->x == 70 || p->y == 25 ) { box(win, '*', '*'); mvwaddstr(win, 13, 30,"the game is over!"); wnoutrefresh(win); doupdate(); getch(); endwin(); exit(1); } if ( q != NULL ) { q = q->next; while ( q != NULL ) { if ( p->x == q->x && p->y == q->y) { box(win, '*', '*'); mvwaddstr(win, 13, 30,"the game is over!"); wnoutrefresh(win); doupdate(); getch(); endwin(); exit(1); } q = q->next; } } } //生成随机食物 dotSnake *randFood(dotLink L, WINDOW *win) { int m,n, flag = 1; dotSnake *p, *q; q = (dotSnake *)malloc(sizeof(dotSnake)); while (flag) { p = L->next; m = (rand() % (MAXX - 2)) + 1; n = (rand() % (MAXY - 2)) + 1; while( p != NULL ) { if ( p->x == m && p->y == n ) { break; } p = p->next; } if ( p == NULL ) { q->x = m; q->y = n; flag = 0; } } return q; } //贪吃蛇不断运动函数 void forwardMove(dotLink L, WINDOW *win) { dotSnake *p; int m, n, i, j; p = L->next; if ( p->next != NULL ) { m = p->x; n = p->y; while ( p->next != NULL ) { i = p->next->x; j = p->next->y; p->next->x = m; p->next->y = n; m = i; n = j; p = p->next; } p = L->next; (p->y)--; } else { (p->x)++; } p = L->next; } //线程函数 void *waitKey(void *prapra) { while (1) { usleep(1000); ch = getch(); } } //控制蛇运动方向函数 int controlSnake(dotLink L) { int m = 0, n = 0, i = 0, j = 0; int flag = 0; dotSnake *p; p = L->next; switch(ch) { case KEY_UP: if ( p->next != NULL ) { m = p->x; n = p->y; while ( p->next != NULL ) { i = p->next->x; j = p->next->y; p->next->x = m; p->next->y = n; m = i; n = j; p = p->next; } p = L->next; (p->y)--; } else { (p->y)--; } flag = 1; break; case KEY_DOWN: if ( p->next != NULL ) { m = p->x; n = p->y; while ( p->next != NULL ) { i = p->next->x; j = p->next->y; p->next->x = m; p->next->y = n; m = i; n = j; p = p->next; } p = L->next; (p->y)++; } else { (p->y)++; } flag = 1; break; case KEY_LEFT: if ( p->next != NULL ) { m = p->x; n = p->y; while ( p->next != NULL ) { i = p->next->x; j = p->next->y; p->next->x = m; p->next->y = n; m = i; n = j; p = p->next; } p = L->next; (p->x)--; } else { (p->x)--; } flag = 1; break; case KEY_RIGHT: if ( p->next != NULL ) { m = p->x; n = p->y; while ( p->next != NULL ) { i = p->next->x; j = p->next->y; p->next->x = m; p->next->y = n; m = i; n = j; p = p->next; } p = L->next; (p->x)++; } else { (p->x)++; } flag = 1; break; case 27: endwin(); exit(0); default : break; } return flag; } //贪吃蛇吃掉食物 int judgeEat(dotLink L, dotSnake *p) { dotSnake *temp; dotLink q; if ( L->next->x == p->x && L->next->y == p->y ) { q = L->next; temp = (dotSnake *)malloc(sizeof(dotSnake)); while ( q->next != NULL ) { q = q->next; } temp->next = q->next; q->next = temp; temp->x = q->x; temp->y = q->y; return 1; } else { return 0; } } // void paint(dotLink L, dotSnake *p, WINDOW *win) { dotSnake *q; q = L->next; mvwaddstr(win, p->y, p->x, "*"); while ( q != NULL ) { mvwaddstr(win, q->y, q->x, "*"); q = q->next; } wnoutrefresh(win); doupdate(); } //建立视窗 int main() { WINDOW *win; dotLink snake; dotSnake *food; int flag, ret; pthread_t tid; initLink(&snake); srand((unsigned int)time(NULL)); initail(); //wnoutrefresh(win); //doupdate(); food = randFood(snake, win); ret = pthread_create(&tid, NULL, waitKey, NULL); if ( ret != 0 ) { exit(1); } while (1) { win = (WINDOW *)malloc(sizeof(WINDOW)); win = newwin(25, 70, 0, 0); box(win, '*', '*'); paint (snake, food, win); usleep(200000); flag = controlSnake(snake); if ( flag == 0 ) { forwardMove(snake, win); } if ( judgeEat(snake, food) == 1 ) { food = randFood(snake, win); paint(snake, food, win); } judgeDeath(snake, win); free(win); } return 0; }