3D Car Game
3D Car Game
DOCTYPE html>
<html>
<head>
<title>super Car Drive</title>
<meta name="viewport" content="width=device-width, initial-scale=1" >
</head>
<style>
body{
margin:0;
height:100vh;
width:100vw;
}
#mainContainer {
position:absolute;
top:0;
left:0;
transform-origin:0% 0%;
width:100%;
transform: translateX(0px) rotate(0deg);
overflow: hidden;
}
#city {
position:absolute;
top:0;
left:0;
height:30vw;
width:100%;
z-index:-1;
display: block;
}
#myCanvas {
background:transparent;
z-index: 100;
}
.aImg {
display:none;
}
#mainContainer {
display:none;
}
#scoreDiv {
position:absolute;
right:0;
background:transparent;
display:inline-block;
border:3px solid red;
padding:3px 50px 3px 10px;
border-radius:20px;
font-weight:700;
font-size:24px;
height:30px;
margin:10px;
z-index:1000;
transform-origin:100% 0%;
}
#cn {
height:52px;
width:52px;
background:red;
position:absolute;
right:-8px;
top:-8px;
border-radius:50%;
}
.gameHome {
position:absolute;
top:0;
left:0;
background:#fff;
height:100vh;
width:100vw;
transform-origin:0% 0%;
text-align:center;
}
#gameTitle {
text-align:center;
font-family:cursive;
font-size:35px;
margin-top:50px;
font-weight:800;
}
#playBtn {
font-size:20px;
display:inline-block;
background:#1642ea;
color:#fff;
padding:3px 25px;
margin-top: 100px;
border:none;
border-radius:10px;
box-shadow:3px 4px #000;
outline:none;
}
#gameConf {
width:50%;
height:100%;
background:red;
}
#gameDifficulty {
width:100%;
height:50%;
background:blue;
padding:0;
margin:0;
}
.diffOpt {
background:green;
font-family:cursive;
font-size:20px;
text-align:center;
}
#loading {
height:60px;
width:60px;
background:transparent;
display:inline-block;
border:10px solid transparent;
border-bottom-color:red;
border-left-color:red;
border-radius:50%;
animation:ld 1s linear infinite;
}
@keyframes ld {
0% {
transform:translateY(-165px) rotate(0deg);
}
100% {
transform:translateY(-165px) rotate(360deg);
}
}
</style>
<body onload="done()">
<div id="scoreDiv">
<span id="score">0</span>
<img src="https://image.ibb.co/jRAp0y/coin.png" id="cn">
</div>
<div class="gameHome" id="startPage">
<div id="gameTitle">3D Car Drive</div>
<button onclick="ld()" id="playBtn"><span style="font-
size:18px">Loading Assets<br>Please Wait...<br>This may take a few seconds for
first time</span></button><br>
<div id="ldc"><div id="loading"></div></div>
<script type="text/javascript">
(function(){
"use strict"
var hw = window.innerWidth || 360;
var hh = window.innerHeight || 560;
var gameHome = document.getElementById("startPage");
var scoreDiv = document.getElementById("scoreDiv");
if(hh > hw){
gameHome.style.height = hw+"px";
gameHome.style.width = hh+"px";
gameHome.style.transform = "translateX("+hw+"px)
rotate(90deg)";
scoreDiv.style.bottom = 0;
scoreDiv.style.transform = "rotate(90deg)
translateX(40px)";
}
})();
</script>
</div>
var ld;
var loaded = false;
function done(){
document.getElementById("ldc").innerHTML="";
document.getElementById("ldc").style.display = "none";
document.getElementById("playBtn").innerHTML = "PLAY";
loaded = true;
}
(function(){
var w = window.innerWidth || 360;
var h = window.innerHeight || 560;
document.getElementById("city").style.height = h*.3+"px";
document.getElementById("city").style.width = w+"px";
var c = document.getElementById("myCanvas");
c.height = h;
c.width = w;
function loadGame(){
"use strict";
function TreeBuilder(src,src2,start,left){
this.src = treeSrc[src];
this.src2 = treeSrc[src2];
this.y = start;
this.x = 0;
this.h = 0;
this.w = 0;
this.dy = 0.01;
this.r = 1.009;
this.left = left;
}
TreeBuilder.prototype.draw = function(){
this.y += this.dy;
this.dy *= this.r;
this.x = (h-this.y)*roadConstant - this.w - this.w*this.left;
this.h = (roadLeft-this.x-this.w*this.left)*treeCnst;
this.w = this.h*2/3;
ctx.drawImage(this.src,this.x,this.y-this.h,this.w,this.h);
ctx.drawImage(this.src2,w-this.x-this.w,this.y-
this.h,this.w,this.h);
function _i(x){
return document.getElementById(x);
}
var treeSrc = [_i("t1"),_i("t2"),_i("t3"),_i("t4")];
var trees = [];
for(var n = 0; n < ((h*0.7)/50-2); n++){
trees.push(new
TreeBuilder(Math.floor(Math.random()*4),Math.floor(Math.random()*4),stp+n*50,2));
}
function CarBuilder(src,start,lane){
this.src = carSrc[src];
this.y = start;
this.x = 0;
this.h = 0;
this.w = 0;
this.dy = 0.5;
this.lane = lane;
}
CarBuilder.prototype.draw = function(){
this.dy *= 1.01;
this.y += this.dy;
this.x = (carWCnst/2)*(h-this.y)+(w-(carWCnst*(h-
this.y)))*this.lane/8;
this.w = carW-carW*carWCnst*(h-this.y)/w;
this.h = 1.7*this.w/3;
ctx.drawImage(this.src,this.x,this.y-this.h,this.w,this.h);
if(this.y >= h-20){
if(Math.abs(this.x-cx) <= carH && Math.abs(this.y-h+carH) <=
carH){
clearInterval(intv);
setTimeout(function(){
document.getElementById("mainContainer").style.display
= "none";
document.getElementById("startPage").style.display =
"block";
},1000);
}
}
if(this.y >= h+100){
this.y = stp;
this.h = 0;
this.w = 0;
this.left = Math.random()*3;
this.dy = 0.5;
this.lane = 1+Math.random()*5;
}
}
CoinBuilder.prototype.draw = function(){
this.dy *= 1.01;
this.y += this.dy;
this.x = (carWCnst/2)*(h-this.y)+(w-(carWCnst*(h-
this.y)))*this.lane/8;
this.w = coinW-coinW*carWCnst*(h-this.y)/w;
this.h = this.w;
ctx.drawImage(this.src,this.x,this.y-this.h,this.w,this.h);
if(this.y >= h-20){
if(Math.abs(this.x-cx) <= coinW && Math.abs(this.y-h+coinW) <=
coinW){
this.y = stp;
this.h = 0;
this.w = 0;
this.left = Math.random()*3;
this.dy = 0.5;
this.lane = Math.floor(1+Math.random()*5);
updateScore(1);
}
}
if(this.y >= h+100){
this.y = stp;
this.h = 0;
this.w = 0;
this.left = Math.random()*3;
this.dy = 0.5;
this.lane = Math.floor(1+Math.random()*5);
}
}
//End Coin...
function rectPoints(n,ho){
n = totalRso-n-1;
var y1 = stp+maxH*cnst1*(Math.pow(1/ratio,n)-1);
var x1 = roadLeft-roadConstant*(y1-stp);
var y2 = y1;
var x2 = x1 + minWidth+(y1-stp)*dif/totalHeight;
var y3 = y1 + maxH*cnst1*(Math.pow(1/ratio,n+1)-1);
var x3 = roadLeft-roadConstant*(y3-stp);
var y4 = y3;
var x4 = x3 + minWidth+(y3-stp)*dif/totalHeight;
return [x1,y1,x2,y2,x4,y4,x3,y3];
}
function draw(){
ctx.beginPath();
ctx.moveTo((w-roadWidth)/2,stp);
ctx.lineTo((w-roadWidth)/2+roadWidth,stp);
ctx.lineTo(w,h);
ctx.lineTo(0,h);
ctx.fillStyle="#555";
ctx.fill();
ctx.closePath();
for(var n = 0; n < totalRso; n++){
ctx.beginPath();
ctx.moveTo(rso[n][0],rso[n][1]);
ctx.lineTo(rso[n][2],rso[n][3]);
ctx.lineTo(rso[n][4],rso[n][5]);
ctx.lineTo(rso[n][6],rso[n][7]);
ctx.lineTo(rso[n][0],rso[n][1]);
ctx.lineWidth = 2;
ctx.fillStyle = rso[n]
[8];//"rgb("+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+","+Ma
th.floor(Math.random()*255)+")";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(w-rso[n][0],rso[n][1]);
ctx.lineTo(w-rso[n][2],rso[n][3]);
ctx.lineTo(w-rso[n][4],rso[n][5]);
ctx.lineTo(w-rso[n][6],rso[n][7]);
ctx.lineTo(w-rso[n][0],rso[n][1]);
ctx.lineWidth = 2;
ctx.fillStyle = rso[n]
[8];//"rgb("+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+","+Ma
th.floor(Math.random()*255)+")";
ctx.fill();
ctx.closePath();
}
var cx = (w-carW)/2;
var cl = false, cr = false;
var car = _i("c1");
var ms = 3*w/560;
function drawCar(){
if(cl) if(cx+carW+50 < w) cx+=ms;
if(cr) if(cx-50 > 0) cx-=ms;
ctx.drawImage(car,cx,h-carH,carW,carH);
}
var m = 0;
var intv = setInterval(function(){
try{
ctx.clearRect(0,0,w,h);
maxH+=0.5;
changedHeight =
maxH*cnst1*(Math.pow(1/ratio,totalRso-1)-1);//maxH*(1-Math.pow(ratio,totalRso-5))/
(1-ratio);
if(changedHeight >= totalHeight){
maxH = maxHF;
m++;
}
for(var n = 0; n < totalRso; n++){
rso[n]=rectPoints(n,h-totalHeight+changedHeight);
if(m%2==0) rso[n][8] = (n%2==0) ? "#000" : "#fff";
else rso[n][8] = (n%2==1) ? "#000" : "#fff";
}
draw();
for(var n = 0; n < trees.length; n++){
trees[n].draw();
}
drawCar();
}catch(err){
},10)
//draw();
//Game Control
//Touch
function getTouch(e){
e.preventDefault();
var to = e.changedTouches[0];
var ty = parseInt(to.clientY);
if(ty>(h/2)){
cl = true;
}
else{
cr = true;
}
}
function getTouchEnd(){
cl = false;
cr = false;
}
c.removeEventListener("touchstart",getTouch);
c.removeEventListener("touchend",getTouchEnd);
c.addEventListener("touchstart",getTouch);
c.addEventListener("touchend",getTouchEnd);
//Key..
function getKey(e){
e.preventDefault();
var ty = e.keyCode;
if(ty===39){
cr = false;
cl = true;
}
else if(ty===37){
cl = false;
cr = true;
}
}
function getKeyEnd(e){
var ty = e.keyCode;
if(ty === 39) cl = false;
else if(ty === 37) cr = false;
}
document.body.removeEventListener("keydown",getKey);
document.body.removeEventListener("keyup",getKeyEnd);
document.body.addEventListener("keydown",getKey);
document.body.addEventListener("keyup",getKeyEnd);
//Accelarometre
function driveCar(e){
var y = e.accelerationIncludingGravity.y;
if(window.DeviceMotionEvent){
window.removeEventListener("devicemotion",driveCar)
window.addEventListener("devicemotion",driveCar,false)
}
//End
}
ld = function(){
if(loaded){
document.getElementById("startPage").style.display = "none";
document.getElementById("mainContainer").style.display = "block";
loadGame();
}
}
})();
</script>
</body>
</html>