-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
146 lines (103 loc) · 3.73 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
$(document).ready(function() {
console.log('script loaded');
// 2 arrays containing 2 types of foods (healthy and junk)
// containing name, img src, score and "healthy" or "junk" identifier
// for now, ignoring the preset scores...
var healthyFoods = [
['Apple','images/apple.png','healthy'],
['Avocado','images/avocado.png','healthy'],
['Blueberries','images/blueberries.png','healthy'],
['Broccoli','images/broccoli.png','healthy'],
['Carrot','images/carrot.png','healthy'],
['Lemon','images/lemon.png','healthy'],
];
var junkFoods = [
['Can','images/can.png','junk'],
['Candy','images/candy.png','junk'],
['Doughnut','images/doughnut.png','junk'],
['Fries','images/fries.png','junk'],
['Hamburger','images/hamburguer.png','junk'],
['Pizza','images/pizza.png','junk'],
];
var stop;
// var - holding the falling foods div
var $fallingFoods = $('#falling-foods');
// variables for calculating healthy score
var $score = $('p.total-score');
var $totalScore = 0;
$score.text('Total Score: ' + $totalScore);
// variables for calculating junk score
var $negScore = $('p.junk-score');
var $junkScore = 0;
$negScore.text('Junk Score: ' + $junkScore);
// borrowed from the Pokemon game
var $randomNum = function randNum (min,max) {
return Math.floor(Math.random() * (max - min +1) + min);}
// replay function - thanks Mike R!
var restart = function() {
location.reload();
}
// function to create the food items
var makeFood = function() {
// when this function is called, make the main container (holding game elements) visible
$('#main-container').css('display', 'block');
// choosing which array with a random number (0 or 1)
var healthOrJunk = Math.round(Math.random());
if (healthOrJunk === 0){
var arr = healthyFoods;
} else {
var arr = junkFoods;
}
// now that the array is chosen, choose the food item
var foodItem = Math.round(Math.random() * arr.length -1);
// var - the individual div holding each food
var $foods = $('<div class="foods"> </div>');
// use random food item above to grab image bground
$foods.css('background-image', 'url(' + arr[foodItem][1] + ')') ;
// randomizing where they fall from; staggering them
$foods.css('left', Math.floor(Math.random()*(1200-320) + 320));
// appending each food div to the foods container
$fallingFoods.append($foods);
$foods.click(function(){
$foods.remove();
// remove foods div altogether when clicked
var $points = $randomNum(5,30)
if (arr[foodItem][2] === 'healthy') {
$score.text('Total Score: ' + ($totalScore += $points));
} else {
// reach goal: turn this into junk-o-meter score
$negScore.text('Junk Score: ' + ($junkScore += $points));
}
if ($totalScore >= 300){
clearInterval(stop); // this doesn't work without the below (why??)
$fallingFoods.html(''); // cleared divs
$('#win-screen').css('display', 'block');
$('#score').css('display','none');
$('#win-screen').toggleClass('animated').addClass('slideInLeft');
console.log($totalScore);
};
if ($junkScore >= 200){
clearInterval(stop);
$fallingFoods.html(''); // cleared divs
$('#lose-screen').css('display', 'block');
$('#score').css('display','none');
$('#lose-screen').toggleClass('animated').addClass('slideInLeft');
console.log($junkScore);
};
});
}
var addEventListeners = function() {
$('#start-button').click(function(){
$('#start-screen').css('display', 'none');
stop = setInterval(function(){
makeFood();
},600)
$('.replay-button').click(function() {
restart();
})
})
}
$(document).ready(function(){
addEventListeners();
})
});