Commit 70d8021e authored by Sander Kvenild's avatar Sander Kvenild
Browse files

Discovered a bug

parent 26e493b8
......@@ -10,103 +10,104 @@
<script>
class Boble {
constructor(x,y,r) {
this.x = x;
this.y = y;
this.r = r;
this.farge = randomColor();
this.orginalFarge = this.farge;
}
flytt() {
this.x = this.x + Math.floor(Math.random() * 10 -5);
this.y = this.y + Math.floor(Math.random() * 10 -5);
}
vis() {
ctx.beginPath();
ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
ctx.fillStyle = this.farge;
ctx.fill();
ctx.strokeStyle = "white";
ctx.stroke();
}
inneholder(musx,musy) {
let a = musx - this.x;
let b = musy - this.y;
let d = Math.sqrt(a*a + b*b);
if (d < this.r) {
return true;
} else {
return false;
// FIXME: Noen bobler blir røde av at andre bobler velges
class Boble {
constructor(x,y,r) {
this.x = x;
this.y = y;
this.r = r;
this.farge = randomColor();
this.orginalFarge = this.farge;
}
flytt() {
this.x = this.x + Math.floor(Math.random() * 10 -5);
this.y = this.y + Math.floor(Math.random() * 10 -5);
}
vis() {
ctx.beginPath();
ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
ctx.fillStyle = this.farge;
ctx.fill();
ctx.strokeStyle = "white";
ctx.stroke();
}
inneholder(musx,musy) {
let a = musx - this.x;
let b = musy - this.y;
let d = Math.sqrt(a*a + b*b);
if (d < this.r) {
return true;
} else {
return false;
}
}
}
}
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.addEventListener("mousedown",musklikk,false);
canvas.addEventListener("mousemove",musbeveg,false);
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.addEventListener("mousedown",musklikk,false);
canvas.addEventListener("mousemove",musbeveg,false);
var bobler = [];
var bobler = [];
for(let i = 0; i < 10; i++) {
let x = Math.floor(Math.random() * canvas.width);
let y = Math.floor(Math.random() * canvas.height);
let r = Math.floor(Math.random() * 40 + 10);
bobler[i] = new Boble(x,y,r);
}
for(let i = 0; i < 10; i++) {
let x = Math.floor(Math.random() * canvas.width);
let y = Math.floor(Math.random() * canvas.height);
let r = Math.floor(Math.random() * 40 + 10);
bobler[i] = new Boble(x,y,r);
}
setInterval(tegn,100);
setInterval(tegn,100);
function tegn() {
reset();
for(let i = 0; i < bobler.length; i++) {
bobler[i].flytt();
bobler[i].vis();
}
}
function tegn() {
reset();
for(let i = 0; i < bobler.length; i++) {
bobler[i].flytt();
bobler[i].vis();
function reset() {
ctx.fillStyle = "black";
ctx.fillRect(0,0,canvas.width,canvas.height);
}
}
function reset() {
ctx.fillStyle = "black";
ctx.fillRect(0,0,canvas.width,canvas.height);
}
function musklikk(event) {
var fikkvalgtenboble = false;
function musklikk(event) {
var fikkvalgtenboble = false;
for(let i = 0; i < bobler.length; i++) {
if(bobler[i].inneholder(event.x,event.y)) {
bobler.splice(i,1);
fikkvalgtenboble = true;
}
}
for(let i = 0; i < bobler.length; i++) {
if(bobler[i].inneholder(event.x,event.y)) {
bobler.splice(i,1);
fikkvalgtenboble = true;
if(fikkvalgtenboble == false) {
let r = Math.floor(Math.random() * 40 + 10);
let b = new Boble(event.x,event.y,r);
bobler.push(b);
}
}
if(fikkvalgtenboble == false) {
let r = Math.floor(Math.random() * 40 + 10);
let b = new Boble(event.x,event.y,r);
bobler.push(b);
}
}
function musbeveg(event) {
for(let i = 0; i < bobler.length; i++) {
if(bobler[i].inneholder(event.x,event.y)) {
bobler[i].farge = "red";
} else {
bobler[i].farge = bobler[i].orginalFarge;
function musbeveg(event) {
for(let i = 0; i < bobler.length; i++) {
if(bobler[i].inneholder(event.x,event.y)) {
bobler[i].farge = "red";
} else {
bobler[i].farge = bobler[i].orginalFarge;
}
}
}
}
function randomColor() {
let r = Math.floor(Math.random()*255).toString(16)
let g = Math.floor(Math.random()*255).toString(16)
let b = Math.floor(Math.random()*255).toString(16)
return `#${r}${g}${b}`
}
function randomColor() {
let r = Math.floor(Math.random()*255).toString(16)
let g = Math.floor(Math.random()*255).toString(16)
let b = Math.floor(Math.random()*255).toString(16)
return `#${r}${g}${b}`
}
</script>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment