-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode
192 lines (167 loc) · 4.46 KB
/
Code
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
// Load previous score from local storage, or initialize to 0 if none exists
var previousScore = localStorage.getItem('dotCount') || 0;
var dotCount = parseInt(previousScore);
//Create canvas
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.id = 'gameCanvas';
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Set up game objects
var player = {
x: 0,
y: 0,
width: 20,
height: 20,
speed: 5,
};
var dots = [];
for (var i = 0; i < 20; i++) {
dots.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: 10,
collected: false,
color: 'blue', // default color is blue
});
}
// Add red dots that remove 10 points when player collides with them
for (var i = 0; i < 5; i++) {
dots.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: 10,
collected: false,
color: 'red', // color is red
});
}
// Add green dot that gives 5 points when player collides with it
dots.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: 10,
collected: false,
color: 'green', // color is green
});
// Set up keyboard input
var keysDown = {};
addEventListener('keydown', function (event) {
keysDown[event.keyCode] = true;
});
addEventListener('keyup', function (event) {
delete keysDown[event.keyCode];
});
// Set up game loop
function update() {
// Move player
if (38 in keysDown) {
// Up arrow
player.y -= player.speed;
}
if (40 in keysDown) {
// Down arrow
player.y += player.speed;
}
if (37 in keysDown) {
// Left arrow
player.x -= player.speed;
}
if (39 in keysDown) {
// Right arrow
player.x += player.speed;
}
// Check if player has gone off the canvas
if (player.x < 0) {
player.x = canvas.width; // wrap around to the other side
} else if (player.x > canvas.width) {
player.x = 0; // wrap around to the other side
}
if (player.y < 0) {
player.y = canvas.height; // wrap around to the other side
} else if (player.y > canvas.height) {
player.y = 0; // wrap around to the other side
}
// Check for collision with dots
for (var i = 0; i < dots.length; i++) {
var dot = dots[i];
if (!dot.collected && collides(player, dot)) {
dot.collected = true;
if (dot.color === 'red') {
// if red dot, decrement dot count
dotCount -= 10;
} else if (dot.color === 'green') {
// if green dot, increment dot count by 5
dotCount += 5;
} else {
// otherwise, increment dot count
dotCount++;
}
// Regenerate dot in a new random location
dot.x = Math.random() * canvas.width;
dot.y = Math.random() * canvas.height;
dot.collected = false;
}
}
}
// Save score to local storage
localStorage.setItem('dotCount', dotCount);
function render() {
// Clear canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw player
context.fillStyle = 'red';
context.fillRect(player.x, player.y, player.width, player.height);
// Draw dots
for (var i = 0; i < dots.length; i++) {
var dot = dots[i];
if (!dot.collected) {
context.fillStyle = dot.color;
context.beginPath();
context.arc(dot.x, dot.y, dot.size, 0, Math.PI * 2);
context.fill();
}
}
// Draw dot count
context.fillStyle = 'black';
context.font = '16px Arial';
context.fillText('Dots collected: ' + dotCount, 10, 20);
// Add red dot
context.fillStyle = 'red';
context.beginPath();
context.arc(canvas.width - 20, 20, 10, 0, 2 * Math.PI);
context.fill();
// Add event listener to red dot
canvas.addEventListener('click', function (event) {
var dotDistance = Math.sqrt(
Math.pow(event.clientX - (canvas.width - 20), 2) +
Math.pow(event.clientY - 20, 2)
);
if (dotDistance < 10) {
dotCount--;
context.clearRect(0, 0, canvas.width, canvas.height);
render();
}
});
}
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
// Helper function for collision detection
function collides(a, b) {
return (
a.x < b.x + b.size &&
a.x + a.width > b.x &&
a.y < b.y + b.size &&
a.y + a.height > b.y
);
}
// Update score in case it was changed in another tab
window.addEventListener('storage', function (event) {
if (event.key === 'dotCount') {
dotCount = parseInt(event.newValue);
}
});
requestAnimationFrame(gameLoop);