-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureCraft.html
197 lines (192 loc) · 6.16 KB
/
AzureCraft.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
<!DOCTYPE html>
<html>
<head>
<title>Farid's snake</title>
</head>
<body>
<p id="score">My score: 0</p>
<canvas id="canvas" width="1024" height="1024"></canvas>
<script>
var game = {
tickNumber: 0,
timer: null,
score: 0,
board: [
"###############################",
"# #",
"# #",
"# # #",
"# #### # #### #",
"# #### # #### #",
"# # #",
"# # #",
"# # #",
"###### ########### ######",
"# #",
"# #",
"# # #",
"# #### # #### #",
"# #### # #### #",
"# # #",
"# # #",
"###############################"
],
fruit: [
{x: 1, y: 1}
],
tick: function() {
window.clearTimeout(game.timer);
game.tickNumber++;
if(game.tickNumber % 10 == 0) {
game.addRandomFruit();
}
var result = snake.move();
if(result == "gameover") {
alert("Sorry, Farid said that your snake crashed. Apples collected: " + game.score);
myMusic.stop();
return;
}
graphics.drawGame();
game.timer = window.setTimeout("game.tick()", 500);
},
addRandomFruit: function() {
var randomY = Math.floor(Math.random() * game.board.length) + 0;
var randomX = Math.floor(Math.random() * game.board[randomY].length) + 0;
var randomLocation = {x: randomX, y: randomY};
if(game.isEmpty(randomLocation) && !game.isFruit(randomLocation)) {
game.fruit.push(randomLocation);
}
},
isEmpty: function(location) {
return game.board[location.y][location.x] == ' ';
},
isWall: function(location) {
return game.board[location.y][location.x] == '#';
},
isFruit: function(location) {
for(var fruitNumber = 0; fruitNumber < game.fruit.length; fruitNumber++) {
var fruit = game.fruit[fruitNumber];
if(location.x == fruit.x && location.y == fruit.y) {
game.fruit.splice(fruitNumber, 1);
return true;
}
}
return false;
},
isSnake: function(location) {
for(var snakePart = 0; snakePart < snake.parts.length; snakePart++) {
var part = snake.parts[snakePart];
if(location.x == part.x && location.y == part.y) {
return true;
}
}
return false;
}
};
var snake = {
parts: [
{x: 4, y: 2},
{x: 3, y: 2},
{x: 2, y: 2}
],
facing: "E",
nextLocation: function() {
var snakeHead = snake.parts[0];
var targetX = snakeHead.x;
var targetY = snakeHead.y;
targetY = snake.facing == "N" ? targetY-1 : targetY;
targetY = snake.facing == "S" ? targetY+1 : targetY;
targetX = snake.facing == "W" ? targetX-1 : targetX;
targetX = snake.facing == "E" ? targetX+1 : targetX;
return {x: targetX, y: targetY};
},
move: function() {
var location = snake.nextLocation();
if(game.isWall(location) || game.isSnake(location)) {
return "gameover";
}
if(game.isEmpty(location)) {
snake.parts.unshift(location);
snake.parts.pop();
}
if(game.isFruit(location)) {
snake.parts.unshift(location);
game.score++;
document.getElementById('score').innerHTML = "My score: " + game.score;
myAppleMusic = new gameControl.sound("apple.mp3");
myAppleMusic.play();
}
}
};
var graphics = {
canvas: document.getElementById("canvas"),
squareSize: 30,
drawBoard: function(ctx) {
var currentYoffset = 0;
game.board.forEach(function checkline(line) {
line = line.split('');
var currentXoffset = 0;
line.forEach(function checkCharacter(character) {
if(character == '#') {
ctx.fillStyle = "black";
ctx.fillRect(currentXoffset, currentYoffset, graphics.squareSize, graphics.squareSize);
}
currentXoffset += graphics.squareSize;
});
currentYoffset += graphics.squareSize;
});
},
draw: function(ctx, source, color) {
source.forEach(function (part) {
var partXlocation = part.x * graphics.squareSize;
var partYlocation = part.y * graphics.squareSize;
ctx.fillStyle = color;
ctx.fillRect(partXlocation, partYlocation, graphics.squareSize, graphics.squareSize);
});
},
drawGame: function() {
var ctx = graphics.canvas.getContext("2d");
ctx.clearRect(0, 0, graphics.canvas.width, graphics.canvas.height);
graphics.drawBoard(ctx);
graphics.draw(ctx, game.fruit, "red");
graphics.draw(ctx, snake.parts, "green");
}
};
var gameControl = {
processInput: function(keyPressed) {
var key = keyPressed.key.toLowerCase();
var targetDirection = snake.facing;
if(key == "w" && snake.facing != "S") targetDirection = "N";
if(key == "a" && snake.facing != "E") targetDirection = "W";
if(key == "s" && snake.facing != "N") targetDirection = "S";
if(key == "d" && snake.facing != "W") targetDirection = "E";
snake.facing = targetDirection;
game.tick();
},
sound: function(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
},
startGame: function() {
window.addEventListener("keypress", gameControl.processInput, false);
myMusic = new gameControl.sound("thriller.mp3");
myMusic.play();
game.tick();
}
};
var myMusic;
var myAppleMusic;
gameControl.startGame();
</script>
</body>
</html>