-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Tuesday, 3 July 2018

Java | Android mini game

This is improved code from the previous post. This mini game which we call "Moon Buggy" is available in beta .

The action is that you control a vechicle on the moon and you defend yourself against evil UFO:s.
I have written a separate class for the UFO which is instanciated once for every UFO:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Looper;

import java.util.Random;

public class AttackingAlien extends Alien {

    private Bitmap alien;
    static boolean recent = true; // switch this in beginning
    private long changeDirections = System.currentTimeMillis();
    private long fireTimeout = System.currentTimeMillis();
    private int screenWidth;
    private int screenHeight;
    private int ufoY = 0;
    private int ufoX = 0;
    private int missileX = 25;
    private int deltaUfoY = 7;
    private int deltaUfoX = 7;
    private int missileOffSetY = 0;
    private int missileYstart = 0;
    private boolean wasHit = false;
    private boolean alienexplode;
    private boolean waitForTimer, waitForUfoTimer;
    private boolean toggleDeltaY = true;
    private boolean runOnce = true;
    private boolean startMissile = true;

    public AttackingAlien(ParallaxView view, Context context, String name, final int screenWidth, int screenHeight, int p) {
        super(context, name);
        this.deltaUfoY = p;
        int alienResID = context.getResources().getIdentifier(name,
                "drawable", context.getPackageName());
        alien = BitmapFactory.decodeResource(context.getResources(), alienResID);
        int max = (int) (0.75 * screenWidth);
        int min = 20;
        int diff = max - min;
        Random rn = new Random();
        int i5 = rn.nextInt(diff + 1);
        i5 += min;
        missileX = i5;
        ufoX = missileX;
        max = 200;
        diff = max - min;
        this.screenHeight = screenHeight;
        this.screenWidth = screenWidth;
        ufoY = 0;
        waitForUfoTimer = true;

        int max2 = 20000;
        int min2 = 18000;
        int diff2 = max2 - min2;
        Random rn2 = new Random();
        int result = rn2.nextInt(diff2 + 1);
        result += min2;

        Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                missileX = ufoX;
                setRecent();
                waitForUfoTimer = false;
            }
        }, result);
    }

    private void changeDirections() {

        int max2 = 500;
        int min2 = 100;
        int diff2 = max2 - min2;
        Random rn2 = new Random();
        int result = rn2.nextInt(diff2 + 1);
        result += min2;

        if (System.currentTimeMillis() - changeDirections >= result) {
            // Change direction here
            toggleDeltaY = !toggleDeltaY;
            changeDirections = System.currentTimeMillis();
        }
    }

    public void update(Canvas canvas, Paint paint, boolean toggleDeltaY, int screenWidth, int screenHeight) {
        if (ufoX > screenWidth - 250 || ufoX < 10) { // UFO change horizontal direction
            deltaUfoX = -deltaUfoX;
        }

        //make sure UFO does not move too low
        if (ufoY >= 20) {
            deltaUfoY = -deltaUfoY;
        }
        if ((ufoY + screenHeight / 100 * 25) <= 0) // don't move outside the top
            deltaUfoY = -deltaUfoY;

        if (!waitForUfoTimer && Background.checkpoint >= 'A') { // && sectionComplete > 0) {
            runOnce = true;
            //alienY++;
            canvas.drawBitmap(alien, ufoX + 10, ufoY + screenHeight / 100 * 25, paint);
        }
        //missileX = missileX + speedAlienX;
        ufoX = ufoX + deltaUfoX;
        if (waitForTimer) missileX = ufoX;
        if (toggleDeltaY) {
            deltaUfoY = -deltaUfoY;
        }
        ufoY = ufoY + deltaUfoY;
        changeDirections();
    }

    public void checkBeingHit(int[] missiles, int buggyXDisplacement, double buggyXDistance, Canvas canvas, Bitmap explode2, Paint paint, int score, ParallaxView pview, int i1, int xbuggy2) {
        // if UFO is being hit by buggy
        if (!waitForTimer && java.lang.Math.abs(ufoX + 10 - 400 - buggyXDistance) * 2 < (alien.getWidth()) && java.lang.Math.abs(ufoY + screenHeight / 100 * 25 - (screenHeight / 100 * 95 - missiles[i1] - xbuggy2)) * 2 < (alien.getHeight())) {
            missileOffSetY = -9999;
            canvas.drawBitmap(explode2, ufoX + 10, ufoY + screenHeight / 100 * 25, paint);
            if (runOnce) {
                ParallaxView.score = ParallaxView.score + 100;
                Handler handler = new Handler(Looper.getMainLooper());
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int max = (int) (0.75 * screenWidth);
                        int min = 20;
                        int diff = max - min;
                        Random rn = new Random();
                        int i5 = rn.nextInt(diff + 1);
                        i5 += min;
                        missileX = i5;//25;
                        ufoX = missileX;
                        ufoY = 0;
                        alienexplode = false;
                        waitForTimer = false;
                        waitForUfoTimer = false;
                        startMissile = true;
                    }
                }, 3000);
            }
            runOnce = false;
            waitForUfoTimer = true;
            startMissile = false;
            waitForTimer = true;
            if (!alienexplode) {
                pview.changeText();
            }
            alienexplode = true;
        }
    }

    private void checkFire() {
        int max = 15000;
        int min = 12000;
        int diff = max - min;
        Random rn = new Random();
        int i5 = rn.nextInt(diff + 1);
        i5 += min;
        if (System.currentTimeMillis() - fireTimeout >= i5) { // means how often the alien fires
            fireTimeout = System.currentTimeMillis();
            missileOffSetY = 0;
            missileX = ufoX;
        }
    }

    private void setRecent() {
        AttackingAlien.recent = false;
    }



    public boolean drawMissile(ParallaxView view, Canvas canvas, Paint paint, int buggyXDisplacement, double buggyXDistance, Bitmap buggy, int jumpHeight, int screenHeight) {
        wasHit = false;
        checkFire();
        // if buggy was hit by a missile
        if (!AttackingAlien.recent && !view.waitForTimer && java.lang.Math.abs(((buggyXDisplacement + buggyXDistance) + buggy.getWidth() / 2) - (missileX + 10 + alien.getWidth() / 2)) < buggy.getWidth() / 2 && java.lang.Math.abs((ufoY + screenHeight / 100 * 25 + 75 + missileOffSetY) - ((screenHeight * 0.3) - jumpHeight + buggy.getHeight())) < 65) {
            AttackingAlien.recent = true;
            canvas.drawBitmap(view.explode, (float) (buggyXDisplacement + buggyXDistance), (float) (screenHeight * 0.5) - jumpHeight, paint);
            ParallaxView.bombed--;
            missileOffSetY = 0;
            wasHit = true;
            view.recent = true;
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    setRecent();
                    waitForTimer = false;
                    wasHit = false;
                }
            }, 7000);
            waitForTimer = true;
        } else {
            // buggy was not hit so UFO fires more missiles
            //TODO: check if the movements are realistic
            if (!waitForTimer && !waitForUfoTimer && Background.checkpoint >= 'A') {
                if (startMissile) {
                    startMissile = false;
                    missileYstart = ufoY;
                }
                canvas.drawText("●", missileX + alien.getWidth() / 2, missileYstart + screenHeight / 100 * 25 + alien.getHeight() + missileOffSetY, paint);
                missileOffSetY = missileOffSetY + 4;
            }
            wasHit = false;
        }
        return wasHit;
    }
}
The remainder of the code is available on request.

No comments:

Post a Comment

Thanks for your comments

Rank

seo