-
Notifications
You must be signed in to change notification settings - Fork 1
/
games.js
201 lines (166 loc) · 5.49 KB
/
games.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//Assigning all the necessary global variables.
var path, gazelle;
var badguy1, badguy2, badguy3;
var pathImg, gazelleRunning, gazelleCollided;
var badguyLionImg, badguyTigerImg, badguyHunterImg;
var gameOverImg, restartImg;
var coinImg;
var lionCG, tigerCG, hunterCG;
var END =0;
var PLAY =1;
var gameState = PLAY;
var scoreSound , checkPointSound, dieSound;
var distance, score;
var gameOver, restart;
var obstaclesGroup, coinsGroup;
var edges;
function preload(){
//Loading all the necessary animations and images.
pathImg = loadImage("Road.png");
gazelleRunning = loadAnimation("gazellestanding.png","gazellerunning.png");
gazelleCollided = loadAnimation("gazelledead.png");
badguyLionImg = loadImage("badguy-lion.png");
badguyTigerImg = loadImage("badguy-tiger.png");
badguyHunterImg = loadImage("badguy-hunter.png");
gameOverImg = loadImage("gameOver.png");
restartImg = loadImage("restart.png");
coinImg = loadImage("coin.png");
scoreSound = loadSound("jump.mp3");
dieSound = loadSound("die.mp3");
checkPointSound = loadSound("checkpoint.mp3");
}
function setup(){
background("white");
//Creating the overall screen.
createCanvas(windowWidth,windowHeight);
//Creating the moving road.
path=createSprite(windowWidth-200,windowHeight/2);
path.addImage(pathImg);
path.velocityX = 5;
path.scale = 0.5;
//Creating the runaway gazelle.
gazelle = createSprite(windowWidth/1.175,160,50,50);
gazelle.addAnimation("gazel",gazelleRunning);
gazelle.addAnimation("Collided", gazelleCollided);
gazelle.scale=0.375;
//Setting the collider for the gazelle.
gazelle.setCollider("rectangle",0,0,330,265);
//gazelle.debug = true;
//Creating the gameover image
gameOver = createSprite(windowWidth/3,185,50,50);
gameOver.addImage(gameOverImg);
gameOver.scale = 0.8;
//Creating the restart button
restart = createSprite(windowWidth/3,285,50,50);
restart.addImage(restartImg);
restart.scale = 0.8;
//Creating the necessary groups.
obstaclesGroup = createGroup();
coinsGroup = createGroup();
//Setting the distance and score as 0.
distance = 0;
score = 0;
}
function draw(){
//Assigning the game properties of the particular gamestate when it is in that gamestate.
if(gameState === PLAY){
gameOver.visible = false;
restart.visible = false;
//Setting difficulty.
path.velocityX = (4 + 0.75* distance/100);
//Scoring.
distance = distance + Math.round(getFrameRate()/60);
//Making the road infinite.
if(path.x > windowWidth){
path.x = width /2;
}
//playing the checkpoint sound.
if(distance>0 && distance%100 === 0){
checkPointSound.play();
}
//Setting the controls for the gazelle.
gazelle.y = World.mouseY;
//Creating the edges.
edges= createEdgeSprites();
gazelle.collide(edges);
//Spawning the obstacles and coins on the road.
spawnObstacles();
spawnCoins();
if(obstaclesGroup.isTouching(gazelle)){
gameState = END;
dieSound.play();
}
if(coinsGroup.isTouching(gazelle)){
score=score+2;
scoreSound.play();
coinsGroup.destroyEach();
}
}
else if(gameState === END){
gameOver.visible = true;
restart.visible = true;
//Killing the gazelle.
gazelle.changeAnimation("Collided", gazelleCollided);
//Stopping the movement.
path.velocityX = 0;
//Setting the lifetime of the game objects so that they are never destroyed.
obstaclesGroup.setLifetimeEach(-1);
coinsGroup.setLifetimeEach(-1);
//Stopping the movement of the objects.
obstaclesGroup.setVelocityXEach(0);
coinsGroup.setVelocityXEach(0);
}
if(mousePressedOver(restart)) {
reset();
}
drawSprites();
//Displaying the distance.
fill('white');
textSize(20);
text("Distance: "+ distance, (windowWidth/1.5)-25, 50);
//Displaying the score
fill('white');
textSize(20);
text("Score: "+ score, (windowWidth/1.15)-25,50);
}
function reset(){
gameState=PLAY;
distance = 0;
score = 0;
obstaclesGroup.destroyEach();
coinsGroup.destroyEach();
gazelle.changeAnimation("gazel",gazelleRunning);
}
function spawnObstacles(){
if (frameCount % 60 === 0){
var obstacle = createSprite(windowWidth/15,Math.round(random(windowHeight,0)),10,40);
obstacle.velocityX = (6 + distance/100);
//Generating the random obstacles.
var rand = Math.round(random(1,3));
switch(rand) {
case 1: obstacle.addImage(badguyHunterImg);
break;
case 2: obstacle.addImage(badguyLionImg);
break;
case 3: obstacle.addImage(badguyTigerImg);
break;
default: break;
}
//assign scale and lifetime to the obstacle
obstacle.scale = 0.25;
obstacle.lifetime = 500;
//add each obstacle to the group
obstaclesGroup.add(obstacle);
}
}
function spawnCoins(){
if (frameCount % 60 === 0) {
var coin = createSprite(windowWidth/15,Math.round(random(windowHeight,0)),40,10);
coin.addImage(coinImg);
coin.scale = 0.15;
coin.velocityX = (6 + distance/100);
//assign lifetime to the variable
coin.lifetime = 500;
coinsGroup.add(coin);
}
}