- Timestamp:
- May 24, 2010, 9:46:26 PM (14 years ago)
- Branches:
- master
- Children:
- 8a4e64f
- Parents:
- 76819ac
- Location:
- src/com/example/helloandroid
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/GameView.java
r76819ac r9b87c8d 1 1 package com.example.helloandroid; 2 3 import java.util.ArrayList; 4 import java.util.Random; 2 5 3 6 import android.content.Context; … … 7 10 import android.graphics.Canvas; 8 11 import android.graphics.Paint; 9 import android.graphics.RectF;10 12 import android.graphics.drawable.Drawable; 11 13 import android.os.Bundle; … … 13 15 import android.os.Message; 14 16 import android.util.AttributeSet; 17 import android.util.Log; 15 18 import android.view.KeyEvent; 16 19 import android.view.SurfaceHolder; … … 93 96 private Bitmap mBackgroundImage; 94 97 95 /**96 * Current height of the surface/canvas.97 *98 * @see #setSurfaceSize99 */100 98 private int mCanvasHeight = 1; 101 102 /**103 * Current width of the surface/canvas.104 *105 * @see #setSurfaceSize106 */107 99 private int mCanvasWidth = 1; 108 100 … … 110 102 private Drawable mCrashedImage; 111 103 112 /** 113 * Current difficulty -- amount of fuel, allowed angle, etc. Default is 114 * MEDIUM. 115 */ 104 /** Default is MEDIUM. */ 116 105 private int mDifficulty; 117 106 118 /** Velocity dx.*/107 /** Velocity */ 119 108 private double mDX; 120 121 /** Velocity dy. */122 109 private double mDY; 123 110 … … 179 166 private boolean mRun = false; 180 167 181 /** Scratch rect object. */182 private RectF mScratchRect;183 184 168 /** Handle to the surface manager object we interact with */ 185 169 private SurfaceHolder mSurfaceHolder; … … 188 172 private int mWinsInARow; 189 173 190 /** X oflander center. */174 /** lander center. */ 191 175 private double mX; 192 193 /** Y of lander center. */194 176 private double mY; 177 178 private ArrayList<Planet> planets; 195 179 196 180 public DrawingThread(SurfaceHolder surfaceHolder, Context context, … … 203 187 Resources res = context.getResources(); 204 188 // cache handles to our key sprites & other drawables 205 mLanderImage = context.getResources().getDrawable( 206 R.drawable.lander_plain); 207 mFiringImage = context.getResources().getDrawable( 208 R.drawable.lander_firing); 209 mCrashedImage = context.getResources().getDrawable( 210 R.drawable.lander_crashed); 189 mLanderImage = context.getResources().getDrawable(R.drawable.lander_plain); 190 mFiringImage = context.getResources().getDrawable(R.drawable.lander_firing); 191 mCrashedImage = context.getResources().getDrawable(R.drawable.lander_crashed); 211 192 212 193 // load background image as a Bitmap instead of a Drawable b/c 213 194 // we don't need to transform it and it's faster to draw this way 214 mBackgroundImage = BitmapFactory.decodeResource(res, 215 R.drawable.earthrise); 195 mBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.earthrise); 216 196 217 197 // Use the regular lander image as the model size for all sprites … … 227 207 mLinePaintBad.setAntiAlias(true); 228 208 mLinePaintBad.setARGB(255, 120, 180, 0); 229 230 mScratchRect = new RectF(0, 0, 0, 0);231 209 232 210 mWinsInARow = 0; … … 241 219 mHeading = 0; 242 220 mEngineFiring = true; 221 222 planets = new ArrayList<Planet>(); 243 223 } 244 224 … … 490 470 mCanvasWidth = width; 491 471 mCanvasHeight = height; 492 472 473 Log.i(this.getClass().getName(), "width: "+mCanvasWidth+", height: "+mCanvasHeight); 474 475 Random rand = new Random(); 476 477 for(int x=0; x<6; x++) { 478 planets.add(new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight))); 479 } 480 493 481 // don't forget to resize the background image 494 482 mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, width, height, true); … … 594 582 // so this is like clearing the screen. 595 583 canvas.drawBitmap(mBackgroundImage, 0, 0, null); 596 584 585 for(Planet p : planets) { 586 canvas.drawCircle(p.getX(), p.getY(), p.getRadius(), mLinePaint); 587 } 588 597 589 int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2); 598 590 int xLeft = (int) mX - mLanderWidth / 2; 599 600 // Draw the fuel gauge601 int fuelWidth = (int) (UI_BAR * mFuel / PHYS_FUEL_MAX);602 mScratchRect.set(4, 4, 4 + fuelWidth, 4 + UI_BAR_HEIGHT);603 canvas.drawRect(mScratchRect, mLinePaint);604 605 // Draw the speed gauge, with a two-tone effect606 double speed = Math.sqrt(mDX * mDX + mDY * mDY);607 int speedWidth = (int) (UI_BAR * speed / PHYS_SPEED_MAX);608 609 if (speed <= mGoalSpeed) {610 mScratchRect.set(4 + UI_BAR + 4, 4,611 4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT);612 canvas.drawRect(mScratchRect, mLinePaint);613 } else {614 // Draw the bad color in back, with the good color in front of615 // it616 mScratchRect.set(4 + UI_BAR + 4, 4,617 4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT);618 canvas.drawRect(mScratchRect, mLinePaintBad);619 int goalWidth = (UI_BAR * mGoalSpeed / PHYS_SPEED_MAX);620 mScratchRect.set(4 + UI_BAR + 4, 4, 4 + UI_BAR + 4 + goalWidth,621 4 + UI_BAR_HEIGHT);622 canvas.drawRect(mScratchRect, mLinePaint);623 }624 625 // Draw the landing pad626 canvas.drawLine(mGoalX, 1 + mCanvasHeight - TARGET_PAD_HEIGHT,627 mGoalX + mGoalWidth, 1 + mCanvasHeight - TARGET_PAD_HEIGHT,628 mLinePaint);629 630 591 631 592 // Draw the ship with its current rotation -
src/com/example/helloandroid/Planet.java
r76819ac r9b87c8d 4 4 int radius; 5 5 int regenRate; // ships per second 6 int x;7 int y;6 private int x; 7 private int y; 8 8 int faction; 9 9 int numShips; … … 27 27 } 28 28 29 public int getRadius() { 30 return radius; 31 } 32 29 33 public void update() { 30 34 //regen ships if not owned by faction 0
Note:
See TracChangeset
for help on using the changeset viewer.