Example #1
0
void	play(int i, Window& win)
{
	nodelay(stdscr, TRUE);
	i = get_difficult(i);
	Player		p(win);
	EnnemyHorde n = EnnemyHorde(i, "^v^");
	Missile miss = Missile(10, "|");
	Rocket rock = Rocket(2, "[]");
	n.randomPOP(win);
	int	input = 0;
	p.setScoreZero();
	while (input != KEY_ECHAP && p.getHP() > 0)
	{
		clear();
		miss.print_score(win);
		win.takeSize();
		win.printBorder();
		input = getch();
		miss.activate(p.getX(), p.getY(), input);
		rock.activate(p.getX(), p.getY(), input);
		miss.checkDamages(n.getHorde(), n.getNumber(), win);
		rock.checkDamages(n.getHorde(), n.getNumber(), win);
		p.checkEnemies(n.getHorde(), n.getNumber(), win);
		p.print_life(win);
		miss.print_chargeur(win);
		rock.print_chargeur(win);
		if (p.getHP() > 0)
		{
			n.printit(win);
			miss.move(win);
			rock.move(win);
			miss.printit(win);
			rock.printit(win);
			p.printit(win);
			p.inputDirection(input);
			wrefresh(win.getWin());
			p.move(win);
			p.printit(win);
			n.move(win);
		}
		while(getch() != ERR);
		usleep(60000);
	}
	if (p.getHP() <= 0)
	{
		clear();
		print_die(win, p);
	}
	 if (p.getScore() > hight_score)
		 hight_score = p.getScore();

}
Example #2
0
void Game() {

	bgm.Play();
	sf::Clock clock;
	sf::Event event;
	while (window.isOpen()) {
		//系统关键位置初始化
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed){
				window.close();
			}
		}
		background.Show();
		sf::Time time1 = clock.getElapsedTime();
		//显示分数
		text.ShowScore();
		//显示敌机以及子弹
		for (int i = 0; i < 80; i++) {
			if (time1 >= sf::seconds(1.5*i)) {
				enemy[i].Show();
				window.draw(enemy_bullet[i].enemy_bullet);
				enemy_bullet[i].enemy_bullet.setPosition(sf::Vector2f(enemy[i].enemy.getPosition().x + 30, enemy[i].enemy.getPosition().y + 60));
				enemy_bullet[i].Show();
			}
		}
		//显示飞机、控制移动
		rocket.move();	
		//开炮
		rocket.Gun();
		bullet.Show();
		if (bullet.bullet.getPosition().y <= 10) { 
			bullet.Gone(); 
		}
		//碰撞处理
		sf::FloatRect collision_enemy[80];
		sf::FloatRect collision_enemy_bullet[80];
		sf::FloatRect boundingBox2 = rocket.rocket.getGlobalBounds();
		for (int i = 0; i < 80; i++) {
			collision_enemy[i] = enemy[i].enemy.getGlobalBounds();
			collision_enemy_bullet[i] = enemy_bullet[i].enemy_bullet.getGlobalBounds();
			sf::FloatRect boundingBox1 = bullet.bullet.getGlobalBounds();
				if ((boundingBox1.intersects(collision_enemy[i])) && (enemy[i].enemy.getPosition().y >= 0.0f)) {
					bullet.bullet.setPosition(sf::Vector2f(1000, 0));
					bgs1.Play();
					score += 100;
				}
			if ((boundingBox2.intersects(collision_enemy_bullet[i]))) {
				enemy_bullet[i].enemy_bullet.setPosition(sf::Vector2f(-400, 0));
				bgs2.Play();
				life--;
			}
			if ((boundingBox2.intersects(collision_enemy[i]))) {
				enemy[i].enemy.setPosition(sf::Vector2f(-400, 0));
				bgs2.Play();
				life--;
			}
		}
		window.display();
		//分数迭代
		if (score >= 100 * level) {level++;}
		if (life <= 0) {window.close();}
		if (score >= 7500) {window.close();}	
	}
}