import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
Serial myPort;
int state=0;
PImage bg, playing, cat87, cat887, ending, end, again;
PImage []rock=new PImage[5];
float catx=300, caty=400, catvx=0, catvy=0;
float []rockx=new float[5];
float []rocky=new float[5];
float []rockvx=new float[5];
float []rockvy=new float[5];
boolean catL=false, catR=false;
int time, score;
void setup() {
size(600, 800);
myPort = new Serial(this, "COM3", 9600);
minim =new Minim(this);
player=minim.loadFile("Bongo Cat - Nyan Cat.mp3");
bg=loadImage("title.png");
playing=loadImage("universe.png");
cat87=loadImage("nyan-cat.png");
cat887=loadImage("nyan-cat87.png");
ending=loadImage("game over.png");
end=loadImage("god_logo_800.jpg");
again=loadImage("play-again.png");
imageMode(CENTER);
for (int i=0; i<5; i++) {
rock[i]=loadImage("ball.png");
rockx[i]=random(50, 550);
rocky[i]=random(50, 750);
rockvx[i]=5*random(-1, 1);
rockvy[i]=5*random(-1, 1);
}
player.loop();
}
void draw() {
frameRate=30;
getData();
if (state==0) {
image(bg, width/2, height/2, width, height);
if (data[2]==1)
state=1;
} else if (state==1) {
image(playing, width/2, height/2, width, height);
if (data[0]==64 || data[1]==64) {
catvx=0;
catvy=0;
}
catvx=(data[0]-64)/5.0;
catvy=(data[1]-64)/5.0;
catx+=catvx;
caty+=catvy;
if (!(catR) && !(catL)) {
image(cat87, catx, caty, 133, 100);
}
if (catvx<0) {
image(cat887, catx, caty, 133, 100);
catL=true;
catR=false;
} else if (catvx>0) {
image(cat87, catx, caty, 133, 100);
catR=true;
catL=false;
} else {
if (catL) image(cat887, catx, caty, 133, 100);
else if (catR) image(cat87, catx, caty, 133, 100);
}
if (catx<60) catx=60;
if (catx>538) catx=538;
if (caty<30) caty=30;
if (caty>763) caty=763;
for (int i=0; i<5; i++) {
image(rock[i], rockx[i], rocky[i], 75, 73);
rockx[i]+=rockvx[i];
rocky[i]+=rockvy[i];
if (frameCount%30==0) {
rockvx[i]=rockvx[i]*random(1.0, 1.1);
rockvy[i]=rockvy[i]*random(1.0, 1.1);
}
if (rockx[i]<=30 || rockx[i]>=566)
rockvx[i] = -rockvx[i];
if (rocky[i]<=30 || rocky[i]>=763)
rockvy[i] = -rockvy[i];
if ((time+29)/30>1 && dist(rockx[i], rocky[i], catx, caty)<(75/2+45/2)) {
state=2;
catx=300;
caty=400;
}
}
time++;
fill(255);
textSize(50);
if ((time+29)/30<10) text(((time+29)/30), 300-10, 60);
else text(((time+29)/30), 300-15-10, 60);
score=(time+29)/30*10;
} else if (state==2) {
image(end, width/2, height/2, width, height);
image(ending, 300, 400+20, 870/4, 460/4);
image(again, 300, 650, 200, 100);
text("your score is "+score, 150-40, 220);
if (data[0]==64 || data[1]==64) {
catvx=0;
catvy=0;
}
catvx=(data[0]-64)/5.0;
catvy=(data[1]-64)/5.0;
catx+=catvx;
caty+=catvy;
if (!(catR) && !(catL)) {
image(cat87, catx, caty, 133, 100);
}
if (catvx<0) {
image(cat887, catx, caty, 133, 100);
catL=true;
catR=false;
} else if (catvx>0) {
image(cat87, catx, caty, 133, 100);
catR=true;
catL=false;
} else {
if (catL) image(cat887, catx, caty, 133, 100);
else if (catR) image(cat87, catx, caty, 133, 100);
}
if (catx<60) catx=60;
if (catx>538) catx=538;
if (caty<30) caty=30;
if (caty>763) caty=763;
if (catx>200-50 && catx<400+50 && caty>550-50 && caty<650+50 && data[2]==1) {
time=0;
score=0;
catx=300;
caty=400;
for (int i=0; i<5; i++) {
rockx[i]=random(50, 550);
rocky[i]=random(50, 750);
rockvx[i]=5*random(-1, 1);
rockvy[i]=5*random(-1, 1);
}
state=1;
}
}
}
byte [] data = new byte[3];
void getData() {
if (myPort.available()>0) {
myPort.readBytes(data);
}
}
沒有留言:
張貼留言