2021/12/13
----------------------------------------
小葉老師在協助同學期末作品
----------------------------------------------------
Doodle Jumping
float x=150,y=500-15,vx=0,vy=0;
boolean jumping=false;
float []boardX=new float[10];
float []boardY=new float[10];
void setup(){
size(300,500);
for(int i=0;i<10;i++){
boardX[i]=random(0,300-80);
boardY[i]=450-i*50;
}
}
void draw(){
background(255);
for(int i=0;i<10;i++){
rect(boardX[i],boardY[i],80,15);
}
ellipse(x,y,15,15);
y+=vy; //x+=0.5;
if(jumping) vy+=0.98;
if(y>500-15) y=500-15;
if(vy>=0){
for(int i=0;i<10;i++){
if(boardX[i]<x && x<boardX[i]+80){
if(y<boardY[i] && y+vy>boardY[i]){
jumping=false;
vy=0;
y= boardY[i];
}
}
}
}
}
void keyPressed(){
if(keyCode==UP){
vy=-20; jumping=true;
}
}
----------------------------------------------------------
大魚吃小魚
float []fishX=new float[30];
float []fishY=new float[30];
void setup(){
size(500,500);
for(int i=0;i<30;i++){
fishX[i]=random(-500,500);
fishY[i]=random(-500,500);
}
}
float x=0,y=0;
void draw(){
background(255);
pushMatrix();
translate(-x,-y);
for(int i=0;i<30;i++){
fill(255); ellipse(fishX[i],fishY[i],10,10);
}
fill(255,0,0); ellipse(250+x,250+y,10,10);
popMatrix();
}
void keyPressed(){
if(keyCode==LEFT) x-=3;
if(keyCode==RIGHT) x+=3;
if(keyCode==UP) y-=3;
if(keyCode==DOWN) y+=3;
}
------------------------------------------------------
乒乓球問題修正
void setup(){
size(600,300,P3D);///場景為3D
camera(0, -40, 100, 0, 0, 0, 0, 1, 0);
}
void draw(){
lights();
background(112,146,190);
fill(34,177,76);
//box(100,5,120);
fill(255,0,0);
box(100,30,5);
fill(#502C0F);
//box(10,120,10);
fill(255,255,0);
pushMatrix();
//translate(-300,-250,70);
//translate(300,150,70);
translate((mouseX-width/2)/2.0, (mouseY-height/2)/2.0,0);
noStroke(); sphere(5);
popMatrix();
}
---------------------------------------------------------
打蟑螂
float x,y,vx,vy;
void setup(){
size(300,300);
generateCockroach();
}
void draw(){
background(128);
fill(0); ellipse(x,y,10,10);
x+=vx; y+=vy;
if(x<0||y<0||x>300||y>300) generateCockroach();
noFill(); ellipse(mouseX,mouseY,80,80);
if(mousePressed && dist(mouseX,mouseY,x,y)<40) generateCockroach();
if(frameCount%10==0){
float angle = atan2(vy,vx);
float angle2 = (angle+radians(random(-30,30)));
vx=cos(angle2);
vy=sin(angle2);
}
}
void generateCockroach(){
int edge=int(random(4));
if(edge==0){
x=300; y=random(300); vx=-random(1,3); vy=random(-3,+3);
}else if(edge==1){
x=random(300); y=300; vx=-random(-3,3); vy=random(1,+3);
}else if(edge==2){
x=0; y=random(300); vx=random(1,3); vy=random(-3,+3);
}else if(edge==3){
x=random(300); y=0; vx=-random(-3,3); vy=random(1,+3);
}
}
-----------------------------------------------------------------
電子雞切換圖片動畫
int remainT=0,state=0;
void setup(){
size(300,400);
}
void draw(){
fill(255); rect(50,50,200,200);
if(state==1){
remainT--;
if(remainT>120){
ellipse(150,150,100,100);
}else if(remainT>60){
ellipse(150,150,50,50);
}else if(remainT>0){
ellipse(150,150,80,80);
}else state=0;
}
fill(255,0,0); rect(0,300,100,100);
fill(0,255,0); rect(100,300,100,100);
fill(255,255,0); rect(200,300,100,100);
}
void mousePressed(){
if(mouseY>300&&mouseX<100) { state=1; remainT=180; }
else if(mouseY>300&&mouseX<200) { state=2; remainT=120; }
else if(mouseY>300&&mouseX<300) { state=3; remainT=240; }
}
---------------------------------------------------------
如何判斷碰撞矩形
float x1,y1,w1,h1;
float x2=0,y2=0,w2=100,h2=100;
void setup(){
size(400,400);
w1=random(40,80);
h1=random(40,80);
x1=random(400-w1);
y1=random(400-h1);
}
void draw(){
background(128);
if(testCollision()||testCollision2()) fill(255,0,0);
else fill(255);
rect(x1,y1,w1,h1);
noFill(); rect(x2,y2,w2,h2);
x2=mouseX-w2/2;
y2=mouseY-h2/2;
}
boolean testCollision2(){
if(oneCollision2(x1,y1)) return true;
else if(oneCollision2(x1+w1,y1)) return true;
else if(oneCollision2(x1+w1,y1+h1)) return true;
else if(oneCollision2(x1,y1+h1)) return true;
else return false;
}
boolean oneCollision2(float x,float y){
if(x2<x && x<x2+w2 && y2<y && y<y2+h2) return true;
else return false;
}
boolean testCollision(){
if(oneCollision(x2,y2)) return true;
else if(oneCollision(x2+w2,y2)) return true;
else if(oneCollision(x2+w2,y2+h2)) return true;
else if(oneCollision(x2,y2+h2)) return true;
else return false;
}
boolean oneCollision(float x,float y){
if(x1<x && x<x1+w1 && y1<y && y<y1+h1) return true;
else return false;
}
-----------------------------------------
沒有留言:
張貼留言