這一次不同於上一次所學,內容開始可以產生互動,以下程式碼是滑鼠移動即可產生長方形
void setup() //Start
{
size(800,500);
}
void draw() //Update
{
rect(mouseX,mouseY,50,20);
}
2.接下來可以開始寫模擬打字機畫面
void setup()
{
size(800,300);
for(int x=0;x<800;x+=16){ //設定長寬
for(int y=0;y<300;y+=30)
rect(x,y,16,30);
}
}
void draw()
{
int nowX=mouseX/16*16,nowY=mouseY/30*30; 製作孔洞
fill(0);
rect(nowX,nowY,16,30);
}
若需要讓孔洞不連續
可改為
void setup()
{
size(800,300);
for(int x=0;x<800;x+=16){ //設定長寬
for(int y=0;y<300;y+=30)
rect(x,y,16,30);
}
}
void draw()
{
int nowX=mouseX/16*16,nowY=mouseY/30*30; 製作孔洞
if(mousePressed&&mouseButton==LEFT){
fill(0);
rect(nowX,nowY,16,30);
}
else if(mousePressed&&mouseButton==LEFT){
fill(255);
rect(nowX,nowY,16,30);
}
}
即可改進
而若要插入圖片
加上紅字畫面即可
PImage img;
void setup()
{
size(800,300);
img=loadImage("https://sites.google.com/site/labixiaoxinderenwu/_/rsrc/1461805754566/home/main_animation%402x.png?height=320&width=320");
image(img,0,0,800,300);
}
void draw()
{
int nowX=mouseX/16*16,nowY=mouseY/30*30;
if(mousePressed&&mouseButton==LEFT){
fill(0);
rect(nowX,nowY,16,30);
}
if(mousePressed&&mouseButton==RIGHT){
fill(255);
rect(nowX,nowY,16,30);
}
}
寫卡片
PImage imgBG;
int table[][]=new int[45][10];
void setup(){
size(711,377);
imgBG=loadImage("card.png");
}
void draw(){
background(imgBG);
int nowI=mouseX/16,nowJ=mouseY/30;
if(mousePressed&&mouseButton==LEFT){
table[nowI][nowJ]=1;
}
else if(mousePressed&&mouseButton==RIGHT){
table[nowI][nowJ]=0;
}
for(int i=0;i<45;i++){
for(int j=0;j<10;j++){
fill(0);
if(table[i][j]==1) rect(i*16,j*30,16,30);
}
}
}




沒有留言:
張貼留言