-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
202 lines (157 loc) · 4.88 KB
/
game.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
202
var PLAY = 1;
var END = 0;
var WIN = 2;
var gameState = PLAY;
var kangaroo, kangaroo_running, kangaroo_collided;
var jungle, invisiblejungle;
var obstaclesGroup, obstacle1;
var score=0;
var gameOver, restart;
function preload() {
kangaroo_running = loadAnimation("assets/kangaroo1.png","assets/kangaroo2.png","assets/kangaroo3.png");
kangaroo_collided = loadAnimation("assets/kangaroo1.png");
jungleImage = loadImage("assets/bg.png");
shrub1 = loadImage("assets/shrub1.png");
shrub2 = loadImage("assets/shrub2.png");
shrub3 = loadImage("assets/shrub3.png");
obstacle1 = loadImage("assets/stone.png");
gameOverImg = loadImage("assets/gameOver.png");
restartImg = loadImage("assets/restart.png");
jumpSound = loadSound("assets/jump.wav");
collidedSound = loadSound("assets/collided.wav");
}
function setup() {
console.log("Game BY:");
console.log("Johaan Paul");
console.log(" ");
console.log("Resorces like images, sounds, ETC BY:");
console.log("https://code.byjusfutureschool.com/");
canvasW = windowWidth/1.5;
canvasH = windowHeight/1.5;
var canvas = createCanvas(canvasW, canvasH);
canvas.parent('Game');
rectMode(CENTER);
score = 0;
jungle = createSprite(400,100,400,20);
jungle.addImage("jungle",jungleImage);
jungle.scale=0.3
jungle.x = width /2;
kangaroo = createSprite(50,200,20,50);
kangaroo.addAnimation("running", kangaroo_running);
kangaroo.addAnimation("collided", kangaroo_collided);
kangaroo.scale = 0.15;
kangaroo.setCollider("circle",0,0,300)
//kangaroo.debug=true;
invisibleGround = createSprite(400,350,1600,10);
invisibleGround.visible = false;
shrubsGroup = new Group();
obstaclesGroup = new Group();
gameOver = createSprite(400, 100);
gameOver.addImage(gameOverImg);
gameOver.scale = 0.5;
gameOver.visible = false;
restart = createSprite(550, 140);
restart.addImage(restartImg);
restart.scale = 0.1;
restart.visible = false;
}
function draw() {
background(255);
kangaroo.x=camera.position.x-270;
if (gameState === PLAY) {
jungle.velocityX=-3
if (jungle.x<100) {
jungle.x=400
}
kangaroo.velocityY = kangaroo.velocityY + 0.8
spawnShrubs();
spawnObstacles();
kangaroo.collide(invisibleGround);
if (obstaclesGroup.isTouching(kangaroo)) {
collidedSound.play();
gameState = END;
}
if (shrubsGroup.isTouching(kangaroo)) {
score = score +1;
shrubsGroup.destroyEach();
}
} else if (gameState === END) {
//set velcity of each game object to 0
kangaroo.velocityY = 0;
jungle.velocityX = 0;
obstaclesGroup.setVelocityXEach(0);
shrubsGroup.setVelocityXEach(0);
//change the kangaroo animation
kangaroo.changeAnimation("collided",kangaroo_collided);
//set lifetime of the game objects so that they are never destroyed
obstaclesGroup.setLifetimeEach(-1);
shrubsGroup.setLifetimeEach(-1);
} else if (gameState === WIN) {
jungle.velocityX = 0;
kangaroo.velocityX = 0;
obstaclesGroup.setVelocityXEach(0);
shrubsGroup.setVelocityXEach(0);
kangaroo.changeAnimation("collided", kangaroo_collided);
obstaclesGroup.setLifetimeEach(-1);
shrubsGroup.setLifetimeEach(-1);
}
drawSprites();
textSize(20);
stroke(3);
fill("black");
text("score: " + score, camera.position.x, 50);
if (score >= 5) {
kangaroo.visable = false;
textSize(30);
stroke(3);
fill("black");
text("Congragulations!! You win the game!! ", 70, 200);
gameState = WIN;
}
}
function spawnShrubs() {
//write code here to spawn the clouds
if (frameCount % 150 === 0) {
var shrub = createSprite(camera.position.x+500,330,40,10);
//shrub.debug=true;
shrub.velocityX = -(6 + 3*score/100)
shrub.scale = 0.6;
var rand = Math.round(random(1,3));
switch(rand) {
case 1: shrub.addImage(shrub1);
break;
case 2: shrub.addImage(shrub2);
break;
case 3: shrub.addImage(shrub3);
break;
default: break;
}
//assign scale and lifetime to the shrub
shrub.scale = 0.05;
//assign lifetime to the variable
shrub.lifetime = 400;
shrub.setCollider("rectangle",0,0,shrub.width/2,shrub.height/2)
//add each cloud to the group
shrubsGroup.add(shrub);
}
}
function spawnObstacles() {
if (frameCount % 120 === 0) {
var obstacle = createSprite(camera.position.x+400,330,40,40);
obstacle.setCollider("rectangle",0,0,200,200)
obstacle.addImage(obstacle1);
obstacle.velocityX = -(6 + 3*score/100)
obstacle.scale = 0.15;
//assign scale and lifetime to the obstacle
// obstacle.debug=true;
obstacle.lifetime = 400;
//add each obstacle to the group
obstaclesGroup.add(obstacle);
}
}
function jump() {
if (kangaroo.y>270 && gameState === PLAY) {
jumpSound.play();
kangaroo.velocityY = -16;
}
}