-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model 1# boxed game
145 lines (129 loc) · 3.32 KB
/
Model 1# boxed game
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
let ball;
let hoop;
let score =0;
let startTime;
let gameDuration = 100;
let xpos, ypos;
let soundFile;
//--------------------------------------
function setup() {
createCanvas(600, 400);
ball = new Ball();
hoop = new Hoop(width - 50, height / 2, 50, 25);
let resetButton = createButton("Reset Score");
resetButton.position(width/60, height-25);
resetButton.mousePressed(resetScore);
startTime = millis();
}
//--------------------------------------------------------
function draw() {
background(255);
ball.update();
ball.display();
ball.checkBoundaries();
ball.applyFriction();
hoop.display();
if (ball.checkCollision(hoop)) {
console.log("You scored!");
score++;
}
textSize(24);
fill(0, 0, 0);
text("Score: " + score, 10, 30);
let elapsedTime = (millis() - startTime) / 1000;
if (elapsedTime >= gameDuration) {
console.log("Time's up!");
noLoop();
}
text("Time remaining: " + (gameDuration - elapsedTime).toFixed(1), 350, 400);
}
//----------------------------------------------
function mousePressed() {
ball.throw(createVector(mouseX, mouseY));
let force = createVector(mouseX, mouseY);
force.sub(ball.pos);
force.normalize();
force.mult(-0.2);
ball.applyForce(force);
soundFile.play();
}
//============================================================
class Ball {
constructor() {
this.pos = createVector(width/2, height/2);
this.vel = createVector(0,0);
this.acc = createVector(0,0.98);
this.friction = 0.98;
this.airResistance = 0.999;
}
//:::::::::::::::::::::::::::::::::::::::::::::::
throw(target) {
this.vel = p5.Vector.sub(target, this.pos);
this.vel.setMag(5);
}
//:::::::::::::::::::::::::::::::::::::::::::::::
update() {
let friction = this.vel.copy();
friction.mult(-1);
friction.normalize();
friction.mult(this.friction);
this.applyForce(friction);
this.vel.mult(this.airResistance);
this.pos.add(this.vel);
}
//::::::::::::::::::::::::::::::::::::::::::::::
display() {
fill(255, 0, 0);
ellipse(this.pos.x, this.pos.y, 25, 25);
}
//:::::::::::::::::::::::::::::::::::::::::::
checkBoundaries() {
if (this.pos.x < 0 || this.pos.x > width) {
this.vel.x *= -1;
}
if (this.pos.y < 0 || this.pos.y > height) {
this.vel.y *= -1;
}
}
//::::::::::::::::::::::::::::::::::::::::::
applyFriction() {
let friction = this.vel.copy();
friction.normalize();
friction.mult(0.5);
this.applyForce(friction);
}
//:::::::::::::::::::::::::::::::::::::::
checkCollision(hoop) {
let x1 = this.pos.x;
let y1 = this.pos.y;
let x2 = hoop.x;
let y2 = hoop.y;
let w2 = hoop.width;
let h2 = hoop.height;
if (x1 > x2 && x1 < x2 + w2 && y1 > y2 && y1 < y2 + h2)
return true;
else
return false;
}
//:::::::::::::::::::::::::::::::::::
applyForce(force) {
this.acc.add(force);
}
}
//---------------------------------------------------------
class Hoop {
constructor(x, y, width, height) {
this.x = x;
this.y = y/2;
this.width = width+width/2;
this.height = height/6;
}
display() {
fill(100, 100, 200);
rect(this.x-this.width/2, this.y - this.height, this.width, this.height);
}
}
//-------------------------------------------
function resetScore() {
score = 0;
}