forked from Luposodrom/Furry_Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
105 lines (90 loc) · 2.77 KB
/
Copy pathgame.js
File metadata and controls
105 lines (90 loc) · 2.77 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var Coin = require("./coin.js");
var Furry = require("./furry.js");
var self;
var Game = function (){
var self = this;
this.board = document.querySelectorAll("#board > div");
this.furry = new Furry();
this.coin = new Coin ();
this.score = 0;
this.index = function(x,y){
return x + (y * 10);
};
this.showFurry = function() {
this.hideVisibleFurry();
this.board[this.index(this.furry.x, this.furry.y)].classList.add("furry");
};
this.hideVisibleFurry = function() {
for (var i = 0; i < this.board.length; i++) {
this.board[i].classList.remove("furry");
}
};
this.showCoin = function() {
this.board[this.index(this.coin.x, this.coin.y)].classList.add("coin");
};
self = this;
this.moveFurry = function(){
if (self.furry.direction === "right"){
self.furry.x++;
} else if (self.furry.direction === "left"){
self.furry.x--;
} else if(self.furry.direction === "up"){
self.furry.y--;
}else if(self.furry.direction === "down"){
self.furry.y++;
// dodałem y
}
this.gameOver();
this.checkCoinCollision();
this.showFurry();
};
this.turnFurry = function(event) {
switch (event.which) {
case 37:
self.furry.direction = "left";
break;
case 38:
self.furry.direction = "up";
break;
case 39:
self.furry.direction = "right";
break;
case 40:
self.furry.direction = "down";
break;
}
};
this.checkCoinCollision = function() {
if (this.furry.x === this.coin.x && this.furry.y === this.coin.y) {
console.log("Booom!");
this.board[this.index(this.coin.x, this.coin.y)].classList.remove("coin");
this.score = this.score + 1;
// tu zmieniłem zapis
document.querySelector("strong").innerHTML = this.score;
this.coin = new Coin();
this.showCoin();
}
};
this.gameOver = function() {
if (this.furry.x <0 || this.furry.y <0 || this.furry.x > 9 || this.furry.y > 9 ) {
clearInterval(this.idSetInterval);
// document.querySelector("#board").classList.toggle("invisible");
// console.log("Nooooooooooo!");
// document.querySelector("#over").classList.toggle("invisible");
// this.hideVisibleFurry();
// u nie też to nie działało i się z tym migającym ekranem męczyłem więc zrobiłem jak poniżej
clearInterval(this.idSetInterval);
var text = document.querySelector("#board");
text.innerHTML = "buuuuuuuuuuuuuuum";
}
}
this.startGame = function() {
this.showFurry();
this.showCoin();
var self = this;
this.idSetinterval = setInterval(function() {
self.moveFurry();
}, 255);
};
}
module.exports = Game;