Android Open Source - FlappyCow Obstacle






From Project

Back to project page FlappyCow.

License

The source code is released under:

MIT License

If you think the Android project FlappyCow 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

/**
 * An obstacle: spider + logHead//from w w  w .  j  a v  a2s.  c  o m
 * 
 * @author Lars Harmsen
 * Copyright (c) <2014> <Lars Harmsen - Quchen>
 */

package com.quchen.flappycow.sprites;

import com.quchen.flappycow.Game;
import com.quchen.flappycow.GameView;

import android.graphics.Canvas;

public class Obstacle extends Sprite{
  private Spider spider;
  private WoodLog log;
  
  /** Necessary so the onPass method is just called once */
  public boolean isAlreadyPassed = false;

  public Obstacle(GameView view, Game game) {
    super(view, game);
    spider = new Spider(view, game);
    log = new WoodLog(view, game);
    
    initPos();
  }
  
  /**
   * Creates a spider and a wooden log at the right of the screen.
   * With a certain gap between them.
   * The vertical position is in a certain area random.
   */
  private void initPos(){
    int height = game.getResources().getDisplayMetrics().heightPixels;
    int gab = height / 4 - view.getSpeedX();
    if(gab < height / 5){
      gab = height / 5;
    }
    int random = (int) (Math.random() * height * 2 / 5);
    int y1 = (height / 10) + random - spider.height;
    int y2 = (height / 10) + random + gab;
    
    spider.init(game.getResources().getDisplayMetrics().widthPixels, y1);
    log.init(game.getResources().getDisplayMetrics().widthPixels, y2);
  }

  /**
   * Draws spider and log.
   */
  @Override
  public void draw(Canvas canvas) {
    spider.draw(canvas);
    log.draw(canvas);
  }

  /**
   * Checks whether both, spider and log, are out of range.
   */
  @Override
  public boolean isOutOfRange() {
    return spider.isOutOfRange() && log.isOutOfRange();
  }

  /**
   * Checks whether the spider or the log is colliding with the sprite.
   */
  @Override
  public boolean isColliding(Sprite sprite) {
    return spider.isColliding(sprite) || log.isColliding(sprite);
  }

  /**
   * Moves both, spider and log.
   */
  @Override
  public void move() {
    spider.move();
    log.move();
  }

  /**
   * Sets the speed of the spider and the log.
   */
  @Override
  public void setSpeedX(float speedX) {
    spider.setSpeedX(speedX);
    log.setSpeedX(speedX);
  }
  
  /**
   * Checks whether the spider and the log are passed.
   */
  @Override
  public boolean isPassed(){
    return spider.isPassed() && log.isPassed();
  }
  
  /**
   * Will call obstaclePassed of the game, if this is the first pass of this obstacle.
   */
  public void onPass(){
    if(!isAlreadyPassed){
      isAlreadyPassed = true;
      view.getGame().increasePoints();
    }
  }

}




Java Source Code List

com.quchen.flappycow.About.java
com.quchen.flappycow.AccomplishmentBox.java
com.quchen.flappycow.GameOverDialog.java
com.quchen.flappycow.GameView.java
com.quchen.flappycow.Game.java
com.quchen.flappycow.MainActivity.java
com.quchen.flappycow.StartscreenView.java
com.quchen.flappycow.Util.java
com.quchen.flappycow.sprites.Background.java
com.quchen.flappycow.sprites.Coin.java
com.quchen.flappycow.sprites.Cow.java
com.quchen.flappycow.sprites.Frontground.java
com.quchen.flappycow.sprites.NyanCat.java
com.quchen.flappycow.sprites.Obstacle.java
com.quchen.flappycow.sprites.PauseButton.java
com.quchen.flappycow.sprites.PlayableCharacter.java
com.quchen.flappycow.sprites.PowerUp.java
com.quchen.flappycow.sprites.Rainbow.java
com.quchen.flappycow.sprites.Spider.java
com.quchen.flappycow.sprites.Sprite.java
com.quchen.flappycow.sprites.Toast.java
com.quchen.flappycow.sprites.Tutorial.java
com.quchen.flappycow.sprites.WoodLog.java