神奇小筆
三種顏色的筆
紅色鋪路,藍色檔烏龜,綠色擋香菇
60秒會碰到公主及過關
<arduino>
int switchStateRed = 0;int switchStateBlue = 0;
int switchStateGreen = 0;
void setup(){
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
Serial.begin(9600);
}
void loop(){
switchStateBlue = digitalRead(2);
switchStateGreen = digitalRead(3);
switchStateRed = digitalRead(4);
if(switchStateRed == HIGH) {
//Serial.write(3);
Serial.print(3);
}
if(switchStateGreen == HIGH) {
//Serial.write(2);
Serial.print(2);
}
if(switchStateBlue== HIGH) {
//Serial.write(1);
Serial.print(1);
}
delay(100);
}
<processing>
import processing.serial.*;
Serial serial;
import ddf.minim.*;
Minim minim;
AudioPlayer star;
PImage imgBG, imgGreen, imgBlue, imgRed, cloudA, cloudB;
PImage man, man1, man2, man3, man4, wife;
PImage mon1, mon2, winImage,paint;
int index;
boolean Game=false;
int wifeX=640, wifeY=250;
boolean wifeF=false;
int cloudX[]= {
640, 800, 900, 1000
};
int cloudY[]= {
100, 150, 80, 75
};
int cloudType[]= {
1, 2, 1, 1
};
int[] I = {
-1282, -1281, -1280, -1279, -1278,
-642, -641, -640, -639, -638,
-2, -1, 0, 1, 2,
638, 639, 640, 641, 642,
1279, 1278, 1280, 1281, 1282
};
int[] I2 = {
-1279, -1278, -1277, -1276, -1275,
-639, -638, -637, -636, -635,
1, 2, 3, 4, 5,
641, 642, 643, 644, 645,
1281, 1282, 1283, 1284, 1285
};
int hole=100;
int HTime=300;
void setup() {
serial=new Serial(this, "COM3", 9600);
minim = new Minim(this);
star=minim.loadFile("Mario.mp3");
music();
size(640, 480);
man=loadImage("man.png");
man1=loadImage("man1.png");
man2=loadImage("man2.png");
man3=loadImage("man3.png");
man4=loadImage("man4.png");
mon1=loadImage("air.png");
mon2=loadImage("mon.png");
winImage=loadImage("win.jpg");
wife=loadImage("wife.png");
cloudA=loadImage("cloud.png");
cloudB=loadImage("cloudB.png");
paint=loadImage("pen.png");
imgBG=createImage(640, 480, RGB);
imgGreen=createImage(640, 480, RGB);
imgRed=createImage(640, 480, RGB);
imgBlue=createImage(640, 480, RGB);
clBK();
clPen(imgGreen);
clPen(imgRed);
clPen(imgBlue);
road();
}
int mon1X[]= {
640, 800
};
int mon1Y[]= {
210, 250
};
int manX=200, manY=250;
int mon2Y=-100;
int S=0, S2=0;
int Color=0;
boolean death=false;
int dlong=80;//if or not fall
int s, read;
boolean win=false;
boolean lossmousic=false;
void draw() {
noCursor();
background(imgBG);
clBK();
if (serial.available()>0)
{
read=serial.read();
println(read);
if (read==49)Color=0;
if (read==50)Color=1;
if (read==51)Color=2;
}
bkDraw();
if (Game) {
if (s==0)s=60;
int s2 = second();
if (s-1==s2)wifeF=true;
if (wifeF) {
image(wife, wifeX, wifeY, 100, 100);
wifeX--;
if (wifeX-manX<50) {
win=true;
Game=false;
}
}
for (int i=0;i<4;i++) {
if (cloudType[i]%2==0)image(cloudA, cloudX[i], cloudY[i], 100, 50);
else image(cloudB, cloudX[i], cloudY[i], 100, 50);
cloudX[i]--;
if (cloudX[i]<0)newcloud(i);
}
runman();
for (int i=0;i<2;i++) {
image(mon1, mon1X[i], mon1Y[i], 100, 100);
int I=(mon1Y[i]-1)*640+mon1X[i]-1;
imgBlue.loadPixels();
for (int j=0;j<100;j++) {
if (imgBlue.pixels[I]==color(#1B16D6)) {
mon1X[i]=640+(int)random(5)*50;
mon1Y[i]=200+(int)random(100);
clPen(imgBlue);
break;
}
I+=640;
}
imgBlue.updatePixels();
if (dist(manX, manY, mon1X[i], mon1Y[i])<=95) {
Game=false;
death=true;
lossmousic=true;
}
mon1X[i]-=3;
} //tortoise
image(mon2, 200, mon2Y, 100, 100);
int J=(mon2Y+100-1)*640+200-1;
imgGreen.loadPixels();
for (int j=0;j<100;j++) {
if (J<0)break;
if (imgGreen.pixels[J]==color(#297111)) {
mon2Y=-(int)random(100)-100;
clPen(imgGreen);
break;
}
J++;
}
imgGreen.updatePixels();
if (dist(manX, manY, 200, mon2Y)<=95) {
Game=false;
death=true;
lossmousic=true;
}
mon2Y++;//mushroom
back();
boolean fall=true;
imgRed.loadPixels();
for (int i=24;i>=0;i--) {
if (imgRed.pixels[223659+I2[i]]==color(#FF0000)) {
manY=(223659+I2[i])/640-100;
fall=false;
dlong=80;
}
}
if (fall) {
dlong--;
if (dlong==0) {
manY=400;
Game=false;
death=true;
lossmousic=true;
}
}
imgRed.updatePixels();
if (Color==0)pen(imgBlue, color(#1B16D6));
else if (Color==1)pen(imgGreen, color(#297111));
else pen(imgRed, color(#FF0000));
}
else {
if (win) {
image(winImage, 0, 0, 640, 480);
}
else {
s = second();
if (death) {
image(man4, manX, manY, 100, 100);
if (lossmousic) {
star.close();
star=minim.loadFile("loss.mp3");
music();
}
}
else image(man, manX, manY, 100, 100);
lossmousic=false;
}
}
image(paint,mouseX-10,mouseY-80,100,100);
}
void runman() {
if (S%20==0)S2++;
if (S2%3==0)image(man1, manX, manY, 100, 100);
else if (S2%3==1)image(man3, manX, manY, 100, 100);
else image(man2, manX, manY, 100, 100);
S++;//run
}
void mouseClicked() {
Game=true;
}
void newcloud(int i) {
cloudX[i]=640+(int)random(100);
cloudY[i]=(int)random(100);
cloudType[i]=(int)random(10);
}
void road() {
imgRed.loadPixels();
int RI=350*640;
for (int i=350*640;i<640*480;i++) {
if (i<352*640)imgRed.pixels[i]=color(#FF0000);
else imgRed.pixels[i]=color(#AA5D1D);
}
imgRed.updatePixels();
}
void back() {
if (HTime==0) {
hole=((int)random(2)+1)*100;
HTime=((int)random(3)+2)*60;
}
if (hole<=0) {
HTime--;
}
imgRed.loadPixels();
for (int i = 1;i<imgRed.pixels.length-1;i++) {
if ((i+1)%640==0) {
imgRed.pixels[i]=color(#000000);
if (i>=224639&&(i-224639)%640==0) {
if (hole<0) {
if (i==224639||i==225279)imgRed.pixels[i]=color(#FF0000);
else imgRed.pixels[i]=color(#AA5D1D);
}
}
continue;
}
imgRed.pixels[i]=imgRed.pixels[i+1];
}
hole--;
imgRed.updatePixels();
}
void bkDraw() {
imgBG.loadPixels();
imgRed.loadPixels();
imgBlue.loadPixels();
imgGreen.loadPixels();
for (int i = 1;i<imgRed.pixels.length-1;i++) {
if (imgRed.pixels[i]!=color(#000000))
imgBG.pixels[i]=imgRed.pixels[i];
if (imgBlue.pixels[i]!=color(#000000))
imgBG.pixels[i]=imgBlue.pixels[i];
if (imgGreen.pixels[i]!=color(#000000))
imgBG.pixels[i]=imgGreen.pixels[i];
}
imgBG.updatePixels();
imgRed.updatePixels();
imgBlue.updatePixels();
imgGreen.updatePixels();
}
void clBK() {
imgBG.loadPixels();
for (int i = 0;i<imgBG.pixels.length;i++) {
imgBG.pixels[i]=color(#18DBEA);
}
imgBG.updatePixels();
}
void clPen(PImage img) {
img.loadPixels();
for (int i = 0;i<imgBG.pixels.length;i++) {
img.pixels[i]=color(#000000);
}
img.updatePixels();
}
void pen(PImage img, color c) {
index=(mouseY-1)*640+mouseX-1;
img.loadPixels();
int I2;
for (int i=0;i<25;i++) {
I2=index+I[i];
if (mouseX>=638||mouseY<=2||mouseX<=2||mouseY>=478)break;
img.pixels[I2]=c;
}
img.updatePixels();
}
void music() {
star.loop();
}
void stop() {
star.close();
minim.stop();
super.stop();
}
沒有留言:
張貼留言