求助:最近在linux下用c语言写了一个贪吃蛇程序,有几个问题,第一:贪吃蛇怎么实现暂停,第二:有时候同时输入上下左右中的两个键就会直接游戏结束
/*
以下是主要的逻辑代码,还有些.c和.h就没发了
*/
#include
#include
#include
#include "snake.h"
#include "input.h"
#include
#include//多线程
WINDOW*w_snake = NULL;
WINDOW*w_title = NULL;
int SNAKE_DIRECTION = UP;
struct snake snake;
struct food food;
/*
函数功能: 创建游戏所需要用到的界面
*/
void inin_windows()
{ /*newwin(nlines ,ncols,begin_y,begin_x)
newwin函数的作用是创建一个新的窗口,该窗口从屏幕位置(begin_y,begin_x)开始
行数和列数分别由参数nlines和ncols指定。
它返回一个指向新窗口的指针,如果新窗口创建失败则返回null。*/
w_snake = newwin(MAP_HEIGHT,MAP_WIDTH, 4, 5);
w_title = newwin(3,20, 0, 20);
}
/*
函数功能: 初始化食物
*/
void draw_food()
{
int flag = 0;
srandom(time(NULL));//设置随机数种子
while(1)
{
flag = 0;
food.x = random()%(MAP_WIDTH - 2) + 1;//从1到69随机生成一个数
food.y = random()%(MAP_HEIGHT - 2) + 1;//从1到28随机生成一个数
int i;
for(i = 0;i
{
if(snake.x[i] == food.x && snake.y[i] == food.y)//不能生成到蛇身体上
{
flag = 1;
break;
}
}
if(flag == 0 && food.x % 2 == 0)//食物的x坐标是奇数
{
break;
}
}
wmove(w_snake,food.y,food.x);
waddstr(w_snake,"*");//画出食物
wrefresh(w_snake);
}
/*
初始化蛇
*/
void init_snake()
{
snake.len = 3;//设置蛇的初始长度
int i;
for(i = 0;i < snake.len;i++)
{
snake.x[i] = MAP_WIDTH/2 - i*2 + 1;
snake.y[i] = MAP_HEIGHT/2;
wmove(w_snake,snake.y[i],snake.x[i]);//移动光标,依次显示
if(i == 0)
{
waddstr(w_snake,"@");
}
else
{
waddstr(w_snake,"* ");
}
}
wrefresh(w_snake);//刷新
}
/*
显示游戏框图
*/
void draw_snake_map()
{
int i;
for(i = 0;i < MAP_WIDTH;i+=2)
{
wmove(w_snake,0,i);//移动光标显示上面的一行
waddstr(w_snake,"+ ");
wmove(w_snake,MAP_HEIGHT-1,i);//移动光标显示下面的一行
waddstr(w_snake,"+ ");
}
for(i = 0;i < MAP_HEIGHT;i++)
{
wmove(w_snake,i,0);//移动光标显示左边的一行
waddstr(w_snake,"+");
wmove(w_snake,i,MAP_WIDTH-1);//移动光标显示右边的一行
waddstr(w_snake,"+");
}
wrefresh(w_snake);//刷新
init_snake();//投放蛇
}
/*
判断游戏是否结束
返回值: 结束: 0
未结束: 1
*/
int game_over()
{
if(snake.y[0] == 0 || snake.y[0] == MAP_HEIGHT - 1)//撞到上下墙壁
{
return 1;
}
if(snake.x[0] == 0 || snake.x[0] >= MAP_WIDTH - 2)//撞到左右墙壁
{
return 1;
}
int i;
for(i = 1;i < snake.len;i++)//撞到自己身体
{
if(snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
{
return 1;
}
}
if(snake.len == MAX_SIZE)//到达最大长度
{
return 1;
}
return 0;
}
/*
画出每次更新的贪吃蛇
*/
void draw_snake()
{
int i;
for(i = 0;i < snake.len;i++)
{
wmove(w_snake,snake.y[i],snake.x[i]);//移动光标显示
if(i == 0)
{
waddstr(w_snake,"0");//显示蛇头
}
else
{
waddstr(w_snake,"*");//显示蛇身体
}
}
wrefresh(w_snake);//刷新
}
/*
函数功能:多线程获取用户输入
*/
void* fun_input(void *yy)
{
while(1)
{
int mv = get_use_input();//保存原先方向
int pre_key = SNAKE_DIRECTION;
SNAKE_DIRECTION = mv;
if(pre_key == LEFT && mv == RIGHT)//原来蛇头是向左,但输入方向是向右,改回左
{
SNAKE_DIRECTION = LEFT;
}
if(pre_key == RIGHT && mv == LEFT)
{
SNAKE_DIRECTION = RIGHT;
}
if(pre_key == UP && mv == DOWN)
{
SNAKE_DIRECTION = UP;
}
if(pre_key == DOWN && mv == UP)
{
SNAKE_DIRECTION = DOWN;
}
if(pre_key != DOWN && pre_key != RIGHT && pre_key != LEFT && pre_key != UP && pre_key != QUIT)
{
SNAKE_DIRECTION = pre_key;
}
}
}
/*
game_snake:贪吃蛇游戏
*/
void game_snake()
{
int num = 0;
int x = 10;
int feed = 500000;
system("clear");
inin_windows();//创建游戏主界面
draw_snake_map();//显示游戏框图
wmove(w_title,0,0);
waddstr(w_title,"Snake_Game");
wmove(w_title,1,0);
waddstr(w_title,"Author: hui");
wmove(w_title,2,0);
waddstr(w_title,"score:");
wmove(w_title,2,6);
char buf[6]="";
sprintf(buf,"%d",num);
waddstr(w_title,buf);
wrefresh(w_title);//显示游戏名,作业,得分
draw_food();//投放食物
pthread_t tid;//实现多线程
pthread_create(&tid,NULL,fun_input,NULL);
while(1)
{
if(snake.x[0] == food.x &&snake.y[0] == food.y)//判断是否吃到食物
{
snake.len ++;//吃到长度 +1,得分 +1,并投放新的食物
draw_food();
num++;
}
else//没有吃到食物就照常
{
wmove(w_snake,snake.y[snake.len-1],snake.x[snake.len-1]);
waddstr(w_snake," ");//清空最后一个
wrefresh(w_snake);
}
int i;
for(i = snake.len - 1;i > 0;i--)//蛇身向他的前一个坐标移动
{
snake.x[i] = snake.x[i-1];
snake.y[i] = snake.y[i-1];
}
switch(SNAKE_DIRECTION)//判断移动方向
{
case UP:
snake.y[0]--;
break;
case DOWN:
snake.y[0]++;
break;
case LEFT:
snake.x[0]-= 2;
break;
case RIGHT:
snake.x[0]+= 2;
break;
}
draw_snake();//画出更新后的蛇
if(num % x == 0 && num != 0)//每吃10个就速度变成原来的0.5倍
{
feed = feed * 0.5;
x *= 2;
}
wmove(w_title,2,6);//显示更新后的分数
char buf[6]="";
sprintf(buf,"%d",num);
waddstr(w_title,buf);
wrefresh(w_title);
usleep(feed);//休眠时间
if(game_over())
{
wmove(w_snake,10,30);//游戏结束提示game over
waddstr(w_snake,"game over!");
wrefresh(w_snake);
sleep(1);
break;
}
}
}