Processing程式碼:
import processing.serial.*;
Serial port;
int word;
PImage img;
int rx=28,ry=5;
boolean move = true;
int a=0,b=0,c=0,d=0;
void setup() {
size(400, 400);
img = loadImage("maze.jpg");
port = new Serial(this, "COM3", 9600);
}
void draw() {
if(port.available()>0){
word = port.read();
}
image(img, 0, 0);
loadPixels();
fill(225, 0, 0);//點點
ellipse(rx, ry, 10, 10);
if (move==true) {
if(word=='W')ry=ry-1;
if(word=='S')ry=ry+1;
if(word=='A')rx=rx-1;
if(word=='D')rx=rx+1;
}
color now_color = pixels[ry*400+rx];
if (rx>398){
rx=398;
fill(0, 0, 0);
rect(0, 0, 400, 400);
textSize(50);
fill(255, 255, 0);
text("Congratulation", 20, 220);
}
if (red(now_color)<100){
move = false;
fill(0, 0, 0);
rect(0, 0, 400, 400);
textSize(50);
fill(255, 255, 0);
text("GAME OVER", 60, 220);
}
else move = true;
}
----------------------------------------------------------------------------
Arduino程式碼:
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (buttonState1 == HIGH) {
Serial.write('W');
}
if (buttonState2 == HIGH) {
Serial.print('S');
}
if (buttonState3 == HIGH) {
tone(8, 165 , 300);
Serial.print('A');
}
if (buttonState4 == HIGH) {
tone(8, 175 , 300);
Serial.print('D');
}
delay(100);
}
沒有留言:
張貼留言