如题,程序如下, 效果如图。新手 求教。
import processing.serial.*;
Main main;
Share coordinate;
Back back;
Serial myPort;
void setup()
{ size(600,400);
coordinate=new Share(300,300);
main=new Main(coordinate);
back=new Back(0,100);
try{
myPort=new Serial(this,Serial.list()[0],9600);
myPort.clear();
}
catch(Exception e) {coordinate.keyboard=true;
}
}
void draw(){background(255);
back.display();
main.display();
bulletDisplay();
obstacleDisplay();
bloodDisplay();
scoreDisplay();
coordinate.display();
if (!coordinate.keyboard) serialControl();
highscore();
mode();
}
void keyPressed()
{if (key=='w') main.direction[0]=true;
if (key=='s') main.direction[1]=true;
if (key=='a') main.direction[2]=true;
if (key=='d') main.direction[3]=true;
}
void keyReleased()
{if (key=='w') main.direction[0]=false;
if (key=='s') main.direction[1]=false;
if (key=='a') main.direction[2]=false;
if (key=='d') main.direction[3]=false;
if (key=='q') {
coordinate.pause=!coordinate.pause;
if(coordinate.pause) noLoop();
else loop();
}
if(key=='r'){coordinate.restart=true;
loop();
}if(key=='c'){if(coordinate.keyboard){try{
myPort=new Serial(this,Serial.list()[0],9600);
myPort.clear();
coordinate.keyboard=false;}
catch(Exception e){}}
else coordinate.keyboard=true;
}}
void bulletDisplay()
{if (coordinate.shoot==true) {
if (frameCount-coordinate.prv>15){
coordinate.bullet.add(new Bullet(coordinate));
coordinate.prv=frameCount;
}
}
for (Bullet i:coordinate.bullet){i.display();}
}
void obstacleDisplay()
{if((coordinate.score%10)==0){coordinate.score++;
coordinate.obstacle.add(new Obstacle(coordinate,1));
}
if(random(10)<0.2){
coordinate.obstacle.add(new Obstacle(coordinate,0));
}
for(Obstacle i:coordinate.obstacle) {i.display();
}
}
void bloodDisplay(){
textSize(12);
fill(0);
text(coordinate.blood,50,50);
fill(255);
}
void scoreDisplay()
{textSize(12);
text("score:",80,50);
text(coordinate.score-1,120,50);
}
void mode()
{if(!coordinate.keyboard){
textSize(12);
text("on serial",400,50);}
else{textSize(12);
text("on keyboard",400,50);
}}
void serialControl()
{String message=myPort.readStringUntil(coordinate.LF);
if (message!=null){
String[] data =message.split(".");
if (data.length>2){
int tempX=Integer.parseInt(data[1]);
int tempY=Integer.parseInt(data[2]);
print("x: ");
println(tempX);
print("y: ");
println(tempY);
if(tempY<400){
main.direction[0]=true;}
else if (tempY>600){
main.direction[1]=true;}
else {
main.direction[0]=false;
main.direction[1]=false;}
if(tempX<400){
main.direction[2]=true;
}else if(tempX>600){
main.direction[3]=true;}
else{
main.direction[2]=false;
main.direction[3]=false;}}}}
void highscore(){
textSize(12);
text("highscore:"+coordinate.oldhighscore,250,50);}
class Back {PImage[] background;
int x;
int y;
int count;
int x1;
int x2;
Back(int x,int y)
{background=new PImage[2];
this.x1=x;
this.x2=x+900;
this.y=y;
for (int i=0;i<2;i++){String a="background"+".bmp";
background[i]=loadImage(a);}}
void run(){
x1--;
x2--;}
void display(){
if(x1==-900){x1=900;
}
if (x2==-900){x2=900;}
image(background[0],x1,y);
image(background[1],x2,y);}}
class Bullet{
float x;
float y;
int r;
boolean exist;
PImage png;
Share coordinate;
Bullet(Share coordinate){
this.coordinate=coordinate;
exist=true;
x=coordinate.x;
y=coordinate.y;
png=loadImage("pic/bullet.png");
r=10;}
void update(){
if(exist){cal();}
}
void cal(){y=-6;}
void des(){if (x<0||x>600||y<0||y>400){
exist=false;
}}
void display(){update();
if (exist){
image(png,x-7,y-7);}
}}
class Explosion{
int state;
float x;
float y;
boolean exist;
PImage[] photo;
String name;
Explosion(float x,float y){state=0;
this.x=x;
this.y=y;
photo=new PImage[6];
for(int i=0;i<6;i++){
name="pic/"+(i+2)+".png";
//sprintln(name);
photo[i]=loadImage(name);}}
void explode(){image(photo[state],x,y);
state=++state%6;}
void display(){if(exist){explode();
if(state==0){exist=false;}}}
}
class Main{
int r;
Share coordinate;
int state;
boolean[] direction;
PImage aircraft;
Main(Share coordinate){
this.coordinate=coordinate;
direction=new boolean[4];
aircraft=loadImage("pic/plane2.png");
r=50;
}
void up(){if(direction[0]&&coordinate.y>0){coordinate.y-=2;}}
void down(){
if(direction[1]&&coordinate.y<350){coordinate.y+=2;
}}
void left(){if(direction[2]&&coordinate.x>0){coordinate.x-=2;
}}
void right(){if(direction[3]&&coordinate.x<600 ){coordinate.x+=2;}}
void display(){up();
down();
left();
right();
image(aircraft,coordinate.x-25,coordinate.y-25);}}
class Obstacle{
float x;
float y;
int r;
int state;
int blood;
float distance1;
float distance2;
boolean exist;
int shape;
Share coordinate;
Explosion temp;
PImage photo;
String name;
boolean ok;
int timer;
Obstacle(Share coordinate,int state){
this.coordinate=coordinate;
this.x=random(600);
this.y=0;
this.state=state;
exist=true;
if(state==0){r=20;
blood=1;
name="pic/bee";
shape=int(random(4));
name+=(shape+1);
name+=".png";
distance1=900;
distance2=1500;
}if(state==1){
r=30;
blood=15;
name="pic/boss";
shape=int(random(4));
name+=(shape+1);
name+=".png";
distance1=3000;
distance2=3000;
}
shape=int(random(4));
photo=loadImage(name);
}void follow(){
if(state==1){if(x<coordinate.x) x++;
if(y<coordinate.y) y++;
if(x>coordinate.x) x--;
if(y>coordinate.y) y--;}
if(state==0){y+=2;}}
void update(){
for (Bullet i:coordinate.bullet){
if(i.exist&&exist&&((pow(x-i.x,2)+pow(y-i.y,2))<distance1)){blood--;
i.exist=false;
if(blood==0){exist =false;
coordinate.score+=7;
}}}
if(exist&&(pow(x-coordinate.x,2)+pow(y-coordinate.y,2))<distance2){
exist=false;
coordinate.blood-=1;
coordinate.score++;}}
void display(){
update();
if(exist){follow();
image(photo,x-20,y-15);
}
else{
if(!ok){
temp=new Explosion(x,y);
temp.exist=true;
ok=!ok;
}
if(timer--==0){
temp.explode();
}
temp.display();
}}}
class Share{
int x;
int y;
int score;
int blood;
int prv;
ArrayList<Bullet>bullet;
ArrayList<Obstacle>obstacle;
boolean pause;
PImage show;
boolean restart;
int oldhighscore;
int LF=10;
boolean keyboard;
boolean shoot;
PrintWriter output;
BufferedReader reader;
String line;
Share(int x, int y){
this.x=x;
this.y=y;
score=1;
prv=-3;
blood=3;
shoot=true;
bullet=new ArrayList<Bullet>();
obstacle=new ArrayList<Obstacle>();
show= loadImage("pic/plane1.png");
reader=createReader("positions.txt");
try{
line=reader.readLine();
}
catch(Exception e){
line=null;
}
if(line==null){
}
else{
oldhighscore=Integer.parseInt(line);
println("this is high;"+oldhighscore);
}}
void display(){
if(blood<1&&!restart){
textSize(20);
text("game is over!",200,200);
if(score>oldhighscore){
output=createWriter("positions.txt");
output.flush();
output.println(""+score);
output.close();
noLoop();
}
}
if (restart){
restart=!restart;
bullet=new ArrayList<Bullet>();
obstacle=new ArrayList<Obstacle>();
if(score>oldhighscore){
oldhighscore=score;
output=createWriter("positions.txt");
output.flush();
output.println(""+score);
output.close();
}
score=1;
prv=-3;
blood=3;
}
for(int i=0,j=60;i<blood;i++,j+=50){
fill(250,0,0);
image(show,j,0);
fill(255);
}}
}
图。。
import processing.serial.*;
Main main;
Share coordinate;
Back back;
Serial myPort;
void setup()
{ size(600,400);
coordinate=new Share(300,300);
main=new Main(coordinate);
back=new Back(0,100);
try{
myPort=new Serial(this,Serial.list()[0],9600);
myPort.clear();
}
catch(Exception e) {coordinate.keyboard=true;
}
}
void draw(){background(255);
back.display();
main.display();
bulletDisplay();
obstacleDisplay();
bloodDisplay();
scoreDisplay();
coordinate.display();
if (!coordinate.keyboard) serialControl();
highscore();
mode();
}
void keyPressed()
{if (key=='w') main.direction[0]=true;
if (key=='s') main.direction[1]=true;
if (key=='a') main.direction[2]=true;
if (key=='d') main.direction[3]=true;
}
void keyReleased()
{if (key=='w') main.direction[0]=false;
if (key=='s') main.direction[1]=false;
if (key=='a') main.direction[2]=false;
if (key=='d') main.direction[3]=false;
if (key=='q') {
coordinate.pause=!coordinate.pause;
if(coordinate.pause) noLoop();
else loop();
}
if(key=='r'){coordinate.restart=true;
loop();
}if(key=='c'){if(coordinate.keyboard){try{
myPort=new Serial(this,Serial.list()[0],9600);
myPort.clear();
coordinate.keyboard=false;}
catch(Exception e){}}
else coordinate.keyboard=true;
}}
void bulletDisplay()
{if (coordinate.shoot==true) {
if (frameCount-coordinate.prv>15){
coordinate.bullet.add(new Bullet(coordinate));
coordinate.prv=frameCount;
}
}
for (Bullet i:coordinate.bullet){i.display();}
}
void obstacleDisplay()
{if((coordinate.score%10)==0){coordinate.score++;
coordinate.obstacle.add(new Obstacle(coordinate,1));
}
if(random(10)<0.2){
coordinate.obstacle.add(new Obstacle(coordinate,0));
}
for(Obstacle i:coordinate.obstacle) {i.display();
}
}
void bloodDisplay(){
textSize(12);
fill(0);
text(coordinate.blood,50,50);
fill(255);
}
void scoreDisplay()
{textSize(12);
text("score:",80,50);
text(coordinate.score-1,120,50);
}
void mode()
{if(!coordinate.keyboard){
textSize(12);
text("on serial",400,50);}
else{textSize(12);
text("on keyboard",400,50);
}}
void serialControl()
{String message=myPort.readStringUntil(coordinate.LF);
if (message!=null){
String[] data =message.split(".");
if (data.length>2){
int tempX=Integer.parseInt(data[1]);
int tempY=Integer.parseInt(data[2]);
print("x: ");
println(tempX);
print("y: ");
println(tempY);
if(tempY<400){
main.direction[0]=true;}
else if (tempY>600){
main.direction[1]=true;}
else {
main.direction[0]=false;
main.direction[1]=false;}
if(tempX<400){
main.direction[2]=true;
}else if(tempX>600){
main.direction[3]=true;}
else{
main.direction[2]=false;
main.direction[3]=false;}}}}
void highscore(){
textSize(12);
text("highscore:"+coordinate.oldhighscore,250,50);}
class Back {PImage[] background;
int x;
int y;
int count;
int x1;
int x2;
Back(int x,int y)
{background=new PImage[2];
this.x1=x;
this.x2=x+900;
this.y=y;
for (int i=0;i<2;i++){String a="background"+".bmp";
background[i]=loadImage(a);}}
void run(){
x1--;
x2--;}
void display(){
if(x1==-900){x1=900;
}
if (x2==-900){x2=900;}
image(background[0],x1,y);
image(background[1],x2,y);}}
class Bullet{
float x;
float y;
int r;
boolean exist;
PImage png;
Share coordinate;
Bullet(Share coordinate){
this.coordinate=coordinate;
exist=true;
x=coordinate.x;
y=coordinate.y;
png=loadImage("pic/bullet.png");
r=10;}
void update(){
if(exist){cal();}
}
void cal(){y=-6;}
void des(){if (x<0||x>600||y<0||y>400){
exist=false;
}}
void display(){update();
if (exist){
image(png,x-7,y-7);}
}}
class Explosion{
int state;
float x;
float y;
boolean exist;
PImage[] photo;
String name;
Explosion(float x,float y){state=0;
this.x=x;
this.y=y;
photo=new PImage[6];
for(int i=0;i<6;i++){
name="pic/"+(i+2)+".png";
//sprintln(name);
photo[i]=loadImage(name);}}
void explode(){image(photo[state],x,y);
state=++state%6;}
void display(){if(exist){explode();
if(state==0){exist=false;}}}
}
class Main{
int r;
Share coordinate;
int state;
boolean[] direction;
PImage aircraft;
Main(Share coordinate){
this.coordinate=coordinate;
direction=new boolean[4];
aircraft=loadImage("pic/plane2.png");
r=50;
}
void up(){if(direction[0]&&coordinate.y>0){coordinate.y-=2;}}
void down(){
if(direction[1]&&coordinate.y<350){coordinate.y+=2;
}}
void left(){if(direction[2]&&coordinate.x>0){coordinate.x-=2;
}}
void right(){if(direction[3]&&coordinate.x<600 ){coordinate.x+=2;}}
void display(){up();
down();
left();
right();
image(aircraft,coordinate.x-25,coordinate.y-25);}}
class Obstacle{
float x;
float y;
int r;
int state;
int blood;
float distance1;
float distance2;
boolean exist;
int shape;
Share coordinate;
Explosion temp;
PImage photo;
String name;
boolean ok;
int timer;
Obstacle(Share coordinate,int state){
this.coordinate=coordinate;
this.x=random(600);
this.y=0;
this.state=state;
exist=true;
if(state==0){r=20;
blood=1;
name="pic/bee";
shape=int(random(4));
name+=(shape+1);
name+=".png";
distance1=900;
distance2=1500;
}if(state==1){
r=30;
blood=15;
name="pic/boss";
shape=int(random(4));
name+=(shape+1);
name+=".png";
distance1=3000;
distance2=3000;
}
shape=int(random(4));
photo=loadImage(name);
}void follow(){
if(state==1){if(x<coordinate.x) x++;
if(y<coordinate.y) y++;
if(x>coordinate.x) x--;
if(y>coordinate.y) y--;}
if(state==0){y+=2;}}
void update(){
for (Bullet i:coordinate.bullet){
if(i.exist&&exist&&((pow(x-i.x,2)+pow(y-i.y,2))<distance1)){blood--;
i.exist=false;
if(blood==0){exist =false;
coordinate.score+=7;
}}}
if(exist&&(pow(x-coordinate.x,2)+pow(y-coordinate.y,2))<distance2){
exist=false;
coordinate.blood-=1;
coordinate.score++;}}
void display(){
update();
if(exist){follow();
image(photo,x-20,y-15);
}
else{
if(!ok){
temp=new Explosion(x,y);
temp.exist=true;
ok=!ok;
}
if(timer--==0){
temp.explode();
}
temp.display();
}}}
class Share{
int x;
int y;
int score;
int blood;
int prv;
ArrayList<Bullet>bullet;
ArrayList<Obstacle>obstacle;
boolean pause;
PImage show;
boolean restart;
int oldhighscore;
int LF=10;
boolean keyboard;
boolean shoot;
PrintWriter output;
BufferedReader reader;
String line;
Share(int x, int y){
this.x=x;
this.y=y;
score=1;
prv=-3;
blood=3;
shoot=true;
bullet=new ArrayList<Bullet>();
obstacle=new ArrayList<Obstacle>();
show= loadImage("pic/plane1.png");
reader=createReader("positions.txt");
try{
line=reader.readLine();
}
catch(Exception e){
line=null;
}
if(line==null){
}
else{
oldhighscore=Integer.parseInt(line);
println("this is high;"+oldhighscore);
}}
void display(){
if(blood<1&&!restart){
textSize(20);
text("game is over!",200,200);
if(score>oldhighscore){
output=createWriter("positions.txt");
output.flush();
output.println(""+score);
output.close();
noLoop();
}
}
if (restart){
restart=!restart;
bullet=new ArrayList<Bullet>();
obstacle=new ArrayList<Obstacle>();
if(score>oldhighscore){
oldhighscore=score;
output=createWriter("positions.txt");
output.flush();
output.println(""+score);
output.close();
}
score=1;
prv=-3;
blood=3;
}
for(int i=0,j=60;i<blood;i++,j+=50){
fill(250,0,0);
image(show,j,0);
fill(255);
}}
}
图。。