private void draw() { // Make sure our drawing surface is valid or we crash if (ourHolder.getSurface().isValid()) { // Lock the canvas ready to draw canvas = ourHolder.lockCanvas(); // Draw the background color canvas.drawColor(Color.argb(255, 26, 128, 182)); // Resources res = getResources(); // Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.background); // canvas.drawBitmap(bitmap, 0, 0, paint); // Choose the brush color for drawing paint.setColor(Color.argb(255, 255, 255, 255)); // Now draw the player spaceship canvas.drawBitmap(playerShip.getBitmap(), playerShip.getX(), screenY - 110, paint); // Draw the invaders for (int i = 0; i < numInvaders; i++) { if (invaders[i].getVisibility()) { if (uhOrOh) { canvas.drawBitmap( invaders[i].getBitmap(), invaders[i].getX(), invaders[i].getY(), paint); } else { canvas.drawBitmap( invaders[i].getBitmap2(), invaders[i].getX(), invaders[i].getY(), paint); } } } // Draw the bricks if visible for (int i = 0; i < numBricks; i++) { if (bricks[i].getVisibility()) { canvas.drawRect(bricks[i].getRect(), paint); } } // Draw the players bullet if active if (bullet.getStatus()) { canvas.drawRect(bullet.getRect(), paint); } // Draw the invaders bullets // Update all the invader's bullets if active for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { canvas.drawRect(invadersBullets[i].getRect(), paint); } } // Draw the score and remaining lives // Change the brush color paint.setColor(Color.argb(255, 249, 129, 0)); paint.setTextSize(40); canvas.drawText("Score: " + score + " Lives: " + lives, 10, 50, paint); // Draw everything to the screen ourHolder.unlockCanvasAndPost(canvas); } }
private void update() { // Did an invader bump into the side of the screen boolean bumped = false; // Has the player lost boolean lost = false; // Move the player's ship playerShip.update(fps); // Update the invaders if visible // Update the players bullet if (bullet.getStatus()) { bullet.update(fps); } // Update all the invaders bullets if active for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { invadersBullets[i].update(fps); } } // Update all the invaders if visible for (int i = 0; i < numInvaders; i++) { if (invaders[i].getVisibility()) { // Move the next invader invaders[i].update(fps); // Does he want to take a shot? if (invaders[i].takeAim(playerShip.getX(), playerShip.getLength())) { // If so try and spawn a bullet if (invadersBullets[nextBullet].shoot( invaders[i].getX() + invaders[i].getLength() / 2, invaders[i].getY(), bullet.DOWN)) { // Shot fired // Prepare for the next shot nextBullet++; // Loop back to the first one if we have reached the last if (nextBullet == maxInvaderBullets) { // This stops the firing of another bullet until one completes its journey // Because if bullet 0 is still active shoot returns false. nextBullet = 0; } } } // If that move caused them to bump the screen change bumped to true if (invaders[i].getX() > screenX - invaders[i].getLength() || invaders[i].getX() < 0) { bumped = true; } } } // Did an invader bump into the edge of the screen if (bumped) { // Move all the invaders down and change direction for (int i = 0; i < numInvaders; i++) { invaders[i].dropDownAndReverse(); // Have the invaders landed if (invaders[i].getY() > screenY - screenY / 10) { lost = true; } } // Increase the menace level // By making the sounds more frequent menaceInterval = menaceInterval - 80; } if (lost) { prepareLevel(); } // Has the player's bullet hit the top of the screen if (bullet.getImpactPointY() < 0) { bullet.setInactive(); } // Has an invaders bullet hit the bottom of the screen for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getImpactPointY() > screenY) { invadersBullets[i].setInactive(); } } // Has the player's bullet hit an invader if (bullet.getStatus()) { for (int i = 0; i < numInvaders; i++) { if (invaders[i].getVisibility()) { if (RectF.intersects(bullet.getRect(), invaders[i].getRect())) { invaders[i].setInvisible(); soundPool.play(invaderExplodeID, 1, 1, 0, 0, 1); bullet.setInactive(); score = score + 10; // Has the player won if (score == numInvaders * 10) { paused = true; String mScore = score + ""; Log.d("Score: ", mScore); Context context = getContext(); Intent intent = new Intent(context, ScoreActivity.class); intent.putExtra("score", mScore); context.startActivity(intent); } } } } } // Has an alien bullet hit a shelter brick for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { for (int j = 0; j < numBricks; j++) { if (bricks[j].getVisibility()) { if (RectF.intersects(invadersBullets[i].getRect(), bricks[j].getRect())) { // A collision has occurred invadersBullets[i].setInactive(); bricks[j].setInvisible(); soundPool.play(damageShelterID, 1, 1, 0, 0, 1); } } } } } // Has a player bullet hit a shelter brick if (bullet.getStatus()) { for (int i = 0; i < numBricks; i++) { if (bricks[i].getVisibility()) { if (RectF.intersects(bullet.getRect(), bricks[i].getRect())) { // A collision has occurred bullet.setInactive(); bricks[i].setInvisible(); soundPool.play(damageShelterID, 1, 1, 0, 0, 1); } } } } // Has an invader bullet hit the player ship for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { if (RectF.intersects(playerShip.getRect(), invadersBullets[i].getRect())) { invadersBullets[i].setInactive(); lives--; soundPool.play(playerExplodeID, 1, 1, 0, 0, 1); // Is it game over? if (lives == 0) { paused = true; // lives = 3; // score = 0; String mScore = score + ""; Context context = getContext(); Intent intent = new Intent(context, ScoreActivity.class); intent.putExtra("score", mScore); context.startActivity(intent); // prepareLevel(); } } } } }