Exemple #1
0
void Snake::Move(Food& food, int dif)
{
	TestBorderCollision();
	if (gameover)
	{
		return;
	}

	for (int i = 0; i < s_parts.size(); i++)
	{
		standard_field.EraseAtMap(s_parts[i].GetX(), s_parts[i].GetY());
		switch (s_parts[i].GetDirection())
		{
		case UP:
			s_parts[i].SetY(s_parts[i].GetY() - 1);
			break;
		case RIGHT:
			s_parts[i].SetX(s_parts[i].GetX() + 1);
			break;
		case DOWN:
			s_parts[i].SetY(s_parts[i].GetY() + 1);
			break;
		case LEFT:
			s_parts[i].SetX(s_parts[i].GetX() - 1);
			break;
		}

		standard_field.AddToMap(s_parts[i].GetX(), s_parts[i].GetY(), SNAKE_PART);
	}

	if (s_parts[0].GetX() == food.GetX() && s_parts[0].GetY() == food.GetY())
	{
		EatFood(food, dif);
		food.Generate();										    
	}

	for (int i = s_parts.size() - 1; i > 0; i--)
	{
		if (s_parts[i].GetDirection() != s_parts[i - 1].GetDirection())
		{
			s_parts[i].SetDirection(s_parts[i - 1].GetDirection());
		}
	}

	TestSelfCollision();
}
Exemple #2
0
void Snake::EatFood(Food& food, int dif)
{
	switch (s_parts.back().GetDirection())
	{
	case LEFT:
		s_parts.push_back(Part(s_parts.back().GetX() + 1, s_parts.back().GetY(), LEFT));
		break;
	case RIGHT:
		s_parts.push_back(Part(s_parts.back().GetX() - 1, s_parts.back().GetY(), RIGHT));
		break;
	case UP:
		s_parts.push_back(Part(s_parts.back().GetX(), s_parts.back().GetY() + 1, UP));
		break;
	case DOWN:
		s_parts.push_back(Part(s_parts.back().GetX(), s_parts.back().GetY() - 1, DOWN));
		break;
	}

	switch (dif)
	{
	case 0:
		score += 150;
		break;
	case 1:
		score += 10;
		break;
	case 2:
		score += 20;
		break;
	case 3:
		score += 30;
		break;
	case 4:
		score += 70;
		break;
	}

	standard_field.AddToMap(food.GetX(), food.GetY(), SNAKE_PART);
}