AudioPlayer player;
AudioPlayer player1;
AudioPlayer player2;
Minim minim;//audio context
PImage img;
PImage img1;
PImage img2;
PImage img3;
PImage img4;
PImage img5;
PImage img6;
PImage win;
PImage lose;
PImage ready;
PImage GO;
int r1x=0;
int r2x=0;
float runnerx=80;
float runner2x=80;
float bgx=1500;
int checktype=-1;
int runtype=1;
int background=0;
int count=0;
int badrunner=0;
int readyt=0;
import processing.serial.*;
Serial myPort;
void setup() {
myPort=new Serial(this,"COM1",9600);
minim = new Minim(this);
player = minim.loadFile("file.mp3", 2048);
player1 = minim.loadFile("winner.wav", 2048);
player2 = minim.loadFile("loser.WAV", 2048);
img = loadImage("bg.jpg");
img1 = loadImage("2.png");
img2 = loadImage("1.png");
img3 = loadImage("3.png");
img4 = loadImage("5.png");
img5 = loadImage("4.png");
img6 = loadImage("6.png");
win = loadImage("win.jpg");
lose = loadImage("lose.jpg");
ready = loadImage("ready.png");
GO = loadImage("GO.png");
imageMode(CENTER);
size(800, 256);
}
int xx=0,zz=0;
void draw() {
//-------------------------------
if(myPort.available()>0)
{
int a=myPort.read();
if(a=='A') {xx=1;}
if(a=='B') {zz=1;}
}
if (background==1)
if (checktype!=-2)
if (xx==1) {
r1x+=20;
checktype=0;
runtype=2;
if (bgx>=-700)bgx-=20;
else runnerx+=20;
xx=0;
}
if (background==1)
if (checktype!=-3)
if (zz==1) {
r1x+=20;
checktype=11;
runtype=3;
if (bgx>=-700)bgx-=20;
else runnerx+=20;
zz=0;
}
//-------------------------------
if (background==0) {
image(img, bgx, 128);
image(img1, runnerx, 160);
image(img5, runner2x, 210);
count++;
if (count<=70)image(ready, 400, 128, 394, 150);
else image(GO, 400, 128, 394, 150);
if (count==100)background=1;
}
if (background==1) {
player.play();
image(img, bgx, 128);
if (runtype==1)image(img1, runnerx, 160);
if (checktype==10) {
runtype=1;
checktype=-2;
}
if (checktype==20) {
runtype=1;
checktype=-3;
}
if (runtype==2&&checktype<=10&&checktype>=0) {
image(img2, runnerx, 160);
checktype++;
}
if (runtype==3&&checktype<=20&&checktype>=11) {
image(img3, runnerx, 160);
checktype++;
}
badrunner++;
if ((badrunner%20)<=10&&(badrunner%20)>=0)image(img5, runner2x, 210);
if ((badrunner%20)>=11&&(badrunner%20)<=19)image(img6, runner2x, 210);
r2x+=5;
runner2x=runnerx+(r2x-r1x);
if (runnerx>=500||r2x>=2630)background=2;
}
if (background==2)
{
if (r2x<2630)
{
player1.play();
image(win, 400, 128);
} else if (r2x>=2630)
{
player2.play();
image(lose, 400, 128);
}
}
}
void keyPressed() {
if (background==1)
if (checktype!=-2)
if (key=='a') {
r1x+=20;
checktype=0;
runtype=2;
if (bgx>=-700)bgx-=20;
else runnerx+=20;
}
if (background==1)
if (checktype!=-3)
if (key=='s') {
r1x+=20;
checktype=11;
runtype=3;
if (bgx>=-700)bgx-=20;
else runnerx+=20;
}
}
沒有留言:
張貼留言