-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
104 lines (92 loc) · 2.57 KB
/
Code
File metadata and controls
104 lines (92 loc) · 2.57 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
//Move the catcher with the left and right arrow keys to catch the falling objects.
/* VARIABLES */
let catcher, fallingObject;
let score = 0;
let bird, fallingObjectImg, catcherImg;
/* PRELOAD LOADS FILES */
function preload(){
birdImg = loadImage("assets/bird.png");
fallingObjectImg = loadImage("assets/bird-second.png");
catcherImg = loadImage("assets/nest.png");
}
/* SETUP RUNS ONCE */
function setup() {
createCanvas(400,400);
//Change the size of the image
birdImg.resize(50,0)
fallingObjectImg.resize(150, 0)
catcherImg.resize(200,0)
//Create catcher
catcher = new Sprite(catcherImg, 200,380,100,20, "k");
catcher.color = color(95,158,160);
//Create falling object
fallingObject = new Sprite(fallingObjectImg, 100,0,10);
fallingObject.color = color(0,128,128);
fallingObject.vel.y = 2;
fallingObject.rotationLock = true;
fallingObject.speed = 5;
}
/* DRAW LOOP REPEATS */
function draw() {
background('pink');
image(birdImg, 350, 2)
// Draw directions to screen
fill('white');
textSize(13);
text("Move the \ncatcher with the \nleft and right \narrow keys to \ncatch the falling \nobjects.", width-100, 20);
if (fallingObject.y >= height) {
fallingObject.y = 0;
fallingObject.x = random(width);
fallingObject.vel.y = random(1,5);
//Missed
score = score -1
}
//Move catcher
if (kb.pressing("left")) {
catcher.vel.x = -3;
} else if (kb.pressing("right")){
catcher.vel.x = 3;
} else {
catcher.vel.x = 0;
}
//Stop catcher at the edges of screen
if (catcher.x < 50) {
catcher.x = 50;
} else if (catcher.x > 350) {
catcher.x = 350;
}
// If fallingObject collides with catcher, move back to random position at top and change the speed
if (fallingObject.collides(catcher)) {
fallingObject.y = 0;
fallingObject.x = random(width);
fallingObject.vel.y = random(1,5);
fallingObject.direction = "down";
score = score + 1;
fallingObject.speed = 7;
}
//Draw the score to the screen
fill('black');
textSize(20);
text("Score = " + score, 10, 30);
if (score <0){
background('pink');
catcher.x = 400;
catcher.y = 450;
fallingObject.x = 400;
fallingObject.y = 450;
fill("black");
textSize(20);
text("Game Over", 100, 200);
}
if (score == 8){
background('pink');
catcher.x = 600;
catcher.y = -300;
fallingObject.x = -100;
fallingObject.y = 0;
fill('white');
textSize(20);
text("You win!!", width/2 - 50, height/2 - 30);
}
allSprites.debug = mouse.pressing();
}