import processing.serial.*;
Serial myPort;
int now=1;
int val;
int ballY=607;
int ballX=367;
float monsterX,monster2X,monster3X,m
float monsterY,monster2Y,monster3Y,m
float startX=0;
float start2X=0;
float start3X=0;
float start4X=0;
float start5X=0;
import ddf.minim.*;
AudioPlayer backmusic,stopcar,frogsound;
Minim minim;
PImage img0,img1,img2,img3,img4,img5,
void setup()
{
size(800,700);
img0=loadImage("background.png
img1=loadImage("frog.gif");
img2=loadImage("show3.png");
img3=loadImage("car1.gif");
img4=loadImage("car2.gif");
img5=loadImage("car3.gif");
img6=loadImage("car4.gif");
img7=loadImage("car5.gif");
img8=loadImage("show4.png");
img9=loadImage("show0.png");
monsterX=0;
monster2X=0;
monster3X=0;
monster4X=0;
monster5X=0;
String portName=Serial.list()[0];
myPort=new Serial(this,"COM3",9600);
minim=new Minim(this);
backmusic=minim.loadFile("back
backmusic.play();
}
void show1(){
image(img9,0,0);
}
void show2(){
image(img0,0,0);
image(img1,ballX,ballY,70,90);
}
void show3(){
image(img2,0,0);
}
void show4(){
image(img8,0,0);
}
void draw(){
while(myPort.available() >0) val=myPort.read();
if(now==1){
show1();
if(mousePressed&&mouseX>=0&&mo
if (val=='a') now=2;
if (val=='c') now=2;
if (val=='e') now=2;
if (val=='g') now=2;
}
if(now==2){
show2();
monsterX-=4;
monsterY=520;
image(img3,monsterX,520);
if (monsterX<-100) {
monsterX +=800;
}
monster2X-=8;
monster2Y=440;
image(img4,monster2X,440);
if (monster2X<-100) {
monster2X +=800;
}
monster3X+=5;
monster3Y=360;
image(img5,monster3X,360);
if (monster3X>+800) {
monster3X -=900;
}
monster4X-=8;
monster4Y=270;
image(img6,monster4X,monster4Y
if (monster4X<-100) {
monster4X +=800;
}
monster5X+=6;
monster5Y=190;
image(img7,monster5X,monster5Y
if (monster5X>+800) {
monster5X -=800;
}
if(ballX<monsterX+45&&ballX>mo
{
now=4;
stopcar=minim.loadFile("stop.w
stopcar.play();
monsterX+=800;
ballY=607;
ballX=367;
}
if(ballX<monster2X+45&&ballX>m
{
now=4;
stopcar=minim.loadFile("stop.w
stopcar.play();
monster2X+=800;
ballY=607;
ballX=367;
}
if(ballX<monster3X+45&&ballX>m
{
now=4;
stopcar=minim.loadFile("stop.w
stopcar.play();
monster3X+=800;
ballY=607;
ballX=367;
}
if(ballX<monster4X+45&&ballX>m
{
now=4;
stopcar=minim.loadFile("stop.w
stopcar.play();
monster4X+=800;
ballY=607;
ballX=367;
}
if(ballX<monster5X+45&&ballX>m
{
now=4;
stopcar=minim.loadFile("stop.w
stopcar.play();
monster5X+=800;
ballY=607;
ballX=367;
}
if (val=='a')ballY-=6; //top
if (val=='c')ballY+=6; //down
if (val=='e')ballX+=8; //right
if (val=='g')ballX-=8; //left
if(ballY>608)ballY=607;
if(ballY<-5)ballY=-4;
if(ballX>728)ballX=727;
if(ballX<2)ballX=3;
if(ballY<20&&ballX<410&&ballX>
if(ballY<20&&ballX<410&&ballX>
if(ballY<20&&ballX<410&&ballX>
println("monster5Y:"+ballY+" monster5X:"+monster5X);
}
if(now==3){
show3();
if(mousePressed&&mouseX>=680&&
now=2;
}
if(now==4){
show4();
if(mousePressed&&mouseX>=680&&
now=2;
}
}arduino
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
int pushButton2 = 3;
int pushButton3 = 4;
int pushButton4 = 5;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
pinMode(pushButton2, INPUT);
pinMode(pushButton3, INPUT);
pinMode(pushButton4, INPUT);
}
int a1=0;
int a2=0;
int a3=0;
int a4=0;
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
int buttonState2 = digitalRead(pushButton2);
int buttonState3 = digitalRead(pushButton3);
int buttonState4 = digitalRead(pushButton4);
// print out the state of the button:
if (buttonState == HIGH && a1==0 ) { // If switch is ON,
Serial.print('a');
a1=1; // send 1 to Processing
}
if (buttonState == LOW && a1==1) { // If the switch is not ON,
Serial.println('b');
a1=0; // send 0 to Processing
}
if (buttonState2 == HIGH && a2==0 ) { // If switch is ON,
Serial.print('c');
a2=1; // send 1 to Processing
}
if (buttonState2 == LOW && a2==1) { // If the switch is not ON,
Serial.println('d');
a2=0; // send 0 to Processing
}
if (buttonState3 == HIGH && a3==0 ) { // If switch is ON,
Serial.print('e');
a3=1; // send 1 to Processing
}
if (buttonState3 == LOW && a3==1) { // If the switch is not ON,
Serial.println('f');
a3=0; // send 0 to Processing
}
if (buttonState4 == HIGH && a4==0 ) { // If switch is ON,
Serial.print('g');
a4=1; // send 1 to Processing
}
if (buttonState4 == LOW && a4==1) { // If the switch is not ON,
Serial.println('h');
a4=0; // send 0 to Processing
}
// Serial.println(buttonState);
delay(10); // delay in between reads for stability
}
http://www.youtube.com/watch?v=g0ORjiFmzmU
沒有留言:
張貼留言