一个linux下的简单的贪吃蛇游戏(链表)
来源:互联网 发布:iphone手机称重软件 编辑:程序博客网 时间:2024/03/28 20:55
1.链表写的贪吃蛇游戏。
2.有加速减速功能,内容简单。
3.本来的设想:
1.随机出现数字,蛇吃掉后会自动加到最后。
2.界面中有四根柱子,撞到就会把最后一个甩掉随机的一个地方。而本来存在的那个仍然存在。
3.随机产生数和已经存在的蛇身数不一样。
4.当舌头
#include <stdio.h>
#include <signal.h>
#include <curses.h>
#include <sys/time.h>
#include <time.h>
/*边界*/
#define LEFT 0
#define RIGHT LINES
#define UP 0
#define DOWN COLS
/*蛇的性质*/
int len = 1; //蛇长
int headx,heady;
int lastx,lasty;
int outx,outy;
int pointx[15],pointy[15];
/*点的性质*/
/*typedef struct newp{
int x = 0;
int y = 0;
int value = 0;
struct newp *next;
}newp;
*/
/*操作*/
int speed = 200;
int lrdir = 0;//左右方向
int uddir = 1;
/*声明*/
void input();
int set_ticker(int n_msecs);
void add();
void yidong();
int main(void)
{
/*初始化贪吃蛇*/
initscr();
crmode();//start break() mode
noecho();//not backshow
clear();
headx = (LEFT+RIGHT)/2;
heady = (UP+DOWN)/2;
move(headx,heady);
addstr("*");
lastx = headx;
lasty = heady;
pointx[0] = headx;
pointy[0] = heady;
/*out point*/
outx = random(RIGHT);
outy = random(DOWN);
move(outx,outy);
addstr("!");
refresh();
signal(SIGALRM,input);//when use it. When the timmer get the time,it should be take on.
set_ticker(speed);
// sleep(1);
while(1)
{
char c;
c = getch(); //读取键值
// int c = getch();//如果没有输入呢?
if(c == 'w')
{
uddir = -1;
lrdir = 0;
}
if(c == 's')
{
uddir = 1;
lrdir = 0;
}
if(c == 'a')
{
uddir = 0;
lrdir = -1;
}
if(c == 'd')
{
uddir = 0;
lrdir = 1;
}
if(c == '0')
{
speed /=2;
if(speed == 0)
speed = 1;
}
if(c == '1')
speed *=2;
if(c == 't')
break;
}
endwin();
return 0;
}
/*键盘操作*/
void input()
{
signal(SIGALRM,input);
// refresh();
int n1,n2;
n1 = pointx[0] + uddir;
n2 = pointy[0] + lrdir;
if((n2 == outy) && (n1 == outx))
{
outx = random(RIGHT);
outy = random(DOWN);
move(outx,outy);
addstr("!");
add();
// refresh();
}
yidong();
pointy[0] += lrdir;
pointx[0] += uddir;
move(pointx[0],pointy[0]);
addstr("*");
move(lastx,lasty);
addstr(" ");
lastx = pointx[len-1];
lasty = pointy[len-1];
refresh();
if((pointx[0] == 0) || (pointx[0] == LINES) || (pointy[0] == 0) || (pointy[0] == COLS))
{
endwin();
exit(0);
}
set_ticker(speed);//这个控制速度的是相反的,改好之后要调这一块
}
int set_ticker(int n_msecs)
{
struct itimerval new_timeset;
long n_sec,n_usecs;
n_sec = n_msecs/1000;
n_usecs=(n_msecs%1000)*1000L;
new_timeset.it_interval.tv_sec=n_sec;
new_timeset.it_interval.tv_usec=n_usecs;
new_timeset.it_value.tv_sec = n_sec;
new_timeset.it_value.tv_usec= n_usecs;
return setitimer(ITIMER_REAL,&new_timeset,NULL);
}
int random(int k)
{
srand((unsigned)time(NULL));
return(rand() % k);
}
void add()
{
move(lastx,lasty);
addstr("!");
pointx[len] = lastx;
pointy[len] = lasty;
len++;
// yidong();
}
void yidong()
{
int i;
lastx = pointx[len - 1];
lasty = pointy[len - 1];
for(i = len - 1;i > 0;i--)
{
pointx[i] = pointx[i-1];
pointy[i] = pointy[i-1];
move(pointx[i],pointy[i]);
addstr("!");
}
refresh();
}
- 一个linux下的简单的贪吃蛇游戏(链表)
- 一个简单的游戏--贪吃蛇
- linux console下的贪吃蛇游戏
- 简单的贪吃蛇游戏
- JavaScript实现一个简单的贪吃蛇游戏
- 游戏编程笔记-起步(一)一个简单的游戏-贪吃蛇
- 分享自己写的一个贪吃蛇的游戏(Linux)
- 一个简单的贪吃蛇
- Linux平台下贪吃蛇游戏的运行
- 基于Linux下的GTK+2.0贪吃蛇游戏开发
- 贪吃蛇游戏的简单设计
- c#简单贪吃蛇游戏的思想
- 简单的贪吃蛇游戏 代码
- qtday03 简单版的贪吃蛇游戏
- 简单贪吃蛇游戏的C++实现
- 简单的贪吃蛇游戏实现
- 简单的贪吃蛇游戏实现
- linux下c语言写的简单的贪吃蛇
- 数据挖掘基本模型一览
- iOS-Pch常用代码
- 求两个数的最大公约数算法
- 一个数据库多Instance的连接解决方案
- 一些有用的网站
- 一个linux下的简单的贪吃蛇游戏(链表)
- 当EditText是多行文本时, 光标如何设置在从左上角
- Scala学习第二十天 Scala中的本地函数与作为语言一等公民的函数详解
- UISegmentedControl的常用属性
- JavaScript写的图片库功能(小图变大图查看浏览)
- activeMQ 入门学习(二)
- JSP 页面中用绝对路径显示图片
- nodejs+express+jade创建新文件
- Preconditions-优雅的检验参数