-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (124 loc) · 3.9 KB
/
Copy pathscript.js
File metadata and controls
174 lines (124 loc) · 3.9 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
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
172
173
174
var score = 0;
// function to randomly select two falling objects//
function random(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function setBG() {
if (Math.round(Math.random())) {
return url(img/cookie.jpg);
} else {
return url(img/cake.jpg);
}
}
// function to set size, velocity and movement of falling objects//
function dropBox() {
var length = random(100, ($(".game").width() - 100));
var velocity = random(1000, 10000);
var size = random(202);
var thisBox = $("<div/>", {
class: "box",
style: "width:" + size + "px; height:" + size + "px; left:" + length + "px; transition: transform " + velocity + "ms linear;"
});
//set data and bg based on data
thisBox.data("test", Math.round(Math.random()));
if (thisBox.data("test")) {
thisBox.css({
"background": "url(img/cookie.jpg)",
"background-size": "contain"
});
} else {
thisBox.css({
"background": "url(img/cake.jpg)",
"background-size": "contain"
});
}
//insert falling elements element
$(".game").append(thisBox);
//random start for animation
setTimeout(function () {
thisBox.addClass("move");
}, random(0, 5000));
//remove this object when animation is over
thisBox.one("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend",
function (event) {
$(this).remove();
});
}
for (i = 0; i < 10; i++) {
dropBox();
}
// click function that changes the score
$(document).on('click', '.box', function () {
if ($(this).data("test")) {
score += 1;
} else {
score -= 1;
}
$(".score").html(score);
$(this).remove();
});
// set interval to start the game
var runGame = setInterval(function () {
for (i = 0; i < 10; i++) {
dropBox();
}
}, 5000);
// Monitor score every 0.5s and change Homer's look according to score
setInterval(function () {
if (score > 5) {
document.getElementById("homerSimpson").style.backgroundImage = "url(img/homerObese.png)";
document.getElementById("endGame").style.visibility = 'visible';
document.getElementById("logoMarge").style.visibility = 'visible';
} else if (score < -5) {
document.getElementById("homerSimpson").style.backgroundImage = "url(img/homerDrunk.png)";
document.getElementById("endGame").style.visibility = 'visible';
document.getElementById("logoMarge").style.visibility = 'visible';
} else {
document.getElementById("homerSimpson").style.backgroundImage = "url(img/homer.png)";
document.getElementById("endGame").style.visibility = 'hidden';
document.getElementById("logoMarge").style.visibility = 'hidden';
}
}, 500);
//Give users the option to choose to continue or quit the game
document.getElementById("continue").addEventListener('click', resumeGame, false);
function resumeGame() {
document.getElementById("endGame").style.visibility = 'hidden';
score = 0;
document.getElementById("score").innerHTML = score;
}
document.getElementById("quit").addEventListener('click', quitGame, false);
function quitGame() {
window.location.replace("http://www.matteofusilli.co.uk/");
}
// Activate game with 'Play' button //
document.getElementById("play").addEventListener('click', startGame, false);
function startGame() {
document.getElementById("landingPage").style.visibility = 'hidden';
}
// Move Homer with mouse over objects //
$(document).bind('mousemove', function (e) {
$('#homerSimpson').css({
left: e.pageX - 80,
top: e.pageY - 80
});
});
// === Alternative function to move with arrow keys === //
/*setInterval(moveHomer, 20);
var keys = {}
$(document).keydown(function(e) {
keys[e.keyCode] = true;
});
$(document).keyup(function(e) {
delete keys[e.keyCode];
});
function moveHomer() {
for (var direction in keys) {
if (!keys.hasOwnProperty(direction)) continue;
if (direction == 37) {
$("#homerSimpson").animate({left: "-=10"}, 0);
}
if (direction == 39) {
$("#homerSimpson").animate({left: "+=10"}, 0);
}
}
}*/