码迷,mamicode.com
首页 > 移动开发 > 详细

架构练习:c语言实现贪吃蛇(三):封装蛇的移动方法

时间:2014-12-28 20:56:56      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:linux   架构   

目前进展:

封装蛇的移动方法:

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 SnakeSt snake = {1,{1,},{GAME_WIDTH/2,},{GAME_HIGTH/2,}, 6};

完成蛇的初始化:

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;
}

源码:

#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 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)
{
	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.
*/

int main(int argc, char *argv[])
{
	int x,y;
   	time_t t;
   	pthread_t thread;
   	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语言实现贪吃蛇(三):封装蛇的移动方法

标签:linux   架构   

原文地址:http://blog.csdn.net/xiangpingli/article/details/42216269

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!