示例#1
0
  public GameScreen() {
    addKeyListener(new TAdapter());
    addMouseListener(new CustomListener());
    addMouseMotionListener(new CustomListener());
    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);
    player = new Sprite();
    player.setX(500);
    player.setY(250);
    player.set_speed(player_speed);
    bullet = new Vector<Bullet>();
    enemy = new Vector<Enemy>();
    v_explosion = new Vector<Explosion>();
    ImageIcon ii =
        new ImageIcon(this.getClass().getResource("Data/Sprite/background/background1.png"));
    fon = ii.getImage();
    timer = new Timer(5, this);
    timer.start();
    timerj = new java.util.Timer();
    timerj.schedule(task, 100);
    ImageIcon ii2 = new ImageIcon(this.getClass().getResource("Data/Sprite/player/explosion.png"));
    explosion = ii2.getImage();

    ImageIcon i3 = new ImageIcon(this.getClass().getResource("Data/Sprite/player/live.png"));
    live = i3.getImage();
    i3 = new ImageIcon(this.getClass().getResource("Data/Sprite/player/str.png"));
    pointer = i3.getImage();
    image_bullet = new Bullet();
    start_game();
  }
示例#2
0
  public void compute() {
    for (int i = 0; i < bullet.size(); i++) {
      bullet.get(i).bullet_x = bullet.get(i).bullet_x + 3 * bullet.get(i).current_direction_x;
      bullet.get(i).bullet_y = bullet.get(i).bullet_y + 3 * bullet.get(i).current_direction_y;
    }
    for (int i = 0; i < bullet.size(); i++) {
      if (bullet.get(i).bullet_x > 1366
          || bullet.get(i).bullet_x < 0
          || bullet.get(i).bullet_y > 768
          || bullet.get(i).bullet_y < 0) bullet.remove(i);
    }
    // Check collision bullet with enemy
    for (int i = 0; i < bullet.size(); i++) {
      for (int j = 0; j < enemy.size(); j++) {
        if (collision.collision_bullet_enemy(
                18,
                (int) bullet.get(i).bullet_x + 18,
                (int) bullet.get(i).bullet_y + 18, // bullet
                (int) enemy.get(j).getX(),
                (int) enemy.get(j).getY(), // point x1, y1
                (int) enemy.get(j).getX() + (int) enemy.get(j).get_spr_w(),
                (int) enemy.get(j).getY(), // point x2, y2
                (int) enemy.get(j).getX() + (int) enemy.get(j).get_spr_w(),
                (int) enemy.get(j).getY() + (int) enemy.get(j).get_spr_h(),
                (int) enemy.get(j).getX(),
                (int) enemy.get(j).getY() + (int) enemy.get(j).get_spr_h() // point x4, y4
                )
            == true) {
          //	create_explosion(bullet.get(i).bullet_x, bullet.get(i).bullet_y);
          if (bullet.get(i).get_type() == 's') // simple bullet
          {
            enemy.get(j).dec_live();
          }
          if (bullet.get(i).get_type() == 'f') // fireball
          {
            enemy.get(j).dec_live();
            enemy.get(j).dec_live();
          }
          if (bullet.get(i).get_type() == 'r') // rocket
          {
            enemy.get(j).dec_live();
            enemy.get(j).dec_live();
            enemy.get(j).dec_live();
          }
          if (enemy.get(j).get_live() <= 0) {
            enemy.remove(j);
          } else // push enemy
          {
            enemy.get(j).push();
          }
          bullet.remove(i);
          break;
        }
      }
    }
    // Преследование игрока
    for (int i = 0; i < enemy.size(); i++) {
      if (enemy.get(i).get_type() == "enemy1"
          || enemy.get(i).get_type() == "enemy2"
          || enemy.get(i).get_type() == "enemy3") {
        angle2 =
            Math.atan2(player.getY() - enemy.get(i).getY(), player.getX() - enemy.get(i).getX())
                * 180
                / 3.14;
        double a = player.getX() - enemy.get(i).getX();
        double b = player.getY() - enemy.get(i).getY();
        double kvadrat = Math.sqrt((a * a + b * b));
        double tempx = a / kvadrat;
        double tempy = Math.sqrt(1 - tempx * tempx);
        if (b < 0) tempy = -tempy;
        enemy.get(i).v_x = enemy.get(i).speed * tempx;
        enemy.get(i).v_y = enemy.get(i).speed * tempy;
        enemy.get(i).setX((enemy.get(i).getX() + enemy.get(i).speed * tempx));
        enemy.get(i).setY((enemy.get(i).getY() + enemy.get(i).speed * tempy));
      } else {
        if (enemy.get(i).created == false) {
          angle2 =
              Math.atan2(player.getY() - enemy.get(i).getY(), player.getX() - enemy.get(i).getX())
                  * 180
                  / 3.14;
          double a = player.getX() - enemy.get(i).getX();
          double b = player.getY() - enemy.get(i).getY();
          double kvadrat = Math.sqrt((a * a + b * b));
          double tempx = a / kvadrat;
          double tempy = Math.sqrt(1 - tempx * tempx);
          if (b < 0) tempy = -tempy;
          enemy.get(i).v_x = enemy.get(i).speed * tempx;
          enemy.get(i).v_y = enemy.get(i).speed * tempy;
          enemy.get(i).created = true;
        }
        enemy.get(i).setX((enemy.get(i).getX() + enemy.get(i).get_vx()));
        enemy.get(i).setY((enemy.get(i).getY() + enemy.get(i).get_vy()));
      }
    }
    for (int j = 0; j < enemy.size(); j++) {
      if (collision.collision_with_box(
              (int) player.getX() + 14,
              (int) player.getY() + 28,
              (int) player.getX() + 114,
              (int) player.getY() + 95,
              (int) enemy.get(j).getX() + 22,
              (int) enemy.get(j).getY() + 30,
              (int) enemy.get(j).getX() + 102,
              (int) enemy.get(j).getY() + 96)
          == true) {
        enemy.remove(j);
        player_live--;
        if (player_live == 0) {
          JOptionPane.showMessageDialog(null, "Game over!");
          start_game();
        }
      }
    }
    if (time_game > frequency) {
      generate_enemy();
      time_game = 0;
    }
    if (left_mouse_pressed == true && allow_shoot == true) {}

    time_game += 0.01;
    frequency -= 0.0001;
  }