Car racing game in Sketchware

1. In main.xml, add two TextViews text_score and text_hs. Also add a Button button1.


2. Add a SharedPreferences component sp:sp, and an Intent component intent.

3. Add onStart event and put blocks as shown below.


4. Create a new page game.xml.

5. In the event button1 onClick, use Intent to move to GameActivity.


6. Add two images in drawable folder with names car_yellow and car_blue.
In Resource manager, in drawable folder, create a new file car_yellow.xml and put following codes in it.

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="400dp"
    android:viewportWidth="80"
    android:viewportHeight="160">

    <path
        android:fillColor="#FFEB3B"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="
            M8,16
            Q16,8 24,8
            L56,8
            Q64,8 72,16
            L72,52
            Q71,53 70,54
            L70,108
            Q71,109 72,110
            L72,144
            Q68,152 64,152
            L16,152
            Q12,148 8,144
            L8,110
            Q9,109 10,108
            L10,54
            Q9,53 8,52
            L8,16
        " />
        
    <path
        android:fillColor="#FFEB3B"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="M22,64 h36 v54 h-36z
        " />
        
    <path
        android:fillColor="#FFEB3B"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="M22,64 L12,40 v98 L22,118Z
        " />
        
    <path
        android:fillColor="#FFEB3B"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="M58,64 L68,40 v98 L58,118Z
        " />
        
    <path
        android:fillColor="#00000000"
        android:strokeColor="#000000"
        android:strokeWidth="1"
        android:strokeLineCap="round"
        android:strokeLineJoin="round"
        android:pathData="M12,40
                          A30,10 0 0,1 68,40" />
        
    <path
        android:fillColor="#00000000"
        android:strokeColor="#000000"
        android:strokeWidth="1"
        android:strokeLineCap="round"
        android:strokeLineJoin="round"
        android:pathData="M12,138
                          A36,10 0 0,0 68,138" />
         
</vector>


In Resource manager, in drawable folder, create a new file car_blue.xml and put following codes in it (Change package name to package name of your own project).

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="400dp"
    android:viewportWidth="80"
    android:viewportHeight="160">

    <path
        android:fillColor="#8080F3"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="
            M8,16
            Q16,8 24,8
            L56,8
            Q64,8 72,16
            L72,52
            Q71,53 70,54
            L70,108
            Q71,109 72,110
            L72,144
            Q68,152 64,152
            L16,152
            Q12,148 8,144
            L8,110
            Q9,109 10,108
            L10,54
            Q9,53 8,52
            L8,16
        " />
        
    <path
        android:fillColor="#8080F3"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="M22,64 h36 v54 h-36z
        " />
        
    <path
        android:fillColor="#808080"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="M22,64 L12,40 v98 L22,118Z
        " />
        
    <path
        android:fillColor="#808080"
        android:strokeColor="#222222"
        android:strokeWidth="1"
        android:strokeLineJoin="round"
        android:strokeLineCap="round"
        android:pathData="M58,64 L68,40 v98 L58,118Z
        " />
        
    <path
        android:strokeColor="#000000"
        android:strokeWidth="1"
        android:strokeLineCap="round"
        android:strokeLineJoin="round"
        android:pathData="M12,40
                          A30,10 0 0,1 68,40" />
        
    <path
        android:strokeColor="#000000"
        android:strokeWidth="1"
        android:strokeLineCap="round"
        android:strokeLineJoin="round"
        android:pathData="M12,138
                          A36,10 0 0,0 68,138" />
         
</vector>


7. In Java/Kotlin manager, create a new Java file RaceGameView.java. Put following codes in it.

package com.sanju.racing;

import android.content.Context;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.HashMap;

public class RaceGameView extends View {
    private Paint myPaint;
    private int speed = 1;
    private int time = 0;
    private int score = 0;
    private int myCarPosition = 0;
    private ArrayList<HashMap<String, Object>> otherCars = new ArrayList<>();
    private SharedPreferences sp;
    private int highscore = 0;

    int viewWidth = 0;
    int viewHeight = 0;

    public RaceGameView(Context context) {
        super(context);
        init(context);
    }

    public RaceGameView(Context context, android.util.AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RaceGameView(Context context, android.util.AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        myPaint = new Paint();
        sp = context.getSharedPreferences("sp", Context.MODE_PRIVATE);
        highscore = Integer.parseInt(sp.getString("hs", "0"));
    }

    private int getRandom(int min, int max) {
        return (int) (Math.random() * (max - min + 1)) + min;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        viewWidth = this.getMeasuredWidth();
        viewHeight = this.getMeasuredHeight();

        if (time % 700 < 10 + speed) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("lane", getRandom(0, 2));
            map.put("startTime", time);
            otherCars.add(map);
        }

        time = time + 10 + speed;

        int carWidth = viewWidth / 5;
        int carHeight = carWidth + 10;

        android.graphics.drawable.Drawable d = getResources().getDrawable(R.drawable.car_blue, null);
        d.setBounds((myCarPosition * viewWidth / 3) + viewWidth / 15 + 25, viewHeight - 2 - carHeight, 
                   (myCarPosition * viewWidth / 3) + (viewWidth / 15) + carWidth - 25, viewHeight - 2);
        d.draw(canvas);

        for (int i = 0; i < otherCars.size(); i++) {
            int carX = ((int) otherCars.get(i).get("lane") * viewWidth / 3) + viewWidth / 15;
            int carY = time - (int) otherCars.get(i).get("startTime");

            android.graphics.drawable.Drawable d2 = getResources().getDrawable(R.drawable.car_yellow, null);
            d2.setBounds(carX + 25, carY - carHeight, carX + carWidth - 25, carY);
            d2.draw(canvas);

            if ((int) otherCars.get(i).get("lane") == myCarPosition) {
                if ((carY > viewHeight - 2 - carHeight) && (carY < viewHeight - 2 + carHeight)) {
                    sp.edit().putString("hs", String.valueOf(highscore)).apply();
                    sp.edit().putString("score", String.valueOf(score)).apply();
                    finishGame();
                    return;
                }
            }

            if (carY > viewHeight + carHeight) {
                otherCars.remove(i);
                score++;
                speed = 1 + Math.abs(score / 8);
                if (score > highscore) {
                    highscore = score;
                }
            }
        }

        myPaint.setColor(Color.WHITE);
        myPaint.setTextSize(40);
        canvas.drawText("Score: " + score, 80f, 80f, myPaint);
        canvas.drawText("Speed: " + speed, 380f, 80f, myPaint);

        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x1 = event.getX();
                if (x1 < viewWidth / 2) {
                    if (myCarPosition > 0) {
                        myCarPosition--;
                    }
                }
                if (x1 > viewWidth / 2) {
                    if (myCarPosition < 2) {
                        myCarPosition++;
                    }
                }
                invalidate();
                break;
        }
        return true;
    }
    
    private void finishGame() {
        if (getContext() instanceof Activity) {
            Activity activity = (Activity) getContext();
            activity.finish();
        }
    }
}

8. In game.xml, add a LinearLayout linear1. Set it's width and height to match_parent, set it's padding to 0, and convert it to com.sanju.racing.RaceGameView. (Change package name to package name of your own project), and set background color to Red (or anything not white since the color of text for displaying score is White).

9. Save and run the project.


Comments

Popular posts from this blog

Simple car racing android game in Sketchware

Simple Audio recorder app in Sketchware

How to enable upload from webview in Sketchware?

Retrieve contact list in Sketchware

Creating a Drawing View in Sketchware