一个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();
}

2 0
原创粉丝点击