Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (68 loc) · 2.2 KB
/
Copy pathindex.js
File metadata and controls
82 lines (68 loc) · 2.2 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
// CONSTANTS
// GAME ELEMENTS - canvas, ctx, keystate
var canvas,
ctx,
keystate;
// DEFINING GAME OBJECTS - player, checker(backgammon ???), dice, board(not if it is just a background) ...
// function newGame() or main()
// create initiate and append game canvas
// keep track of keyboard presses
// init() - INITIATE GAME OBJECTS;
// loop() - GAME LOOP FUNCTION, STARTS THE ANIMATION;
function newGame() {
canvas = document.getElementById('board-canvas');
ctx = canvas.getContext('2d');
canvas.addEventListener('click', showXY);
function showXY(event) {
var x = event.offsetX;
var y = event.offsetY;
var selectedArrayPosition = 0;
if (y > 19 && y < 515 && ((x > 19 && x < 345) || (x > 375 && x < 700))) {
if (y < canvas.height / 2) {
y = 19;
selectedArrayPosition = 12;
} else {
y = canvas.height / 2;
selectedArrayPosition = 11;
}
if (x > 19 && x < 345) {
x = Math.floor((x - 19) / 54);
if (selectedArrayPosition == 12) {
selectedArrayPosition += x;
}
else {
selectedArrayPosition -= x;
}
x = (x * 54) + 19;
}
if (x > 375 && x < 700) {
x = Math.floor((x - 375) / 54);
if (selectedArrayPosition == 12) {
selectedArrayPosition += (6 + x);
}
else {
selectedArrayPosition -= (6 + x);
}
x = (x * 54) + 375;
}
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.strokeRect(x, y, 54, (canvas.height / 2) - 19);
ctx.closePath();
ctx.stroke();
alert("Selected element of the array : " + selectedArrayPosition);
}
}
}
// function loop()
// update()
// draw()
// requestAnimationFrame
// function init()
// initiate game objects and set start positions
// function update()
// calls game objects .update()
// function draw ()
// calls game objects .draw()
// main();
window.onload = newGame();