Commit 766765fa authored by Thomas Poimenidis's avatar Thomas Poimenidis
Browse files

created GameView, GameActivity, Background classes. implemented infinite background illusion

parent e421bc18
Loading
Loading
Loading
Loading

app/.DS_Store

0 → 100644
+6 KiB

File added.

No diff preview for this file type.

+3 −0
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@
        android:supportsRtl="true"
        android:theme="@style/Theme.8bitInvader"
        tools:targetApi="31">
        <activity
            android:name=".GameActivity"
            android:theme="@style/Theme.Design.NoActionBar" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
+20 −0
Original line number Diff line number Diff line
package com.example.a8_bitinvader;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Background {

    int xPos = 0, yPos = 0;
    Bitmap background;

    /*
     * Creates background from 'background1.jpeg' in res/drawable directory
     */
    Background(int screenWidth, int screenHeight, Resources res) {
        background = BitmapFactory.decodeResource(res, R.drawable.background);
        background = Bitmap.createScaledBitmap(background, screenWidth, screenHeight, false);
    }

}
+40 −0
Original line number Diff line number Diff line
package com.example.a8_bitinvader;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import android.graphics.Point;              // necessary to get ScreenWidth and ScreenHeight
import android.view.WindowManager;

public class GameActivity extends AppCompatActivity {

    private GameView gameView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // puts app into fullscreen when GameActivity is running
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


        Point point = new Point();
        getWindowManager().getDefaultDisplay().getSize(point);

        gameView = new GameView(this, point.x, point.y);

        setContentView(gameView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        gameView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        gameView.resume();
    }
}
+111 −0
Original line number Diff line number Diff line
package com.example.a8_bitinvader;

import android.content.Context;
import android.view.SurfaceView;
import android.graphics.Canvas;
import android.graphics.Paint;             // necessary when drawing Bitmaps with Canvas objects

public class GameView extends SurfaceView implements Runnable{

    private Thread thread;
    private boolean isRunning;
    private int screenWidth, screenHeight;
    private float screenRatioWidth, screenRatioHeight;
    private Background background1, background2;
    private Paint paint;

    public GameView(Context context, int screenWidth, int screenHeight) {
        super(context);

        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;

        screenRatioWidth = 1080f / screenWidth;
        screenRatioHeight = 1920f / screenHeight;

        background1 = new Background(screenWidth, screenHeight, getResources());
        background2 = new Background(screenWidth, screenHeight, getResources());

        background2.yPos = -screenHeight;

        paint = new Paint();
    }

    /*
     * Continuously loops through update(), draw(), and sleep() if the app is running
     * Overrides from Runnable class, which Thread automatically calls once resume() initializes said Thread
     */
    @Override
    public void run() {

        while(isRunning) {
            update();
            draw();

            // creates 30 millisecond delay in between draw() and update()
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*
     * updates position of backgrounds.
     * checks if either background is offscreen. If so, it places that background above the other
     * giving the illusion of an infinitely moving background.
     */
    private void update() {
        background1.yPos += 5 * (int) screenRatioHeight;
        background2.yPos += 5 * (int) screenRatioHeight;

        if(background1.yPos > screenHeight) {
            background1.yPos = -screenHeight;
        }

        if(background2.yPos > screenHeight) {
            background2.yPos = -screenHeight;
        }
    }

    /*
     * draws both backgrounds onto the screen given their initialized
     */
    private void draw() {
        if(getHolder().getSurface().isValid()){
            Canvas canvas = getHolder().lockCanvas();
            canvas.drawBitmap(background1.background, background1.xPos, background1.yPos, paint);
            canvas.drawBitmap(background2.background, background2.xPos, background2.yPos, paint);

            getHolder().unlockCanvasAndPost(canvas);
        }
    }

    /*
     * https://developer.android.com/reference/java/lang/Thread#start()
     * initializes a thread and starts it, automatically calling the overridden run() method above.
     */
    public void resume() {

        isRunning = true;
        thread = new Thread(this);
        thread.start();

    }

    /*
     * https://developer.android.com/reference/java/lang/Thread#join()
     * waits for thread to die if game is paused
     */
    public void pause() {

        try {
            isRunning = false;
            thread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
 No newline at end of file
Loading