首页 > 代码库 > 架构练习:c语言实现贪吃蛇(四):接收按键改变蛇的前进方向

架构练习:c语言实现贪吃蛇(四):接收按键改变蛇的前进方向

创建一个线程,接收按键,改变蛇的前进方向:

根据小键盘数值对应的方向:

‘2’:蛇向下行

‘4’:蛇向左行

‘6’:蛇向右行

‘8’:蛇向上行


源码:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <curses.h>
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>


#define GAME_WIDTH 50
#define GAME_HIGTH 25


int targetGenerate(int *x_site, int *y_site)
{
	int seconds= time((time_t*)NULL); 
	
	*y_site = abs(seconds*random())%GAME_HIGTH;		
	*x_site = abs(seconds*random())%GAME_WIDTH;
	
	return 0;
}

typedef unsigned char uchar;

typedef struct snakeinfo
{
	int numParts;/* how many parts,蛇身体分多少个段 */
	int lenParts[GAME_WIDTH];/* 蛇身体每段的长度 */
	int xPartsHead[GAME_WIDTH];/* 蛇身体第i段的x坐标,初始值为1 */
	int yPartsHead[GAME_WIDTH];/* 蛇身体第i段的y坐标,初始值为1 */
	uchar direction;/* 蛇当前在像哪个方向移动,2:下,4:左,6:右,8:上 */
	int (*moveDown)(WINDOW *win);
	int (*moveUp)(WINDOW *win);
	int (*moveLeft)(WINDOW *win);
	int (*moveRight)(WINDOW *win);
}SnakeSt;
static char charDir = 6;
static SnakeSt snake = {1,{1,},{GAME_WIDTH/2,},{GAME_HIGTH/2,}, 6};
/* 初始值蛇身体长1段,第一段长度为1,初始向右移动,蛇的初始位置在中间 */
int snakeMove(WINDOW *win);

int snakeMoveDown(WINDOW *win)
{
	snake.yPartsHead[0] += 1;

	return 0;
}

int snakeMoveUp(WINDOW *win)
{

	snake.yPartsHead[0] -= 1;

	return 0;
}

int snakeMoveLeft(WINDOW *win)
{

	snake.xPartsHead[0] -= 1;

	return 0;
}

int snakeMoveRight(WINDOW *win)
{

	snake.xPartsHead[0] += 1;

	return 0;
}

int snakeInit(WINDOW *win)
{
	int i;

	for(i=0; i<snake.lenParts[0];i++)
	{
		mvwprintw(win,snake.yPartsHead[0],snake.xPartsHead[0],"*");
	}

	snake.moveDown = snakeMoveDown;
	snake.moveUp   = snakeMoveUp;
	snake.moveLeft = snakeMoveLeft;
	snake.moveRight= snakeMoveRight;

	snake.direction = 6;

	return 0;
}

int snakeMove(WINDOW *win)
{
	snake.direction = charDir;

	printf("snake.direction:%d\n", snake.direction);

	switch(snake.direction)
	{
		case 2:
			snake.moveDown(win);
			break;
		case 4:
			snake.moveLeft(win);
			break;
		case 6:
			snake.moveRight(win);
			break;
		case 8:
			snake.moveUp(win);
			break;
		default:
			printf("Invalid Direct!\n");
			return -1;
	}
}


int frameCreate(WINDOW *win,int x_site, int y_site)
{
	int i, j;

	for(j=0;j<GAME_HIGTH;j++)
	{
		for(i=0;i<GAME_WIDTH;i++)
		{
			if(i == snake.xPartsHead[0] && j == snake.yPartsHead[0])
			{
				mvwprintw(win,j,i,"+");
			}
			else if(i == x_site && j == y_site)
			{
				mvwprintw(win,j,i,"*");
			}
			else if(j == 24 || j ==0)
			{
				mvwprintw(win,j,i,"-");
			}
			else if(1 == i)
			{
				mvwprintw(win,j,i,"|");
			}
			else if(i== (GAME_WIDTH-1))
			{
				mvwprintw(win,j,i,"|");
			}
			else 
			{
				mvwprintw(win,j,i," ");
			}
		}
	}

	return 0;
}


static int need_new_target = 1;
static int x_site, y_site;

int snakeCreate(WINDOW *win)
{
	int i, j;

	if(need_new_target == 1)
	{
		targetGenerate(&x_site, &y_site);
		need_new_target = 0;
	}

	snakeInit(win);

	snakeMove(win);

	frameCreate(win, x_site, y_site);

	return 0;
}

/*
resource ncurses_newwin ( int rows, int cols, int y, int x);

ncurses_newwin() creates a new window to draw elements in. Windows can be positioned using x, y, rows and cols. When creating additional windows, remember to use ncurses_getmaxyx() to check for available space, as terminal size is individual and may vary. The return value is a resource ID used to differ between multiple windows.
*/
void* get_new_direction (void* unused)
{
	while (1)
	{
		charDir = getchar();
		printf("charDir:%c", charDir);
		switch(charDir)
		{
			case '2':
				charDir = 2;
				break;
			case '4':
				charDir = 4;
				break;
			case '6':
				charDir = 6;
				break;
			case '8':
				charDir = 8;
				break;
			default:
				break;
		}
		
	//	sleep(1);
	}

	return NULL;
}

int main(int argc, char *argv[])
{
	int x,y;
   	time_t t;
	pthread_t thread_id;

	pthread_create(&thread_id, NULL, &get_new_direction, NULL);
	
   	WINDOW *win;

   	initscr();          
   	curs_set(0);        
   	noecho();           
	win=newwin(25,50,0,0);
	getmaxyx(win,y,x);   
 	printf("a:%d, b:%d\n",x,y);
   	refresh();          
   	wrefresh(win);      
	while(1)
	{
		snakeCreate(win);
		wrefresh(win);  
		sleep(1);
	}
	
	endwin();
	
	return 0;
}


架构练习:c语言实现贪吃蛇(四):接收按键改变蛇的前进方向