=====rex lee surprising space movement===== {{:螢幕擷取畫面_2022-06-22_110843.png?800|}} function setup() { createCanvas(1700, 800); //frameRate(5); noLoop(); } let shapes = ["flower"]; let colors = ["pink", "lightgrey", "lightblue","lavender"]; function pick(inputArray) { return inputArray[Math.floor(inputArray.length * Math.random())]; } function buildArray(n, fillFunction) { let outputArray = []; for (let i = 0; i < n; i++) { outputArray.push(fillFunction(i)); } return outputArray; } function randomRange(min, max) { return min + (max - min) * Math.random(); } class Alien { constructor(x,maxX,y,c) { //this.color = pick(colors); this.color = c; this.shape = pick(shapes); this.xposition = randomRange(x,x+maxX); this.yposition = randomRange(y, y+314); this.size = randomRange(44, 44); } draw() { if (this.color == "black" && !(this.shape == "ellipse")) { this.xposition = 266; this.size = 400; } if (this.yposition>144) {} if (this.shape == "rect") { rect(this.xposition, this.yposition, this.size, this.size); } else if (this.shape == "ellipse") { ellipse(this.xposition, this.yposition, this.size, this.size); } else if (this.shape == "flower") { translate(this.xposition, this.yposition); fill(163, 152, 163); rect(35, 20, 14, 135, 20); rect(5, 64, 55, 25, 20); rect(30, 64, 55, 25, 20); //petal //fill(this.color); if (this.color=="random"){fill(pick(colors))} else {fill(this.color);} rect(0, 20, this.size, this.size, 20); rect(30, 20, this.size, this.size, 20); fill(191, 170, 191); rect(30, 35, 25, 25, 10); resetMatrix(); } else { triangle( this.xposition, this.yposition, this.xposition + this.size, this.yposition + this.size, this.xposition - this.size, this.yposition + this.size ); } //squares fill(this.color); rect(this.xposition, this.yposition, this.size, this.size); fill(227, 94, 84); rect(this.xposition + 10, this.yposition + 10, this.size/2, this.size/2); //alpacas let leftEarPosition = [this.xposition+180, this.yposition+140]; let rightEarPosition = [this.xposition+300,this.yposition+140]; //triangle(30 + leftEarPosition[0], 75 + leftEarPosition[1], 58 + leftEarPosition[0], 20 + leftEarPosition[1], 86 + leftEarPosition[0], 75 + leftEarPosition[1]); //triangle(30 + rightEarPosition[0], 75 + rightEarPosition[1], 58 + rightEarPosition[0], 20 + rightEarPosition[1], 86 + rightEarPosition[0], 75 + rightEarPosition[1]); //head circle(this.xposition+327,this.yposition+304, 84); //eyes ellipse(this.xposition+310,this.yposition+ 300, 5, 25); ellipse(this.xposition+350,this.yposition+ 300, 5, 25); } } //check buildArray on the wiki to make 10 objects // buildArray can use an arrow function; check arrow function on the wiki let myShapes = buildArray(114, (i) => new Alien(0,250,0,"rgb(245,201,205)")); let myShapes2 = buildArray(114, (i) => new Alien(275,800,0,"random")); myShapes2 [myShapes2.length - 1 ].color = "orange" myShapes2 [myShapes2.length - 1 ].size = 77 // i => new RandomShape() // is the same as // function makeShape () {return new RandomShape()} function draw() { background(244, 214, 244); //let shape1 = new RandomShape(); // shape1.draw(); // use forEach to do something with every item in an array // forEach can also use an arrow function console.log(myShapes); myShapes.forEach((x) => x.draw()); myShapes2.forEach((x) => x.draw()); } {{:螢幕擷取畫面_2022-06-22_104958.png?1600|}} function setup() { createCanvas(1920, 1080); noLoop(); } let shapes = ["stars"]; let colors = ["pink", "lightgrey", "lightblue", "lavender"]; function pick(inputArray) { return inputArray[Math.floor(inputArray.length * Math.random())]; } function buildArray(n, fillFunction) { let outputArray = []; for (let i = 0; i < n; i++) { outputArray.push(fillFunction(i)); } return outputArray; } class Spaceship { constructor(size, color, xposition, yposition) { this.size = size; this.color = color; this.xposition = xposition; this.yposition = yposition; } draw() { fill(24, 227, 250) ellipse (this.xposition+34, this.yposition+14, this.size+44, this.size+2); fill(24, 227, 250) ellipse (this.xposition+334, this.yposition+37, this.size+44, this.size+2); fill(244, 227, 250) ellipse (this.xposition+354, this.yposition+27, this.size-2, this.size-24); fill(244, 227, 250) ellipse (this.xposition+180, this.yposition+-43, this.size+44, this.size+2); fill(244, 227, 250) ellipse (this.xposition+54, this.yposition+7, this.size-2, this.size-24); fill(244, 227, 250) ellipse (this.xposition+534, this.yposition+-14, this.size+44, this.size+2); fill(24, 227, 250) ellipse (this.xposition+201, this.yposition+-49, this.size-2, this.size-24); fill(24, 227, 250) ellipse (this.xposition+554, this.yposition+-23, this.size-2, this.size-24); } } class Star { constructor(size, color,xposition,yposition) { this.size = size; this.color = color; this.xposition = xposition; this.yposition = yposition; } draw() { fill(this.color) beginShape(); vertex(this.xposition + 30, this.yposition + 20); vertex(this.xposition + 85, this.yposition + 20); vertex(this.xposition + 85, this.yposition + 75); vertex(this.xposition + 30, this.yposition + 75); vertex(this.xposition + 85, this.yposition + 75); vertex(this.xposition + 30, this.yposition + 75); vertex(this.xposition + 85, this.yposition + 75); vertex(this.xposition + 30, this.yposition + 75); vertex(this.xposition + 85, this.yposition + 75); vertex(this.xposition + 30, this.yposition + 75); vertex(this.xposition + 85, this.yposition + 75); endShape(CLOSE); } } class runningMachine { constructor(size, color,xposition,yposition) { this.size = size; this.color = color; this.xposition = xposition; this.yposition = yposition; } draw() { fill(this.color) rect(this.xposition-200,this.yposition+51,10*this.size,1*this.size) rect(this.xposition+-20,this.yposition-69,1*this.size,7*this.size) rect(100, 369, 200, 44, 20); }} class astronaunt { constructor(size, color,xposition,yposition,radian) { this.size = size; this.color = color; this.xposition = xposition; this.yposition = yposition; this.radian = radian } draw() { fill(this.color) rect(160, 300, 7, 55, 20); rect(144, 300, 7, 55, 20); rect(140,250, 33, 55, 20); rect(130, 200, 55, 55, 20); translate(width / 2, height / 2); rotate(PI / 3.0); rect(169, 251, 10, 50, 20); rect(134, 251, 10, 50, 20); }} let spaceShip1 = new Spaceship (55, "lightgrey", 100, 100) let star1 = new Star (10, "orange",200,200) let runningMachine1 = new runningMachine (20, "lightpurple", 300, 300) let astronaunt1 = new astronaunt (30,"white", 400, 400) function draw() { background(237, 127, 244); fill("grey") ellipse(34,44,-1400,1000) spaceShip1.draw() star1.draw() runningMachine1.draw() astronaunt1.draw() }