-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.ts
41 lines (33 loc) · 931 Bytes
/
ball.ts
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
import p5 from 'p5';
export class Ball {
private direction = 1;
constructor(private position: p5.Vector, private diameter: number, private speed?: p5.Vector) {
}
private move(){
if(this.speed == null){ return; }
this.position = this.position.add(p5.Vector.mult(this.speed, this.direction));
}
private turn(p: p5){
if (this.position.x < 0){
this.position.x = 0;
this.direction = -this.direction;
}
if (this.position.x > p.width){
this.position.x = p.width;
this.direction = -this.direction;
}
if (this.position.y < 0){
this.position.y = 0;
this.direction = -this.direction;
}
if (this.position.y > p.height){
this.position.y = p.height;
this.direction = -this.direction;
}
}
draw(p: p5): void{
p.ellipse(this.position.x, this.position.y, this.diameter, this.diameter);
this.move();
this.turn(p);
}
}