-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
255 lines (223 loc) · 6.46 KB
/
Copy pathscript.js
File metadata and controls
255 lines (223 loc) · 6.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//Move the catcher with the left and right arrow keys to catch the falling objects.
/* VARIABLES */
let catcher, fallingObject1, fallingObject2;
let score = 0;
let fallingObjectImg1, fallingObjectImg2, bgImg, catcherImg, loseImg, winImg;
let startButton, directionsButton, backButton;
let screen = 0;
let myFont, normalFont;
/* PRELOAD LOADS FILES */
function preload(){
bgImg = loadImage("assets/bg.jpeg");
catcherImg = loadImage("assets/catcher.png");
fallingObjectImg1 = loadImage("assets/fallingObjectG.webp");
fallingObjectImg2 = loadImage("assets/fallingObjectB.png");
loseImg = loadImage("assets/lose.gif");
winImg = loadImage("assets/win.jpeg");
startImg = loadImage("assets/start.webp");
myFont = loadFont('assets/impostograph.otf');
normalFont = loadFont('assets/normal.otf');
}
/* SETUP RUNS ONCE */
function setup() {
createCanvas(400,400);
//Resize images
bgImg.resize(400, 400);
catcherImg.resize(70, 0);
fallingObjectImg1.resize(30, 0);
fallingObjectImg2.resize(60,0);
startImg.resize(100,0);
homeScreen();
}
/* DRAW LOOP REPEATS */
function draw() {
if (screen == 0) {
if (directionsButton.mouse.presses()) {
//screen 1 is directions screen
screen = 1;
directionsScreen();
} else if (startButton.mouse.presses()) {
//screen 2 is play screen
screen = 2;
playScreenAssets();
}
}
if (screen == 1) {
if (backButton.mouse.presses()) {
//screen 0 is home screen
screen = 0;
homeScreen();
backButton.pos = { x: -300, y: -300 };
}
}
if (screen == 2) {
background(bgImg);
//If fallingObject reaches bottom, move back to random position at top
if (fallingObject1.y >= height){
fallingObject1.y = 0;
fallingObject1.x = random(50,370);
fallingObject1.vel.y = random(1, 5);
score = score - 1;
}
if (fallingObject2.y >= height){
fallingObject2.y = 0;
fallingObject2.x = random(50,370);
fallingObject2.vel.y = random(1, 5);
}
//move catcher
if (kb.pressing("left")){
catcher.vel.x = -3;
} else if (kb.pressing("right")){
catcher.vel.x = 3;
} else {
catcher.vel.x = 0;
catcher.vel.y = 0;
}
//stop catcher at edges of screen
if (catcher.x < 50){
catcher.x = 50;
} else if (catcher.x > 350){
catcher.x = 350;
}
//if fallingObject1 collides with catcher, move back to random position at top and gain a point
if (fallingObject1.collides(catcher)){
fallingObject1.y = 0;
fallingObject1.x = random(50,370);
fallingObject1.vel.y = random(1,5);
fallingObject1.direction = "down";
score = score + 1;
}
//if fallingObject2 collides with catcher, move back to random position at top and lose a point
if (fallingObject2.collides(catcher)){
fallingObject2.y = 0;
fallingObject2.x = random(50,370);
fallingObject2.vel.y = random(1,5);
fallingObject2.direction = "down";
score = score - 1;
}
//if fallingObject1 collides with fallingObject2, move back to random position at top and lose a point
if (fallingObject1.collides(fallingObject2)){
fallingObject1.direction = "down";
fallingObject2.direction = "down";
}
//Draw score on screen
fill("purple");
textSize(25);
textFont(normalFont);
text ("Score = " + score, 75, 30);
noStroke();
}
//checks if user wins and restarts
if (score > 19){
youWin();
if (mouseIsPressed){
restart();
}
}
//checks if user loses
if (score<0){
youLose();
}
}
//create functions
function homeScreen() {
background(bgImg);
//Create title
fill('white');
textAlign(CENTER);
textSize(100);
textFont(myFont);
text("Among Us:", 200, 90);
textSize(50);
text("Collection Style", 200, 130);
//Create start button
startButton = new Sprite(startImg,300,300, 'k');
//Create directions button
directionsButton = new Sprite(100,300,120,60, 'k');
directionsButton.color = "black";
directionsButton.textColor = "white";
directionsButton.textSize = 35;
directionsButton.text = "How to play";
strokeWeight(2);
stroke("white");
}
function directionsScreen() {
background(bgImg);
startButton.pos = { x: -200, y: -100 };
directionsButton.pos = { x: -500, y: -100 };
// Draw directions to screen
fill("white");
textSize(42);
textAlign(CENTER);
text("Move the catcher with the left and \nright arrow keys to gather coins \nto win against the impostor. \navoid the asteroids!", 200, 90);
noStroke();
//Create back button
backButton = new Sprite(200,350,120,70, "k");
backButton.color = "black";
backButton.textColor = "white";
backButton.textSize = 30;
backButton.text = "Back to Home";
strokeWeight(2);
stroke("white");
}
function playScreenAssets(){
background(bgImg);
startButton.pos = { x: -200, y: -100 };
directionsButton.pos = { x: -500, y: -100 };
//Create catcher
catcher = new Sprite(catcherImg,200,380,"k");
//Create falling objects
fallingObject1 = new Sprite(fallingObjectImg1,random(50,370),0);
fallingObject1.color = color(0,128,128);
fallingObject1.vel.y = 2;
fallingObject1.rotationLock = true;
fallingObject1.direction = "down";
fallingObject2 = new Sprite(fallingObjectImg2,random (width),0);
fallingObject2.color = color(0,128,128);
fallingObject2.vel.y = 2;
fallingObject2.rotationLock = true;
fallingObject2.direction = "down";
}
function youWin(){
catcher.pos = {x: -50, y: -50};
fallingObject1.pos = {x: -150, y: -150};
fallingObject2.pos = {x: -150, y: -150};
background(winImg);
fill("white");
textFont(normalFont);
textAlign(CENTER);
textSize(20);
text("You win! \nScore: 20", 200, 360);
textSize(15);
text("Click the mouse \nanywhere to play again",width-100,20);
noStroke();
}
function restart (){
score = 0;
catcher.pos = {x: 200, y: 380};
fallingObject1.y = 0;
fallingObject1.x = (random(50,370));
fallingObject1.vel.y = random (1,5);
fallingObject1.direction = "down";
fallingObject2.y = 0;
fallingObject2.x = (random(50,370));
fallingObject2.vel.y = random (1,5);
fallingObject2.direction = "down";
}
function youLose (){
catcher.pos = {x: -50, y: -50};
fallingObject1.pos = {x: -150, y: -150};
fallingObject2.pos = {x: -150, y: -150};
background(loseImg);
textAlign(CENTER);
textFont(normalFont);
textSize(30);
fill("red");
text("DEFEAT", 200,100);
fill("white");
textSize(20);
text("The impostor wins :(", 200, 350);
textSize(15);
text("Click the run \nbutton to play again",width-110,20);
noStroke();
}