generated from SISE-Web-Development-Environments/Assignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
172 lines (166 loc) · 3.65 KB
/
app.js
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
var context;
var shape = new Object();
var board;
var score;
var pac_color;
var start_time;
var time_elapsed;
var interval;
$(document).ready(function() {
context = canvas.getContext("2d");
Start();
});
function Start() {
board = new Array();
score = 0;
pac_color = "yellow";
var cnt = 100;
var food_remain = 50;
var pacman_remain = 1;
start_time = new Date();
for (var i = 0; i < 10; i++) {
board[i] = new Array();
//put obstacles in (i=3,j=3) and (i=3,j=4) and (i=3,j=5), (i=6,j=1) and (i=6,j=2)
for (var j = 0; j < 10; j++) {
if (
(i == 3 && j == 3) ||
(i == 3 && j == 4) ||
(i == 3 && j == 5) ||
(i == 6 && j == 1) ||
(i == 6 && j == 2)
) {
board[i][j] = 4;
} else {
var randomNum = Math.random();
if (randomNum <= (1.0 * food_remain) / cnt) {
food_remain--;
board[i][j] = 1;
} else if (randomNum < (1.0 * (pacman_remain + food_remain)) / cnt) {
shape.i = i;
shape.j = j;
pacman_remain--;
board[i][j] = 2;
} else {
board[i][j] = 0;
}
cnt--;
}
}
}
while (food_remain > 0) {
var emptyCell = findRandomEmptyCell(board);
board[emptyCell[0]][emptyCell[1]] = 1;
food_remain--;
}
keysDown = {};
addEventListener(
"keydown",
function(e) {
keysDown[e.keyCode] = true;
},
false
);
addEventListener(
"keyup",
function(e) {
keysDown[e.keyCode] = false;
},
false
);
interval = setInterval(UpdatePosition, 250);
}
function findRandomEmptyCell(board) {
var i = Math.floor(Math.random() * 9 + 1);
var j = Math.floor(Math.random() * 9 + 1);
while (board[i][j] != 0) {
i = Math.floor(Math.random() * 9 + 1);
j = Math.floor(Math.random() * 9 + 1);
}
return [i, j];
}
function GetKeyPressed() {
if (keysDown[38]) {
return 1;
}
if (keysDown[40]) {
return 2;
}
if (keysDown[37]) {
return 3;
}
if (keysDown[39]) {
return 4;
}
}
function Draw() {
canvas.width = canvas.width; //clean board
lblScore.value = score;
lblTime.value = time_elapsed;
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
var center = new Object();
center.x = i * 60 + 30;
center.y = j * 60 + 30;
if (board[i][j] == 2) {
context.beginPath();
context.arc(center.x, center.y, 30, 0.15 * Math.PI, 1.85 * Math.PI); // half circle
context.lineTo(center.x, center.y);
context.fillStyle = pac_color; //color
context.fill();
context.beginPath();
context.arc(center.x + 5, center.y - 15, 5, 0, 2 * Math.PI); // circle
context.fillStyle = "black"; //color
context.fill();
} else if (board[i][j] == 1) {
context.beginPath();
context.arc(center.x, center.y, 15, 0, 2 * Math.PI); // circle
context.fillStyle = "black"; //color
context.fill();
} else if (board[i][j] == 4) {
context.beginPath();
context.rect(center.x - 30, center.y - 30, 60, 60);
context.fillStyle = "grey"; //color
context.fill();
}
}
}
}
function UpdatePosition() {
board[shape.i][shape.j] = 0;
var x = GetKeyPressed();
if (x == 1) {
if (shape.j > 0 && board[shape.i][shape.j - 1] != 4) {
shape.j--;
}
}
if (x == 2) {
if (shape.j < 9 && board[shape.i][shape.j + 1] != 4) {
shape.j++;
}
}
if (x == 3) {
if (shape.i > 0 && board[shape.i - 1][shape.j] != 4) {
shape.i--;
}
}
if (x == 4) {
if (shape.i < 9 && board[shape.i + 1][shape.j] != 4) {
shape.i++;
}
}
if (board[shape.i][shape.j] == 1) {
score++;
}
board[shape.i][shape.j] = 2;
var currentTime = new Date();
time_elapsed = (currentTime - start_time) / 1000;
if (score >= 20 && time_elapsed <= 10) {
pac_color = "green";
}
if (score == 50) {
window.clearInterval(interval);
window.alert("Game completed");
} else {
Draw();
}
}