PImage imgMario,banna,terriost,spider,pie,start;
import processing.serial.*;
Serial serial;
float marioX=100,marioY=750;
float marioVX=0,marioVY=0;
float[] bulletX=new float[300];
float[] bulletY=new float[300];
boolean[] bulletAlive=new boolean[300];
int bulletN=0;
float[] spiderX=new float[100];
float[] spiderY=new float[100];
float[] pieX=new float[100];
float[] pieY=new float[100];
boolean[] spiderAlive=new boolean[300];
boolean[] pieAlive=new boolean[300];
void setup(){
size(800,955);
serial = new Serial(this, "COM4", 9600);
imgMario=loadImage("mouse.png");
banna=loadImage("banna.png");
terriost=loadImage("background1.jpg");
spider=loadImage("spider.png");
pie=loadImage("pie.png");
start=loadImage("start.jpg");
for(int i=0;i<100;i++){
spiderX[i]=random(1024)+100;
spiderY[i]=-100*i;
spiderAlive[i]=true;
}
for(int i=0;i<100;i++){
pieX[i]=random(1024)+100;
pieY[i]=-100*i;
pieAlive[i]=true;
}
}
int x=512, y=512;
int score=0;
int count=2400;
int stage=0;
void draw(){
if(stage==0)
{
background(start);
if(mousePressed) stage=1;
}
if(stage==1)
{
gun();
if(count==0||score<0)
{
stage=2;
}
}
if(stage==2)
{
count=2400;
score=0;
background(255,0,0);
if(mousePressed)
stage=1;
}
textSize(40);
fill(255,0,0);
text("Time:"+int(count/100),100, 50);
textSize(40);
fill(255,0,0);
text("The highest score:"+int(score), 300, 50);
}
void gun()
{
background(terriost);
image(imgMario,marioX,marioY,200,200);
marioX+=marioVX;
marioY+=marioVY;
if(marioY<=100) marioVY+=0.9;
else marioY=750;
if (marioX<-20) marioX=-20;
if(marioX>700) marioX=700;
if(serial.available()>0){
String now = serial.readString();
println(now);
String [] xy = splitTokens(now);
marioVX=(int(xy[0])-500)/200.0;
marioVY=(int(xy[1])-500)/200.0;
println("x:"+x+" y:"+y+"==");
}
for(int i=0;i<100;i++){
if(spiderAlive[i]) {
image(spider, spiderX[i], spiderY[i], 80, 80);
if (spiderY[i]>800) {
spiderY[i]=-1000;
spiderX[i]=random(750-50)+25;
}
if(dist(spiderX[i], spiderY[i], marioX, marioY )<60){
score-=1;
spiderY[i]=-1000;
spiderX[i]=random(750-50)+25;
}
}
spiderY[i]+=1;
}
for(int i=0;i<100;i++){
if(pieAlive[i]) {
image(pie, pieX[i], pieY[i], 80, 80);
if (pieY[i]>800) {
pieY[i]=-1000;
pieX[i]=random(750-50)+25;
}
if(dist(pieX[i], pieY[i], marioX, marioY )<60){
score-=1;
pieY[i]=-1000;
pieX[i]=random(750-50)+25;
}
}
pieY[i]+=1;
}
for(int i=0;i<bulletN;i++){
if(bulletAlive[i]){
fill(0);
ellipse(bulletX[i],bulletY[i],8,8);
bulletY[i]-=8;
if(bulletY[i]<0) bulletAlive[i]=false;
for(int k=0;k<100;k++){
if(dist(bulletX[i],bulletY[i],spiderX[k],spiderY[k])<40){
spiderAlive[k]=false;
bulletAlive[i]=false;
score+=1;
}
}
for(int k=0;k<100;k++){
if(dist(bulletX[i],bulletY[i],pieX[k],pieY[k])<40){
pieAlive[k]=false;
bulletAlive[i]=false;
score+=1;
}
}
}
}
count--;
}
void recycle()
{
for(int i=0;i<bulletN;i++){
if(bulletAlive[i]==false) {
for(int k=i;k<bulletN-1;k++){
bulletX[k]=bulletX[k+1];
bulletY[k]=bulletY[k+1];
bulletAlive[k]=bulletAlive[k+1];
}
bulletN--;
}
}
}
void keyPressed(){
if(keyCode=='a')
if(bulletN>=30) recycle();
int now=bulletN;
bulletX[now]=marioX;
bulletY[now]=marioY;
bulletAlive[now]=true;
bulletN++;
}
沒有留言:
張貼留言