Commit d7e8c15c authored by Ankita Tiwari's avatar Ankita Tiwari
Browse files

merged ChalkGame

parent cf147013
Loading
Loading
Loading
Loading
+17 −14
Original line number Diff line number Diff line
@@ -4,34 +4,37 @@ import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class ChalkGameActivity extends AppCompatActivity {
    private ChalkGameClass gameView;
    private Button StartGameAgain;
    private Handler handler = new Handler();
    private final static long Interval = 50;

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

        gameView = new ChalkGameClass(this);
        setContentView(R.layout.activity_chalk_game);

        StartGameAgain = (Button) findViewById(R.id.play_again_btn);

        StartGameAgain.setOnClickListener(new View.OnClickListener()
        {
        setContentView(gameView);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void onClick(View v)
            {
                Intent mainIntent  = new Intent(ChalkGameActivity.this, ChalkActivity.class);
                mainIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(mainIntent);
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        gameView.invalidate();
                    }

                });
            }
        }, 0, Interval);

    }

}
 No newline at end of file
+158 −12
Original line number Diff line number Diff line
@@ -8,45 +8,191 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class ChalkGameClass extends View {
    private Bitmap BackgroundImage;
    //PLAYER
    private Bitmap player[] = new Bitmap[2];
    private int playerX = 10;
    private int playerY;
    private int playerSpeed;
    private boolean touch = false;
    private Paint redPaint = new Paint();
    private int jumpcount;

    public ChalkGameClass(Context context) {
    //CANVAS
    private int canvasWidth, canvasHeight;
    private Bitmap backgroundImage ;

    //CHALK
    private Bitmap chalk[] = new Bitmap[5];
    private int chalkNum = 7;
    private int chalkCol;
    private int[] chalkX = new int[chalkNum];
    private int[] chalkY = new int[chalkNum];
    private int chalkXSpeed = 20;
    //private int chalkYSpeed = -5;

    //HEALTH SYSTEM
    private Bitmap lives[] = new Bitmap[2];
    private int lifecounter;
    private Paint lostLifePaint = new Paint();


    public ChalkGameClass(Context context)
    {
        super(context);
        BackgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.background);
        redPaint.setColor(Color.RED);
        redPaint.setAntiAlias(false);
        player[0] = BitmapFactory.decodeResource(getResources(), R.drawable.girl);
        player[1] = BitmapFactory.decodeResource(getResources(), R.drawable.girl_jump);
        playerY = 650;
        jumpcount = 0;

        backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.background);

        //Initializing the chalk array
        chalk[0] = BitmapFactory.decodeResource(getResources(), R.drawable.green_chalk);
        chalk[1] = BitmapFactory.decodeResource(getResources(), R.drawable.yellow_chalk);
        chalk[2] = BitmapFactory.decodeResource(getResources(), R.drawable.red_chalk);
        chalk[3] = BitmapFactory.decodeResource(getResources(), R.drawable.white_chalk);
        chalk[4] = BitmapFactory.decodeResource(getResources(), R.drawable.blue_chalk);

        lives[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hearts);
        lives[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_grey);
        lifecounter = 3;

        lostLifePaint.setColor(Color.WHITE);
        lostLifePaint.setAntiAlias(false);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvasWidth = canvas.getWidth();
        canvasHeight = canvas.getHeight();

        if (backgroundImage == null) return;
        // Use the same Matrix over and over again to minimize
        // allocation in onDraw.
        Matrix matrix = new Matrix();

        float vw = canvasWidth;
        float vh = canvasHeight;
        float bw = (float) backgroundImage.getWidth ();
        float bh = (float) backgroundImage.getHeight ();

        //canvas.drawBitmap(BackgroundImage, 0, 0, null);
        // First scale the bitmap to fit into the view.
        float s1x = vw / bw;
        float s1y = vh / bh;
        matrix.postScale (s1x, s1y);

        canvas.drawBitmap(backgroundImage, matrix, null);

        int minplayerY = 0;
        int maxplayerY = canvasHeight - player[0].getHeight();

        //PLAYER MECHANICS
        playerY = playerY + playerSpeed;

        if (playerY < minplayerY) {
            playerY = minplayerY;
            touch = false;
            playerSpeed = 125;
        }       //limiting the player's y to the top of the screen

        if (playerY > maxplayerY) {
            playerY = maxplayerY;
            touch = false;
        }       //limiting the player's y tot he bottom of the screen
        playerSpeed = playerSpeed + 2; //gravity

        if (touch) { //jumping animation
            if (jumpcount > 0) { //double jump
                canvas.drawBitmap(player[0], playerX, playerY, null);
                jumpcount = 0;
            } else
                canvas.drawBitmap(player[1], playerX, playerY, null);

        } else {
            canvas.drawBitmap(player[0], playerX, playerY, null);
        }

        //CHALK MECHANICS

        for(int ii = 0; ii < chalkNum; ii ++) {
            if (hitChalkChecker(chalkX[ii], chalkY[ii])) //if player collides with chalk
            {
                chalkX[ii] = chalkX[ii] - 200; //makes chalk disappear
                lifecounter--; //reduces health;

        if (touch) {
            canvas.drawCircle(30, 30, 100, redPaint);
            Intent mainIntent = new Intent(getContext(), ChalkGameActivity.class);
                if(lifecounter == 0)  //if all lives used up
                {
                    Intent mainIntent  = new Intent(getContext(), ChalkActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    getContext().startActivity(mainIntent);

                }
            }
            chalkX[ii] = chalkX[ii] - chalkXSpeed; //making the chalk move across the screen


            if (chalkX[ii] < 0) {
                chalkX[ii] = canvasWidth + (ii * 200);
                if(ii == chalkNum -1)
                    chalkXSpeed += 2;
                chalkY[ii] = (int) Math.floor(Math.random() * ((maxplayerY + player[0].getHeight()) - chalk[0].getHeight())) + chalk[0].getHeight(); //making the chalk appear at random  heights
            }
            chalkCol = ii;
            while(chalkCol > 4) //keeping chalkCol between 0 and 4;
            {
                chalkCol-= 5;
            }
            //chalkY[ii] = chalkY[ii] - chalkYSpeed; //gravity
            canvas.drawBitmap(chalk[chalkCol], chalkX[ii], chalkY[ii],  null);
        }

        //Health System Display

        for(int ii = 0; ii < 3; ii++)
        {
            int lifeX = (int) (1550 + lives[0].getWidth() * 1.5 * ii);
            int lifeY = 30;

            if(ii < lifecounter)
            {
                canvas.drawBitmap(lives[0], lifeX, lifeY, null); //drawing full heart
            }
            else
            {
                canvas.drawBitmap(lives[1], lifeX, lifeY, lostLifePaint); //drawing lost heart
            }
        }

    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            touch = true;
            playerSpeed = -30;
            jumpcount ++;
        }
        return true;
    }

    public boolean hitChalkChecker(int x, int y)
    {
        if(playerX < x && x < (playerX + player[0].getWidth())
                && playerY < y && y < (playerY + player[0].getHeight()))
        {
            return true;
        }
        return false;
    }
}