-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyFirstGame.html
270 lines (230 loc) · 8.16 KB
/
MyFirstGame.html
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html>
<html>
<head>
<title>躲汽车</title>
<meta charset="utf-8">
<script type="text/javascript" src="./jquery-1.10.2.min.js"> </script>
<style type="text/css">
#main{width: 800px; height: 900px; background:#fff; }
.control{padding-left: 120px;}
</style>
</head>
<body>
<div id="background">
<div id="head">
<img src="" alt=""/>
<span>Score:</span>
<span id="score">0</span>
<span>High Score:</span>
<span id="highscore">0</span>
</div>
<canvas id="main" >浏览器不支持canvas功能</canvas>
<div class="control">
<button id="start">开始</button>
<button id="left">左</button>
<button id="right">右</button>
</div>
</div>
</body>
<script type="text/javascript">
var GameParam = {};
GameParam.position = 1;
GameParam.score = 0;
if(localStorage.getItem("highscore"))
{
GameParam.highestscore =localStorage.getItem("highscore");
}
else{
GameParam.highestscore = 0;
}
$("#highscore").html(GameParam.highestscore);
$().ready(function(){
var game_canvas = document.getElementById("main");
var context = game_canvas.getContext("2d");
// 参数定义
var Speed = 5; //定义
//逻辑定义 .
drawBackground();
var draw_handle = setInterval(drawCar,100);
var create_handle = setInterval(CreateComeCar,600);
var drawCome_handle = setInterval(drawComeCar,50);
// document.addEventListener("keydown",KeyProcess,false);
//画背景
function drawBackground(){
context.fillStyle = "#aaa";
context.strokeStyle ="000";
context.lineWidth = 2;
context.strokeRect(30,10,200,100);
context.beginPath();
context.moveTo(96,10);
context.lineTo(96,110);
context.stroke();
context.closePath();
context.beginPath();
context.moveTo(162,10);
context.lineTo(162,110);
context.stroke();
context.closePath();
}
//画汽车
function drawCar(){
//draw picture
var play_car = new Image();
// play_car.addEventListener('load',drawCar3,false);
play_car.src = "./img/car4.jpg";
var startx = 50; //起始地址
var starty = 90;
var img_width = 12;
var img_height = 18;
context.clearRect(startx,starty,12,18);
context.clearRect(startx+70,starty,12,18);
context.clearRect(startx+140,starty,12,18);
switch(GameParam.position)
{
case 0:
drawCar1();
break;
case 1:
drawCar2();
break;
case 2:
drawCar3();
break;
default :
break;
}
function drawCar1(){
context.drawImage(play_car,startx,starty,img_width,img_height);
}
function drawCar2(){
context.drawImage(play_car,startx+70,starty,img_width,img_height);
}
function drawCar3(){
context.drawImage(play_car,startx+140,starty,img_width,img_height);
}
}
var car_list = new Array();
var count = 1;
function ComeCar()
{
var x, y,speed,type;
}
//生成随机的的到来汽车
function CreateComeCar(){
var index = Math.floor(Math.random()*3);
var type = Math.floor(Math.random()*10);
var come_car = new ComeCar();
if(type < 8){
come_car.type = 1; // 表示普通汽车;
}
else if(type == 9){
come_car.type = 2; //表示快速汽车;
}
else if(type == 8 ){
come_car.type = 3; //表示财宝;
}
come_car.x = index * 70 + 50;
come_car.y = 10;
if(come_car.type == 1)
{
come_car.speed =2;
count ++;
if(count %10 === 0)
{
var mod = count /10;
if(mod < 8){
come_car.speed += mod;
}else{
come_car.speed += mod*0.8;
}
}
}
else if(come_car.type == 2){
come_car.speed = 6+mod*0.5;
}
else if(come_car.type == 3){
come_car.speed = 4;
}
car_list.push(come_car);
}
//画随机生成的到来汽车
function drawComeCar(){
context.clearRect(50,13,18,77);
context.clearRect(120,13,18,77);
context.clearRect(190,13,18,77);
for(var i = 0 ; i < car_list.length; i ++){
var come_car = new Image();
// come_car.src = "./img/car2.jpg";
if(car_list[i].type == 1) {
come_car.src = "./img/car2.jpg";
}else if(car_list[i].type == 2){
come_car.src = "./img/car1.jpg";
}else if(car_list[i].type == 3){
come_car.src = "./img/yuanbao.jpg";
}
var collision = new Image();
collision.src = "./img/collision.jpg";
console.log("x is "+ car_list[i].x +"y is" +car_list[i].y);
if(car_list[i].type <= 2){
context.drawImage(come_car,car_list[i].x,car_list[i].y+car_list[i].speed,8,10);
}else{
context.drawImage(come_car,car_list[i].x,car_list[i].y+car_list[i].speed,16,10);
}
car_list[i].y +=car_list[i].speed;
if(car_list[i].x == GameParam.position*70 + 50 && car_list[i].y >= 80 )
{
if(car_list[i].type <= 2)
{
context.drawImage(collision,car_list[i].x,car_list[i].y,18,12);
alert("You lose the game!");
if(GameParam.score > GameParam.highestscore){
alert("You create a new record! contragulations!");
GameParam.highestscore = GameParam.score;
$("#highscore").html(GameParam.highestscore);
localStorage.setItem("highscore",GameParam.highestscore);
}
clearInterval(create_handle);
clearInterval(draw_handle);
clearInterval(drawCome_handle);
}
else if(car_list[i].type == 3){
GameParam.score += 2;
}
}
if(car_list[i].y >= 80)
{
car_list.splice(i,1);
GameParam.score ++;
$("#score").html(GameParam.score);
}
}
}
});
$("#left").click(function(){
if(GameParam.position > 0){
GameParam.position -= 1;
}
});
$("#right").click(function(){
if(GameParam.position < 2) {
GameParam.position += 1;
}
});
document.onkeydown = KeyProcess;
//
function KeyProcess(e){
var keycode = e.keyCode;
if(keycode === 37){
//left
if(GameParam.position > 0){
GameParam.position -= 1;
}
}
else if(keycode == 39){
if(GameParam.position < 2) {
GameParam.position += 1;
}
}
}
</script>
</html>