Android Open Source - DroidRunJump Pothole






From Project

Back to project page DroidRunJump.

License

The source code is released under:

"Droid-Run-Jump" Copyright (c) 2011 Donald E. Llopis <machinezilla@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentati...

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

package com.android.sofla.drj;
/*from  www .ja v a2 s . com*/
import android.content.SharedPreferences;
import android.graphics.Canvas;


class Pothole {

  float x, y;
  float w, h;
  boolean alive;
  
  Game game;
  
  public void reset() {    
    alive = false;
  }

  public void spawn(float xOffset) {
    
    //
    // spawn a pothole starting beyond right side of the display
    // apply additional xOffset and vary the width of the pothole
    //
    
    w = game.random(game.MIN_POTHOLE_WIDTH, game.MAX_POTHOLE_WIDTH);
    x = game.width + w + xOffset;
    alive = true;
  }

  public void update() {
    
    //
    // potholes move from right to left
    //
    
    x -= 10.0f;
    
    
    //
    // if pothole beyond left hand side of display then disable it
    //
    if (x < -w) {
      alive = false;
    }
  }

  public void draw(Canvas canvas) {
    canvas.drawRect(x, y, x + w, y + h, game.clearPaint);
  }

  //
  // workshop 2 code
  //
  
  int id;

  public Pothole(int id, Game game) {    
    this.id = id;
    this.game = game;
    y = game.groundY;
    h = game.roadImage.getHeight();
    alive = false;
  }  
  
  public void restore(SharedPreferences savedState) {
    x = savedState.getFloat("ph_" + id + "_x", 0);
    y = savedState.getFloat("ph_" + id + "_y", 0);
    w = savedState.getFloat("ph_" + id + "_w", 0);
    h = savedState.getFloat("ph_" + id + "_h", 0);
    alive = savedState.getBoolean("ph_" + id + "_alive", false);
  }
  
  public void save(SharedPreferences.Editor map) {    
    map.putFloat("ph_" + id + "_x", x);
    map.putFloat("ph_" + id + "_y", y);
    map.putFloat("ph_" + id + "_w", w);
    map.putFloat("ph_" + id + "_h", h);
    map.putBoolean("ph_" + id + "_alive", alive);
  }
}




Java Source Code List

com.android.sofla.drj.DroidRunJumpActivity.java
com.android.sofla.drj.DroidRunJumpView.java
com.android.sofla.drj.Droid.java
com.android.sofla.drj.Game.java
com.android.sofla.drj.Pastry.java
com.android.sofla.drj.Pothole.java
com.android.sofla.drj.Road.java