Question: 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 onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout container = findViewById(R.id.player_view_container);
playerView = new PlayerView(this);
container.addView(playerView);
setupEnemySpawner();
}
public class PlayerView extends View {
private float playerX;
private float playerY;
private float speed =5f;
private int health =100;
private Paint paint;
private List bullets = new ArrayList<>();
private Bitmap playerBitmap; // New field for the player image
public PlayerView(Context context){
super(context);
init();
}
public PlayerView(Context context, AttributeSet attrs){
super(context, attrs);
init();
}
private void init(){
setBackgroundColor(Color.TRANSPARENT);
// Load the player image
playerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.player_image);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
// Set the initial player position to the center bottom of the screen
playerX = w /2f;
playerY = h - playerBitmap.getHeight()/2f;
}
@Override
protected void onDraw(Canvas canvas){
// Draw the bullets
for (Bullet bullet : bullets){
bullet.move();
canvas.drawCircle(bullet.getX(), bullet.getY(),10, paint);
}
// Draw the health
paint.setColor(Color.WHITE);
paint.setTextSize(50);
canvas.drawText("Health: "+ health, 50,50, paint);
paint.setColor(Color.RED);
}
@Override
public boolean onTouchEvent(MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
playerX = event.getX();
shootBullet();
invalidate();
return true;
}
return super.onTouchEvent(event);
}
private void shootBullet(){
Bullet bullet = new Bullet(playerX, playerY, 10f);
bullets.add(bullet);
}
public void takeDamage(int damage){
health -= damage;
if (health <=0){
// Implement game over logic here (e.g., restart level, show game over screen, etc.)
health =0;
invalidate();
}
}
public void movePlayer(float dx){
playerX += dx;
invalidate();
}
}
public class Bullet {
private float x;
private float y;
private float speed;
public Bullet(float 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 EnemySpawner(this, null);
setContentView(enemySpawner);
Bitmap[] enemyBitmaps ={
BitmapFactory.decodeResource(getResources(), R.drawable.enemy1),
BitmapFactory.decodeResource(getResources(), R.drawable.enemy2),
BitmapFactory.decodeResource(getResources(), R.drawable.enemy3)
};
// Scale down the bitmaps
for (int i =0; i < enemyBitmaps.length; i++){
int width = enemyBitmaps[i].getWidth()/10; // Change these values as needed
int height = enemyBitmaps[i].getHeight()/10; // Change these values as needed
enemyBitmaps[i]= Bitmap.createScaledBitmap(enemyBitmaps[i], width, height, true);
}
enemySpawner.setEnemyBitmaps(enemyBitmaps);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!