random seat
void setup(){ size(600,500);
}
void draw(){ background(255);
for(int i=0;i<4;i++){ for(int j=0;j<6;j++){ fill(#F5B4B4); rect(j*100,i*100,100,100);
fill(0); text("Hello",j*100+25,i*100+50); }
}
}
random seat array value
- 另存程式碼,將hello改成value
- value[i][j]=int(random(40)); //產生亂數
value[i][j]=int(random(40));
int [][] value=new int[4][6];
fill(#F5B4B4); rect(j*100,i*100,100,100);
fill(0); text(value[i][j],j*100+25,i*100+50);
random seat array value swap
- 加上for迴圈,讓兩對數值交換位子(總共有四個數位子會變)
int i=int(random(4)),j=int(random(6));
int i2=int(random(4)),j2=int(random(6));
value[i][j]=value[i2][j2];
int [][] value=new int[4][6];
fill(#F5B4B4); rect(j*100,i*100,100,100);
fill(0); text(value[i][j],j*100+25,i*100+50);
fill(0); text("Click to Start",80,200);
many states mousePressed keyPressed
- 再做一個灰色方塊在左上角,要設 x=50,y=50;
- 加入keyPressed程式碼,使灰色方塊可以向左移動
fill(0); text("Click to Start",80,200); fill(255); rect(300,50,50,50);
fill(128); rect(x,y,50,50);
if(state==1 && keyCode==RIGHT){
many states mousePressed keyPressed milits t 60
fill(0); text("Click to Start",80,200); fill(255); rect(300,50,50,50);
fill(128); rect(x,y,50,50);
fill(255,255,0); text("Wait "+t,80,200); if(state==1 && keyCode==RIGHT){
投籃機
- 新增程式碼視窗,寫出投籃的程式碼
- 要記得下載圖片跟將圖片拖進程式碼內
imgRack = loadImage("rack.jpg"); fill(255); ellipse(posX,posY,20,20);
line(posX,posY,mouseX,mouseY);
fill(#FFA652); ellipse(mouseX,mouseY,20,20);
float posX=-100,posY=-100;
投籃機 正常投籃
- 另存程式碼,加入程式碼,新增滑鼠事件,讓球可以被射出去
imgRack = loadImage("rack.jpg"); fill(255); ellipse(posX,posY,20,20);
fill(#FFA652); ellipse(x,y,20,20);
float posX=-100,posY=-100;
投籃機 可以暫停
- 更改程式碼,改成gameOver
- 加入程式碼,如果按按鍵就會暫停
PImage imgRack;
void setup(){ size(300,300);
imgRack = loadImage("rack.jpg");}
boolean gameOver=false;
void keyPressed(){ gameOver=true;
}
void draw(){ background(255);
image(imgRack,0,0);
if(flying){ if(!gameOver){ x+=vx;
y+=vy;
vy+=0.98;
}
}else{ x=mouseX;
y=mouseY;
if(mousePressed){ fill(255); ellipse(posX,posY,20,20);
line(posX,posY,x,y);
}
}
fill(#FFA652); ellipse(x,y,20,20);
}
boolean flying = false;
float x,y,vx,vy;
float posX=-100,posY=-100;
void mousePressed(){ x=posX=mouseX;
y=posY=mouseY;
flying=false;
}
void mouseReleased(){ vx=posX-mouseX;
vy=posY-mouseY;
flying=true;
}
踩地雷
int [][] mine =new int[9][9];
for(int i=0;i<9;i++) mine[i][0]=9;
int i=int(random(9)),j=int(random(9));
int i2=int(random(9)),j2=int(random(9));
void addNumber(int i,int j){ if(i<0||j<0||i>=9||j>=9)return;
if(mine[i][j]!=9)mine[i][j]++;
if(mine[i][j]==9)fill(255,128,128);
fill(0); text(""+mine[i][j],j*50+23,i*50+23,45,45);
貪食蛇
void setup(){ size(600,400);
}
float x=300,y=200,dir=0;
void draw(){ //background(0);
joker(x,y,dir);
x+=cos(dir);
y+=sin(dir);
}
void joker(float x,float y,float dir){ ellipse(x,y,50,50);
line(x,y,x+25*cos(dir),y+25*sin(dir));
}
void keyPressed(){ if(keyCode==RIGHT)dir+=0.1;
if(keyCode==LEFT)dir-=0.1;
}
沒有留言:
張貼留言