-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
201 lines (165 loc) · 5.46 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<style>
html {
height: 100%
}
h1 {
font-size: 40px;
}
body {
background: linear-gradient(to right, #ffd000 0%, #5500ff 100%);
text-align: center;
}
.container {
margin: auto;
max-width: 800px;
}
canvas {
margin-top: auto;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="container">
<h1>Day and Night FIGHT!!!</h1>
<canvas id="canvas" width="600" height="600"></canvas>
<p id="score"></p>
</div>
</body>
<script>
// Yagzul! Canvas lives..
const canvas = document.getElementById("canvas")
const score = document.getElementById("score")
const ctx = canvas.getContext("2d")
const colours = {
SunYellow: "#ffd000",
MoonPurple: "#5500ff",
White: "#ffffff",
Black: "#000000"
}
const DAY_COLOUR = colours.White
const NIGHT_COLOUR = colours.Black
const SQUARE_SIZE = 25
const MIN_SPEED = 5
const MAX_SPEED = 10
const gridWidth = canvas.width / SQUARE_SIZE
const gridHeight = canvas.height / SQUARE_SIZE
let dayScore = 0
let nightScore = 0
const balls = [
{ // Left ball, destroys night and is night colour
x: canvas.width / 4,
y: canvas.height / 2,
dx: 8, //Initial vel x, y below
dy: -8,
colour: NIGHT_COLOUR,
reverseColour: DAY_COLOUR
},
{
x: (canvas.width / 4) * 3,
y: canvas.height / 2,
dx: -8,
dy: 8,
colour: DAY_COLOUR,
reverseColour: NIGHT_COLOUR
}
]
const squares = []
// Initial grid draw
for (let i = 0; i < gridWidth; i++) {
squares[i] = []
for (let j = 0; j < gridHeight; j++) {
squares[i][j] = i < gridWidth / 2 ? DAY_COLOUR : NIGHT_COLOUR
}
}
// Funcs
function clamp(num, min, max) {
return Math.min(Math.max(num, min), max)
}
function drawSquares() {
dayScore = 0
nightScore = 0
for (let i = 0; i < gridWidth; i++) {
for (let j = 0; j < gridHeight; j++) {
ctx.fillStyle = squares[i][j]
ctx.fillRect(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE)
if (squares[i][j] == DAY_COLOUR) dayScore++
if (squares[i][j] == NIGHT_COLOUR) nightScore++
}
}
}
function drawBall(ball) {
ctx.beginPath()
ctx.arc(ball.x, ball.y, SQUARE_SIZE / 2, 0, Math.PI * 2)
ctx.fillStyle = ball.colour
ctx.fill()
ctx.closePath()
}
function squareCollision(ball) {
for (let angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) {
const checkX = ball.x + Math.cos(angle) * (SQUARE_SIZE / 2)
const checkY = ball.y + Math.sin(angle) * (SQUARE_SIZE / 2)
const i = Math.floor(checkX / SQUARE_SIZE)
const j = Math.floor(checkY / SQUARE_SIZE)
if (i >= 0 && i < gridWidth && j >= 0 && j < gridHeight) {
if (squares[i][j] != ball.reverseColour) {
squares[i][j] = ball.reverseColour
if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) {
ball.dx = -ball.dx
} else {
ball.dy = -ball.dy
}
}
}
}
}
function boundaryCollision(ball) {
// ball position exceeds right boundary, as canvas width - square size / 2 is smaller. This is done to check collision on edges
// of ball instead of center by offsetting collision check by -squaresize/2 from the right
// OR
// if ball position is less than square size / 2 we know ball has hit left side since coords for left is 0 + squaresize/2 for center offset
// this is done for both X and Y, although example is for x
if (ball.x + ball.dx > canvas.width - SQUARE_SIZE / 2 || ball.x + ball.dx < SQUARE_SIZE / 2) {
ball.dx = -ball.dx
}
if (ball.y + ball.dy > canvas.height - SQUARE_SIZE / 2 || ball.y + ball.dy < SQUARE_SIZE / 2) {
ball.dy = -ball.dy
}
}
function addRandomness(ball) {
ball.dx += (Math.random()*0.01)-0.005
ball.dy += (Math.random()*0.01)-0.005
}
function speedController(ball) {
// Ensure speed is within acceptable bounds
ball.dx = clamp(ball.dx, -MAX_SPEED, MAX_SPEED)
ball.dy = clamp(ball.dy, -MAX_SPEED, MAX_SPEED)
// Ensure speed is above minimum
if (Math.abs(ball.dx) < MIN_SPEED) {
ball.dx = ball.dx > 0 ? MIN_SPEED : -MIN_SPEED
}
if (Math.abs(ball.dy) < MIN_SPEED) {
ball.dy = ball.dy > 0 ? MIN_SPEED : -MIN_SPEED
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
drawSquares()
balls.forEach(ball => {
drawBall(ball)
squareCollision(ball)
boundaryCollision(ball)
ball.x += ball.dx
ball.y += ball.dy
addRandomness(ball)
speedController(ball)
})
score.textContent = `Day: ${dayScore} Night: ${nightScore}`
requestAnimationFrame(draw)
}
requestAnimationFrame(draw)
</script>
</html>