-
Notifications
You must be signed in to change notification settings - Fork 0
/
florian-die-Kugel-darf-nicht-verbrennen.html
114 lines (93 loc) · 2.66 KB
/
florian-die-Kugel-darf-nicht-verbrennen.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
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
text-transform: uppercase;
}
.holder {
text-align: center;
}
.center {
display: inline-block;
}
.bordered {
border: solid 1px lightgray;
}
</style>
</head>
<body>
<h1> Die Kugel darf nicht verbrennen! </h1>
<div class="holder">
<canvas id="canvas" class="center bordered" width="800" height="800"></canvas>
</div>
<script>
const squareOf = 3;
const context = canvas.getContext("2d");
context.fillStyle = "white";
const tileWidth = canvas.width / 16 ;
const tileHeight = tileWidth;
const colRow2coords = ({col, row}) => ({
x: col * tileWidth,
y: row * tileHeight
});
const coords2colRow = ({x, y}) => ({
col: Math.floor(x / tileWidth) ,
row: Math.floor(y / tileHeight)
});
const paintTile = ({col, row}) => {
const {x,y} = colRow2coords({col,row});
context.fillRect(x+1, y+1, tileWidth - 2, tileHeight - 2);
};
const paintBall = ({col, row}) => {
const {x,y} = colRow2coords({col,row});
context.beginPath();
context.arc(x+ tileWidth / 2, y+tileHeight / 2, tileWidth / 2 - 4, 0, 6.3, false);
context.fill();
};
function paintInit(size) {
context.fillStyle = "lightgrey";
context.fillRect(0, 0, canvas.width, canvas.height);
for (let row=0; row < size ; row++) {
for (let col = 0; col < size; col++) {
context.fillStyle = "red";
paintTile({col,row});
}
}
}
let pathColRows = [];
let nextToSelect = 0;
paintInit(squareOf);
const guessing = evt => {
context.fillStyle = "black";
const colRow = coords2colRow({x: evt.offsetX, y: evt.offsetY});
if ( colRow.col === pathColRows[nextToSelect].col &&
colRow.row === pathColRows[nextToSelect].row) {
paintBall(colRow);
nextToSelect++;
} else {
nextToSelect = 0;
paintInit(squareOf);
}
};
let lastColRow = {col: squareOf-1, row:0};
const preparing = evt => {
const colRow = coords2colRow({x: evt.offsetX, y: evt.offsetY});
if(Math.abs(colRow.col - lastColRow.col) > 1 || Math.abs(colRow.row - lastColRow.row) > 1) {
return;
}
lastColRow = colRow;
pathColRows.push(colRow);
context.fillStyle = "blue";
paintBall(colRow);
if ( colRow.col === 0 &&
colRow.row === squareOf-1) {
paintInit(squareOf);
canvas.onclick = guessing;
}
};
canvas.onclick = preparing;
</script>
</body>
</html>