小朋友過馬路
processing:
import processing.serial.*;
Serial myPort;
int savetime=20;//time
PImage people, amb, imgBG;
PImage car[]=new PImage[8];
float pX=300, pY=366;
void setup() {
size(600, 400);
frameRate(50);
myPort = new Serial(this, "COM4", 9600);
imgBG = loadImage("bg.png");
people = loadImage("super.png");//people
amb = loadImage("amb.png");//ambullance
for (int i=0;i<8;i++) {
car[i]=loadImage("car"+i+".png");
}
}
float carX[]= { 600, 0, 600, 600, 0, 0, 600, 0};//direction
float carVX[]= { -8, 4, -4, -5, 9, 5, -7, 6};//speed
int but;//button
void draw() {
background(imgBG);
image(people, pX, pY, 25, 35);
if (myPort.available()>0)
{
but = myPort.read();
}
textSize(24);//time
int passtime = millis() - savetime;
int gametime = 20-passtime/1000;
textSize(24);
text("T I M E : "+gametime, 400, 50);
for (int i=0;i<8;i++) { //car
image(car[i], carX[i], 85+i*35, 50, 35);
carX[i]+=carVX[i];
if (carX[i]>600) carX[i]=0;
if (carX[i]<0) carX[i]=+600;
}
for (int i=0;i<8;i++) {
if(dist(carX[i],85+i*35,pX,pY)<=20){
carX[i] = pX;
rect(150,100,300,200);
fill(255,255,0);
textSize(30);
text(" L O S E ",250,200);
fill(255,0,0);
}
}
//people
if (but == 'U'&& pY>50) pY-=5;//up
if (but == 'D'&& pY<365) pY+=5;//down
if (but == 'L'&& pX>15) pX-=5;//left
if (but == 'R'&& pX<565) pX+=5;//right
if( gametime <= 0 ){
//rect(150,100,300,200);
//fill(255,255,255);
background(#81FF7C);
textSize(30);
text(" T I M E I S U P ",170,200);
fill(255,0,0);
}
if(pY>35 && pY<50) {
//rect(150,100,300,200);
//fill(255,255,255);
background(#81FF7C);
textSize(30);
text(" W I N ",250,200);
fill(255,0,0);
}
}
arduino:
void setup(){
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
}
int up=0,down=0,left=0,right=0;
void loop(){
if(up==0 && digitalRead(2)==LOW){//pressed
Serial.write('u'); up=1;
}
if(up==1 && digitalRead(2)==HIGH){//not pressed
Serial.write('U'); up=0;
}
if(down==0 && digitalRead(3)==LOW){//pressed
Serial.write('d'); down=1;
}
if(down==1 && digitalRead(3)==HIGH){//not pressed
Serial.write('D'); down=0;
}
if(left==0 && digitalRead(4)==LOW){//pressed
Serial.write('l'); left=1;
}
if(left==1 && digitalRead(4)==HIGH){//not pressed
Serial.write('L'); left=0;
}
if(right==0 && digitalRead(5)==LOW){//pressed
Serial.write('r'); right=1;
}
if(right==1 && digitalRead(5)==HIGH){//not pressed
Serial.write('R'); right=0;
}
delay(10);//to avoid noise while pressing
}
沒有留言:
張貼留言