forked from lovy003/First-PR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar game I made
219 lines (188 loc) · 4.72 KB
/
car game I made
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel = "stylesheet" href = "style.css">
</head>
<body>
<div id="grid">
<div id = "road">
<div id="lane1"></div>
<div id="lane2"></div>
<div id="lane3"></div>
</div>
</div>
<script src = "app.js" charset="utf-8"></script>
</body>
</html>
*{
margin: 0;
padding: 0;
}
#grid{
width: 800px;
margin: auto;
background-color: greenyellow;
height: 576px;
}
#road{
width: 480px;
height: 576px;
margin: auto;
background-color: grey;
position: relative;
overflow: hidden;
text-align: center;
}
#lane1,#lane2,#lane3{
border-right: white 1px solid;
width: 156px;
height: 576px;
display: inline-block;
margin: 0px;
padding: 0px;
position: relative;
}
#car{
width: 70px;
height: 100px;
background-color: red;
position:absolute;
}
.obstacle{
width: 70px;
height: 100px;
background-color: green;
position:absolute;
}
#score{
font-size: 60px;
color: white;
width: 90px;
height: 90px;
position: absolute;
}
#gameOver{
font-size: 120px;
background-color: aqua;
text-align: center;
}
#reset{
margin-top: 15vh;
font-size: 30px;
padding: 2px;
width: 50%;
}
let road = document.getElementById('road');
let lane1= document.getElementById('lane1');
let lane2= document.getElementById('lane2');
let lane3= document.getElementById('lane3');
let score = 0;
let carLeftSpace;
let carBottomSpace = 20;
let lane1left = 0 + 43;
let lane2left = 156 + 43;
let lane3left = 312 + 43;
let laneArray = [lane1left,lane2left,lane3left];
let carCount = 2;
let obstacles = [];
//let obstacleLeftSpace = laneArray[Math.floor(Math.random()*3 )];
function createCar(){
let car = document.createElement('div');
car.setAttribute('id','car');
road.appendChild(car);
car.style.bottom = carBottomSpace;
carLeftSpace = lane2left;
car.style.left = carLeftSpace;
}
function scoreArea(){
let scoreDiv = document.createElement('div');
scoreDiv.setAttribute('id','score');
road.appendChild(scoreDiv);
scoreDiv.style.bottom = 476 + 'px';
scoreDiv.style.left = lane2left;
scoreDiv.innerHTML = score;
}
function moveLeft(){
if(carLeftSpace>=lane2left){
carLeftSpace-=156;
car.style.left = carLeftSpace;
}
}
function moveRight(){
if(carLeftSpace<=lane2left){
carLeftSpace+=156;
car.style.left = carLeftSpace;
}
}
function control(e){
if(e.key==="ArrowLeft"){
moveLeft();
}else if(e.key==="ArrowRight"){
moveRight();
}
}
class obstacle{
constructor(obstacleBottomSpace){
this.bottom = obstacleBottomSpace;
this.left = laneArray[Math.floor(Math.random()*3 )];
this.obsCar = document.createElement('div');
let obsCar = this.obsCar;
obsCar.setAttribute('class','obstacle');
obsCar.style.left = this.left;
obsCar.style.bottom = this.bottom;
road.appendChild(obsCar);
}
}
function createObstacles(){
for(i=0;i<carCount;i++){
let carGap = 338;//576/carCount;
let obstacleBottomSpace = 500 + i*carGap;
let newObstacle = new obstacle(obstacleBottomSpace)
obstacles.push(newObstacle);
//console.log(obstacles);
}
}
function moveObstacles(){
obstacles.forEach(function(car){
car.bottom-=15;
let visual = car.obsCar;
visual.style.bottom = car.bottom;
if(car.bottom<=-130){
visual.removeAttribute('class');
obstacles.shift();
score++;
document.getElementById('score').innerHTML= score;
let newObstacle = new obstacle(576);
obstacles.push(newObstacle);
}
if(carLeftSpace<=car.left+70
&&carBottomSpace<=car.bottom+100
&&carBottomSpace+100>=car.bottom
&&carLeftSpace>=car.left){
gameOver();
}
})
}
function restart(){
window.location.reload();
}
function gameOver(){
let grid = document.getElementById("grid")
let body = document.querySelector('body')
grid.style.visibility = "hidden";
body.setAttribute("id","gameOver");
body.innerHTML = 'score: '+ score + '<br><button id = "reset">Reset</button>';
//body.innerHTML(score)
let s = document.querySelector('#reset'); //restart button
s.addEventListener('click',restart);
}
function start(){
createCar();
scoreArea();
document.addEventListener('keydown',control);
createObstacles();
setInterval(moveObstacles,30);
}
start();