Answered step by step
Verified Expert Solution
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.csubhunter;
These are all the classes of other people's
Android code that we use in Sub Hunt
import android.app.Activity;
import android.osBundle;
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 ;
int gridHeight;
float horizontalTouched ;
float verticalTouched ;
int subHorizontalPosition;
int subVerticalPosition;
boolean hit false;
int shotsTaken;
int distanceFromSub;
boolean debugging false;
Here are all the objectsinstances
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 onetime setup.
@Override
protected void onCreateBundle savedInstanceState
super.onCreatesavedInstanceState;
Get the current device's screen resolution
Display display getWindowManagergetDefaultDisplay;
Point size new Point;
display.getSizesize;
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.createBitmapnumberHorizontalPixels
numberVerticalPixels,
Bitmap.Config.ARGB;
canvas new CanvasblankBitmap;
gameView new ImageViewthis;
paint new Paint;
Tell Android to set our drawing
as the view for this app
setContentViewgameView;
Log.dDebuggingIn 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.nextIntgridWidth;
subVerticalPosition random.nextIntgridHeight;
shotsTaken ;
Log.dDebuggingIn newGame";
Here we will do all the drawing.
The grid lines, the HUD and
the touch indicator
void draw
gameView.setImageBitmapblankBitmap;
Wipe the screen with a white color
canvas.drawColorColorargb;
Change the paint color to black
paint.setColorColorargb;
Draw the vertical lines of the grid
forint i ; i gridWidth; i
canvas.drawLineblockSize i
blockSize i numberVerticalPixels,
paint;
Draw the horizontal lines of the grid
forint i ; i gridHeight; i
canvas.drawLine blockSize i
numberHorizontalPixels, blockSize i
paint;
Draw the player's shot
canvas.drawRecthorizontalTouched blockSize,
verticalTouched blockSize,
horizontalTouched blockSize blockSize,
verticalTouched blockSize blockSize,
paint ;
Resize the text appropriate for the
score and distance text
paint.setTextSizeblockSize ;
paint.setColorColorargb;
canvas.drawText
"Shots Taken: shotsTaken
Distance: distanceFromSub,
blockSize, blockSize f
paint;
Log.dDebuggingIn draw";
if debugging
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started