-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathcanvas.js
More file actions
89 lines (77 loc) · 2.15 KB
/
canvas.js
File metadata and controls
89 lines (77 loc) · 2.15 KB
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
class HangmanCanvas {
constructor(secretWord) {
this.context = document.getElementById('hangman').getContext('2d');
// ... your code goes here
this.secretWord = secretWord;
}
createBoard() {
// ... your code goes here
this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
this.drawLines();
}
drawLines() {
// ... your code goes here
const numberOfLetters = this.secretWord.length;
const startX = 300;
const startY = 700;
const lineWidth = 50;
const lineSpacing = 10;
for (let i = 0; i < numberOfLetters; i++) {
this.context.beginPath();
this.context.moveTo(startX + i * (lineWidth + lineSpacing), startY);
this.context.lineTo(startX + i * (lineWidth + lineSpacing) + lineWidth, startY);
this.context.stroke();
}
}
writeCorrectLetter(index) {
// ... your code goes here
const letter = this.secretWord[index].toUpperCase();
const startX = 305 + index * 60; // Adjusted positions
const startY = 690;
this.context.font = '48px Arial';
this.context.fillText(letter, startX, startY);
}
writeWrongLetter(letter, errorsLeft) {
const startX = 600;
const startY = 200;
const spacing = 50;
this.context.font = '48px Arial';
this.context.fillText(letter.toUpperCase(), startX + (10 - errorsLeft) * spacing, startY);
// ... your code goes here
}
drawHangman(errorsLeft) {
// ... your code goes here
const ctx = this.context;
switch (errorsLeft) {
case 9:
// Draw the base
ctx.beginPath();
ctx.moveTo(100, 700);
ctx.lineTo(200, 700);
ctx.lineTo(150, 650);
ctx.closePath();
ctx.stroke();
break;
// Continue adding cases for each errorLeft value
// ...
default:
break;
}
}
gameOver() {
// ... your code goes here
const img = new Image();
img.src = './images/gameover.png';
img.onload = () => {
this.context.drawImage(img, 200, 100, 400, 400);
};
}
winner() {
// ... your code goes here
const img = new Image();
img.src = './images/awesome.png';
img.onload = () => {
this.context.drawImage(img, 200, 100, 400, 400);
};
}
}