-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.js
98 lines (79 loc) · 2.72 KB
/
screen.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
class Screen {
constructor(squares) {
this.windowWidth = window.innerWidth;
this.windowHeight = window.innerHeight;
this.counterBoxHeight = 50;
// square size in pixels
this.squareSize = Math.trunc(this.windowWidth / squares)
this.horizontalSquares = squares;
this.verticalSquares = Math.trunc((this.windowHeight - this.counterBoxHeight)
/ this.squareSize);
this.width = this.squareSize * this.horizontalSquares
this.height = (this.squareSize * this.verticalSquares) + this.counterBoxHeight
this.context = this.context;
this.canv=document.getElementById("gc");
this.context=this.canv.getContext("2d");
this.canv.height = this.height;
this.canv.width = this.width;
}
drawnBackground() {
this.context.fillStyle="black";
this.context.fillRect(0,0,this.width, this.height);
}
draw(snake, apple, counter) {
this.drawnBackground();
this.drawSnake();
this.drawApple();
this.drawCounter(counter);
}
drawSnake() {
this.context.fillStyle="lime";
for (let i =0; i<snake.positions.length; i++) {
this.context.fillRect(
// minus one because of snake logic begins from 1,
// and plus one to make a border line
(snake.positions[i].horizontal - 1) * this.squareSize + 1,
(snake.positions[i].vertical -1) * this.squareSize + 1,
// minus one to centralize the green square on the black square
this.squareSize -1,
this.squareSize -1
);
}
}
drawApple() {
this.context.fillStyle="red";
this.context.fillRect(
(apple.position.horizontal - 1) * this.squareSize, // minus one because of apple logic begins from 1
(apple.position.vertical -1) * this.squareSize,
this.squareSize,
this.squareSize
);
}
drawCounter(counter) {
let verticalCounterPosition = this.verticalSquares * this.squareSize;
// print black bottom
this.context.fillStyle="grey";
this.context.fillRect(
1,
verticalCounterPosition + 1,
this.width - 2,
this.counterBoxHeight - 2);
this.context.fillStyle="white";
this.context.font = "30px Arial";
// centralize text on vertical
let textPosition = verticalCounterPosition + 40
this.context.fillText("Points:",10,textPosition);
this.context.fillStyle="orange";
this.context.font = "30px Arial";
this.context.fillText(counter.value(),110,textPosition);
}
blinkSnakeHead(snake) {
this.context.fillStyle=(this.context.fillStyle == "#ffa500")?"lime":"orange";
this.context.fillRect(
(snake.positions[0].horizontal - 1) * this.squareSize + 1,
(snake.positions[0].vertical -1) * this.squareSize + 1,
this.squareSize -1,
this.squareSize -1
);
}
}