-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
171 lines (142 loc) · 4.34 KB
/
script.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
class ElementRender {
constructor(renderCallback) {
this.renderCallback = renderCallback;
}
render() {
this.renderCallback(this);
}
}
class Hailstone extends ElementRender {
constructor(x, y, side, downSpeed, renderCallback) {
super(renderCallback);
this.x = x;
this.y = y;
this.w = side;
this.h = side;
this.downSpeed = downSpeed;
}
moveDown() {
this.y += this.downSpeed;
}
}
class Player extends ElementRender {
constructor(x, y, side, color, renderCallback) {
super(renderCallback);
this.x = x;
this.y = y;
this.w = side;
this.h = side;
this.color = color;
this.dx = 0;
this.dy = 0;
}
moveRight() {
this.dx = 5;
this.dy = 0;
}
moveLeft() {
this.dx = -5;
this.dy = 0;
}
moveUp() {
this.dx = 0;
this.dy = -5;
}
moveDown() {
this.dx = 0;
this.dy = 5;
}
}
class Game {
constructor(window, document) {
this.window = window;
this.hailstones = [];
let screen_side = 500;
this.canvas = document.getElementById("game_screen"); // tham chiếu đến canvas trên màn hình
this.canvas.width = screen_side;
this.canvas.height = screen_side;
this.context = this.canvas.getContext("2d");
const playerRenderCallback = (player) => {
this.context.fillStyle = player.color;
player.x += player.dx;
player.y += player.dy;
this.context.fillRect(player.x, player.y, player.w, player.h);
}
const player_side = 50;
const xPosition = this.canvas.width / 2 - player_side / 2;
this.player = new Player(xPosition, this.canvas.height - player_side, player_side, "green", playerRenderCallback);
this.hailstoneRenderCallback = (hailstone) => {
this.context.fillStyle = "red";
this.context.fillRect(hailstone.x, hailstone.y, hailstone.w, hailstone.h);
}
this.createStone();
}
createStone() {
const randomX = Math.floor(Math.random() * this.canvas.width);
const positionY = 0;
const hailstone = new Hailstone(randomX, positionY, 10, 5, this.hailstoneRenderCallback);
this.hailstones.push(hailstone);
}
controlDirection(event) {
// let KEY_UP = 38;
// let KEY_DOWN = 40;
let KEY_RIGHT = 39;
let KEY_LEFT = 37;
switch (event.keyCode) {
case KEY_RIGHT:
this.player.moveRight();
break;
case KEY_LEFT:
this.player.moveLeft();
break;
// case KEY_UP:
// this.player.moveUp();
// break;
// case KEY_DOWN:
// this.player.moveDown();
// break;
}
}
clearScreen() {
this.context.clearRect(0,0,this.canvas.width, this.canvas.height);
}
repeatRender() {
this.clearScreen();
this.player.render();
for (let hailstone of this.hailstones) {
hailstone.moveDown();
hailstone.render();
}
if (this.shouldStop()) this.stopRender();
}
shouldStop() {
for (let hailstone of this.hailstones) {
if (this.isCollision(this.player, hailstone)) return true;
}
return false;
}
// xác định va chạm giữa 2 hình chữ nhật.
isCollision(rect1, rect2) {
let distX = (rect1.x + (rect1.w/2)) - (rect2.x + (rect2.w)/2);
if (distX < 0)
distX = -distX;
const distW = (rect1.w + rect2.w)/2;
let distY =(rect1.y + (rect1.h/2)) - (rect2.y + (rect2.h)/2);
if(distY < 0)
distY = -distY;
const distH = (rect1.h + rect2.h)/2;
return (distX <= distW && distY <= distH);
}
stopRender() {
clearInterval(this.interval);
}
begin() {
this.player.render();
let game_loop_time = 20;
this.window.addEventListener("keydown", this.controlDirection.bind(this));
this.interval = setInterval(this.repeatRender.bind(this), game_loop_time);
setInterval(this.createStone.bind(this), 250);
}
}
let game = new Game(window, document);
game.begin();