Android Open Source - Hungry-Mouse Projectile






From Project

Back to project page Hungry-Mouse.

License

The source code is released under:

MIT License

If you think the Android project Hungry-Mouse listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//Name:   Projectile.java
//Purpose:  the functions of the bullets
//  w  w  w.j  a  va 2  s .co  m
package com.hungry.mouse.main;

//android libraries stored in SDK platform
import android.graphics.Rect;//hold 4 integer coordinates for rectangle

public class Projectile {

  //local variables
  private int x, y, speedX;//coordinates and speed of bullet
  private boolean visible;//until bullet hit or move out of screen is visible
  
  private Rect r;
  
  //constructor
  public Projectile(int startX, int startY){
    x = startX;
    y = startY;
    speedX = 7;
    visible = true;
    
    r = new Rect(0, 0, 0, 0);
  }
  
  //change position of bullet and check for collision
  public void update(){
    x += speedX;
    r.set(x, y, x+10, y+5);
    
    //check bullet if there is inside of the screen
    if (x > 800){
      visible = false;
      r = null;
    }
    //so if there is inside of the screen, we can check for collision
    if (visible){
      checkCollision();
      
    }
    
  }

  //check for collision of enemies and the bullets
  private void checkCollision() {
    
    //check if the bullet hit and enemy and subtract from health
    
    //enemy 1
    if (Rect.intersects(r, GameScreen.hb.r)){
      visible = false;

      if (GameScreen.hb.getHealth() > 0) {
        GameScreen.hb.subtractHealth(1);
      }
      if (GameScreen.hb.getHealth() == 0) {
        GameScreen.hb.setCenterX(-100);
        GameScreen.hb.checkKillsByBullets();
      }

    }
    
    //enemy2
    if (Rect.intersects(r, GameScreen.hb2.r)){
      visible = false;

      if (GameScreen.hb2.getHealth() > 0) {
        GameScreen.hb2.subtractHealth(1);
      }
      if (GameScreen.hb2.getHealth() == 0) {
        GameScreen.hb2.setCenterX(-100);
        GameScreen.hb2.checkKillsByBullets();

      }
    }
    
    //enemy3
    if (Rect.intersects(r, GameScreen.hb3.r)){
      visible = false;

      if (GameScreen.hb3.getHealth() > 0) {
        GameScreen.hb3.subtractHealth(1);
      }
      if (GameScreen.hb3.getHealth() == 0) {
        GameScreen.hb3.setCenterX(-100);
        GameScreen.hb3.checkKillsByBullets();

      }
    }
    //enemy4
    if (Rect.intersects(r, GameScreen.hb4.r)){
      visible = false;

      if (GameScreen.hb4.getHealth() > 0) {
        GameScreen.hb4.subtractHealth(1);
      }
      if (GameScreen.hb4.getHealth() == 0) {
        GameScreen.hb4.setCenterX(-100);
        GameScreen.hb4.checkKillsByBullets();

      }
    }
    //enemy5
    if (Rect.intersects(r, GameScreen.hb5.r)){
      visible = false;

      if (GameScreen.hb5.getHealth() > 0) {
        GameScreen.hb5.subtractHealth(1);
      }
      if (GameScreen.hb5.getHealth() == 0) {
        GameScreen.hb5.setCenterX(-100);
        GameScreen.hb5.checkKillsByBullets();

      }
    }
    //enemy6
    if (Rect.intersects(r, GameScreen.hb6.r)){
      visible = false;

      if (GameScreen.hb6.getHealth() > 0) {
        GameScreen.hb6.subtractHealth(1);
      }
      if (GameScreen.hb6.getHealth() == 0) {
        GameScreen.hb6.setCenterX(-100);
        GameScreen.hb6.checkKillsByBullets();

      }
    }
    //enemy7
    if (Rect.intersects(r, GameScreen.hb7.r)){
      visible = false;

      if (GameScreen.hb7.getHealth() > 0) {
        GameScreen.hb7.subtractHealth(1);
      }
      if (GameScreen.hb7.getHealth() == 0) {
        GameScreen.hb7.setCenterX(-100);
        GameScreen.hb7.checkKillsByBullets();

      }
    }
    //enemy8
    if (Rect.intersects(r, GameScreen.hb8.r)){
      visible = false;

      if (GameScreen.hb8.getHealth() > 0) {
        GameScreen.hb8.subtractHealth(1);
      }
      if (GameScreen.hb8.getHealth() == 0) {
        GameScreen.hb8.setCenterX(-100);
        GameScreen.hb8.checkKillsByBullets();

      }
    }
    //enemy9
    if (Rect.intersects(r, GameScreen.hb9.r)){
      visible = false;

      if (GameScreen.hb9.getHealth() > 0) {
        GameScreen.hb9.subtractHealth(1);
      }
      if (GameScreen.hb9.getHealth() == 0) {
        GameScreen.hb9.setCenterX(-100);
        GameScreen.hb9.checkKillsByBullets();

      }
    }
  }
  
  public boolean isVisible() {
    return visible;
  }
  
  //getters//
  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  public int getSpeedX() {
    return speedX;
  }



  //setters//
  public void setX(int x) {
    this.x = x;
  }

  public void setY(int y) {
    this.y = y;
  }

  public void setSpeedX(int speedX) {
    this.speedX = speedX;
  }

  public void setVisible(boolean visible) {
    this.visible = visible;
  }
  
  
}




Java Source Code List

com.hungry.mouse.framework.Audio.java
com.hungry.mouse.framework.FileIO.java
com.hungry.mouse.framework.Game.java
com.hungry.mouse.framework.Graphics.java
com.hungry.mouse.framework.Image.java
com.hungry.mouse.framework.Input.java
com.hungry.mouse.framework.Music.java
com.hungry.mouse.framework.Pool.java
com.hungry.mouse.framework.Screen.java
com.hungry.mouse.framework.Sound.java
com.hungry.mouse.framework.implementation.AccelerometerHandler.java
com.hungry.mouse.framework.implementation.AndroidAudio.java
com.hungry.mouse.framework.implementation.AndroidFastRenderView.java
com.hungry.mouse.framework.implementation.AndroidFileIO.java
com.hungry.mouse.framework.implementation.AndroidGame.java
com.hungry.mouse.framework.implementation.AndroidGraphics.java
com.hungry.mouse.framework.implementation.AndroidImage.java
com.hungry.mouse.framework.implementation.AndroidInput.java
com.hungry.mouse.framework.implementation.AndroidMusic.java
com.hungry.mouse.framework.implementation.AndroidSound.java
com.hungry.mouse.framework.implementation.MultiTouchHandler.java
com.hungry.mouse.framework.implementation.SingleTouchHandler.java
com.hungry.mouse.framework.implementation.TouchHandler.java
com.hungry.mouse.main.AboutScreen.java
com.hungry.mouse.main.Animation.java
com.hungry.mouse.main.Assets.java
com.hungry.mouse.main.Background.java
com.hungry.mouse.main.Bomb.java
com.hungry.mouse.main.Cheese.java
com.hungry.mouse.main.Enemy.java
com.hungry.mouse.main.GameScreen.java
com.hungry.mouse.main.HelpScreen1.java
com.hungry.mouse.main.HelpScreen2.java
com.hungry.mouse.main.HelpScreen3.java
com.hungry.mouse.main.HelpScreen4.java
com.hungry.mouse.main.Kamikazi.java
com.hungry.mouse.main.LevelSelectorScreen.java
com.hungry.mouse.main.LoadingScreen.java
com.hungry.mouse.main.MainMenuScreen.java
com.hungry.mouse.main.Mouse.java
com.hungry.mouse.main.Projectile.java
com.hungry.mouse.main.Rewards.java
com.hungry.mouse.main.SampleGame.java
com.hungry.mouse.main.Settings.java
com.hungry.mouse.main.Sign.java
com.hungry.mouse.main.SplashLoadingScreen.java
com.hungry.mouse.main.Tile.java