Created
February 19, 2021 16:25
-
-
Save janeoa/77ccf504b24899dd1a504fbaf72d06df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pos = [0,0] | |
var dir = [1,1] | |
var vel = 5 | |
let bou = [600, 400] | |
class Ball { | |
constructor() { | |
this.x = random(bou[0]); | |
this.y = random(bou[1]); | |
this.diameter = 80; | |
this.speed = vel; | |
} | |
move() { | |
var dx = mouseX - this.x; | |
var dy = mouseY - this.y; | |
var len = sqrt(pow(dx,2) + pow(dy,2)); | |
if(len < 2){ | |
}else{ | |
dx = dx/len; | |
dy = dy/len; | |
this.x += this.speed* dx; | |
this.y += this.speed* dy; | |
} | |
} | |
display() { | |
ellipse(this.x, this.y, this.diameter, this.diameter); | |
print(this.x, this.y) | |
} | |
} | |
function setup() { | |
bug = new Ball(); | |
createCanvas(bou[0], bou[1]); | |
pos = [random(255), random(255)] | |
dir = [random(-1,1),random(-1,1)] | |
print('The value of r is ' + pos); | |
} | |
function draw() { | |
background(220); | |
bug.move(); | |
bug.display(); | |
ellipse(pos[0], pos[1], 80, 80); | |
if(pos[0] > bou[0] - 40) dir[0] = -1 | |
if(pos[1] > bou[1] - 40) dir[1] = -1 | |
if(pos[0] < 0 + 40) dir[0] = 1 | |
if(pos[1] < 0 + 40) dir[1] = 1 | |
pos = [pos[0] + vel * dir[0], pos[1] + vel * dir[1]] | |
} | |
// editor.p5js.org |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment