Ad Code

Make Simple Car Racing Game Android Studio Java | Card Racing game source code for Android studio java

Hello Developers, Welcome to our blog. In this blog we are sharing an amazing Car Racing Game project for you with free of cost and open source project. You can open this project in your Android Studio easily. The project source code is available below. If you visited first time to our blog then bookmark it into your browser for future. Thank You





How to open this game project to our Android Studio ?

Create an new project in to your Android Studio. After created the project make files with the given name into your project. After created files Download all drawable resource which is given below in a zip file, move all files to your drawable folder in the project. Now, copy all the codes into the file you have created. After copied successfully run this project and you can also customise it according to your choice.

main.xml
<LinearLayout
	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"
	android:orientation="vertical">
	<LinearLayout
		android:id="@+id/linear1"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:padding="8dp"
		android:background="#000000"
		android:gravity="center_horizontal|center_vertical"
		android:orientation="vertical">
		<LinearLayout
			android:id="@+id/linear2"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:padding="8dp"
			android:background="#BDBDBD"
			android:gravity="left"
			android:orientation="horizontal">
			<TextView
				android:id="@+id/textview1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="The points:"
				android:textSize="12sp"
				android:textColor="#000000" />
			<TextView
				android:id="@+id/textview_on"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="TextView"
				android:textSize="12sp"
				android:textColor="#000000" />
		</LinearLayout>
		<LinearLayout
			android:id="@+id/linear3"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:padding="8dp"
			android:background="#757575"
			android:gravity="left"
			android:orientation="horizontal">
			<TextView
				android:id="@+id/textview3"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="Maximum degree:"
				android:textSize="12sp"
				android:textColor="#FFFFFF" />
			<TextView
				android:id="@+id/textview_two"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="TextView"
				android:textSize="12sp"
				android:textColor="#FFFFFF" />
		</LinearLayout>
		<LinearLayout
			android:id="@+id/linear4"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:padding="8dp"
			android:gravity="center_horizontal|center_vertical"
			android:orientation="horizontal">
			<Button
				android:id="@+id/button1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:text="start game"
				android:textSize="50sp"
				android:textColor="#000000" />
		</LinearLayout>
	</LinearLayout>
</LinearLayout>
MainActivity.java
package com.my.newproject160;

import android.app.Activity;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.text.style.*;
import android.util.*;
import android.webkit.*;
import android.animation.*;
import android.view.animation.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.text.*;
import org.json.*;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import android.content.SharedPreferences;
import android.content.Intent;
import android.net.Uri;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.DialogFragment;

public class MainActivity extends Activity {
	
	private LinearLayout linear1;
	private LinearLayout linear2;
	private LinearLayout linear3;
	private LinearLayout linear4;
	private TextView textview1;
	private TextView textview_on;
	private TextView textview3;
	private TextView textview_two;
	private Button button1;
	
	private SharedPreferences sp;
	private Intent i = new Intent();
	private AlertDialog.Builder sketchware;
	private Intent a = new Intent();
	
	@Override
	protected void onCreate(Bundle _savedInstanceState) {
		super.onCreate(_savedInstanceState);
		setContentView(R.layout.main);
		initialize(_savedInstanceState);
		initializeLogic();
	}
	
	private void initialize(Bundle _savedInstanceState) {
		linear1 = findViewById(R.id.linear1);
		linear2 = findViewById(R.id.linear2);
		linear3 = findViewById(R.id.linear3);
		linear4 = findViewById(R.id.linear4);
		textview1 = findViewById(R.id.textview1);
		textview_on = findViewById(R.id.textview_on);
		textview3 = findViewById(R.id.textview3);
		textview_two = findViewById(R.id.textview_two);
		button1 = findViewById(R.id.button1);
		sp = getSharedPreferences("sp", Activity.MODE_PRIVATE);
		sketchware = new AlertDialog.Builder(this);
		
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View _view) {
				i.setClass(getApplicationContext(), GameActivity.class);
				startActivity(i);
			}
		});
	}
	
	private void initializeLogic() {
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if (sp.getString("hs", "").equals("")) {
			textview_two.setText("00");
		}
		else {
			textview_two.setText(sp.getString("hs", ""));
		}
		if (sp.getString("ponto", "").equals("")) {
			textview_on.setText("00");
		}
		else {
			textview_on.setText(sp.getString("ponto", ""));
		}
	}
	
	@Deprecated
	public void showMessage(String _s) {
		Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
	}
	
	@Deprecated
	public int getLocationX(View _v) {
		int _location[] = new int[2];
		_v.getLocationInWindow(_location);
		return _location[0];
	}
	
	@Deprecated
	public int getLocationY(View _v) {
		int _location[] = new int[2];
		_v.getLocationInWindow(_location);
		return _location[1];
	}
	
	@Deprecated
	public int getRandom(int _min, int _max) {
		Random random = new Random();
		return random.nextInt(_max - _min + 1) + _min;
	}
	
	@Deprecated
	public ArrayList<Double> getCheckedItemPositionsToArray(ListView _list) {
		ArrayList<Double> _result = new ArrayList<Double>();
		SparseBooleanArray _arr = _list.getCheckedItemPositions();
		for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) {
			if (_arr.valueAt(_iIdx))
			_result.add((double)_arr.keyAt(_iIdx));
		}
		return _result;
	}
	
	@Deprecated
	public float getDip(int _input) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, _input, getResources().getDisplayMetrics());
	}
	
	@Deprecated
	public int getDisplayWidthPixels() {
		return getResources().getDisplayMetrics().widthPixels;
	}
	
	@Deprecated
	public int getDisplayHeightPixels() {
		return getResources().getDisplayMetrics().heightPixels;
	}
}
game.xml
<LinearLayout
	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"
	android:orientation="vertical">
	<LinearLayout
		android:id="@+id/linear1"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:padding="8dp"
		android:orientation="horizontal" />
</LinearLayout>
GameActivity.java
package com.my.newproject160;

import android.app.Activity;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.text.style.*;
import android.util.*;
import android.webkit.*;
import android.animation.*;
import android.view.animation.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.text.*;
import org.json.*;
import android.widget.LinearLayout;
import android.content.SharedPreferences;
import android.view.View;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.DialogFragment;

public class GameActivity extends Activity {
	
	private double highscore = 0;
	
	private LinearLayout linear1;
	
	private SharedPreferences sp;
	
	@Override
	protected void onCreate(Bundle _savedInstanceState) {
		super.onCreate(_savedInstanceState);
		setContentView(R.layout.game);
		initialize(_savedInstanceState);
		initializeLogic();
	}
	
	private void initialize(Bundle _savedInstanceState) {
		linear1 = findViewById(R.id.linear1);
		sp = getSharedPreferences("sp", Activity.MODE_PRIVATE);
		
		linear1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View _view) {
				
			}
		});
	}
	
	private void initializeLogic() {
		if (sp.getString("hs", "").equals("")) {
			highscore = 00;
		}
		else {
			highscore = Double.parseDouble(sp.getString("hs", ""));
		}
		GameView bcv = new
		GameView(this);
		linear1.addView(bcv);
		linear1.setBackgroundResource(R.drawable.images);
	}
	
	public void _axtre() {
	}
	
	public class GameView 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<>();
		
		int viewWidth = 0;
		int viewHeight = 0;
		
		public GameView(Context context){
			super(context);
			myPaint = new Paint();
		}
		
		@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;
			
			myPaint.setStyle(android.graphics.Paint.Style.FILL);
			
			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);
			
			myPaint.setColor(Color.GREEN);
			
			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){
						sp.edit().putString("hs", String.valueOf((int)highscore)).commit();
						sp.edit().putString("score", String.valueOf(score)).commit();
						finish();
					}}
				
				if (carY > viewHeight + carHeight){
					otherCars.remove(i);
					score++;
					speed = 1 + Math.abs(score/8);
					if (score > highscore){
						highscore = score;
					}
				}
			}
			
			myPaint.setColor(Color.BLACK);
			myPaint.setTextSize(40);
			canvas.drawText("Score: " + String.valueOf(score), 80f, 80f, myPaint);
			canvas.drawText("Speed: " + String.valueOf(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;
				case MotionEvent.ACTION_UP:
				break;
			}
			return true;
			
			
		}
		
		
	}
	
	
	@Deprecated
	public void showMessage(String _s) {
		Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
	}
	
	@Deprecated
	public int getLocationX(View _v) {
		int _location[] = new int[2];
		_v.getLocationInWindow(_location);
		return _location[0];
	}
	
	@Deprecated
	public int getLocationY(View _v) {
		int _location[] = new int[2];
		_v.getLocationInWindow(_location);
		return _location[1];
	}
	
	@Deprecated
	public int getRandom(int _min, int _max) {
		Random random = new Random();
		return random.nextInt(_max - _min + 1) + _min;
	}
	
	@Deprecated
	public ArrayList<Double> getCheckedItemPositionsToArray(ListView _list) {
		ArrayList<Double> _result = new ArrayList<Double>();
		SparseBooleanArray _arr = _list.getCheckedItemPositions();
		for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) {
			if (_arr.valueAt(_iIdx))
			_result.add((double)_arr.keyAt(_iIdx));
		}
		return _result;
	}
	
	@Deprecated
	public float getDip(int _input) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, _input, getResources().getDisplayMetrics());
	}
	
	@Deprecated
	public int getDisplayWidthPixels() {
		return getResources().getDisplayMetrics().widthPixels;
	}
	
	@Deprecated
	public int getDisplayHeightPixels() {
		return getResources().getDisplayMetrics().heightPixels;
	}
}
AndroidManifest.xml

<manifest
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.my.newproject160">
	<application
		android:allowBackup="true"
		android:icon="@drawable/app_icon"
		android:label="@string/app_name"
		android:name=".SketchApplication"
		android:usesCleartextTraffic="true"
		android:theme="@style/AppTheme">
		<activity
			android:name=".MainActivity"
			android:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout"
			android:hardwareAccelerated="true"
			android:supportsPictureInPicture="true"
			android:screenOrientation="portrait">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity
			android:name=".GameActivity"
			android:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout"
			android:hardwareAccelerated="true"
			android:supportsPictureInPicture="true"
			android:theme="@style/FullScreen"
			android:screenOrientation="portrait"
			android:windowSoftInputMode="stateHidden" />

	</application>
</manifest>

Here I have putted drawable images ito a zip file. You can download this images by Click Here

Thank you for visiting our blog and hope that you have enjoyed this Article. Thank You

Post a Comment

0 Comments

Ad Code