xxxxxxxxxx
121
let head=[];
let h1,h2;
let body;
let legs,left,right;
let GRAVITY =0.1;
let virus;
function preload(){
h1 = loadImage("assets/exb1.png");
h2 = loadImage("assets/exb4.png");
body = loadImage("assets/exb2.png");
legs = loadImage("assets/exb3.png");
virus =loadImage("assets/coldflu.png");
left = loadImage("assets/left.png");
right = loadImage("assets/right.png");
}
function setup() {
createCanvas(400,600);
textSize(10);
imageMode(CENTER);
virus.resize(30,30);
}
function draw() {
background("#A5DEE4");
text("Press any key to start.",10,50);
push();
translate(0,40);
//draw the sprites
drawSprites();
loadHead();
loadlegs();
loadbody();
// cursorPos();
if(keyIsPressed){
let newSprite = createSprite(260,110);
newSprite.width = 10;
newSprite.height= 10;
newSprite.setVelocity(random(1,5),random(-5,5));
newSprite.addImage(virus);
}
pop();
for(var i=0; i<allSprites.length; i++) {
var s = allSprites[i];
if(s.position.x<0) {
s.position.x = 1;
s.velocity.x = abs(s.velocity.x);
}
if(s.position.x>width) {
s.position.x = width-1;
s.velocity.x = -abs(s.velocity.x);
}
if(s.position.y<0) {
s.position.y = 1;
s.velocity.y = abs(s.velocity.y);
}
if(s.position.y>height) {
s.position.y = height-1;
s.velocity.y = -abs(s.velocity.y);
}
if(frameCount%600===0){
s.remove();
}
}
}
function loadHead(){
if(keyIsPressed){
text(key,0,100);
image(h2,width/2,100);
h2.resize(180,250);
}else{
image(h1,width/2,100);
h1.resize(180,250);
}
}
function loadbody(){
body.loadPixels();
push();
translate(60,180);
for (let i = 0; i < body.width; i+=10) {
for (let j = 0; j < body.height; j+=10) {
let px = body.get(i, j);
fill(px,10);
noStroke();
ellipse(i, j, 10+(mouseX-i)/50, 10+(mouseY-j)/100);
}
}
pop();
}
function loadlegs(){
imageMode(CORNER);
push();
translate(100,360);
// image(legs,0,0,legs.width/2+10*cos(mouseX/10),legs.height/2+100*sin(mouseY/50));
image(left,0,0,left.width/2+10*cos(mouseX/10),left.height/2+100*cos(mouseY/50)); image(right,100,0,right.width/2+10*cos(mouseX/10),right.height/2+100*sin(mouseY/50));
pop();
}
function cursorPos(){
color(255);
text(mouseX+","+mouseY,0,20);
}
//reference https://p5js.org/reference/#/p5.Image/loadPixels
//http://molleindustria.github.io/p5.play/examples/index.html?fileName=sprite.js
//http://molleindustria.github.io/p5.play/examples/index.html?fileName=collisions4.js
//libraries p5.play.js http://molleindustria.github.io/p5.play/