#processing
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer bgm;
PImage imgcat,imgrat,imgcave,imglife;
Serial myPort;
void setup(){
size(500,800);
myPort = new Serial(this, "COM7", 115200);
imgcat=loadImage("cat.png");
imgrat=loadImage("rat.png");
imgcave=loadImage("cave.png");
imglife=loadImage("life.png");
for(int i=0;i<60;i++){
ratX[i]=100+int(random(3))*150;
ratY[i]=random(-8000);
imageMode(CENTER);
}
minim=new Minim(this);
bgm=minim.loadFile("music.mp3",2048);
bgm.play();
bgm.loop();
}
float []ratX=new float[80];
float []ratY=new float[80];
float catX=250,catY=700;
int score=0;
int life=3;
int oldx=128,oldy=128;
int t=0, s=48, x=128, y=128;
boolean Gameover=false;
int stage=0,time=0;
void draw(){
background(129,214,125);
while(myPort.available() > 0){
t = myPort.read();
if(t=='w') s = myPort.read();
else if(t=='x') x=myPort.read();
else if(t=='y') y=myPort.read();
if(x>200&&oldx<200) catX+=4;
if(x<50&&oldx>50) catX-=4;
}
if(stage==0)
{
fill(11,35,152); textSize(60);
text("Click to Start",68,350);
}
else if(stage==1){
if(life==0)stage=2;
}else if(stage==2){
fill(11,35,152); textSize(40);
text("Game Over!!",48,350);
text("You score is "+score,100,400);
}
fill(#286A25); rect(0,740,600,100);
image(imglife,420,100,100,100);
image(imgcave,100,10,200,120);
image(imgcave,250,10,200,120);
image(imgcave,400,10,200,120);
fill(255,0,0);textSize(30); text("Score:"+score,10,100);
textSize(40); text(":"+life,450,100);
image(imgcat,catX,catY,150,150);
if(stage==1){
for(int i=0;i<60;i++){
image(imgrat,ratX[i],ratY[i],70,70);
ratY[i]+=2;
if(dist(catX,catY,ratX[i],ratY[i])<75){
ratY[i]=100+random(-8000);
score+=5;
}
if(Gameover) return;
if(ratY[i]>=700) {
ratY[i]=100+random(-8000);
life-=1;
if(life==0) {
Gameover=true;
}
}
}
}
}
void keyPressed(){
if(key=='a') catX-=75;
if(key=='s') catX+=75;
}
void mousePressed(){
if(stage==0) {stage=1;}
if(stage==2) {stage=0;}
}
#arduino
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 3; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
int s = digitalRead(SW_pin);
int x = analogRead(X_pin);
int y = analogRead(Y_pin);
if (x / 4 == 'w' || x / 4 == 'x' || x / 4 == 'y' ) return;
if (y / 4 == 'w' || y / 4 == 'x' || y / 4 == 'y' ) return;
Serial.print("w");
Serial.print(s);
Serial.print("x");
Serial.write(x / 4);
Serial.print("y");
Serial.write(y / 4);
delay(50);
}
沒有留言:
張貼留言