Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA. ANDROID STUDIO. I want to implement some features into this code. I just dont know how to go about it . I would like

JAVA. ANDROID STUDIO.
I want to implement some features into this code. I just dont know how to go about it.
I would like a start button, image after "boom" (after submarine was found). I would also like to clean up the code.
Thank you so much!
// If so, keep YOUR line- not this one
package com.gamecodeschool.c7subhunter;
// These are all the classes of other people's
//(Android) code that we use in Sub Hunt
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.util.Log;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.widget.ImageView;
import java.util.Random;
public class SubHunter extends Activity {
// These variables can be "seen"
// throughout the SubHunter class
int numberHorizontalPixels;
int numberVerticalPixels;
int blockSize;
int gridWidth =40;
int gridHeight;
float horizontalTouched =-100;
float verticalTouched =-100;
int subHorizontalPosition;
int subVerticalPosition;
boolean hit = false;
int shotsTaken;
int distanceFromSub;
boolean debugging = false;
// Here are all the objects(instances)
// of classes that we need to do some drawing
ImageView gameView;
Bitmap blankBitmap;
Canvas canvas;
Paint paint;
/*
Android runs this code just before
the app is seen by the player.
This makes it a good place to add
the code that is needed for
the one-time setup.
*/
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// Get the current device's screen resolution
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// Initialize our size based variables based on the screen resolution
numberHorizontalPixels = size.x;
numberVerticalPixels = size.y;
blockSize = numberHorizontalPixels / gridWidth;
gridHeight = numberVerticalPixels / blockSize;
// Initialize all the objects ready for drawing
blankBitmap = Bitmap.createBitmap(numberHorizontalPixels,
numberVerticalPixels,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(blankBitmap);
gameView = new ImageView(this);
paint = new Paint();
// Tell Android to set our drawing
// as the view for this app
setContentView(gameView);
Log.d("Debugging","In onCreate");
newGame();
draw();
}
/*
This code will execute when a new
game needs to be started. It will
happen when the app is first started
and after the player wins a game.
*/
void newGame(){
Random random = new Random();
subHorizontalPosition = random.nextInt(gridWidth);
subVerticalPosition = random.nextInt(gridHeight);
shotsTaken =0;
Log.d("Debugging","In newGame");
}
/*
Here we will do all the drawing.
The grid lines, the HUD and
the touch indicator
*/
void draw(){
gameView.setImageBitmap(blankBitmap);
// Wipe the screen with a white color
canvas.drawColor(Color.argb(255,255,255,255));
// Change the paint color to black
paint.setColor(Color.argb(255,0,0,0));
// Draw the vertical lines of the grid
for(int i =0; i < gridWidth; i++){
canvas.drawLine(blockSize * i,0,
blockSize * i, numberVerticalPixels,
paint);
}
// Draw the horizontal lines of the grid
for(int i =0; i < gridHeight; i++){
canvas.drawLine(0, blockSize * i,
numberHorizontalPixels, blockSize * i,
paint);
}
// Draw the player's shot
canvas.drawRect(horizontalTouched * blockSize,
verticalTouched * blockSize,
(horizontalTouched * blockSize)+ blockSize,
(verticalTouched * blockSize)+ blockSize,
paint );
// Re-size the text appropriate for the
// score and distance text
paint.setTextSize(blockSize *2);
paint.setColor(Color.argb(255,0,0,255));
canvas.drawText(
"Shots Taken: "+ shotsTaken +
" Distance: "+ distanceFromSub,
blockSize, blockSize *1.75f,
paint);
Log.d("Debugging","In draw");
if (debugging){

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

Determine the group delay for 0 (a) n-1. 1sn Answered: 1 week ago

Answered: 1 week ago