generated from TH-Activities/saturday-hack-night-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
80 lines (66 loc) · 1.34 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
let x;
let y;
let size;
let position;
let velocity;
let score;
let gameOver;
function setup() {
createCanvas(500, 500).position((windowWidth - width) / 2, (windowHeight - height) / 2);
size = 50;
x = width / 2 - size / 2;
y = height - size;
position = createVector(random(width), 0);
velocity = createVector(0, 5);
score = 0;
gameOver = false;
}
function draw() {
if (gameOver) {
displayGameOver();
return;
}
background(255, 0, 102);
handleMovement();
updatePosition();
handleCollision();
displayScore();
drawPlayer();
}
function displayGameOver() {
textSize(40);
textAlign(CENTER, CENTER);
text("Game Over!", width / 2, height / 2);
}
function handleMovement() {
if (keyIsDown(LEFT_ARROW) && x > 0) {
x -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x < width - size) {
x += 5;
}
}
function updatePosition() {
fill(255, 255, 0);
ellipse(position.x, position.y, 20, 20);
position.add(velocity);
}
function handleCollision() {
if (position.y >= height) {
if (position.x > x && position.x < x + size) {
score++;
} else {
gameOver = true;
}
position.set(random(width), 0);
}
}
function displayScore() {
textFont('Handjet', 70);
textAlign(CENTER, CENTER);
text(score, width / 2, (height / 2) - 50);
}
function drawPlayer() {
fill(255, 255, 0);
square(x, y, size);
}