Commit e43d3973 authored by Thomas Poimenidis's avatar Thomas Poimenidis
Browse files

implented bounding box for Player

parent 8089e325
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@ public class Bullet extends Sprite {
    /**
     * Creates Bullet object using Sprite constructor
     */
    public Bullet(int xPos, int yPos, int xVel, int yVel, Resources res) {
        super(xPos, yPos, xVel, yVel);
    public Bullet(int xPos, int yPos, int xVel, int yVel, int screenWidth, int screenHeight, Resources res) {
        super(xPos, yPos, xVel, yVel, screenWidth, screenHeight);
        spriteImage = BitmapFactory.decodeResource(res, R.drawable.red);
        spriteImage = Bitmap.createScaledBitmap(spriteImage, screenWidth, screenHeight, false);
    }
+2 −2
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ public class Enemy extends Sprite{
     * @param xVel X velocity
     * @param yVel Y velocity
     */
    public Enemy(int xPos, int yPos, int xVel, int yVel) {
        super(xPos, yPos, xVel, yVel);
    public Enemy(int xPos, int yPos, int xVel, int yVel, int screenWidth, int screenHeight) {
        super(xPos, yPos, xVel, yVel, screenWidth, screenWidth);

    }

+4 −2
Original line number Diff line number Diff line
@@ -28,8 +28,6 @@ public class GameView extends SurfaceView implements Runnable{
     */
    public GameView(Context context, int screenWidth, int screenHeight) {
        super(context);
        joystick = new Joystick(screenWidth/2,screenHeight*9/10,130,40);
        p1 = new Player(screenWidth/2,screenHeight*7/10,0,0, getResources());
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;

@@ -42,6 +40,9 @@ public class GameView extends SurfaceView implements Runnable{
        background2.yPos = -screenHeight;

        paint = new Paint();

        joystick = new Joystick(screenWidth/2,screenHeight*9/10,130,40);
        p1 = new Player(screenWidth/2,screenHeight*7/10,0,0, screenWidth, screenHeight, getResources());
    }

    /*
@@ -84,6 +85,7 @@ public class GameView extends SurfaceView implements Runnable{
        if(background2.yPos > screenHeight) {
            background2.yPos = -screenHeight;
        }

        p1.update((int)joystick.XVelocity, (int)joystick.YVelocity);
        joystick.update(joystick);
    }
+16 −6
Original line number Diff line number Diff line
package com.example.a8_bitinvader;

import java.util.LinkedList;
import java.util.Queue;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


public class Player extends Sprite{

@@ -24,8 +20,8 @@ public class Player extends Sprite{
     * @param yVel Y velocity of player
     * @param res Resource object
     */
    public Player(int xPos, int yPos, int xVel, int yVel, Resources res) {
        super(xPos,yPos,xVel,yVel);
    public Player(int xPos, int yPos, int xVel, int yVel, int screenWidth, int screenHeight, Resources res) {
        super(xPos,yPos,xVel,yVel, screenWidth, screenHeight);
        this.xPos = xPos;
        this.yPos = yPos;
        spriteImage = BitmapFactory.decodeResource(res, R.drawable.player);
@@ -45,8 +41,22 @@ public class Player extends Sprite{
        xVel = joyStickInputX * SPEED_MULTIPLIER;
        yVel = joyStickInputY * SPEED_MULTIPLIER;

        // calls Sprite update(). Updates xPos and yPos.
        update();

        // checks if Player is offscreen. If so, repositions them within the screen.
        if(xPos < 0){
            xPos = 5;
        } else if(xPos + getSpriteWidth() > screenWidth){
            xPos = screenWidth - getSpriteWidth() - 5;
        }

        if(yPos < 0){
            yPos = 5;
        } else if(yPos + getSpriteHeight() > screenHeight){
            yPos = screenHeight - getSpriteHeight() - 5;
        }

    }

    /**
+5 −2
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ public class Sprite {
    Bitmap spriteImage;

    //private properties
    private final int spriteHeight = 160, spriteWidth = 160;
    private final int spriteHeight = 128, spriteWidth = 128;

    //private properties

@@ -32,12 +32,15 @@ public class Sprite {
     * @param xVel initial x velocity of Sprite
     * @param yVel initial y velocity of Sprite
     */
    public Sprite(int xPos, int yPos, int xVel, int yVel)
    public Sprite(int xPos, int yPos, int xVel, int yVel, int screenWidth, int screenHeight)
    {
        this.xPos = xPos;
        this.yPos = yPos;
        this.xVel = xVel;
        this.yVel = yVel;

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

    /**