-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
112 lines (99 loc) · 2.15 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
let running = false;
let score = 0;
let catX = 200;
let catY = 200;
const startingMoveTime = 3 * 1000;
let moveTime = startingMoveTime;
let timeLastMoved = 0;
let startTime = 0;
let gameTime = 30 * 1000;
let success;
let click;
let purr;
function preload() {
success = loadSound("success.mp3");
click = loadSound("click.mp3");
purr = loadSound("purr.mp3");
}
function setup() {
createCanvas(400, 400);
}
function drawCat() {
push();
translate(catX - 80, catY - 40);
fill("orange");
stroke("orange");
strokeWeight(8);
line(110, 115, 130, 90);
strokeWeight(1);
ellipse(100, 100, 35, 50);
ellipse(100, 65, 30);
triangle(85, 65, 85, 45, 100, 55);
triangle(115, 65, 115, 45, 100, 55);
pop();
}
function startGame() {
print("Starting game");
moveTime = startingMoveTime;
startTime = millis();
score = 0;
running = true;
moveCat();
}
function stopGame() {
print("Stopping game");
running = false;
success.play();
purr.play();
}
function moveCat() {
catX = random(0, width - 55);
catY = random(0, height - 85);
timeLastMoved = millis();
}
function mousePressed() {
if (running === false) {
startGame();
}
if (mouseX >= catX && mouseX <= catX + 55 &&
mouseY >= catY && mouseY <= catY + 85 &&
running === true) {
score++;
moveCat();
moveTime *= 0.9;
print("Setting move time to " + moveTime);
click.play();
}
}
function draw() {
background(220);
let timeElapsed = millis() - startTime;
let timeString = "Time left: ";
if (running === false && startTime == 0) {
textSize(30);
fill("navy");
text("Welcome to Pet the Cat!", 50, 130);
text("Click to begin", 110, 160);
}
if (running === false && startTime > 0) {
textSize(30);
text("Click to play again", 90, 130);
}
if (running === true) {
timeString = timeString + (round(gameTime - timeElapsed) / 1000);
} else {
timeString = timeString + '0';
}
textSize(20);
text(timeString, 30, 40);
text("Score: " + score, 30, 60);
if (running === true) {
if (gameTime - timeElapsed <= 0) {
stopGame();
}
if (millis() - timeLastMoved >= moveTime) {
moveCat();
}
}
drawCat();
}