Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you help me figure out what it wrong with my code? The goal is to have the player at the bottom of the screen
Can you help me figure out what it wrong with my code? The goal is to have the player at the bottom of the screen and shoot at enemies.
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;
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;
Load the player image
playerBitmap BitmapFactory.decodeResourcegetResources Rdrawable.playerimage;
@Override
protected void onSizeChangedint w int h int oldw, int oldh
super.onSizeChangedw h oldw, oldh;
Set the initial player position to the center bottom of the screen
playerX w f;
playerY h playerBitmap.getHeightf;
@Override
protected void onDrawCanvas canvas
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;
private EnemySpawner enemySpawner;
private void setupEnemySpawner
enemySpawner new EnemySpawnerthis null;
setContentViewenemySpawner;
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;
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