Beispiel #1
0
int entity_move(ENTITY *e,int frame_time)
{
	if(e==0)
		return 0;
	switch(e->type){
	case PLAYER1:
		move_player1(e,frame_time);
		break;
	case BULLET1:
		move_bullet(e,frame_time);
		break;
	}
	return 0;
}
Beispiel #2
0
/** Constructor for Instructions class. Loads all images and initializes timers and variables necessary to animate images of player and enemies
 * @param parent is the parent widget
 */
Instructions::Instructions(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Instructions)
{
    ui->setupUi(this);

    // loads all instruction text
    instructions1 = QPixmap(":/image/IMAGES/instructions1.png");
    instructions2 = QPixmap(":/image/IMAGES/instructions2.png");
    instructions3 = QPixmap(":/image/IMAGES/instructions3.png");
    instructions3_5 = QPixmap(":/image/IMAGES/instructions3.5.png");
    instructions4 = QPixmap(":/image/IMAGES/instructions4.png");

    // loads images
    player = QPixmap(":/image/IMAGES/spaceship.png");
    enemy = QPixmap(":/image/IMAGES/invader.png");
    player_bullet = QPixmap(":/image/IMAGES/player_bullet.png");
    enemy_bullet = QPixmap(":/image/IMAGES/enemy_bullet.png");
    boss = QPixmap(":/image/IMAGES/boss.png");

    // sets up positions of images of player and enemies
    moving_right = true;
    boss_moving_right = true;
    player_position = std::make_pair(560, 0);
    starting_player_position = player_position;
    player_bullet_position = std::make_pair(568, 125);
    enemy_bullet_position = std::make_pair(566,220);
    boss_position = std::make_pair(500, 325);
    starting_boss_position = boss_position;

    // sets up timer for animating images
    move_timer = new QTimer(this);
    move_timer->setInterval(15);
    move_timer->start();

    QObject::connect(move_timer, SIGNAL(timeout()), this, SLOT(move_player()));
    QObject::connect(move_timer, SIGNAL(timeout()), this, SLOT(move_bullet()));
}
Beispiel #3
0
void timer(int num)
{
	//不能在定时器里刷新屏幕,频率太高会显示不出来
	if(game_state == GAME_RUNNING)
	{

		time_count++;		//用于控制运动的速度
		//背景移动
		count_1++;
		if(count_1 == 4)
		{
			count_1 = 0;
			background.src_rect[0].y--;
			if(background.src_rect[0].y == 0)
				background.src_rect[0].y = 560;
		}

	//飞机移动,通过按键控制固定的速度
		player.dst_rect.y += player.speed_y;
		player.dst_rect.x += player.speed_x;
		//检查飞机是否碰到边缘
		check_edge(&player);
		//子弹运动
		move_bullet(bullet);
	
		//敌人自动向下运动
		int i, j;
		if(time_count%2 == 0)
		{
			for(i = 0; i < MAX_SMALL_ENEMY; i++)
			{
		 		if(small_enemy[i].life > 0 && small_enemy[i].pic_index == 0)
				{
					small_enemy[i].dst_rect.y += 2;
				}
				//敌人死亡之后,显示爆炸效果的图片
				else if(small_enemy[i].life == 0 && small_enemy[i].pic_index != 0)
		  		{	
						if(time_count%4 == 0)
						{
							//显示被射击之后的图像效果
							small_enemy[i].pic_index++;
							//图像效果显示完之后,消失
							if(small_enemy[i].pic_index == small_enemy[i].pic_num)
							{
								small_enemy[i].pic_index = 0;
								//分数增加
								score_record += 100;
							}
						}
				}
				//到底端消失
				if(small_enemy[i].dst_rect.y > SCREEN_HEIGHT)
				{
					small_enemy[i].life = 0;
				}
			}
		}
		//---------------中型飞机---------------
		if(time_count%4 == 0)
		{
			for(i = 0; i < MAX_MID_ENEMY; i++)
			{
		 		if(mid_enemy[i].life > 0 && mid_enemy[i].pic_index == 0)
				{
					mid_enemy[i].dst_rect.y += 2;
				}
				else if(mid_enemy[i].life >= 0 && mid_enemy[i].pic_index != 0)
		  		{	
						if(time_count%8 == 0)
						{
							//显示被射击之后的图像效果
							if(mid_enemy[i].pic_index == 1 && mid_enemy[i].life > 0)
							{
								mid_enemy[i].pic_index = 0;
								mid_enemy[i].dst_rect.y += 2;
							}
							else if(mid_enemy[i].pic_index >= 1 && mid_enemy[i].life == 0)
								mid_enemy[i].pic_index++;
							//图像效果显示完之后,消失
							if(mid_enemy[i].pic_index == mid_enemy[i].pic_num)
							{
								mid_enemy[i].pic_index = 0;						
								score_record += 300;
							}
						}
				}
				//到底端消失
				if(mid_enemy[i].dst_rect.y > SCREEN_HEIGHT)
				{
					mid_enemy[i].life = 0;
				}
			}
		}
		//--------------------大飞机-----------------------
		if(time_count%4 == 0)
		{
			for(i = 0; i < MAX_BIG_ENEMY; i++)
			{
		 		if(big_enemy[i].life > 0 && big_enemy[i].pic_index == 0)
				{
					big_enemy[i].dst_rect.y += 2;
				}
				else if(big_enemy[i].life >= 0 && big_enemy[i].pic_index != 0)
		  		{	
						if(time_count%8 == 0)
						{
							//显示被射击之后的图像效果
							if(big_enemy[i].pic_index == 1 && big_enemy[i].life > 0)
							{
	
								big_enemy[i].pic_index = 0;
								big_enemy[i].dst_rect.y += 2;
							}
							//死亡后显示被射击的效果
							else if(big_enemy[i].pic_index >= 1 && big_enemy[i].life == 0)
								big_enemy[i].pic_index++;
							//图像效果显示完之后,消失
							if(big_enemy[i].pic_index == big_enemy[i].pic_num)
							{
								big_enemy[i].pic_index = 0;
								score_record += 600;
							}
						}
				}
				//到底端消失
				if(big_enemy[i].dst_rect.y > SCREEN_HEIGHT)
				{
					big_enemy[i].life = 0;
				}
			}
		}
		//将分数转化为字符串,并进行显示	
		show_score();
	
		//玩家的飞机
		if(time_count%2 == 0)
		{
			//死亡后显示爆炸效果
			if(player.life == 0 && player.pic_index != 0)
	  		{	
				if(time_count%4 == 0)
				{
					//显示被射击之后的图像效果
					player.pic_index++;
					//图像效果显示完之后,消失
				}
			}
			//到底端消失
			if(player.dst_rect.y > (SCREEN_HEIGHT-player.src_rect[0].h))
			{
				player.life = 0;
				player.dst_rect.y = 0;
			}
		}
		//飞机飞行动态	
		if(time_count == 10)
		{
			player.src_rect[0].x = 433;
			player.src_rect[0].y = 331;
		}
		else if(time_count > 50 && time_count < 100)
		{
			player.src_rect[0].x = 433;
			player.src_rect[0].y = 0;
		}
		else if(time_count == 100)
			time_count = 0;
	
	
		//自动射击
		if(time_count %30 == 0)
		{
		//	time_count = 0;
			if(auto_shot_flag)
			{
				shot_bullet(&player, bullet);
			}
		}
	}
}
static void update_game_on_ingame_scene(struct invaders_game *game,
                                        long elapsed_time, int *scene_change) {
  int i, j, k, n_living_invaders, invader_move_speed, block_hit_with, n_living_lines;
  bool stepable, is_annihilation;
  struct invader *shooting_invader, *line_head_invader,
    *line_head_invaders[N_INVADERS_LAYOUT_Y], *invader_hit_with;
  struct tochca *tochca_hit_with;
  struct vector2 block_position;

  if (GAME_EVENT_NONE == game->event) {
    /* Interpret the key inputs */
    switch (getch()) {
      case 'a':
      case KEY_LEFT:
        --game->player_jet.position.y;
        break;
      case 'd':
      case KEY_RIGHT:
        ++game->player_jet.position.y;
        break;
      case 'w':
      case KEY_UP:
        if (!game->player_bullet.active) {
          game->player_bullet.active = true;
          memcpy(&game->player_bullet.position,
                 &game->player_jet.position,
                 sizeof(game->player_bullet.position));
          ++game->player_bullet.position.y;
          clear_timer(&game->player_bullet.moving_timer);
        }
        break;
      default:
        break;
    }


    /* Decide the current aggression level */
    n_living_invaders = 0;
    n_living_lines = 0;
    memset(line_head_invaders, 0, sizeof(line_head_invaders));
    for (i = 0; i < N_INVADERS_LAYOUT_Y; ++i) {
      line_head_invader = NULL;
      for (j = N_INVADERS_LAYOUT_X - 1; j >= 0; --j) {
        struct invader *invader = &game->invader_team.members[i
            * N_INVADERS_LAYOUT_X + j];
        if (invader->alive) {
          ++n_living_invaders;
          if (NULL == line_head_invader) {
            line_head_invader = invader;
          }
        }
      }
      if (NULL != line_head_invader) {
        line_head_invaders[n_living_lines] = line_head_invader;
        ++n_living_lines;
      }
    }
    if (LEVEL7_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL7_MOVE_SPEED;
    } else if (LEVEL6_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL6_MOVE_SPEED;
    } else if (LEVEL5_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL5_MOVE_SPEED;
    } else if (LEVEL4_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL4_MOVE_SPEED;
    } else if (LEVEL3_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL3_MOVE_SPEED;
    } else if (LEVEL2_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL2_MOVE_SPEED;
    } else if (LEVEL1_THRESHOLD >= n_living_invaders) {
      invader_move_speed = LEVEL1_MOVE_SPEED;
    } else {
      invader_move_speed = LEVEL0_MOVE_SPEED;
    }

    /* The commander invader appear on schedule */
    if (!game->invader_team.commander.alive &&
        count_timer(&game->invader_team.commander_turn_timer, elapsed_time)) {
      game->invader_team.commander.alive = true;
      game->invader_team.commander.position.x = COMMANDER_INVADER_START_POSITION_X;
      game->invader_team.commander.position.y = COMMANDER_INVADER_START_POSITION_Y;
      clear_timer(&game->invader_team.commander.moving_timer);
    }

    /* move the invaders */
    stepable = false;
    for (i = 0; i < N_ELEMENTS(game->invader_team.members); ++i) {
      if (game->invader_team.members[i].alive) {
        if (0 > game->invader_team.members[i].moving_speed_y
            &&
            INVADER_MOVING_RANGE_Y_MIN
                >= game->invader_team.members[i].position.y) {
          stepable = true;
          break;
        } else if (0 < game->invader_team.members[i].moving_speed_y
            &&
            INVADER_MOVING_RANGE_Y_MAX
                <= game->invader_team.members[i].position.y) {
          stepable = true;
          break;
        }
      }
    }
    for (i = 0; i < N_ELEMENTS(game->invader_team.members); ++i) {
      if (game->invader_team.members[i].alive) {
        if (count_timer(&game->invader_team.members[i].moving_timer,
                        elapsed_time * invader_move_speed / 100)) {
          if (stepable) {
            game->invader_team.members[i].position.x += INVADER_INVASION_STEP_X;
            game->invader_team.members[i].moving_speed_y *= -1;
          } else {
            game->invader_team.members[i].position.y += game->invader_team
                .members[i].moving_speed_y;
          }
        }
      }
    }
    if (game->invader_team.commander.alive) {
      if (INVADER_MOVING_RANGE_Y_MAX <= game->invader_team.commander.position.y) {
        game->invader_team.commander.alive = false;
        clear_timer(&game->invader_team.commander_turn_timer);
      } else if (count_timer(&game->invader_team.commander.moving_timer, elapsed_time)) {
        ++game->invader_team.commander.position.y;
      }
    }

    /* Make the invader to shoot his bullet */
    if (count_timer(&game->invader_team.shooting_timer, elapsed_time)) {
      shooting_invader = line_head_invaders[rand() % n_living_lines];
      assert(NULL != shooting_invader);
      for (i = 0; i < N_ELEMENTS(game->invader_bullets); ++i) {
        if (!game->invader_bullets[i].active) {
          game->invader_bullets[i].active = true;
          memcpy(&game->invader_bullets[i].position, &shooting_invader->position,
                 sizeof(game->invader_bullets[i].position));
          game->invader_bullets[i].position.x += 2;
          ++game->invader_bullets[i].position.y;
          clear_timer(&game->invader_bullets[i].moving_timer);
          break;
        }
      }
    }

    /* move bullets */
    move_bullet(&game->player_bullet, elapsed_time);
    for (i = 0; i < N_ELEMENTS(game->invader_bullets); ++i) {
      move_bullet(&game->invader_bullets[i], elapsed_time);
    }

    /* Detect player bullet hit */
    if (game->player_bullet.active) {
      tochca_hit_with = detect_collieded_with_tochcas(&game->player_bullet.position,
                                                      game->tochcas,
                                                      N_ELEMENTS(game->tochcas),
                                                      &block_hit_with);
      if (NULL != tochca_hit_with) {
        game->player_bullet.active = false;
        tochca_hit_with->block_standings[block_hit_with] = false;
      } else {
        invader_hit_with = NULL;
        for (j = 0; j < N_ELEMENTS(game->invader_team.members); ++j) {
          if (detect_collieded_with_invader(&game->player_bullet.position,
                                            &game->invader_team.members[j])) {
            invader_hit_with = &game->invader_team.members[j];
            break;
          }
        }
        if (NULL == invader_hit_with) {
          if (game->invader_team.commander.alive &&
              detect_collided(&game->player_bullet.position, NULL,
                              &game->invader_team.commander.position,
                              &game->invader_team.commander.size)) {
            invader_hit_with = &game->invader_team.commander;
          }
        }
        if (NULL != invader_hit_with) {
          game->player_bullet.active = false;
          invader_hit_with->alive = false;
          game->score +=
              (COMMANDER_INVADER == invader_hit_with->type) ?
              COMMANDER_INVADER_SCORE :
              (SENIOR_INVADER == invader_hit_with->type) ?
              SENIOR_INVADER_SCORE :
              (YOUNG_INVADER == invader_hit_with->type) ?
                  YOUNG_INVADER_SCORE : LOOKIE_INVADER_SCORE;
        }
      }
    }

    /* Detect invader bullet hit */
    for (i = 0; i < N_ELEMENTS(game->invader_bullets); ++i) {
      struct bullet *bullet = &game->invader_bullets[i];
      if (bullet->active) {
        if (detect_collided(&bullet->position, NULL, &game->player_jet.position,
                            &game->player_jet.size)) {
          bullet->active = false;
          if (0 < game->credit) {
            game->credit -= 1;
          } else {
            invoke_event(game, GAME_OVER_EVENT);
          }
        } else if (game->player_bullet.active &&
                   game->player_bullet.position.x <= bullet->position.x &&
                   game->player_bullet.position.y == bullet->position.y) {
          game->player_bullet.active = false;
          bullet->active = false;
        } else {
          tochca_hit_with = detect_collieded_with_tochcas(&bullet->position,
                                                          (struct tochca *) game->tochcas,
                                                          N_ELEMENTS(game->tochcas),
                                                          &block_hit_with);
          if (NULL != tochca_hit_with) {
            bullet->active = false;
            tochca_hit_with->block_standings[block_hit_with] = false;
          }
        }
      }
    }

    /* Detect invaders hit with tochcas */
    for (i = 0; i < N_ELEMENTS(game->tochcas); ++i) {
      for (j = 0; j < N_ELEMENTS(game->tochcas[i].block_standings); ++j) {
        if (game->tochcas[i].block_standings[j]) {
          for (k = 0; k < N_ELEMENTS(game->invader_team.members); ++k) {
            get_tochca_block_position(&game->tochcas[i], j, &block_position);
            if (detect_collieded_with_invader(&block_position,
                                              &game->invader_team.members[k])) {
              game->tochcas[i].block_standings[j] = false;
              break;
            }
          }
        }
      }
    }

    /* Check the annihilation */
    is_annihilation = true;
    for (i = 0; i < N_ELEMENTS(game->invader_team.members); ++i) {
      if (game->invader_team.members[i].alive) {
        is_annihilation = false;
        break;
      }
    }
    if (is_annihilation) {
      invoke_event(game, GAME_CLEAR_EVENT);
    }

    /* Detect player jet hit with the invaders */
    if (GAME_EVENT_NONE == game->event) {
      for (i = 0; i < N_ELEMENTS(game->invader_team.members); ++i) {
        if (game->invader_team.members[i].alive &&
            detect_collided(&game->player_jet.position,
                            &game->player_jet.size,
                            &game->invader_team.members[i].position,
                            &game->invader_team.members[i].size)) {
          invoke_event(game, GAME_OVER_EVENT);
          break;
        }
      }
    }

    /* Check the invasion */
    if (GAME_EVENT_NONE == game->event) {
      for (i = 0; i < n_living_lines; ++i) {
        if (line_head_invaders[i]->alive &&
            INVADER_INVASION_THRESHOLD_POSITION_X
            <= line_head_invaders[i]->position.x + 1) {
          invoke_event(game, GAME_OVER_EVENT);
          break;
        }
      }
    }
  } else {
    /* Clean the input buffer */
    getch();
  }

  /* Update the caption timer */
  if (game->event_caption.displaying
      && count_timer(&game->event_caption.timer, elapsed_time)) {
    game->event_caption.displaying = false;
    *scene_change = TITLE_SCENE;
  }
}