Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you edit this code make the player visible at the bottom of the screen, make the bullets functional, collision functional, and allow the player
Can you edit this code make the player visible at the bottom of the screen, make the bullets functional, collision functional, and allow the player to move across the bottom of the screen? Also, can you make the background of the drawables clear? This is being done in Android Studio: public class MainActivity extends AppCompatActivity
private PlayerView playerView;
@Override
protected void onCreateBundle savedInstanceState
super.onCreatesavedInstanceState;
setContentViewRlayout.activitymain;
FrameLayout container findViewByIdRidplayerviewcontainer;
playerView new PlayerViewthis;
container.addViewplayerView;
setupEnemySpawner;
private void setupEnemySpawner
Initialize enemy spawner
EnemySpawner enemySpawner new EnemySpawnerthis null;
FrameLayout container findViewByIdRidenemyviewcontainer;
container.addViewenemySpawner;
Bitmap enemyBitmaps
BitmapFactory.decodeResourcegetResources Rdrawable.enemy
BitmapFactory.decodeResourcegetResources Rdrawable.enemy
BitmapFactory.decodeResourcegetResources Rdrawable.enemy
;
Scale down the bitmaps
for int i ; i enemyBitmaps.length; i
int width enemyBitmapsigetWidth; Change these values as needed
int height enemyBitmapsigetHeight; Change these values as needed
enemyBitmapsi Bitmap.createScaledBitmapenemyBitmapsi width, height, true;
enemySpawner.setEnemyBitmapsenemyBitmaps;
public class PlayerView extends View
private float playerX;
private float playerY;
private float speed f;
private int health ;
private Paint paint;
private List bullets new ArrayList;
private Bitmap playerBitmap; New field for the player image
public PlayerViewContext context
supercontext;
init;
public PlayerViewContext context, AttributeSet attrs
supercontext attrs;
init;
private void init
setBackgroundColorColorTRANSPARENT;
playerBitmap BitmapFactory.decodeResourcegetResources Rdrawable.playerimage;
paint new Paint;
@Override
protected void onSizeChangedint w int h int oldw, int oldh
super.onSizeChangedw h oldw, oldh;
playerX w f;
playerY h playerBitmap.getHeightf;
@Override
protected void onDrawCanvas canvas
super.onDrawcanvas;
Draw the player
if playerBitmap null
canvas.drawBitmapplayerBitmap playerX playerBitmap.getWidth playerY playerBitmap.getHeight null;
Draw the bullets
for Bullet bullet : bullets
bullet.move;
canvas.drawCirclebulletgetX bullet.getY paint;
Draw the health
paint.setColorColorWHITE;
paint.setTextSize;
canvas.drawTextHealth: health, paint;
paint.setColorColorRED;
@Override
public boolean onTouchEventMotionEvent event
switch eventgetAction
case MotionEvent.ACTIONDOWN:
case MotionEvent.ACTIONMOVE:
playerX event.getX;
shootBullet;
invalidate;
return true;
return super.onTouchEventevent;
private void shootBullet
Bullet bullet new BulletplayerX playerY, f;
bullets.addbullet;
public void takeDamageint damage
health damage;
if health
Implement game over logic here eg restart level, show game over screen, etc.
health ;
invalidate;
public void movePlayerfloat dx
playerX dx;
invalidate;
public class Bullet
private float x;
private float y;
private float speed;
public Bulletfloat x float y float speed
this.x x;
this.y y;
this.speed speed;
public void move
y speed;
public float getX
return x;
public float getY
return y;
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