コード例 #1
0
ファイル: game.c プロジェクト: idr77/SnakeGame
/* Fonction qui ajoute une position au serpent */
int addPositionSnake(Damier *map, Snake *snake, Position pos, Sound sound)
{
	if (snake->tete-1 >= MAXSNAKE)
	{
		perror("Plus de place !");
		return ERR_SIZE_SNAKE;
	}
	
	if (!testPosSnake(pos))
	{
		return ERR_POSITION;
	}
	
	if (!isProie(*map, pos))
	{
		snake->posSnake[snake->queue] = initCaseSnake();
		snake->posSnake[++snake->queue].etat = QUEUE;
	}
	
	else
	{
		snake->posSnake[snake->queue + 1].etat = CORPS;
		snake->posSnake[snake->queue].etat = QUEUE;
		playSound(sound, CROC_SOUND);
	}
		
	if (isSnake(*map, pos))
	{
		return END_GAME;
	}
	
	snake->posSnake[snake->tete].etat = CORPS;	
	snake->posSnake[++snake->tete].position = pos;
	snake->posSnake[snake->tete].etat = TETE;
	
	return 0;
}
コード例 #2
0
ファイル: Snake2Sequence.cpp プロジェクト: RobertCL/LedCube
void Snake2Sequence::next(){

	if (stuck > 0) {
		if (stuck < 10)
			Translate(0, 0, -1);

		stuck--;
		if (stuck == 0)
			finished = true;

		return;
	}

	// Work out if we need to turn because either
	// - we are about to go out of the cube
	// - we are about to eat ourselves
	// - we just randomly fancy turning
	short attempts = 0;
	while (true){
		int next_x = snake_x[0] + delta_x;
		int next_y = snake_y[0] + delta_y;
		int next_z = snake_z[0] + delta_z;
		if (next_x < 0 || next_x > 7 || next_y < 0 || next_y > 7 || next_z < 0 || next_z > 7
				|| isSnake(next_x, next_y, next_z)
				|| makeRandomTurn()){

			delta_x = 0;
			delta_y = 0;
			delta_z = 0;

			// Pick a new random direction
			int r = rand() % 6;
			switch(r){
				case 0:
					delta_x = -1;
					break;
				case 1:
					delta_x = 1;
					break;
				case 2:
					delta_y = -1;
					break;
				case 3:
				default:
					delta_y = 1;
					break;
				case 4:
					delta_z = -1;
					break;
				case 5:
					delta_z = 1;
					break;
			}
		}
		else {
			break;
		}

		attempts++;
		if (attempts == 90){
			stuck = 40;
			delay = 20;
			return;
		}
	}

	// Move snake
	shuffleSnake();

	// Set new head
	snake_x[0] = snake_x[1] + delta_x;
	snake_y[0] = snake_y[1] + delta_y;
	snake_z[0] = snake_z[1] + delta_z;

	// See if we've eaten anything
	short food = isFood(snake_x[0], snake_y[0], snake_z[0]);
	if (food >= 0){
		// Mmm, food
		// grow
		growBy += 3;
		while (snakeLen + growBy > MAX_SNAKE_LEN) { growBy--; }
		// speed up
		delay--;
		if (delay < 1) delay = 1;

		// Replace with more food
		food_x[food] = rand() % 8;
		food_y[food] = rand() % 8;
		food_z[food] = rand() % 8;
	}

	// Draw new snake
	drawSnakeInCube();
	drawFoodInCube();
}