Dæmi: blómaklasinn:
var flowers = []; var number = 20; function setup(){ createCanvas(600, 600); for(var i = 0; i < number; i++){ flowers[i] = new Flower(); } } function draw() { for(var i = 0; i < number; i++){ flowers[i].drawFlower(); } } function Flower(){ this.x = random(width); this.y = random(height); this.flowerDiameter = random(40); this.drawFlower = function(){ fill(100,0,140); ellipse(this.x+this.flowerDiameter/2, this.y, this.flowerDiameter, this.flowerDiameter); ellipse(this.x, this.y+this.flowerDiameter/2, this.flowerDiameter, this.flowerDiameter); ellipse(this.x-this.flowerDiameter/2, this.y, this.flowerDiameter, this.flowerDiameter); ellipse(this.x, this.y-this.flowerDiameter/2, this.flowerDiameter, this.flowerDiameter); fill(180,0,120); ellipse(this.x, this.y, this.flowerDiameter, this.flowerDiameter); }; this.newPosition = function(){ this.x = random(width); this.y = random(height); }; } function mousePressed(){ for(var i = 0; i< number; i++){ flowers[i].newPosition(); } }
Leikur með 2 listum af hlutum
var flame; // Declare object var friendFlames = []; var enemyFlames = []; var number = 10; var playerX; var playerY; var stig = 0; function setup() { createCanvas(600,600); playerX = width/2; playerY = height/2; stig = 0; var friendColor = color(200,100,200, 230); var enemyColor = color(0); // Create object for(var i = 0; i < number; i++){ friendFlames[i] = new Flame(random(width), random(height), friendColor); enemyFlames[i] = new Flame(random(width), random(height), enemyColor); } } function draw() { background(255); for(var i = 0; i< friendFlames.length ; i++){ friendFlames[i].move(); friendFlames[i].display(); enemyFlames[i].move(); enemyFlames[i].display(); if(friendFlames[i].collision(playerX,playerY)){ friendFlames[i].fillC=color(255); stig = stig+1; } if(enemyFlames[i].collision(playerX,playerY)){ enemyFlames[i].fillC=color(255); stig = stig-1; } } console.log(stig); fill(0); if(stig< 0){ background(255); text("Þú tapaðir...", 50,50) }else{ text(str(stig), 50,50, 100, 100); } fill(200,200,0); ellipse(playerX, playerY, 20,20); if (keyIsDown(LEFT_ARROW)) playerX-=5; if (keyIsDown(RIGHT_ARROW)) playerX+=5; if (keyIsDown(UP_ARROW)) playerY-=5; if (keyIsDown(DOWN_ARROW)) playerY+=5; } // Jitter class function Flame(x,y, fillC) { this.fillC = fillC; this.x = x; this.y = y; this.diameter = random(10, 30); this.xSpeed = random(-5,5); this.ySpeed = random(-5,5); this.alive = true; this.move = function() { this.x += this.xSpeed; this.y += this.ySpeed; if ((this.x > width) || (this.x < 0)) { this.xSpeed = this.xSpeed * -1; } if ((this.y > height) || (this.y < 0)) { this.ySpeed = this.ySpeed * -1; } }; this.display = function() { fill(this.fillC); ellipse(this.x, this.y, this.diameter, this.diameter); } this.collision = function(x,y){ if(dist(x,y,this.x,this.y)<(this.diameter/2) && this.alive){ this.alive = false; return true; } else{ return false; } } }