Ejemplo n.º 1
0
void Snake::createSnake(const int length)
{
	//Set Head Int
	head = 0;

	//Create Body Tiles
	for(int i=0; i <= length; ++i)
	{
		sf::RectangleShape rectangle = createSeg();
		if(i==0)
		{
			rectangle.setPosition(400,300);
		}
		else
		{
			rectangle.setPosition(400,snakeBody[i-1].getPosition().y+20);
		}
		snakeBody.push_back(rectangle);
	}
}
Ejemplo n.º 2
0
/*
 * map_seg: Creates a new segement that is unused in memory of designated size.
 */
void map_seg(Seq_T segMem, Seq_T delMem, uint32_t *seg_num, uint32_t *size)
{
       *seg_num = createSeg(segMem, delMem, *size);
}
Ejemplo n.º 3
0
void Snake::shiftSeg()
{

	//Check if food eaten and if so leave last segment else take segment off
	if(foodEaten == true)
	{
		foodEaten = false;
	}
	else
	{
		snakeBody.pop_back();
	}

	//Set direction
	sf::RectangleShape rectangle = createSeg();

	if(direction == 1) //UP
	{
		if(facing == "UP" || facing == "LEFT" || facing == "RIGHT" ) // Go new direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x,snakeBody[head].getPosition().y-20);
			snakeBody.push_front(rectangle);
			facing = "UP";
		}
		else //Keep going opposite direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x,snakeBody[head].getPosition().y+20);
			snakeBody.push_front(rectangle);
			facing = "DOWN";
		}
	}
	else if(direction == 2) //DOWN
	{
		if(facing == "DOWN" ||facing == "LEFT" || facing == "RIGHT") // Go new direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x,snakeBody[head].getPosition().y+20);
			snakeBody.push_front(rectangle);
			facing = "DOWN";
		}
		else //Keep going opposite direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x,snakeBody[head].getPosition().y-20);
			snakeBody.push_front(rectangle);
			facing = "UP";
		}
	}
	else if(direction == 3) //LEFT
	{
		if(facing == "UP" || facing == "DOWN" || facing == "LEFT") // Go new direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x-20,snakeBody[head].getPosition().y);
			snakeBody.push_front(rectangle);
			facing = "LEFT";
		}
		else //Keep going opposite direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x+20,snakeBody[head].getPosition().y);
			snakeBody.push_front(rectangle);
			facing = "RIGHT";
		}
	}
	else if(direction == 4) //RIGHT
	{
		if(facing == "UP" || facing == "DOWN"|| facing == "RIGHT") // Go new direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x+20,snakeBody[head].getPosition().y);
			snakeBody.push_front(rectangle);
			facing = "RIGHT";
		}
		else //Keep going opposite direction
		{
			rectangle.setPosition(snakeBody[head].getPosition().x-20,snakeBody[head].getPosition().y);
			snakeBody.push_front(rectangle);
			facing = "LEFT";
		}
	}
	else // FIRST START GAME (START UP)
	{
		head = 0;
		rectangle.setPosition(snakeBody[head].getPosition().x, snakeBody[head].getPosition().y-20);
		snakeBody.push_front(rectangle);
		facing = "UP";
	}
}