参考的别人的贪吃蛇程序,只做了少许的更改,惭愧。http://bbs.csdn.net/topics/350080780

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <curses.h>
#include <sys/time.h>struct Dir {int dx;int dy;
};struct Node {int nx;int ny;struct Node *prev;struct Node *next;
};struct Node *head, *tail;
struct Dir dir, food;
int hour, minute, second;
int length, ctrltime;void init_game();
void dis_first_line();
void game_over(int);
void rand_food();
void dis_snake();
void set_timeval(int, struct timeval *);
void free_last_node();
void insert_after_head(int, int);
void free_link();
void display();
void deal_cmd();void init_game()
{cbreak();noecho();curs_set(0);keypad(stdscr, true);hour = minute = second = ctrltime = 0;length = 1;food.dx = 19;food.dy = 9;dir.dx = 1;dir.dy = 0;head = (struct Node *)malloc(sizeof(struct Node));tail = (struct Node *)malloc(sizeof(struct Node));head->next = tail;head->prev = NULL;tail->next = NULL;tail->prev = head;tail->nx = 17;tail->ny = 7;move(1, 0);int i;for(i = 0; i < COLS; ++i)addstr("-");
}void dis_first_line()
{ctrltime += 200;if(ctrltime % 1000 != 200)return;if(ctrltime > 7000000)ctrltime = 0;move(0, 3);printw("time = %d:%d:%d", hour, minute, second);++second;if(second > 59) {second = 0;++minute;}if(minute > 59) {minute = 0;++hour;}if(hour > 23) {game_over(4);}move(0, COLS / 2 - 7);printw("length = %d", length);move(0, COLS / 2 + 25);printw("level = %d", length / 17 + 1);
}void game_over(int x)
{move(1, 0);switch(x) {case 1: printw("crash the wall, game over!");break;case 2: printw("crash snake itself, game over!");break;case 3: printw("congratulations, you just won.");break;case 4: printw("you have played the whole day, now exit.");free_link();endwin();exit(0);case 5: printw("ok, exit now.");sleep(2);free_link();endwin();exit(0);}move(2, 0);printw("press anykey to exit the game.");int ch = getch();endwin();free_link();exit(0);
}void rand_food()
{int x, y;while(1) {srand(time(0));x = rand() % COLS;y = rand() % (LINES - 2) + 2;if(x == 0 && y == 0)continue;struct Node *pnode = head->next;int flag = 1;while(pnode) {if(pnode->nx == x && pnode->ny == y) {flag = 0;break;}pnode = pnode->next;}if(flag) break;}food.dx = x;food.dy = y;
}void dis_snake()
{struct Node *pnode = head->next;if( COLS-1==pnode->nx && 1==dir.dx \|| 0==pnode->nx && -1==dir.dx  \|| LINES-1==pnode->ny && 1==dir.dy \|| 2==pnode->ny && -1==dir.dy)game_over(1);if('*' == mvinch(pnode->ny + dir.dy, pnode->nx + dir.dx))game_over(2);move(food.dy, food.dx);printw("@");insert_after_head(pnode->nx + dir.dx, pnode->ny + dir.dy);move(pnode->ny + dir.dy, pnode->nx + dir.dx);printw("*");if(pnode->nx + dir.dx == food.dx && pnode->ny + dir.dy == food.dy) {++length;if(length > 137)game_over(3);rand_food();} else {move(tail->ny, tail->nx);printw(" ");free_last_node();}
}void set_timeval(int n, struct timeval *ptv)
{ptv->tv_sec = n / 1000;ptv->tv_usec = n % 1000 * 1000;
}void free_last_node()
{struct Node *pnode = tail;tail = tail->prev;tail->next = NULL;free(pnode);
}void insert_after_head(int x, int y)
{struct Node *pnode = (struct Node *)malloc(sizeof(struct Node));pnode->nx = x;pnode->ny = y;pnode->next = head->next;pnode->prev = head;head->next->prev = pnode;head->next = pnode;
}void free_link()
{while(head->next)free_last_node();free(head);
}void display()
{dis_first_line();dis_snake();refresh();
}void deal_cmd()
{int cmd;while(1) {cmd = getch();if(KEY_LEFT == cmd) {dir.dx = -1;dir.dy = 0;} else if(KEY_RIGHT == cmd) {dir.dx = 1;dir.dy = 0;} else if(KEY_DOWN ==cmd) {dir.dx = 0;dir.dy = 1;} else if(KEY_UP == cmd) {dir.dx = 0;dir.dy = -1;} else if('q' == cmd) {game_over(5);}}
}int main(void)
{initscr();init_game();struct itimerval itv;set_timeval(1, &itv.it_value);set_timeval(200, &itv.it_interval);setitimer(ITIMER_REAL, &itv, NULL);signal(SIGALRM, display);deal_cmd();endwin();return 0;
}

模仿别人的简单贪吃蛇程序相关推荐

  1. 【PROTEUS仿真】AT89C51实现简单贪吃蛇程序

    原理图 源程序 #include <reg51.h> #define uchar unsigned char #define SNAKE 20 //最大长长度 #define TIME 5 ...

  2. 基于C语言Ncurse库和链表的简单贪吃蛇小游戏

    参考:基于C语言Ncurse库和链表的简单贪吃蛇小游戏 作者:三速何时sub20 发布时间:2020-09-29 10:23:51 网址:https://blog.csdn.net/weixin_44 ...

  3. JAVA小程序:一个基于MVC框架的贪吃蛇程序

    学习JAVA也有一段时间了,之前看了翁恺老师的视频,跟着做了一个细胞自动机,粗浅地了解了一点MVC框架的知识,感觉获益匪浅.但是细胞自动机毕竟是跟着视频完成的,有很大程度上都是参考了视频里的代码,没有 ...

  4. 完整版! 大一新手C语言数据结构实现简单贪吃蛇游戏

    大一新手C语言数据结构实现简单贪吃蛇游戏含源代码 贪吃蛇 1.前言简述 2.编写语言及工具 3.游戏的头文件代码(.h) 3.1map.h 3.2snake.h 3.3Food.h 4.游戏的源文件代 ...

  5. 【python教程入门学习】Python实现自动玩贪吃蛇程序

    这篇文章主要介绍了通过Python实现的简易的自动玩贪吃蛇游戏的小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学一学 实现效果 先看看效果 这比我手动的快多了,而且是单机的,自动玩没惹 ...

  6. c语言中怎么暂停一个一个游戏,求助:最近在linux下用c语言写了一个贪吃蛇程序,有几个问题,第一:贪吃蛇怎么实现暂停,第二:有时候同时输入上下左右中的两个键就会直接游戏结束...

    求助:最近在linux下用c语言写了一个贪吃蛇程序,有几个问题,第一:贪吃蛇怎么实现暂停,第二:有时候同时输入上下左右中的两个键就会直接游戏结束 /* 以下是主要的逻辑代码,还有些.c和.h就没发了 ...

  7. html与js简单小游戏,JS实现简单贪吃蛇小游戏

    本文实例为大家分享了JS实现简单贪吃蛇游戏的具体代码,供大家参考,具体内容如下 1.使用语言 HTML+CSS+JavaScript 2.使用工具 visual studio code 3.GitHu ...

  8. 用python写一个贪吃蛇程序

    我可以为您提供一些有关如何使用Python编写贪吃蛇程序的指导.首先,您需要确定您的程序所需的功能,以及游戏的规则和特性.接下来,您可以编写代码来实现这些功能,并使用Python的图形库来绘制游戏界面 ...

  9. 简单贪吃蛇c语言代码,一个C语言写简单贪吃蛇源代码.doc

    一个C语言写简单贪吃蛇源代码 #include #include #include #include #include #include int grade=5,point=0,life=3; voi ...

最新文章

  1. Django 使用celery任务队列的配置
  2. Mybatis 实现SQL拦截并在控制台打印SQL和参数
  3. 安卓虚拟机_安卓虚拟机(*New*)v1.1.31去广告/去推荐/Mod/精简/VIP版
  4. MongoDB学习笔记(二)使用Java操作MongoDB
  5. 02 socketserver客户端
  6. layerconfirm 自动关闭问题 没有阻塞问题_微信新版本自动更新?赶紧关闭这个功能...
  7. JVM证书制作步骤+耶鲁 CAS 配置
  8. 疑似iQOO Neo6通过3C认证:最高支持80W快充
  9. hashmap删除指定key_Java集合之HashMap源码解析(JDK8)
  10. [数据结构与算法] (顺序)线性表简单demo程序
  11. IDEA导入jar包之后引用不了
  12. Hi3516DV300开发板——2.uboot、kernel、fs文件系统烧写
  13. R语言while循环计算圆周率
  14. 数据结构--树与二叉树
  15. 如何在虚拟主机上部署java项目_如何在虚拟主机上部署java项目
  16. VDD、VBAT、VDDA、VREF+区别
  17. 使用Docker+Jenkin自动化流水线
  18. 计算机无线传输的标准,最受欢迎的几个无线标准
  19. pyinstaller 打包 torch\_jit_internal.py:750: UserWarning: Unable to retrieve source for @torch.jit._ov
  20. 翻译软件哪个准确度高【免费】

热门文章

  1. 强化学习快餐教程(1) - gym环境搭建
  2. 页面置换算法(局部、全局)-局部性很关键
  3. 知识变现海哥:如何把自己的想法变现?
  4. html中的关键词批量加粗,excel表格怎么批量加粗中指定的关键字?
  5. 读取文件字节流到字节数组,字节数组写入到文件
  6. 联想服务器做系统2018,联想小新潮7000 2018版(i5-8250U)如何用u盘装系统win10
  7. c语言函数将如何返数组,声明一个C函数返回一个数组
  8. 仓库已开始作业无法修改地址_Shopee SLS仓库异常件怎么退货?Shopee SLS仓库异常件退货流程介绍...
  9. 三星android5.0基带,基带难关解决 三星今年芯片全部支持全网通
  10. 【论文写作】利用word尾注写论文参考文献,endnote清楚文献格式