-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
124 lines (110 loc) · 3.89 KB
/
main.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
class gameItem {
constructor(width, height, x, y, r, g, b) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.r = r;
this.g = g;
this.b = b;
}
collision(object1) {
if ((this.x >= object1.x && this.x <= object1.x + object1.width) || (object1.x >= this.x && object1.x <= this.x + this.width)) {
if ((this.y >= object1.y && this.y <= object1.y + object1.height) || (object1.y >= this.y && object1.y <= this.y + this.height)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
drawItem() {
mainContext.fillStyle = "rgb(" + this.r.toString() + ", " + this.g.toString() + ", " + this.b.toString() + ")";
mainContext.fillRect(this.x, this.y, this.width, this.height);
}
}
var mainCanvas = document.getElementById("main");
updateCanvasSize();
var start, keybuffer;
var lives = 3;
var score = 0;
var nomovement = 0;
var windowResize = window.addEventListener('resize', updateCanvasSize);
var detectKey = window.addEventListener('keydown', getInput);
var resetKey = window.addEventListener('keyup', resetInput);
var mainContext = mainCanvas.getContext("2d");
var player = new gameItem(100, 100, 0, 0, 255, 0, 0);
player.drawItem();
var badGuys = [];
for(var i = 0; i < Math.floor(document.body.clientWidth/200); i ++) {
badGuys.push(new gameItem(50, 50, document.body.clientWidth + 200*(i+1), Math.floor(Math.random() * (document.body.clientHeight - 100)), 0, 255, 255));
}
function updateCanvasSize() {
mainCanvas.style.width = document.body.clientWidth.toString() + "px";
mainCanvas.style.height = document.body.clientHeight.toString() + "px";
mainCanvas.width = document.body.clientWidth;
mainCanvas.height = document.body.clientHeight;
}
function getInput(e) {
if (e.which === 38) {
keybuffer = "up";
} else if (e.which === 40) {
keybuffer = "down";
} else {
keybuffer = undefined;
}
console.log(keybuffer);
}
function resetInput() {
keybuffer = undefined;
}
function test(timestamp) {
if (start === undefined) {
start = timestamp;
}
mainContext.fillStyle = '#FFFFFF';
mainContext.fillRect(0, 0, document.body.clientWidth, document.body.clientHeight);
player.drawItem();
mainContext.font = "50px sans-serif"
if (keybuffer === "down") {
player.y += 10;
} else if (keybuffer === "up") {
player.y -= 10;
} else {
nomovement ++;
}
if (player.y + player.height > document.body.clientHeight) {
player.y = document.body.clientHeight - player.height;
} else if (player.y < 0) {
player.y = 0;
}
for (var i = 0; i < badGuys.length; i++) {
if (player.collision(badGuys[i]) === true) {
badGuys[i].x = document.body.clientWidth;
badGuys[i].y = Math.floor(Math.random() * (document.body.clientHeight - 100));
lives--;
}
badGuys[i].drawItem();
badGuys[i].x -= 10 + (score * 0.001);
if (badGuys[i].x < -100) {
badGuys[i].x = document.body.clientWidth;
if(nomovement < 60) {
badGuys[i].y = Math.floor(Math.random() * (document.body.clientHeight - 100));
} else {
badGuys[i].y = player.y + Math.floor(Math.random() * 100);
nomovement = 0;
}
score += 20;
}
}
mainContext.fillStyle = "#000000";
mainContext.fillText("Lives: " + lives.toString(), document.body.clientWidth - 200, 50);
mainContext.fillText("Score: " + score.toString(), 10, 50);
if (lives !== 0) {
window.requestAnimationFrame(test);
} else {
mainContext.fillText("Game Over", 0.5 * document.body.clientWidth, 0.5 * document.body.clientHeight);
}
}
window.requestAnimationFrame(test);