-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (110 loc) · 3.46 KB
/
script.js
File metadata and controls
128 lines (110 loc) · 3.46 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
//variables
var scene;
var plane;
var background;
var batteries;
var gasCans;
var bombs;
var scoreTime;
var currentTime;
var userScore = 0;
//constants
var SPEED_OF_BACKGROUND = 15;
var NUM_BATTERIES = 1;
var NUM_GAS_CANS = 1;
var NUM_BOMBS = 1;
function counter() {
var aRand = Math.floor(Math.random() * 2);
return aRand;
} //end counter()
function Plane() {
tPlane = new Sprite(scene, "Images/RetroPlane.png", 50, 50);
tPlane.setSpeed(0);
tPlane.setPosition();
tPlane.setBoundAction(STOP);
tPlane.followMouse = function() {
this.setPosition(scene.getMouseX(), 500);
}
return tPlane;
} //end Plane()
function Battery() {
tBattery = new Sprite(scene, "Images/BatteryPic.png", 100, 100);
tBattery.reset = function(){
this.setDY((Math.random() * SPEED_OF_BACKGROUND) + 5);
this.setDX((Math.random() * 10) - 5);
newX = Math.random() * scene.width;
this.setPosition(newX, 50);
}
tBattery.checkBounds = function() {
if (this.y > scene.height){
this.reset();
}
}
tBattery.reset();
return tBattery;
} //end Battery()
function makeBatteries() {
batteries = new Array(NUM_BATTERIES);
for (i = 0; i < NUM_BATTERIES; i++){
batteries[i] = new Battery();
}
} //end makeBatteries()
function updateBatteries() {
for (i = 0; i < NUM_BATTERIES; i++){
batteries[i].update();
}
} //end updateBatteries
function Background() {
tBackground = new Sprite(scene, "Images/OverheadGroundPic.jpg", 800, 1440);
tBackground.setDX(0);
tBackground.setDY(SPEED_OF_BACKGROUND);
tBackground.setPosition(400, 0);
tBackground.checkBounds = function(){
if (this.y > 720){
this.setPosition(400, -120)
}
}
return tBackground;
} //end Background()
function checkCollisions() {
for (i = 0; i < NUM_BATTERIES; i++){
if (plane.collidesWith(batteries[i])){
batteries[i].reset();
userScore += 5;
}
}
} //end checkCollisions()
function init() {
scene = new Scene();
scene.hideCursor();
currentTime = new Timer();
currentTime.reset();
plane = new Plane();
plane.show();
makeBatteries();
background = new Background();
scene.start();
} //end init()
function update() {
scene.clear();
plane.followMouse();
background.update();
plane.update();
updateBatteries();
checkCollisions();
findScore();
isEnd();
scoreTime = Math.round(30 - currentTime.getElapsedTime());
document.getElementById('timeLeft').innerHTML = scoreTime;
} //end update()
function resetGame() { alert("Your score was" + userScore);
currentTime = reset();
} //end reset()
function findScore() {
userScore += currentTime.getElapsedTime();
} //end findScore()
function isEnd() {
if (scoreTime === 0) {
resetGame();
} //endif
}