Commit 11d5f802 authored by Ankita Tiwari's avatar Ankita Tiwari
Browse files

added pause to ChalkGame

parent 12e53ae0
Loading
Loading
Loading
Loading
+66 −15
Original line number Diff line number Diff line
package com.example.game2d;

import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
@@ -12,9 +10,6 @@ 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 {
    //PLAYER
@@ -36,13 +31,23 @@ public class ChalkGameClass extends View {
        private int[] chalkX = new int[chalkNum];
        private int[] chalkY = new int[chalkNum];
        private int chalkXSpeed = 20;
    //private int chalkYSpeed = -5;
        private int chalkPassed;

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

    private Bitmap pauseButton;
        private int pauseX = 20;
        private int pauseY = 20;
    private boolean pauseTouch = false;

    private Bitmap hintButton;
        private int hintX = 110;
        private int hintY = 20;



    public ChalkGameClass(Context context)
    {
@@ -60,6 +65,7 @@ public class ChalkGameClass extends View {
        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);
        chalkPassed = 0;

        lives[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hearts);
        lives[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_grey);
@@ -68,6 +74,9 @@ public class ChalkGameClass extends View {
        lostLifePaint.setColor(Color.WHITE);
        lostLifePaint.setAntiAlias(false);

        pauseButton = BitmapFactory.decodeResource(getResources(), R.drawable.pause);
        hintButton = BitmapFactory.decodeResource(getResources(), R.drawable.question);

    }

    @Override
@@ -94,7 +103,8 @@ public class ChalkGameClass extends View {
        canvas.drawBitmap(backgroundImage, matrix, null);

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

        //PLAYER MECHANICS
        playerY = playerY + playerSpeed;
@@ -132,8 +142,8 @@ public class ChalkGameClass extends View {

                if(lifecounter == 0)  //if all lives used up
                {
                    Intent mainIntent  = new Intent(getContext(), ChalkActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    Intent mainIntent = new Intent(getContext(), MainActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    getContext().startActivity(mainIntent);

                }
@@ -141,12 +151,23 @@ public class ChalkGameClass extends View {
            chalkX[ii] = chalkX[ii] - chalkXSpeed; //making the chalk move across the screen


            if (chalkX[ii] < 0) {
            if (chalkX[ii] < 0)
            {
                chalkPassed++;
                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
            }

            if(chalkPassed == chalkNum * 3)
            {
                chalkX[ii] = chalkX[ii] - 200;
                chalkPassed = 0;
                Intent chalkQuestionIntent  = new Intent(getContext(), ChalkActivity.class);
                chalkQuestionIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                getContext().startActivity(chalkQuestionIntent);
            }
            chalkCol = ii;
            while(chalkCol > 4) //keeping chalkCol between 0 and 4;
            {
@@ -173,10 +194,24 @@ public class ChalkGameClass extends View {
            }
        }

        //PAUSE BUTTON
        canvas.drawBitmap(pauseButton, pauseX, pauseY, null);
        if (pauseTouch == true)
        {
            pauseTouch = false;
            Intent pauseIntent = new Intent(getContext(), ChalkGamePauseWindow.class);
            pauseIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            getContext().startActivity(pauseIntent);

        }

        canvas.drawBitmap(hintButton,hintX, hintY, null);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        pauseTouch = onSingleTap(event);
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            touch = true;
@@ -195,4 +230,20 @@ public class ChalkGameClass extends View {
        }
        return false;
    }

    public boolean onSingleTap(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        if ((pauseX < x && x < (pauseX + pauseButton.getWidth()) //if user touches pause button
                && pauseY < y && y < (pauseY + pauseButton.getHeight())) ||

                ( hintX < x && x < (hintX + hintButton.getWidth()) // or if user touches hint button
                    && hintY < y && y < (hintY + hintButton.getHeight())) )
        {
            return true;
        }

        return false;
    }
}
+31 −0
Original line number Diff line number Diff line
package com.example.game2d;

import androidx.appcompat.app.AppCompatActivity;

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

public class ChalkGamePauseWindow extends AppCompatActivity {
    private Button resumeButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pause_window);

        resumeButton = findViewById(R.id.chalkGameResumeButton);

        resumeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                Intent resumeIntent = new Intent(ChalkGamePauseWindow.this, ChalkGameActivity.class);
                resumeIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(resumeIntent);
            }
        });
    }

}
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".pauseWindow">

</androidx.constraintlayout.widget.ConstraintLayout>
 No newline at end of file