-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
199 lines (179 loc) · 5.25 KB
/
game.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
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
// Set up canvas
const canvas = document.createElement('canvas')
const restart = document.getElementById('restart')
canvas.id = "canvas"
document.body.insertBefore(canvas, restart)
const ctx = canvas.getContext('2d')
const width = canvas.width = 900
const height = canvas.height = 700
canvas.style.border = '30px solid #a7cdff'
canvas.style.borderRadius = '20px'
let ball = {
direction: {
x: -1,
y: -1
},
velocity: {
x:-11,
y: -9
},
position: {
x: 500,
y: 688
}
}
let game = {
started: false,
lostGame: false,
wonGame: false
}
let paddle = {
moveRight: () => {
if (paddle.farRight < width) {
paddle.farLeft += 10
paddle.left += 10
paddle.center += 10
paddle.right += 10
paddle.farRight += 10
}
},
moveLeft: () => {
if (paddle.farLeft > 0) {
paddle.farLeft -= 10
paddle.left -= 10
paddle.center -= 10
paddle.right -= 10
paddle.farRight -= 10
}
},
collisionPoint: undefined,
direction: undefined,
farLeft: 20,
left: 56,
center: 92,
right: 128,
farRight: 164
}
// Event handlers to move paddle
const keydown = e => {
let code = e.keyCode
game.started = true
switch (code) {
case 37:
paddle.direction = 'left'
break
case 39:
paddle.direction = 'right'
break
case 13:
if (game.lostGame == true || game.wonGame == true) location.reload()
break
default:
break
}
}
const keyup = () => {
paddle.direction = undefined
}
const drawBlock = (x, y, w, h) => {
ctx.fillStyle = '#a7cdff'
ctx.fillRect(x, y, w, h)
ctx.fillStyle = '#dd7777'
ctx.fillRect(x+5, y+5, w-10, h-10)
if (x == null) {
ctx.fillStyle = '#a7cdff'
ctx.fillRect(x, y, w, h)
}
}
// Store blocks in 2D array -> [x-coord, y-coord, width, height]
let blocks = [...Array(4)].map(e => [...Array(10)])
blocks = blocks.map((row, i)=>row.map((v, j) => [(width / 10) * j, 40 * i, width/10, 40]))
const draw = () => {
if (game.started) {
if (game.lostGame == true) {
ctx.fillStyle = '#ff0000'
ctx.fillRect(0, 0, width, height)
ctx.fillStyle = 'white'
ctx.font = '55px helvetica'
ctx.fillText(':( Press enter to play again!', 105, height/2)
}
if (game.wonGame == true) {
ctx.fillStyle = '#1a5733'
ctx.fillRect(0, 0, width, height)
ctx.fillStyle = 'white'
ctx.font = '120px helvetica'
ctx.fillText('YOU WON :)', 105, height/2)
} else {
if (game.lostGame == false) {
// Draw background
ctx.fillStyle = '#a7cdff'
ctx.fillRect(0, 0, width, height)
// Draw paddle
if (paddle.direction == 'left') paddle.moveLeft()
else if (paddle.direction == 'right') paddle.moveRight()
ctx.fillStyle = '#1a5733'
ctx.fillRect(paddle.farLeft, height - 30, 144, 20)
// Draw blocks
blocks.map(row => row.map(block => drawBlock(...block)))
// :(
for (let row = 0; row < blocks.length; row++) {
for (let block = 0; block < blocks[row].length; block++) {
if (blocks[row][block][0] < ball.position.x && blocks[row][block][1] < ball.position.y && ball.position.x < blocks[row][block][0] + blocks[row][block][2] && ball.position.y < blocks[row][block][1] + blocks[row][block][3]) {
if (blocks[row][block][0] != null) {
blocks[row][block] = [null, null, null, null]
ball.velocity.y *= -1
}
}
}
}
// Check if ball has hit something
if (ball.position.x >= width - 13) ball.velocity.x *= -1
if (ball.position.x <= 20) ball.velocity.x *= -1
if (ball.position.y <= 20) ball.velocity.y *= -1
if (ball.position.x > paddle.farLeft &&
ball.position.x < paddle.farRight &&
ball.position.y >= height - 52) {
ball.velocity.y *= -1
paddle.collisionPoint = ball.position.x - paddle.center
if (paddle.collisionPoint < 0) {
ball.velocity.x -= Math.abs(Math.round(paddle.collisionPoint/30))
} else if (paddle.collisionPoint > 0) {
ball.velocity.x += Math.abs(Math.round(paddle.collisionPoint/30))
}
}
if (ball.position.y >= height) {
game.lostGame = true
}
if (ball.velocity.x == 0) {
ball.velocity.x += (Math.round(Math.random())) ? -1 : 1
}
// Draw ball
ctx.beginPath()
ctx.fillStyle = '#ffffff'
ctx.ellipse(ball.position.x, ball.position.y, 15, 15, 0, 0, 2 * Math.PI)
ctx.closePath()
ctx.fill()
// Set ball position for next render
ball.position.x += ball.velocity.x
ball.position.y += ball.velocity.y
}
}
} else {
// Game not started
ctx.fillStyle = '#a7cdff'
ctx.fillRect(0, 0, width, height)
ctx.fillStyle = 'black'
ctx.font = '70px helvetica'
ctx.fillText('Use the arrow keys', 105, 170)
ctx.fillText('to control the paddle.', 105, 270)
ctx.fillText('_________________', 105, 360)
ctx.fillText('Press any key to play!', 105, 500)
}
if (blocks.flat().flat().every(e => e == null)) {
game.wonGame = true
}
requestAnimationFrame(draw)
}
requestAnimationFrame(draw)
document.addEventListener("keydown", keydown)
document.addEventListener("keyup", keyup)