Basic android Game Design Space Ship Game - part 5 (Enemy Ship)
 

package com.chitraksh.chigamebasicbuilding;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

public class EnemyShip {
    private Bitmap bitmap;
    private int x, y;
    private int speed = 1;
    // Detect enemies leaving the screen
    private int maxX;
    private int minX;
    // Spawn enemies within screen bounds
    private int maxY;
    private int minY;
    public Bitmap getBitmap(){
        return bitmap;
        }
    public int getX() {
        return x;
        }
    public int getY() {
        return y;
        }
    public EnemyShip(Context context, int screenX, int screenY){
        bitmap = BitmapFactory.decodeResource
                (context.getResources(), R.drawable.enemy);
        maxX = screenX;
        maxY = screenY;
        minX = 0;
        minY = bitmap.getHeight();
        Random generator = new Random();
        speed = generator.nextInt(6)+10;
        x = screenX;
        y = generator.nextInt(maxY);
        if(y<=bitmap.getHeight())
            y=bitmap.getHeight();
        else if(y>=(maxY - bitmap.getHeight()))
            y=generator.nextInt(maxY) - bitmap.getHeight();
    }
    public Rect getRectangle()
    {
        Rect rect=new Rect(x, y, x+bitmap.getWidth(), y+bitmap.getHeight());
        return rect;
    }
    public void update(int playerSpeed){
        // Move to the left
        x -= playerSpeed;
        x -= speed;
        //respawn when off screen
        if(x < minX-bitmap.getWidth()){
            Random generator = new Random();
            speed = generator.nextInt(10)+10;
            x = maxX;
            y = generator.nextInt(maxY) - bitmap.getHeight();
        }
    }
}

share on whatapp
582 Views

Comments

Show All Amazon Product

Private Policy   Terms of Service