void update(void){
			f.update();
			razer_convert_keycode_to_pos(keys_history[0],&pos);
			if (last_pos.x!=pos.x || last_pos.y!=pos.y){
				last_pos.x=pos.x; last_pos.y=pos.y;
				//cout <<"key pos = "<<(pos).x<<","<<(pos).y<<endl;
			}
			direction_t d=LEFT;
			if(pos.x == 16 && pos.y == 4)
				d=UP;
			else if(pos.x == 16 && pos.y == 5)
				d=DOWN;
			else if(pos.x == 15 && pos.y == 5)
				d=LEFT;
			else if(pos.x == 17 && pos.y == 5)
				d=RIGHT;
			s.move(d);
			g.update();
			s.update();
			razer_update_keys(chroma,chroma->keys);
			usleep(400000);
			Cord sc = s.getFront();
			Cord fc = g.getPosition();
			if(sc.x==fc.x && sc.y==fc.y){
				Food tmp = Food();
				s.grow();
				s.grow();
				g = tmp;
			}
			razer_update(chroma);
			razer_frame_limiter(chroma,13);
		}
Пример #2
0
void InGameLayer::hitFoodTest()
{
	Food* getFood = nullptr;
	Point foodWorldPoint(0,0);
	float dist = 0;
	float dx = 0;
	float dy = 0;
	for (int i = foodList.size() - 1; i >= 0 ; i--)
	{
		getFood = foodList.at(i);
		foodWorldPoint = getFood->getParent()->convertToWorldSpace(getFood->getPosition());
		dx = mPanda->getPositionX() - foodWorldPoint.x;
		dy = mPanda->getPositionY() - foodWorldPoint.y;
		dist = sqrt(dx * dx + dy * dy );
		if (dist <= 50 + 30)
		{
			if (getFood->getType() == FOOD_TYPE_JIU)
			{
				jiuPower += 150;
				score	+= 50;
				mPanda->playEffect(PANDA_EFFECT::PANDA_EFFECT_SPEED);
			}else if (getFood->getType()	== FOOD_TYPE_FOOD)
			{
				zhu++;
				score	+= 20;
				updateTextFiled(zhu,txtBambooCount);
			}else if (getFood->getType()	== FOOD_TYPE_ZHONG)
			{
				zhong++;
				score	+= 10;
				updateTextFiled(zhong,txtZhongCount);
			}else if (getFood->getType()	== FOOD_TYPE_BAO)
			{
				score	+= 60;
			}
			mFoodPool.takeIn(getFood);
			elementLayer->removeChild(getFood);
			foodList.eraseObject(getFood);
		}
	}
}
Пример #3
0
/// ------------------------------------------------------------------------------------------------
void World::update()
{
	if (tick_cnt++ <= Param::n_ticks)
	{
		Food* pfood;
		for (auto fi : fishes_)
		{
			fi->update(foods_);

			pfood = foods_.at(fi->getClosestFood());

			if (Vec2DLength(fi->getPosition() - pfood->getPosition())
					< (pfood->getRadius() + fi->getRadius()))
			{
				fi->incrementFood(Param::n_food - foods_.size());

				foods_.erase(foods_.begin() + fi->getClosestFood());
//				foods_.push_back(new Food());

				if (foods_.empty())
				{
					cerr << "No more foods" << endl;
					foods_.push_back(new Food());
				}
			}

		}

		/// Highlight best fish
		RGBA rgba =
		{ 0, 0, 0, 255 };

		fish_best_->setRGBA(rgba);

		for (uint16_t i = 0; i < fishes_.size(); i++)
		{
			if (fishes_.at(i)->getFitness() > fish_best_->getFitness())
			{
				fish_best_ = fishes_.at(i);
			}
		}

		rgba =
		{	255,0,0,255};
		fish_best_->setRGBA(rgba);
	}
	else
	{
		/// Resupply food
		for (uint16_t i = foods_.size(); i < Param::n_food; i++)
		{
			foods_.push_back(new Food());
		}

		cout << "Next Generation: " << gen_algo_->getGenerationCount() << endl;

		tick_cnt = 0;

		/// Update chromosomes fitness
		vector<Chromosome> pop = gen_algo_->getPopulation();
		for (uint16_t i = 0; i < fishes_.size(); i++)
		{
			pop[i].setFitness(fishes_.at(i)->getFitness());
		}

		/// Run Genetic Algorithm
		pop = gen_algo_->NextGeneration(pop);

		/// Update Fishes' brain with new values
		for (uint16_t i = 0; i < fishes_.size(); i++)
		{
			fishes_[i]->putWeights(pop[i].getGenes());

			fishes_[i]->reset();
		}

		cout << "Average Fitness: " << gen_algo_->getAverageFitness() << endl;
		cout << "Best Fitness:    " << gen_algo_->getBestFitness() << endl << endl;

	}
}