-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAVEPRINCESS.html
302 lines (272 loc) · 8.79 KB
/
SAVEPRINCESS.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Bot can save Princess</title>
<meta name="description" content="An interactive getting started guide for Brackets.">
<link rel="stylesheet" href="Style.css">
<script id="script1" type="text/javascript">
var imageObj = new Image();
var imageObj2 = new Image();
var imageObj3 = new Image();
var imgMonster = new Image();
imageObj.src = 'princess.jpg';
imageObj2.src = 'Robot-walking.jpg';
imageObj3.src = 'rsz_11wall.png';
imgMonster.src = 'slime.png';
var wallX = [0,100,200,300,400,500,600,700,800,900,1000,0,0,0,0,0,0,0,0,0,0,0,0,100,200,300,400,500,600,700,800,900,1000,900,900,900,900,900,900,900,900,900,900,400,600,200,400,600,700,800,300,300,300,300,300,300];
var wallY = [0,0,0,0,0,0,0,0,0,0,0,0,100,200,300,400,500,600,700,800,900,900,900,900,900,900,900,900,900,900,900,100,200,300,400,500,600,700,800,900,200,500,500,500,200,400,300,200,300,600,0,200,300,400,500];
var frame= 0;
var Monster = function(x,y) {
this.x = x;
this.y = y;
this.move = function() {
var dir = Math.round(Math.random()*10)%4;
if(dir===0)
{
this.moveUp();
}
if(dir===1)
{
this.moveDown();
}
if(dir===2)
{
this.moveLeft();
}
if(dir===3)
{
this.moveRight();
}
}
this.moveUp = function () {
if (this.y >=100&& !isWall(this.x,this.y-100)) {
this.y -= 100;
}
}
this.moveDown = function () {
if (this.y <=board.height - 200&& !isWall(this.x,this.y+100)) {
this.y += 100;
}
}
this.moveLeft = function () {
if (this.x >=100&& !isWall(this.x-100,this.y)) {
this.x -= 100;
}
}
this.moveRight = function () {
if (this.x <=board.width - 200 && !isWall(this.x+100,this.y)) {
this.x += 100;
}
}
this.draw = function (context) {
context.drawImage(imgMonster,this.x,this.y);
}
}
function isWall(x,y){
for (var i = 0;i < wallX.length;i++) {
if(x==wallX[i] && y==wallY[i] ) {
return true;
}
}
return false;
}
var Wall = function () {
this.draw = function (context) {
for (var i = 0;i < wallX.length;i++) {
context.drawImage(imageObj3,wallX[i],wallY[i]);
}
}
}
var Princess = function() {
this.x = 100;
this.y = 100;
this.width = 100;
this.height = 100;
this.moveUp = function () {
if (this.y >=100&& !isWall(this.x,this.y-100)) {
this.y -= 100;
}
}
this.moveDown = function () {
if (this.y <=board.height - 200&& !isWall(this.x,this.y+100)) {
this.y += 100;
}
}
this.moveLeft = function () {
if (this.x >=100&& !isWall(this.x-100,this.y)) {
this.x -= 100;
}
}
this.moveRight = function () {
if (this.x <=board.width - 200&& !isWall(this.x+100,this.y)) {
this.x += 100;
}
}
this.draw = function(context) {
context.drawImage(imageObj, this.x, this.y);
}
}
var Robot = function() {
this.x = 800;
this.y = 800;
this.width = 100;
this.height = 100;
this.moveUp = function () {
console.log("tttt:"+this.y);
if (this.y >=100&& !isWall(this.x,this.y-100)) {
this.y -= 100;
console.log("UPPPP:"+this.y);
}
console.log("PPPPPU");
}
this.moveDown = function () {
if (this.y <=board.height - 200&& !isWall(this.x,this.y+100)) {
this.y += 100;
}
}
this.moveLeft = function () {
if (this.x >=100&& !isWall(this.x-100,this.y)) {
this.x -= 100;
}
}
this.moveRight = function () {
if (this.x <=board.width - 200 && !isWall(this.x+100,this.y)) {
this.x += 100;
}
}
this.draw = function(context) {
context.drawImage(imageObj2, this.x, this.y);
}
}
var Board = function() {
this.x = 0;
this.y = 0;
this.width = 1000;
this.height = 1000;
this.princess = new Princess();
this.robot = new Robot();
this.draw = function (context) {
context.fillStyle = "#FFFF00";
context.fillRect(0,0,this.width,this.height);
for (var i = 0;i < this.width;i += 100) {
context.moveTo(100 + i,0);
context.lineTo(100 + i, this.height);
}
for (var j = 0;j < this.height;j += 100) {
context.moveTo(0,100 + j);
context.lineTo(this.width, 100 + j);
}
context.stroke();
this.princess.draw(context);
this.robot.draw(context);
}
}
var moveSound = function () {
var audio = new Audio('Flappy.mp3');
audio.play();
}
var board = new Board();
var wall = new Wall();
var monster = new Monster(500,700);
var monster2 = new Monster(400,800);
var monster3 = new Monster(700,400);
var monster4 = new Monster(200,200);
var monster5 = new Monster(600,300);
setInterval(function(){
monster.move();
monster2.move();
monster3.move();
monster4.move();
monster5.move();
}, 1000);
function resetGame()
{
board.robot.x=800;
board.robot.y=800;
var div = document.getElementById('Status');
div.innerHTML="Game Running...";
update();
}
function update()
{
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
board.draw(ctx);
wall.draw(ctx);
monster.draw(ctx);
monster2.draw(ctx);
monster3.draw(ctx);
monster4.draw(ctx);
monster5.draw(ctx);
if (board.robot.x === board.princess.x && board.robot.y === board.princess.y) {
var div = document.getElementById('Status');
var win = new Audio('Win.mp3');
win.play();
div.innerHTML="WIN! <button onclick='resetGame();'>Reset</button>";
return;
} else if (board.robot.x === monster.x && board.robot.y === monster.y||board.robot.x === monster2.x&&board.robot.y === monster2.y||board.robot.x === monster3.x&&board.robot.y === monster3.y||board.robot.x === monster4.x&&board.robot.y === monster4.y||board.robot.x === monster5.x&&board.robot.y === monster5.y){
var div = document.getElementById('Status');
var lose = new Audio('Lose.mp3');
lose.play();
div.innerHTML="Loooose! <button onclick='resetGame();'>Reset</button>";
return;
}
requestAnimationFrame(update);
}
function onKeyPressed(evt) {
if(evt.key=='w')
{
board.robot.moveUp();
//board.robot.y-=100;
moveSound();
}
if(evt.key=='a')
{
board.robot.moveLeft();
//board.robot.x-=100;
moveSound();
}
if(evt.key=='d')
{
board.robot.moveRight();
//board.robot.x+=100;
moveSound();
}
if(evt.key=='s')
{
board.robot.moveDown();
//board.robot.y+=100;
moveSound();
}
if (evt.key =='8') {
board.princess.moveUp();
moveSound();
}
if (evt.key =='5') {
board.princess.moveDown();
moveSound();
}
if (evt.key =='4') {
board.princess.moveLeft();
moveSound();
}
if (evt.key =='6') {
board.princess.moveRight();
moveSound();
}
console.log(evt);
}
document.addEventListener("keypress", onKeyPressed);
</script>
</head>
<body onload="update()">
<span>
<canvas id="myCanvas" width="1000" height="1000" style="border:1px solid #000000;">
</canvas>
<div id="Status" style="display:inline;font-size:50px;color:red">Game Running...</div>
<div id="startButton" style="display:inline;font-size:40px;color:green"></div>
</span>
</body>
</html>