- Timestamp:
- May 25, 2010, 2:46:37 PM (15 years ago)
- Branches:
- master
- Children:
- 3e9f39e
- Parents:
- 9b87c8d
- Location:
- src/com/example/helloandroid
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/GameView.java
r9b87c8d r8a4e64f 99 99 private int mCanvasWidth = 1; 100 100 101 /** What to draw for the Lander when it has crashed */102 private Drawable mCrashedImage;103 104 101 /** Default is MEDIUM. */ 105 102 private int mDifficulty; … … 112 109 private boolean mEngineFiring; 113 110 114 /** What to draw for the Lander when the engine is firing */115 private Drawable mFiringImage;116 117 111 /** Fuel remaining */ 118 112 private double mFuel; … … 152 146 153 147 /** Paint to draw the lines on screen. */ 154 private Paint mLinePaint ;148 private Paint mLinePaint, mTextPaint; 155 149 156 150 /** "Bad" speed-too-high variant of the line color. */ … … 188 182 // cache handles to our key sprites & other drawables 189 183 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);192 184 193 185 // load background image as a Bitmap instead of a Drawable b/c … … 207 199 mLinePaintBad.setAntiAlias(true); 208 200 mLinePaintBad.setARGB(255, 120, 180, 0); 201 202 mTextPaint = new Paint(); 203 mTextPaint.setAntiAlias(true); 204 mTextPaint.setARGB(255, 255, 0, 0); 209 205 210 206 mWinsInARow = 0; … … 422 418 * thread, which updates the user-text View. 423 419 */ 420 boolean test = true; 421 if(test) 422 return; 424 423 synchronized (mSurfaceHolder) { 425 424 mMode = mode; … … 475 474 Random rand = new Random(); 476 475 477 for(int x=0; x<6; x++) { 478 planets.add(new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight))); 476 for(int x=0; x<10; x++) { 477 Planet p = new Planet(rand.nextInt(45)+5, rand.nextInt(mCanvasWidth), rand.nextInt(mCanvasHeight)); 478 p.setNumShips(rand.nextInt(150)); 479 480 if(Planet.collisionDetected(p, planets)) { 481 x--; 482 }else if(p.getX()-p.getRadius() < 0 || mCanvasWidth<=p.getX()+p.getRadius() || 483 p.getY()-p.getRadius() < 0 || mCanvasHeight<=p.getY()+p.getRadius()) { 484 x--; 485 }else { 486 planets.add(p); 487 } 479 488 } 480 489 … … 519 528 return true; 520 529 } else if (mMode == STATE_RUNNING) { 521 // center/space -> fire 522 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER 523 || keyCode == KeyEvent.KEYCODE_SPACE) { 524 setFiring(true); 525 return true; 526 // left/q -> left 527 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT 528 || keyCode == KeyEvent.KEYCODE_Q) { 529 mRotating = -1; 530 return true; 531 // right/w -> right 532 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT 533 || keyCode == KeyEvent.KEYCODE_W) { 534 mRotating = 1; 535 return true; 536 // up -> pause 537 } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { 538 pause(); 539 return true; 540 } 530 return true; 541 531 } 542 532 … … 559 549 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER 560 550 || keyCode == KeyEvent.KEYCODE_SPACE) { 561 setFiring(false);562 551 handled = true; 563 552 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT … … 565 554 || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT 566 555 || keyCode == KeyEvent.KEYCODE_W) { 567 mRotating = 0;568 556 handled = true; 569 557 } … … 585 573 for(Planet p : planets) { 586 574 canvas.drawCircle(p.getX(), p.getY(), p.getRadius(), mLinePaint); 575 canvas.drawText(Integer.toString(p.getNumShips()), p.getX(), p.getY(), mTextPaint); 587 576 } 588 577 … … 592 581 // Draw the ship with its current rotation 593 582 canvas.save(); 594 canvas.rotate((float) mHeading, (float) mX, mCanvasHeight 595 - (float) mY); 583 canvas.rotate((float)mHeading, (float)mX, mCanvasHeight - (float)mY); 596 584 if (mMode == STATE_LOSE) { 597 mCrashedImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop 598 + mLanderHeight); 599 mCrashedImage.draw(canvas); 585 //mCrashedImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 586 //mCrashedImage.draw(canvas); 600 587 } else if (mEngineFiring) { 601 mFiringImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop 602 + mLanderHeight); 603 mFiringImage.draw(canvas); 588 //mFiringImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 589 //mFiringImage.draw(canvas); 604 590 } else { 605 mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop 606 + mLanderHeight); 591 mLanderImage.setBounds(xLeft, yTop, xLeft + mLanderWidth, yTop + mLanderHeight); 607 592 mLanderImage.draw(canvas); 608 593 } … … 665 650 } 666 651 652 for(Planet p : planets) { 653 p.update(); 654 } 655 667 656 double dxOld = mDX; 668 657 double dyOld = mDY; -
src/com/example/helloandroid/Planet.java
r9b87c8d r8a4e64f 1 1 package com.example.helloandroid; 2 3 import java.util.ArrayList; 2 4 3 5 public class Planet { … … 31 33 } 32 34 35 public int getNumShips() { 36 return numShips; 37 } 38 39 public void setNumShips(int num) { 40 numShips = num; 41 } 42 33 43 public void update() { 34 44 //regen ships if not owned by faction 0 45 numShips++; 35 46 } 36 47 … … 38 49 39 50 } 51 52 public boolean collides(Planet p) { 53 double dist = Math.sqrt(Math.pow(this.x-p.x, 2) + Math.pow(this.y-p.y, 2)); 54 55 return dist <= this.radius + p.radius; 56 } 57 58 public static boolean collisionDetected(Planet p, ArrayList<Planet> curPlanets) { 59 for(Planet p2 : curPlanets) { 60 if(p.collides(p2)) 61 return true; 62 } 63 64 return false; 65 } 40 66 }
Note:
See TracChangeset
for help on using the changeset viewer.