document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.pickle.PongWallPaper;
  2.  
  3. import org.anddev.andengine.engine.camera.Camera;
  4. import org.anddev.andengine.engine.handler.IUpdateHandler;
  5. import org.anddev.andengine.engine.options.EngineOptions;
  6. import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
  7. import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
  8. import org.anddev.andengine.entity.primitive.Line;
  9. import org.anddev.andengine.entity.primitive.Rectangle;
  10. import org.anddev.andengine.entity.scene.Scene;
  11. import org.anddev.andengine.entity.scene.background.ColorBackground;
  12. import org.anddev.andengine.entity.util.FPSLogger;
  13. import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
  14. import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
  15.  
  16. import android.content.SharedPreferences;
  17.  
  18. public class PongWallPaper extends BaseLiveWallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener
  19. {
  20.  // ===========================================================
  21.  // Constants
  22.  // ===========================================================
  23.  
  24.  public static final String SHARED_PREFS_NAME = "livewallpaperPongWallPapersettings";
  25.  
  26.  // Camera Constants
  27.   private static int CAMERA_WIDTH = 480;
  28.   private static int CAMERA_HEIGHT = 720;
  29.  
  30.   private static float BALL_VELOCITY = 120.0f;
  31.   private static float PADDLE_VELOCITY = 160.0f;
  32.  
  33.   public static int GAME_WIDTH = 480;
  34.   public static int GAME_WIDTH_HALF = GAME_WIDTH / 2;
  35.   public static int GAME_HEIGHT = 720;
  36.   public static int GAME_HEIGHT_HALF = GAME_HEIGHT / 2;
  37.  
  38.   // ===========================================================
  39.   // Fields
  40.   // ===========================================================
  41.  
  42.   private Camera mCamera;
  43.  
  44.   private Rectangle mBall;
  45.  
  46.   // Shared Preferences
  47.   private SharedPreferences mSharedPreferences;
  48.  
  49.  // ===========================================================
  50.  // Constructors
  51.  // ===========================================================
  52.  
  53.  // ===========================================================
  54.  // Getter & Setter
  55.  // ===========================================================
  56.  
  57.  // ===========================================================
  58.  // Methods for/from SuperClass/Interfaces
  59.  // ===========================================================
  60.  
  61.  @Override
  62.  public org.anddev.andengine.engine.Engine onLoadEngine()
  63.  {
  64.  
  65.   int cameraWidth = 480;
  66.   int cameraHeight = 720;
  67.  
  68.   CAMERA_WIDTH = cameraWidth;
  69.   CAMERA_HEIGHT = cameraHeight;
  70.  
  71.   GAME_WIDTH = cameraWidth;
  72.   GAME_WIDTH_HALF = GAME_WIDTH / 2;
  73.   GAME_HEIGHT = cameraHeight;
  74.   GAME_HEIGHT_HALF = GAME_HEIGHT / 2;
  75.  
  76.   this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  77.   this.mCamera.setCenter(0, 0);
  78.  
  79.   return new org.anddev.andengine.engine.Engine(
  80.     new EngineOptions(true, ScreenOrientation.PORTRAIT,
  81.       new FillResolutionPolicy(), mCamera));
  82.  }
  83.  
  84.  @Override
  85.  public void onLoadResources()
  86.  {
  87.   //Set the Base Texture Path
  88.   TextureRegionFactory.setAssetBasePath("gfx/");
  89.  }
  90.  
  91.  @Override
  92.  public Scene onLoadScene()
  93.  {
  94.   this.mEngine.registerPostFrameHandler(new FPSLogger());
  95.  
  96.   final Scene scene = new Scene(1);
  97.   scene.setBackground(new ColorBackground(0, 0, 0));
  98.  
  99.   /* Walls */
  100.  
  101.   scene.getBottomLayer().addEntity(
  102.     new Line(-GAME_WIDTH_HALF + 1, -GAME_HEIGHT_HALF,
  103.       -GAME_WIDTH_HALF + 1, GAME_HEIGHT_HALF)); // Left
  104.   scene.getBottomLayer().addEntity(
  105.     new Line(GAME_WIDTH_HALF, -GAME_HEIGHT_HALF, GAME_WIDTH_HALF,
  106.       GAME_HEIGHT_HALF)); // Right
  107.  
  108.   scene.getBottomLayer().addEntity(
  109.     new Line(-GAME_WIDTH_HALF, -GAME_HEIGHT_HALF + 1,
  110.       GAME_WIDTH_HALF, -GAME_HEIGHT_HALF + 1)); // Top
  111.   scene.getBottomLayer().addEntity(
  112.     new Line(-GAME_WIDTH_HALF, GAME_HEIGHT_HALF, GAME_WIDTH_HALF,
  113.       GAME_HEIGHT_HALF)); // Bottom
  114.  
  115.   scene.getBottomLayer().addEntity(
  116.     new Line(0, -GAME_HEIGHT_HALF, 0, GAME_HEIGHT_HALF)); // Middle
  117.  
  118.   /* Ball */
  119.   final Rectangle ball = new Rectangle(0, 0, 20, 20) {
  120.    @Override
  121.    protected void onManagedUpdate(float pSecondsElapsed) {
  122.     if (this.mX < -GAME_WIDTH_HALF) {
  123.      //this.setVelocityX(BALL_VELOCITY);
  124.      this.setPosition(0, 0);
  125.     } else if (this.mX + this.getWidth() > GAME_WIDTH_HALF) {
  126.      //this.setVelocityX(-BALL_VELOCITY);
  127.      this.setPosition(0, 0);
  128.     }
  129.  
  130.    
  131.    
  132.     if (this.mY < -GAME_HEIGHT_HALF) {
  133.      this.setVelocityY(BALL_VELOCITY);
  134.     } else if (this.mY + this.getHeight() > GAME_HEIGHT_HALF) {
  135.      this.setVelocityY(-BALL_VELOCITY);
  136.     }
  137.  
  138.     super.onManagedUpdate(pSecondsElapsed);
  139.    }
  140.   };
  141.  
  142.   ball.setVelocityX(BALL_VELOCITY);
  143.   ball.setVelocityY(BALL_VELOCITY);
  144.   scene.getBottomLayer().addEntity(ball);
  145.  
  146.   /* Paddles */
  147.   final Rectangle leftPaddle = new Rectangle(-GAME_WIDTH_HALF + 20, 0,
  148.     20, 90);
  149.   scene.getBottomLayer().addEntity(leftPaddle);
  150.  
  151.   final Rectangle rightPaddle = new Rectangle(GAME_WIDTH_HALF - 40, 0,
  152.     20, 90);
  153.   scene.getBottomLayer().addEntity(rightPaddle);
  154.  
  155.   scene.registerPreFrameHandler(new IUpdateHandler() {
  156.  
  157.    @Override
  158.    public void reset() {
  159.    }
  160.  
  161.    @Override
  162.    public void onUpdate(final float pSecondsElapsed) {
  163.  
  164.     /* Update Left Paddle to track ball */
  165.     if ( ( leftPaddle.getY() + 40) > ball.getY()) {
  166.      leftPaddle.setVelocityY(-PADDLE_VELOCITY);
  167.     } else {
  168.      leftPaddle.setVelocityY(PADDLE_VELOCITY);
  169.     }
  170.  
  171.     /* Update Right Paddle to track ball */
  172.     if ( ( rightPaddle.getY() + 40 ) > ball.getY()) {
  173.      rightPaddle.setVelocityY(-PADDLE_VELOCITY);
  174.     } else {
  175.      rightPaddle.setVelocityY(PADDLE_VELOCITY);
  176.     }
  177.  
  178.     if (rightPaddle.collidesWith(ball)) {
  179.      ball.setVelocityX(-BALL_VELOCITY);
  180.     }
  181.  
  182.     if (ball.getVelocityX() == BALL_VELOCITY) {
  183.      leftPaddle.setVelocityY(0f);
  184.     } else {
  185.      rightPaddle.setVelocityY(0f);
  186.     }
  187.  
  188.     if (leftPaddle.collidesWith(ball)) {
  189.      ball.setVelocityX(BALL_VELOCITY);
  190.     }
  191.    }
  192.   });
  193.  
  194.   return scene;
  195.  }
  196.  
  197.  @Override
  198.  public void onLoadComplete()
  199.  {
  200.  
  201.  }
  202.  
  203.  @Override
  204.  protected void onTap(final int pX, final int pY)
  205.  {
  206.  
  207.  }
  208.  
  209.  @Override
  210.  public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
  211.  {
  212.  
  213.  }
  214.  
  215.  // ===========================================================
  216.  // Methods
  217.  // ===========================================================
  218.  
  219.  // ===========================================================
  220.  // Inner and Anonymous Classes
  221.  // ===========================================================
  222. }
');