コード例 #1
0
int main()
{
    srand(time(NULL));
    Stat stat;
    char name[50];
    Snake snake;
    Pos food;
    ToGiveTread toGiveThread = {RIGHT, false, false};
    showLeaderboard();
    initStat(&stat);
    strcpy(name, stat.nickname);
    HANDLE hReadThread = CreateThread(NULL, 0, ReadThread, &toGiveThread, 0, NULL);
    strcpy(stat.nickname, name);
    initField();
    initSnake(&snake);
    initFood(&snake, &food, &stat);
    updateAds(&stat);
    do{
        Sleep(sleepTime(stat.level));
    }while(!updateSnake(&snake, &food, &toGiveThread, &stat));
    toGiveThread.toStopThread = true;
    clearScr();
    updateScore(&stat);
    CloseHandle(hReadThread);
    showLeaderboard();
    return 0;
}
コード例 #2
0
void LittleSnake::update(float dt)
{
    if (gameState != PLAYING) 
    {
        return;
    }

	processSwipe(dt);

    rotateSnakeHead(snakeNewDirection);

	updateSnake(dt);
}
コード例 #3
0
ファイル: main.c プロジェクト: mdkarch/Snake_Plus
// track snake movement
int movement(alt_u8 key, struct Snake snake[], int dir_array [],
		int pressed, int player, struct SnakeInfo * info){

	int old_dir = -1;
	if( dir_array[left_dir] )
		old_dir = left_dir;
	else if( dir_array[right_dir] )
		old_dir = right_dir;
	else if( dir_array[up_dir] )
		old_dir = up_dir;
	else if( dir_array[down_dir] )
		old_dir = down_dir;

	int xCoor = snake[0].xCoord;
	int yCoor = snake[0].yCoord;

	int pressed_dir = get_dir_from_pressed(pressed);

	//printf("Pressed: %d\n", pressed);
	switch(pressed_dir){
	case 1://0x1C://'a'
		if( dir_array[up_dir] || dir_array[down_dir] )
			moveLeft(dir_array);
		break;
	case 2://0x1D://'w'
		if( dir_array[left_dir] || dir_array[right_dir] )
			moveUp(dir_array);
		break;
	case 0://0x23://'d'
		if( dir_array[up_dir] || dir_array[down_dir] )
			moveRight(dir_array);
		break;
	case 3://0x1B://'s'
		if( dir_array[left_dir] || dir_array[right_dir])
			moveDown(dir_array);
		break;
	default:
		break;
	}
	if(dir_array[right_dir]){
		xCoor+= 1;//16;
		if(xCoor > RIGHT_BOUND - 2/**col_offset*/){
			//printf("Collision with right boundary!\n");
			return 1;
		}
		updateSnake(snake, xCoor, yCoor, right_dir, old_dir, player, info);
		dir_arg = right_dir;
		//checkFood(snake, right_dir, player, info);
	}else if(dir_array[left_dir]){
		xCoor-=1;//16;
		if(xCoor < (LEFT_BOUND+1)){//16
			//printf("Collision with left boundary!\n");
			return 1;
			//collision
		}
		updateSnake(snake, xCoor, yCoor, left_dir, old_dir, player, info);
		dir_arg = left_dir;
		//checkFood(snake, left_dir, player, info);
	}else if(dir_array[up_dir]){
		yCoor-=1;//16;
		if(yCoor < (TOP_BOUND + 1)){//16
			//printf("Collision with top boundary!\n");
			return 1;
			//collision
		}
		updateSnake(snake, xCoor, yCoor, up_dir, old_dir,player, info);
		dir_arg = up_dir;
		//checkFood(snake, up_dir, player, info);
	}else if(dir_array[down_dir]){
		yCoor+=1;//16;
		if(yCoor > BOT_BOUND - 2/**col_offset*/){
			//printf("Collision with bottom boundary!\n");
			return 1;
			//collision
		}
		updateSnake(snake, xCoor, yCoor, down_dir, old_dir,player, info);
		dir_arg = down_dir;
		//checkFood(snake, down_dir, player, info);
	}
	/*checkFood(snake, down_dir, player, info);
	checkSpeed(snake, player, info);
	checkFreeze(snake, player, info);
	recalc_freeze_times(snake, player,info);
	checkEdwards(snake, player, info);*/
	//PLAY_SOUND(1);
	int check_brick_col = brickCol(snake, player);
	int check_self_col = traverseList(snake, player);
	if(check_brick_col || check_self_col){
		return 1;
	}else{
		return 0;
	}

	//return traverseList(snake);
	//printf("x: %d y: %d\n", xCoor, yCoor);
}
コード例 #4
0
ファイル: snake.c プロジェクト: haha370104/OSCourse
void HariMain(void)
{


	char *buf;
	int win, i, x, y, timer;
	api_initmalloc();
	buf = api_malloc(200 * 200);
	char* title;
	char keyflag[4];
	char *last_direction;
	*last_direction = '4';


	//snake
	struct snakeNode *head = api_malloc(sizeof(struct snakeNode));
	head->preNode = NULL;
	head->posX = 75;
	head->posY = 55;
	head->moveDirection = 4;

	struct snakeNode *tail = head;
	//snake

	//food
	struct Food food;
	food.posX = 69;
	food.posY = 61;
	food.isEaten = 1;// not eaten;
	//food



	sprintf(title, "posX: %d, posY: %d, mp: %d", tail->posX, tail->posY, tail->moveDirection);
	//sprintf(title, "Hungry Snake");
	win = api_openwin(buf, 200, 200, -1, title);
	timer = api_alloctimer();

	api_inittimer(timer, 128);

	api_boxfilwin(win, 5, 25, 195, 195, 0);
	x = 75;
	y = 55;
	tail->posX = 75;
	tail->posY = 55;
	api_putstrwin(win, tail->posX, tail->posY, 3, 1, "*");
	//scrnRefresher(win, tail, 3, "*");





	for (;;) {

		i = *last_direction;
		scrnRefresher(win, tail, 0, "*");
		// updateSnake(head);
		updateSnake(tail);
		scrnRefresher(win, tail, 3, "*");

		//add food here
		foodInit(&food, win, 4, "+");
		//add food here


		/*detecting*/
		detecting(head, &tail, &food);



		delay();
		//if (snakeLength < 10) {tail = addSegment(tail, &snakeLength);}

		wait(1, timer, keyflag, last_direction, head);
	}




	api_closewin(win);
	api_end();
}