-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
194 lines (147 loc) · 4.57 KB
/
sketch.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
var path,mainCyclist;
var player1,player2,player3;
var pathImg,mainRacerImg1,mainRacerImg2;
var oppPink1Img,oppPink2Img;
var oppYellow1Img,oppYellow2Img;
var oppRed1Img,oppRed2Img;
var gameOverImg,cycleBell;
var pinkCG, yellowCG,redCG;
var END =0;
var PLAY =1;
var gameState = PLAY;
var distance=0;
var gameOver, restart, crash;
function preload(){
pathImg = loadImage("images/Road.png");
mainRacerImg1 = loadAnimation("images/mainPlayer1.png","images/mainPlayer2.png");
mainRacerImg2= loadAnimation("images/mainPlayer3.png");
oppPink1Img = loadAnimation("images/opponent1.png","images/opponent2.png");
oppPink2Img = loadAnimation("images/opponent3.png");
oppYellow1Img = loadAnimation("images/opponent4.png","images/opponent5.png");
oppYellow2Img = loadAnimation("images/opponent6.png");
oppRed1Img = loadAnimation("images/opponent7.png","images/opponent8.png");
oppRed2Img = loadAnimation("images/opponent9.png");
cycleBell = loadSound("sound/bell.mp3");
crash = loadSound("sound/crash.mp3");
gameOverImg = loadImage("images/gameOver.png");
}
function setup(){
createCanvas(1200,300);
// Moving background
path=createSprite(100,150);
path.addImage(pathImg);
path.velocityX = -5;
//creating boy running
mainCyclist = createSprite(70,150);
mainCyclist.addAnimation("SahilRunning",mainRacerImg1);
mainCyclist.scale=0.07;
mainCyclist.debug = true;
mainCyclist.setCollider("circle", 0, 0, 40);
//set collider for mainCyclist
gameOver = createSprite(650,150);
gameOver.addImage(gameOverImg);
gameOver.scale = 0.8;
gameOver.visible = false;
pinkCG = new Group();
yellowCG = new Group();
redCG = new Group();
}
function draw() {
background(0);
drawSprites();
textSize(20);
fill(255);
text("Distance: "+ distance,900,30);
if(gameState===PLAY){
distance = distance + Math.round(getFrameRate()/50);
path.velocityX = -(6 + 2*distance/150);
mainCyclist.y = World.mouseY;
edges= createEdgeSprites();
mainCyclist .collide(edges);
//code to reset the background
if(path.x < 0 ){
path.x = width/2;
}
//code to play cycle bell sound
if(keyDown("space")) {
cycleBell.play();
}
//creating continous opponent players
var select_oppPlayer = Math.round(random(1,3));
if (World.frameCount % 150 == 0) {
if (select_oppPlayer == 1) {
pinkCyclists();
} else if (select_oppPlayer == 2) {
yellowCyclists();
} else {
redCyclists();
}
}
if(pinkCG.isTouching(mainCyclist)){
gameState = END;
player1.velocityY = 0;
player1.addAnimation("opponentPlayer1",oppPink2Img);
crash.play();
}
if(yellowCG.isTouching(mainCyclist)){
gameState = END;
player2.velocityY = 0;
player2.addAnimation("opponentPlayer2",oppYellow2Img);
crash.play();
}
if(redCG.isTouching(mainCyclist)){
gameState = END;
player3.velocityY = 0;
player3.addAnimation("opponentPlayer3",oppRed2Img);
crash.play();
}
}else if (gameState === END) {
gameOver.visible = true;
text("Press the up arrow to reset the game", 490, 200);
path.velocityX = 0;
mainCyclist.velocityY = 0;
mainCyclist.addAnimation("SahilRunning",mainRacerImg2);
pinkCG.setVelocityXEach(0);
pinkCG.setLifetimeEach(-1);
yellowCG.setVelocityXEach(0);
yellowCG.setLifetimeEach(-1);
redCG.setVelocityXEach(0);
redCG.setLifetimeEach(-1);
if(keyDown("up")){
reset();
}
}
}
function pinkCyclists(){
player1 =createSprite(1100,Math.round(random(50, 250)));
player1.scale =0.06;
player1.velocityX = -(6 + 2*distance/150);
player1.addAnimation("opponentPlayer1",oppPink1Img);
player1.setLifetime=170;
pinkCG.add(player1);
}
function yellowCyclists(){
player2 =createSprite(1100,Math.round(random(50, 250)));
player2.scale =0.06;
player2.velocityX = -(6 + 2*distance/150);
player2.addAnimation("opponentPlayer2",oppYellow1Img);
player2.setLifetime=170;
yellowCG.add(player2);
}
function redCyclists(){
player3 =createSprite(1100,Math.round(random(50, 250)));
player3.scale =0.06;
player3.velocityX = -(6 + 2*distance/150);
player3.addAnimation("opponentPlayer3",oppRed1Img);
player3.setLifetime=170;
redCG.add(player3);
}
function reset(){
gameState = PLAY;
gameOver.visible = false;
mainCyclist.addAnimation("SahilRunning",mainRacerImg1);
distance = 0;
pinkCG.destroyEach();
redCG.destroyEach();
yellowCG.destroyEach();
}