Arduino+Processing小蜜蜂-Arduino中文社区 - Powered by Discuz! Archiver

MicroMaker 发表于 2017-4-4 20:58

Arduino+Processing小蜜蜂

本帖最后由 MicroMaker 于 2017-4-5 20:49 编辑

      这个小蜜蜂游戏是有声音的,调用了Minim库。      利用Arduino Leonardu可以控制键盘的特性,用一个四向操纵杆和6个按钮做了一个游戏机,图下:
      
      
      
视频要用高清或高清以上看!!!用高清或高清以上看!!!用高清或高清以上看!!!
http://player.youku.com/player.php/sid/XMjY4NzI2ODU5Ng==/v.swf
(最底下有组图和附件)
      现在有个问题,我对Minim库不熟悉,游戏启动时间长了,音频文件堆起来,几分钟内存就2GB了,我现在发个代码和视频,哪位大神有办法能帮我解决这个问题?谢谢啦。
      Processing:
      import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
int numParticles = 600;
GenParticle[] p = new GenParticle;
Back back;
Main main;
String face = "D:/face/";
int musicmemony = 1024;
//Nucking nukenuke;
Minim minim;
Minim minima;
Minim minimb;
Minim minimc;
AudioPlayer bmusic;
AudioPlayer bbmusic;
AudioPlayer mbmusic;
AudioPlayer dianliumusic;
AudioPlayer bgm;
AudioPlayer boom;
AudioPlayer bvmusic;
AudioPlayer tpm;
AudioPlayer nuckmusic;
AudioPlayer xuepingmusicll;
AudioPlayer hdmusic;
ArrayList<Bullet> bullet = new ArrayList<Bullet>();
ArrayList<Hudun> hudun = new ArrayList<Hudun>();
ArrayList<BulletBob> bulletbob = new ArrayList<BulletBob>();
ArrayList<Explosion> explosiondisplay = new ArrayList<Explosion>();
ArrayList<Main> mainm = new ArrayList<Main>();
ArrayList<st> sting = new ArrayList<st>();
ArrayList<Nucking> nucking = new ArrayList<Nucking>();
ArrayList<Obstacle> obstacle = new ArrayList<Obstacle>();
int prv = frameCount;
int bobprv = frameCount;
float pprv = frameCount;
int ppppp = frameCount;
int tpprv = frameCount;
int bossprv = frameCount;
int rgbprv = frameCount;
int hudunprv = frameCount;
int hudunprvs = frameCount;
int hudunprvm = frameCount;
int hudunnumber = 5;
int musicseconds = frameCount;
float musicsuijishu = 0;
float mx;
float my;
boolean boss=false;
int bv = 50;
int prvbv = frameCount;
boolean eatbv;
float bvx;
float bvy;
float xuepingx;
float xuepingy = 100;
boolean xuepingeat;
int xuepingprv = frameCount;
int score = 1;
int hp = 5;
int stb = 5;
int nuck = 5;
int dianliuprv = frameCount;
boolean bgma = true;
PImage xuepingtu;
int difficulty;
boolean right = false;
float armymove;
float bossmove;
float armydan;
float bossdan;
float xuepingdan;
float danyaodan;
float guangbosu;
float mainmove;
float huduntime;
float huduntimes;
int bobshesu;
boolean o = true;
int tptime;
boolean boomm = false;
boolean bexist;
boolean nexist;
boolean sexist;
String cname = "1";
boolean bossspawn=true;
float bbx=0.0,bby=0.0;
float r=255,g=0,b=0;
void setup()
{
//frameRate(300);
size(1200,1050);
back = new Back();
main = new Main();
//nukenuke = new Nucking();
minim = new Minim(this);
bmusic= minim.loadFile(face+"bmusic.mp3", musicmemony);
nuckmusic= minim.loadFile(face+"nmusic.mp3", musicmemony);
musicsuijishu = random(1.0000001,1.0000003);
bgm= minim.loadFile(face+musicsuijishu+".mp3", musicmemony);
bgm.play();
bgm = null;
musicseconds = frameCount;
noStroke();
smooth();
fill(255);
for(int i = 0;i<p.length;i++)
{
float velX = random(-1,1);
float velY = -i;
p = new GenParticle(100,100,velX,velY,5.0,width/2,height/2);
}
}
void draw()
{
background(255);
if(key == '`')
{
minim.stop();
bgma = true;
o=false;
}
if(key == '2')
{
minim.stop();
musicsuijishu = random(1.0000001,1.0000003);
bgm= minim.loadFile(face+musicsuijishu+".mp3", musicmemony);
bgm.play();
bgm = null;
musicseconds = frameCount;
bgma = false;
o=true;
}
if(right == false)
{
back.display();
mousexy();
textSize(50);
text("Difficuly:   ",100,300);
textSize(30);
text("EASE      key'e'",300,400);
text("NORMAL      key'n'",300,500);
text("HARD      key'h'",300,600);
if(key == 'e')
{
armymove = 6;
bossmove = 3;
armydan = 0.6;
bossdan = 16;
xuepingdan = 1000;
danyaodan = 1000;
guangbosu = 200;
mainmove = 20;
bobshesu=50;
tptime=10;
huduntime = 1000;
huduntimes = 700;
right = true;
}
if(key == 'n')
{
armymove = 9;
bossmove = 5;
armydan = 1;
bossdan = 12;
xuepingdan = 2000;
danyaodan = 2500;
guangbosu = 1500;
mainmove = 16;
bobshesu=40;
tptime=15;
huduntime = 1250;
huduntimes = 550;
right = true;
}
if(key == 'h')
{
armymove = 13;
bossmove = 10;
armydan = 2;
bossdan = 7;
xuepingdan = 2500;
danyaodan = 3100;
guangbosu = 2000;
mainmove = 12;
bobshesu=30;
tptime=20;
huduntime = 1500;
huduntimes = 400;
right = true;
}
}
if(right == true)
{
if(hp >= 1)
{
back.display();
main.display();
nuckingdisplay();
bulletdisplay();
bobbulletdisplay();
obstacleDisplay();
stdisplay();
mousexy();
hudundisplay();
/*if(key == 'c')
{
cname = "1";
}
if(key == 'p')
{
cname = "0";
}*/
textSize(20);
if(hp>=1000000000)
{
text("HP:    ∞",40,100);
}
if(hp>=6&&hp<=999999999)
{
text("HP:    "+hp,40,100);
}
if(hp<=5)
{
text("HP:",40,100);
if(hp==5)
{
fill(0,255,0);
rect(80,87,200,20);
}
if(hp==4)
{
fill(125,255,0);
rect(80,87,160,20);
}
if(hp==3)
{
fill(255,255,0);
rect(80,87,120,20);
}
if(hp==2)
{
fill(255,125,0);
rect(80,87,80,20);
}
if(hp==1)
{
fill(255,0,0);
rect(80,87,40,20);
}
if(hp<=0)
{
text("Death",60,100);
}
}
fill(255,0,0);
text("Score:   "+score,300,100);
}
else
{
back.display();
main.display();
mousexy();
//bulletdisplay();
//nuckingdisplay();
//stdisplay();
textSize(45);
text("Game Over!!!",900,900);
text("Press'R'!!!",900,950);
text("Score:   "+score,100,150);
textSize(20);
if(key == 'r')
{
right = false;
}
}
if(boomm == true)
{
if(o==true)
{
boom= minim.loadFile(face+"mainzhadan.mp3", musicmemony);
boom.play();
boom = null;
}
boomm = false;
}
}
if(key == 'r')
{
hp = 5;
score = 1;
bv = 50;
stb = 6;
nuck = 6;
}
if(key == ';')
{
hp = -2147483648;
}
if(key == 'y')
{
hp = 2147483647;
}
if(key == 'o')
{
nuck = 2147483647;
}
}

void mousexy()
{
int mousex;
int mousey;
mousex=mouseX;
mousey=mouseY;
textSize(20);
text("MouseX:    "+mousex,1000,50);
text("MouseY:    "+mousey,1000,100);
fill(255,255,0);
rect(mousex-9,mousey,19,2);
rect(mousex,mousey-9,2,19);
textSize(20);

fill(255,0,0);
}
class Back
{
PImage png;
int x1 = -1200;
Back()
{
png = loadImage(face+"background.jpg");
}
void display()
{
x1--;
if(x1 <= -1994)
{
x1 = 0;
}
image(png,x1,0);
fill(0,36);
rect(0,0,width,height);
fill(255);
for(int i = 0;i < p.length;i++)
{
p.updata();
p.regenerate();
p.display();
}
/*for(float i = 0;i<=360;i=(frameCount-pprv)/guangbosu*360)
{
arc(870,950,100,100,270,i);
}*/
println(frameCount - musicseconds);
if(musicsuijishu!=0)
{
if(musicsuijishu==1.0000001)
{
if(frameCount-musicseconds>5800)
{
musicsuijishu = random(1.0000001,1.0000003);
bgm= minim.loadFile(face+musicsuijishu+".mp3", musicmemony);
bgm.play();
bgm = null;
musicseconds = frameCount;
}
}
if(musicsuijishu==1.0000002)
{
if(frameCount-musicseconds>7000)
{
musicsuijishu = random(1.0000001,1.0000003);
bgm= minim.loadFile(face+musicsuijishu+".mp3", musicmemony);
bgm.play();
bgm = null;
musicseconds = frameCount;
}
}
}
}
}
class Main
{
PImage png;
float x = 500;
float y = 500;
Main()
{
png = loadImage(face+"Main"+cname+".jpg");
}
void tp()
{
if(keyPressed&&key==' '&&frameCount - tpprv > 10)
{

x=mouseX-20;
y=mouseY-40;
if(o==true)
{
tpm = minim.loadFile(face+"dianliu.mp3", musicmemony);
tpm.play();
tpm = null;
}

tpprv=frameCount;
}
}
void display()
{
tp();
if(keyPressed)
{
if(key == 'w' || key == 'W') y-=mainmove;
if(key == 's' || key == 'S') y+=mainmove;
if(key == 'a' || key == 'A') x-=mainmove;
if(key == 'd' || key == 'D') x+=mainmove;
if(key == 'u' || key == 'U') bv=2147483647;
if(key == 'm' || key == 'M') bv=10;
if(key == 'p' || key == 'P') stb=2147483647;
if(key == '[' || key == '[')o=false;
if(key == 'i' || key == 'I') hudunnumber = 2147483647;
}
image(png,x,y);
mx = x;
my = y;
if(dist(bvx,bvy,x,y)<30)
{
if(bv<300)
{
bv+=50;
}
if(stb<15)
{
stb+=1;
}
if(o==true)
{
bvmusic= minim.loadFile(face+"bvmusic.mp3", musicmemony);
bvmusic.play();
bvmusic = null;
}
eatbv = true;
bvx = -10000;
bvy = -10000;
}
//------------------
if(dist(xuepingx,xuepingy,x,y)<30)
{
if(hp<=4)
{
hp+=1;
}
if(o==true)
{
xuepingmusicll= minim.loadFile(face+"xuepingmusic.mp3", musicmemony);
xuepingmusicll.play();
xuepingmusicll = null;
}
xuepingeat = true;
xuepingx = -10000;
xuepingy = -10000;
}
}
}
class Bullet
{
float x1;
float y1;
PImage png;
boolean exist;
int x2;
int y2;
float random;
Bullet()
{
random = random(0,10);
if(random>0&&random<=7.4)
{
png = loadImage(face+"Bullet2"+cname+".png");
}
if(random >=7.4&&random <=10)
{
if(cname == "1")
{
png = loadImage(face+"Bullet2"+cname+"2"+".png");
}
}
x1= mx+15;
y1= my;
exist = true;
}
void display()
{
if(exist)
{
y1-=26;
image(png,x1,y1);
if(bv<=999999999)
{
text("BULLET:    "+bv,800,800);
}
else
{
text("BULLET:    ∞ ",800,800);
}
}
else
{

}
}
}
void bulletdisplay()
{
if(mousePressed&&mouseButton == LEFT&&frameCount - prv > 10&&bv>=1)
{
bullet.add(new Bullet());
//for(Bullet k : bullet)
//{
//if(k.exist&&bexist)
//{
    if(o==true)
{
bmusic= minim.loadFile(face+"bmusic.mp3", musicmemony);
bmusic.play();
bmusic = null;
}
//fill(0,36);
//rect(0,0,width,height);
//fill(255);
//for(int i = 0;i < p.length;i++)
//{
//p.updata();
//p.regenerate();
//p.display();
//}
// }
//}
prv = frameCount;
bv -= 1;
}
if((frameCount - prvbv > danyaodan&&eatbv==true)||key =='h')
{
prvbv = frameCount;
bvx = random(100,1000);
bvy = 100;
eatbv = false;
}
if(eatbv == false)
{
fill(255,0,0);
if(cname == "0")
{
rect(bvx,bvy,30,30);
}
if(cname == "1")
{
PImage danyaoxiangphoto;
danyaoxiangphoto = loadImage(face+"danyaoxiang.png");
image(danyaoxiangphoto,bvx,bvy);
}
bvy+=2;
if(bvy>1000)
{
eatbv = true;
}
}
///----------------
if(hp>=1)
{
if(frameCount - xuepingprv > xuepingdan&&xuepingeat==true||key == 'z')
{
xuepingprv = frameCount;
xuepingx = random(100,1000);
xuepingy = 1;
xuepingeat = false;
}
if(xuepingeat == false)
{
xuepingtu = loadImage(face+"xueping"+cname+".png");
image(xuepingtu,xuepingx,xuepingy);
xuepingy+=2;
if(xuepingy>1000)
{
xuepingeat = true;
}
}
for(Bullet i : bullet)
{
i.display();
}
}
}
class st
{
//Super star
float x1;
float y1;
boolean exist;
PImage[] photo;
st()
{
x1= mx+40;
y1= my;
exist = true;
}
void display()
{
sexist = exist;
if(exist)
{
y1-=10;
if(cname == "0"||cname == "1")
{
fill(255,0,0);
rect(x1-1000,y1,2000,20);
}
/*if(cname == "1")
{
for(int ii = 0;ii<=39;ii++)
{
photo = loadImage("dianliutu.jpg");
}
for(int ii = 0;ii<39;ii++)
{
image(photo,x1-1000,y1);
x1 += 50;
}
rect(x1-1000,y1,2000,20);

}*/
if(stb<=999999999)
{
text("Super laser:   "+stb,800,900);
}
else
{
text("Super laser:   ∞",800,900);
}
}
else
{
}
}
}
void stdisplay()
{
if(keyPressed&&key == 'q'&&frameCount - pprv > guangbosu&&stb>=1)
{
sting.add(new st());
//nmusic= minim.loadFile("bmusic.mp3", 1024);
//bmusic.play();
stb -= 1;
pprv = frameCount;
}
if(stb<=999999999)
{
text("Super laser:   "+stb,800,900);
}
else
{
text("Super laser:   ∞",800,900);
}
//text("Super laser(>300):   "+pprv,800,960);
for(st i : sting)
{
i.display();
}
}
////--------------------------------
class Nucking
{
float x1;
float y1;
PImage png;
boolean exist;
Nucking()
{
png = loadImage(face+"nuke"+cname+".png");
x1= mx+40;
y1= my;
exist = true;
}
void display()
{
   nexist = exist;
if(exist)
{
y1-=1;
image(png,x1,y1);
text("Nuck:   "+nuck,800,900);
}
else
{
}
}
}
void nuckingdisplay()
{
if(keyPressed&&key == 'e'&&frameCount - pprv > guangbosu&&nuck>=1)
{
fill(255,0,0);

nucking.add(new Nucking());
for(Nucking m : nucking)
{
if(m.exist)
{
    if(o==true)
{
nuckmusic= minim.loadFile(face+"nmusic.mp3",musicmemony);
nuckmusic.play();
nuckmusic = null;
}
nuck -= 1;
}

ppppp = frameCount;
pprv = frameCount;

}
}
if(nuck<=999999999)
{
text("Nuck:   "+nuck,800,400);
}
else
{
text("Nuck:   ∞",800,400);
}


for(Nucking i : nucking)
{
i.display();
}
}
class Obstacle
{
boolean hd = true;
int ppp;
boolean la=false;
boolean lpo = false;
boolean kill = false;;
int ppprv = frameCount;
float x;
int r;
float y;
float bbbx=0;
float bbby = 0;
PImage photo;
String name;
int blood;
int di1;
int di2;
int state2;
boolean exist;
boolean bossss = false;
boolean ok;
int timer;
boolean bbb;
Explosion temp;
float bossx;
float bossy;
int i = 0;
Obstacle(int state)
{
this.x = random(1200);
this.y = 0;
state2 = state;
exist = true;
bbb = false;
if(state2 == 0)
{
r = 10;
blood = 1;
name = face+"army"+cname+".png";
di1 = 900;
di2 = 900;
}
if(state2 ==1)
{
r = 50;
blood = 15;
if(random(0,9)>1.3)
{
name = face+"boss"+cname+".png";
bossss = false;
}
else
{
if(cname == "1")
{
name = face+"boss"+cname+"1"+".png";
bossss = true;
}
else
{
name = face+"boss"+cname+".png";
bossss = false;
}
}
di1 = 120000;
di2 = 120000;
}

photo = loadImage(name);
}
void bong()
{
if(state2 == 1)
{
bossx=x;
bossy=y;
if(x < 100)
{
bbb = true;
}
if(x>1000)
{
bbb = false;
}
if(bbb == true)
{
x +=bossmove;
}
else
{
x -=bossmove;
}
y=200;

}
if(state2 == 0)
{
y+=armymove;

}

}
void up()
{
for (Bullet i : bullet)
{
if(i.exist&&exist&&pow(x-i.x1,2)+pow(y-i.y1,2)<di1)
{
blood--;

i.exist = false;
if(blood == 0||kill)
{
if(state2==1)
{
bossspawn=true;
la=false;
}
exist = false;
score+=7;
boomm = true;

}
}
if(exist&&pow(x-mx,2)+pow(y-my,2)<di2)
{
exist = false;
if(state2==0)
{
hp -=1;
}
else
{
hp-=2;
}
score++;
boomm = true;
}
}
for (st j :sting)
{
if(j.exist&&exist&&dist(0,y,0,j.y1)<40)
{
blood-=0.25;
if(j.y1<0)
{
j.exist = false;
}
else
{
if(frameCount - dianliuprv >4)
{
for(st l : sting)
{
if(l.exist)
{
    if(o==true)
{
dianliumusic= minim.loadFile(face+"dianliu.mp3", musicmemony);
dianliumusic.play();
dianliumusic = null;
}
dianliuprv = frameCount;
}
}
}
}
if(blood <=0)
{
kill = true;
if(state2==1)
{
score+=1;
la=false;
}
}
//ppprv = frameCount;
// }
}
}
//...............
for (Nucking n : nucking)
{

if(n.exist&&exist&&dist(x,y,n.x1,n.y1)<1000)
{
blood--;
exist = false;
score+=7;
boomm = true;
if(state2 ==1)
{
la=false;
}
if(frameCount - ppppp >15)
{
n.exist = false;
}
else
{
ppppp = frameCount;
}
}
}
//..............
for (Hudun mm :hudun)
{
mm.display();
if(mm.exist&&exist&&dist(x,y,mm.x1,mm.y1)<150)
{
blood-=0.25;
exist=false;
    if(o==true&&hd == true)
    {
hdmusic= minim.loadFile(face+"hudunmp.mp3", musicmemony);
hdmusic.play();
hdmusic = null;
hd = false;
    }
}
else
{


}
}
}
void display()
{

if(state2==1)
{
la = true;
}
if(la==true)
{
if(frameCount - bossprv >=100&&la)
{
if(o==true)
{
bbmusic= minim.loadFile(face+"bmusic.mp3", musicmemony);
bbmusic.play();
bbmusic = null;
}
bossprv=frameCount;
bulletbob.add(new BulletBob(bossx,bossy));

for(BulletBob ll : bulletbob)
{
ll.display();
}
}

}
up();
if(exist&&kill ==false)
{
bong();
bbbx=x;
bbby=y;
if(state2 ==1&&bossss == false)
{
image(photo,x-190,y-15);
}
if(state2 ==1&&bossss)
{
image(photo,x-55,y-15);
}
if(state2 ==0)
{
image(photo,x+1,y-15);
}
}
else
{
if(!ok)
{
temp = new Explosion(x,y);
temp.exist = true;
ok = !ok;
}
if(timer-- ==0)
{
temp.explode();
}
temp.display();
for(Obstacle n : obstacle)
{
n=null;
}
}
}

}

void obstacleDisplay()
{
if(((score%bossdan)==0&&bossspawn)||bossspawn==true&&(key=='j'&&keyPressed))
{
score++;
obstacle.add(new Obstacle(1));
boss=true;
bossspawn=false;
}
else if(random(10)<armydan)
{
boss=false;
obstacle.add(new Obstacle(0));
}

for(Obstacle n : obstacle)
{

n.display();
}
}


class Particle
{
float x,y;
float vx,vy;
float ra;
float g = 0.1;
Particle(int xpos,int ypos,float velx,float vely,float r)
{
x = xpos;
y = ypos;
vx = velx;
vy = vely;
ra = r;
}
void updata()
{
vy+= g;
y+=vy;
x+=vx;
}
void display()
{
fill(255,0,0);
ellipse(x,y,4,4);

}
}

class GenParticle extends Particle
{
float originX,originY;
GenParticle(int xIn,int yIn,float vxIn,float vyIn,float r,float ox,float oy)
{


super(xIn,yIn,vxIn,vyIn,r);
originX = (int)bbx;
originY = (int)bby;

fill(255,0,0);
}
void regenerate()
{
if((x > width+ra)||(x<-ra)||(y>height+ra)||(y<-ra))
{
x = originX;
y = originY;
vx = random(-1.0,1.0);
vy = random(-4.0,-2.0);
}
}
}

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;
for(int i = 0;i<6;i++)
{
name = face+"0"+(i+1)+".png";
photo = loadImage(name);
}
}
void explode()
{
image(photo,x,y);
state = ++state%6;
}
void display()
{
if(exist)
{
explode();
if(state==0)
{
exist =false;
}
}
}
}
class BulletBob
{
int prv = frameCount;
float x1;
boolean hd = true;
float y1;
PImage png;
boolean exist;
int x2;
int y2;
float random;
int di2=900;
BulletBob(float x,float y)
{
random = random(0,10);
if(random>0&&random<=7.4)
{
png = loadImage(face+"Bullet2"+cname+".png");
}
if(random >=7.4&&random <=10)
{
if(cname == "1")
{
png = loadImage(face+"Bullet2"+cname+"3"+".png");
}
}
if(x!=-100&&y!=-100)
{
for(Obstacle n : obstacle)
{
x1=n.x;
y1=n.y;
}
}
else
{
x1 = x;
y1 = y;
}
exist = true;
}

void display()
{
if(exist)
{
y1+=13;
if(exist&&pow(x1-mx,2)+pow(y1-my,2)<di2)
{
exist = false;
hp -=1;
if(o==true)
{
mbmusic= minim.loadFile(face+"mainzhadan.mp3", musicmemony);
mbmusic.play();
mbmusic = null;
}
}
for (Nucking n : nucking)
{
if(n.exist&&exist&&dist(x1,y1,n.x1,n.y1)<1000)
{
exist = false;
}
}
for (st j :sting)
{
if(j.exist&&exist&&dist(0,y1,0,j.y1)<40)
{
exist=false;
}
image(png,x1,y1);
}
if(exist!=true)
{
   
}
}
//H.D.
for (Hudun mm :hudun)
{

if(mm.exist&&exist&&dist(x1,y1,mm.x1,mm.y1)<150)
{
exist=false;

    if(o==true&&hd == true)
    {
hdmusic= minim.loadFile(face+"hudunmp.mp3", 1024);
hdmusic.play();
hdmusic = null;
hd = false;
    }
}
else
{


}
//ppprv = frameCount;
// }
}
}
}
void bobbulletdisplay()
{

if(frameCount - bobprv > bobshesu)
{
bulletbob.add(new BulletBob(-100,-100));

for(BulletBob ll : bulletbob)
{
bobprv=ll.prv;
}
}
for(BulletBob ll : bulletbob)
{
ll.display();
}



//fill(0,36);
//rect(0,0,width,height);
//fill(255);
//for(int i = 0;i < p.length;i++)
//{
//p.updata();
//p.regenerate();
//p.display();
//}
// }
//}

}
class Hudun
{
float x1;
float y1;
int clour = frameCount;
boolean exist = true;
Hudun()
{
clour = frameCount;
exist = true;
}
void display()
{
x1=mx+22;
y1=my+50;
if(exist)
{

   
if(frameCount-hudunprvs<huduntimes)
{
    strokeWeight(10);
if(frameCount-clour>=350)
{
   stroke(255,0,0);
}
if(frameCount-clour>=300&&frameCount-clour<=349)
{
stroke(255,125,0);
}
else if(frameCount-clour>=250&&frameCount-clour<=299)
{

stroke(255,255,0);
}
    else if(frameCount-clour>=125&&frameCount-clour<=249)
{

stroke(0,255,0);
}
    else if(frameCount-clour>=1&&frameCount-clour<=124)
{

stroke(0,10,255);
}

noFill();

ellipse(x1,y1,120,120);


strokeWeight(1);
fill(255,0,0);
noStroke();


}

else
{
hudunprvs = frameCount;
exist=false;
}
}
if(exist!=true)
{

}
}
}

void hudundisplay()
{
if(mousePressed&&mouseButton == CENTER&&frameCount - hudunprv > huduntime&&hudunnumber>=1)
{
hudun.add(new Hudun());
hudunnumber--;

//H.D.
text("H.D.:    "+hudunnumber,800,450);
for(Hudun mm : hudun)
{
mm.display();
if(mm.exist)
{
hudunprv=frameCount;   
}
}
}
else
{
}
}Arduino:
#include <Keyboard.h>
#include <Mouse.h>
int wb = 3;//w
int ab = 4;//a
int sb = 5;//s
int db = 6;//d
int rb = 7;//Mleft
int eb = 8;//q
int nb = 9;//e
int hb = 10; //r
int ob = 11; //2
int pb = 12; //n
int w = 3;
int a = 4;
int s = 5;
int d = 6;
int r = 7;
int e = 8;
int n = 9;
int h = 10;
int o = 11;
int p = 12;
/*boolean ww = false;
boolean aa = false;
boolean ss = false;
boolean dd = false;
boolean rr = false;
boolean ee = false;
boolean nn = false;
boolean hh = false;
boolean oo = false;
boolean pp = false;*/
long wdelay;
long adelay;
long sdelay;
long ddelay;
long rdelay;
long edelay;
long ndelay;
long hdelay;
long odelay;
long pdelay;
void setup()
{
Serial.begin(115200);
Keyboard.begin();
Mouse.begin();
pinMode(wb,INPUT_PULLUP);
pinMode(ab,INPUT_PULLUP);
pinMode(sb,INPUT_PULLUP);
pinMode(db,INPUT_PULLUP);
pinMode(rb,INPUT_PULLUP);
pinMode(eb,INPUT_PULLUP);
pinMode(nb,INPUT_PULLUP);
pinMode(hb,INPUT_PULLUP);
pinMode(ob,INPUT_PULLUP);
pinMode(pb,INPUT_PULLUP);
}
void loop()
{
w = digitalRead(wb);
a = digitalRead(ab);
s = digitalRead(sb);
d = digitalRead(db);
r = digitalRead(rb);
e = digitalRead(eb);
n = digitalRead(nb);
h = digitalRead(hb);
o = digitalRead(ob);
p = digitalRead(pb);
if(w == 0)
{
Keyboard.press('w');
wdelay = millis();
}
if(w == 1&&millis()-wdelay>=1)
{
Keyboard.release('w');
}
if(a == 0)
{
Keyboard.press('a');
adelay = millis();
}
if(a == 1&&millis()-adelay>=1)
{
Keyboard.release('a');
}
if(s == 0)
{
Keyboard.press('s');
sdelay = millis();
}
if(s == 1&&millis()-sdelay>=1)
{
Keyboard.release('s');
}
if(d == 0)
{
Keyboard.press('d');
ddelay = millis();
}
if(d == 1&&millis()-ddelay>=1)
{
Keyboard.release('d');
}
//------
if(r == 0)
{
Mouse.press(MOUSE_LEFT);
rdelay = millis();
}
if(r == 1&&millis()-rdelay>=4)
{
Mouse.release(MOUSE_LEFT);
}
if(e == 0&&millis()-edelay>=90)
{
Keyboard.press('q');
edelay = millis();
}
if(e == 1)
{
Keyboard.release('q');
}
if(n == 0&&millis()-ndelay>=90)
{
Keyboard.press('e');
ndelay = millis();
}
if(n == 1)
{
Keyboard.release('e');
}
if(h == 0&&millis()-hdelay>=90)
{
Keyboard.press('r');
hdelay = millis();
}
if(h == 1)
{
Keyboard.release('r');
}
//----------
if(o == 0&&millis()-odelay>=20)
{
Keyboard.press('2');
odelay = millis();
}
if(o == 1)
{
Keyboard.release('2');
}
if(p == 0&&millis()-pdelay>=90)
{
Keyboard.press('n');
pdelay = millis();
}
if(p == 1)
{
Keyboard.release('n');
}
}
//


组图:

游戏刚开始,168MB,毫无压力


游戏第15秒,563MB,有点开始升高了


游戏45秒,1350MB,有点卡了


游戏75秒,1789MB,MC都没这么高过


游戏105秒,1973MB,电脑还好吗


游戏125秒,2308MB,电脑要炸了


游戏150秒,2341MB,炸了








资源包太大,上传不了,大家去百度网盘下载吧 https://pan.baidu.com/s/1dFOPRah
(1.把A和P程序放在任何目录下,把face资源包放在XXX路径下,打开Processing,把11行的字符串赋值为face的路径,如D:/abc/face,字符串赋值为D:/abc/face/   )
哪位大神有办法能帮我解决这个问题?谢谢啦:)

caonimaB 发表于 2017-4-5 17:58

虽然我表示不懂,我个人觉得可以参考 安卓的生命周期

suoma 发表于 2017-4-5 19:37

太占cpu了

MicroMaker 发表于 2017-4-5 20:40

补充:
      首先是Arduino程序把操控杆的4个按钮和操纵板上的6个按钮的幸好转换成模拟键盘输出(操纵杆往前,模拟按W,操纵杆往后,模拟按S,操纵杆往左,模拟按A,操纵杆往右,模拟按D,按钮1号,模拟按鼠标左键,按钮2号,模拟按Q,按钮3号,模拟按E,按钮4号,模拟按R,按钮5号,模拟按2,按钮6号,模拟按N)
      然后是Processing的程序:
      1.Minim音乐库,变量定义、类声明......
      2.setup:变量初始化赋值,for语句是一个烟花效果的测试版。【setup函数结束】
      3.draw:按~键停止播放所有音乐;按2停止播放所有音乐,开始播放音乐;如果还没开始,选难度,Back和MouseXY开启,按E是简单,N是普通,H是困难,并按照难度设好相应规则。如果准备好了,如果血大于0,加载Back、Main、Nucking、Bullte、BobBullte、Obstacle、St、MouseXY、Hudun引导函数游戏开始。血量显示:大于1000000000,显示无限(作弊),大于5用数字表示,5用绿色,血量条长200P,4用绿加点红,长160P,3用绿红,长120P,2用红加点绿,长80P,1用红色,长40,小于1,显示Death。显示分数。如果血小于1,加载Back、Main、MouseXY,显示“Gameover”“Press’R‘”,显示死亡前分数。如果按R,重新回到还没开始的地方,自己的数据初始化。如果自己被撞了,将boom赋值为mainzhadan.mp3,播放(null没用)。如果按;,血量为-2147483648(作弊),如果按Y,血量为2147483647(作弊),按O,有2147483647颗核弹(作弊)。【draw函数结束】
      4.MouseXY函数(Debug):在右上角显示鼠标的坐标,并用一个+跟随鼠标。【MouseXY函数结束】
      5.Back类:加载左右无缝连接背景星空图,让背景图动起来,左右无缝连接。for烟花测试版。显示在监控窗口打印当前frameCount(Debug)。背景音乐自动循环播放(测试版)。【Back类结束】
      6.Main类:加载本方战舰图。创建tp函数(作弊):如果按空格,将战舰传送到鼠标位置,并加载传送声音,播放。【tp函数结束】。加载tp函数,如果按W前进,按S后退,按A左移,按D右移,按U有2147483647个弹药(作弊),按M有10个弹药(Debug),按P有2147483647个激光(作弊),按O只放背景音乐,停止其他音乐(Debug),按I有2147483647个护盾(作弊)。如果捡到弹药箱,弹药没满+50,激光没满+1,清除弹药箱,播放装弹声音。如果捡到血瓶,血没满+1,清除弹药箱,播放喝药水声音。【Main类结束】
      7.Bullet类:载入子弹图片,让子弹飞,显示子弹数量。【Bullet类结束】
      8.Bullet引导:左键发射子弹,播放子弹声音。在隔X个时间单位后,或者按H(作弊),随机生成弹药箱。在隔X个时间单位后,或者按Z(作弊),随机生成血瓶【结束】
      9.st类:产生一条红线激光,往上走。显示激光数量。【st类结束】
    10.st引导:按Q发射激光。显示激光数量。【结束】
    11.Nucking类:加载图片。让核弹飞。显示核弹数量。【Nucking类结束】
    12.Nucking引导:按E发射核弹。播放核弹声音。显示核弹数量。【结束】
    13.Obstacle类:如果生成小兵,载入小兵图片,如果碰到子弹,分数+7,如果碰到激光,分数+0,播放电流声,碰到核弹爆炸,分数+7,碰到护盾,分数+0,播放护盾声音,死后加载Explosion类。如果生成Boss,载入Boss图片,设置15血,如果碰到子弹,血-1,如果血为0,分数+7,如果碰到激光,血-5,如果血为0,分数+0,播放电流声,碰到核弹爆炸,分数+7,碰到护盾,分数+0,播放护盾声音,死后加载Explosion类。【Obstacle类结束】
    14.Obstacle引导:根据分数决定生成Boss还是小兵。【结束】
    15.Particle类:测试版Null【Particle类结束】
    16.GenParticle类:测试版Null【GenParticle类结束】
    17.Explosion类:把爆炸6图从第一张放映到第六张,模拟爆炸效果。【Explosion类结束】
    18.BulletBob:(敌人的子弹,Boss出现后并且使用过激光会显示)加载子弹图片,让子弹飞,打中自己扣1血,播放被打中声音,会被核弹、激光、护盾撞没,撞上护盾时会播放护盾声音。【BulletBob结束】
    19.BulletBob引导:每过X个时间单位发射一颗子弹,每个敌舰都有可能发。【结束】
    20.Hudun类:(每次使用期间会卡,问题之一)在战舰外包一层noFill、stroke、stroke粗10的圆,圆的颜色是由能使用的时间流逝变色的(蓝-绿-绿黄-黄-黄红-红-护盾消失)【Hudun类结束】
    21.Hudun引导:按鼠标滚轮,放护盾。显示护盾个数【结束】


      错误信息是:OutOfMemoryError:You may need to increase the memory setting in Preferences.
                        An OutOfMemoryError means that your code is either using up too much memory because of a bug(e.g. creating an array that's too large, or
                        unintentionally loading thousands of images), or that your sketch may need more memory to run.If your sketch uses a lot of memory(for instance
                        if it loads a lot of data files) you can increase the memory available to your sketch using the Preferences window.

MicroMaker 发表于 2017-4-5 20:47

suoma 发表于 2017-4-5 19:37
太占cpu了

http://www.arduino.cn/forum.php?mod=image&aid=31818&size=300x300&key=45c5ddb635e931e9&nocache=yes&type=fixnone好像是的

单片机菜鸟 发表于 2017-4-6 08:43

有点意思

ultrasonicer 发表于 2018-8-4 16:33

其乐无穷啊
页: [1]
查看完整版本: Arduino+Processing小蜜蜂