一、期末作品processing程式碼(未加連動arduino的程式)
PImage imgBG, imgMAN;int score=0,time=3600;
boolean t=true;
ArrayList<PVector>dots;
ArrayList<PVector>pts;
void addPT(float x, float y, float w, float h){
pts.add(new PVector(x, y, 100));
}
void addD(float x, float y, float w, float h) {
dots.add(new PVector(x, y, 100));
}
void setup() {
size(788, 857);
imgBG=loadImage("bg.jpg");
imgBG.loadPixels();
pts = new ArrayList<PVector>();//white dot
dots = new ArrayList<PVector>();//red dot
//white dot 可以吃掉的白點點
for (int i=1; i<12; i++) {
addPT(25+i*30, 50, 17, 17);
}
for (int i=1; i<12; i++) {
addPT(400+i*30, 50, 17, 17);
}
for (int i=1; i<24; i++) {
addPT(25+i*30, 160, 17, 17);
}
for (int i=1; i<10; i++) {
addPT(240+i*30, 320, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(190, 43+i*30, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(352, 43+i*30, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(430, 43+i*30, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(590, 43+i*30, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(720, 43+i*30, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(720, 155+i*30, 17, 17);
}
for (int i=1; i<4; i++) {
addPT(55, 155+i*30, 17, 17);
}
for (int i=1; i<20; i++) {
addPT(590, 155+i*30, 17, 17);
}
for (int i=1; i<20; i++) {
addPT(190, 155+i*30, 17, 17);
}
for (int i=1; i<24; i++) {
addPT(25+i*30, 800, 17, 17);
}
for (int i=1; i<13; i++) {
addPT(200+i*30, 640, 17, 17);
}
for (int i=1; i<6; i++) {
addPT(200+i*30, 560, 17, 17);
}
for (int i=1; i<6; i++) {
addPT(410+i*30, 560, 17, 17);
}
for (int i=1; i<6; i++) {
addPT(585+i*30, 560, 17, 17);
}
for (int i=1; i<6; i++) {
addPT(20+i*30, 560, 17, 17);
}
//red dot 小精靈可以走的路徑
for (int i=1; i<23; i++) {
addD(30+i*15, 50, 17, 17);
}//1
for (int i=1; i<23; i++) {
addD(403+i*15, 50, 17, 17);
}//2
for (int i=1; i<48; i++) {
addD(30+i*15, 160, 17, 17);
}//3
for (int i=1; i<12; i++) {
addD(30+i*15, 240, 17, 17);
}//4
for (int i=1; i<9; i++) {
addD(240+i*15, 240, 17, 17);
}//5
for (int i=1; i<9; i++) {
addD(403+i*15, 240, 17, 17);
}//6
for (int i=1; i<13; i++) {
addD(560+i*15, 240, 17, 17);
}//7
for (int i=1; i<20; i++) {
addD(239+i*15, 320, 17, 17);
}//8
for (int i=1; i<7; i++) {
addD(195+i*15, 402, 17, 17);
}//9
for (int i=1; i<7; i++) {
addD(481+i*15, 402, 17, 17);
}//10
for (int i=1; i<20; i++) {
addD(241+i*15, 480, 17, 17);
}//11
for (int i=1; i<23; i++) {
addD(30+i*15, 560, 17, 17);
}//12
for (int i=1; i<23; i++) {
addD(405+i*15, 560, 17, 17);
}//13
for (int i=1; i<7; i++) {
addD(30+i*15, 640, 17, 17);
}//14
for (int i=1; i<30; i++) {
addD(163+i*15, 640, 17, 17);
}//15
for (int i=1; i<7; i++) {
addD(643+i*15, 640, 17, 17);
}//16
for (int i=1; i<12; i++) {
addD(30+i*15, 720, 17, 17);
}//17
for (int i=1; i<9; i++) {
addD(243+i*15, 720, 17, 17);
}//18
for (int i=1; i<9; i++) {
addD(405+i*15, 720, 17, 17);
}//19
for (int i=1; i<12; i++) {
addD(570+i*15, 720, 17, 17);
}//20
for (int i=1; i<48; i++) {
addD(30+i*15, 800, 17, 17);
}//21
for (int i=1; i<7; i++) {
addD(50, 51+i*15, 17, 17);
}//1
for (int i=1; i<7; i++) {
addD(190, 51+i*15, 17, 17);
}//2
for (int i=1; i<7; i++) {
addD(350, 51+i*15, 17, 17);
}//3
for (int i=1; i<7; i++) {
addD(430, 51+i*15, 17, 17);
}//4
for (int i=1; i<7; i++) {
addD(590, 51+i*15, 17, 17);
}//5
for (int i=1; i<7; i++) {
addD(720, 51+i*15, 17, 17);
}//6
for (int i=1; i<5; i++) {
addD(50, 163+i*15, 17, 17);
}//7
for (int i=1; i<5; i++) {
addD(190, 163+i*15, 17, 17);
}//8
for (int i=1; i<5; i++) {
addD(270, 163+i*15, 17, 17);
}//9
for (int i=1; i<5; i++) {
addD(510, 163+i*15, 17, 17);
}//10
for (int i=1; i<5; i++) {
addD(590, 163+i*15, 17, 17);
}//11
for (int i=1; i<5; i++) {
addD(720, 163+i*15, 17, 17);
}//12
for (int i=1; i<21; i++) {
addD(190, 242+i*15, 17, 17);
}//13
for (int i=1; i<5; i++) {
addD(350, 242+i*15, 17, 17);
}//14
for (int i=1; i<5; i++) {
addD(430, 242+i*15, 17, 17);
}//15
for (int i=1; i<21; i++) {
addD(590, 242+i*15, 17, 17);
}//16
for (int i=1; i<5; i++) {
addD(270, 325+i*15, 17, 17);
}//17
for (int i=1; i<5; i++) {
addD(510, 325+i*15, 17, 17);
}//18
for (int i=1; i<5; i++) {
addD(270, 405+i*15, 17, 17);
}//19
for (int i=1; i<5; i++) {
addD(510, 405+i*15, 17, 17);
}//20
for (int i=1; i<5; i++) {
addD(270, 482+i*15, 17, 17);
}//21
for (int i=1; i<5; i++) {
addD(510, 482+i*15, 17, 17);
}//22
for (int i=1; i<5; i++) {
addD(50, 562+i*15, 17, 17);
}//23
for (int i=1; i<5; i++) {
addD(190, 562+i*15, 17, 17);
}//24
for (int i=1; i<5; i++) {
addD(350, 562+i*15, 17, 17);
}//25
for (int i=1; i<5; i++) {
addD(430, 562+i*15, 17, 17);
}//26
for (int i=1; i<5; i++) {
addD(590, 562+i*15, 17, 17);
}//27
for (int i=1; i<5; i++) {
addD(720, 562+i*15, 17, 17);
}//28
for (int i=1; i<5; i++) {
addD(110, 642+i*15, 17, 17);
}//29
for (int i=1; i<5; i++) {
addD(190, 642+i*15, 17, 17);
}//30
for (int i=1; i<5; i++) {
addD(270, 642+i*15, 17, 17);
}//31
for (int i=1; i<5; i++) {
addD(510, 642+i*15, 17, 17);
}//32
for (int i=1; i<5; i++) {
addD(590, 642+i*15, 17, 17);
}//33
for (int i=1; i<5; i++) {
addD(670, 642+i*15, 17, 17);
}//34
for (int i=1; i<5; i++) {
addD(50, 725+i*15, 17, 17);
}//35
for (int i=1; i<5; i++) {
addD(350, 725+i*15, 17, 17);
}//36
for (int i=1; i<5; i++) {
addD(430, 725+i*15, 17, 17);
}//37
for (int i=1; i<5; i++) {
addD(720, 725+i*15, 17, 17);
}//38
}
float userX=390, userY=480, bgX=0, bgY=0, a=0, b=0;
void draw() {
image(imgBG, bgX, bgY);//背景圖
fill(255, 255, 0);
ellipse(userX, userY, 30, 30);//小精靈
if(time<660)fill(255,0,0);//如果時間於10秒字變紅色
else fill(255, 255, 255);//否則自是白色
textSize(25);
text("SCORE:"+score, 20, 400);//計分數
textSize(25);
text("TIME:"+(int)time/60, 650, 400);//計時間
//小精靈的上下左右
for (PVector dot : dots) {
if(t&&dot.x-userX<40&&dot.x-userX>=0&&userY<dot.y+10&&dot.y-10<userY){
if (keyPressed && keyCode==RIGHT) {
userX+=1;
}
}
else if(t&&dot.x-userX>-40&&dot.x-userX<=0&&userY<dot.y+10&&dot.y-10<userY){
if (keyPressed && keyCode==LEFT) {
userX-=1;
}
}
else if(t&&dot.y-userY>-40&&dot.y-userY<=0&&userX<dot.x+10&&dot.x-10<userX){
if (keyPressed && keyCode==UP) {
userY-=1;
}
}
else if(t&&dot.y-userY<40&&dot.y-userY>=0&&userX<dot.x+10&&dot.x-10<userX){
if (keyPressed && keyCode==DOWN) {
userY+=1;
}
}
}
//之前偵測用
println(green(imgBG.pixels[int(userX-bgX)+int(userY-bgY)*imgBG.width]));
if (green(imgBG.pixels[int(userX-bgX)+int(userY-bgY)*imgBG.width])>0)
{
println("OK");
}
fill(255, 0, 0);
for (PVector dot : dots) {//劃出紅點(小精靈可以走的路徑)
if(dot.z>0){
ellipse(dot.x, dot.y, 17, 17);
dot.z=-100;//讓紅點不會顯示在畫面上,讓它的z值往後
}
}
fill(255, 255, 255);
for(PVector pt : pts){//畫出白點點
if(pt.z>0) ellipse(pt.x, pt.y, 17,17);
if(pt.z>0 && dist(userX, userY, pt.x, pt.y)<24){//當小精靈碰到白點,白點消失
pt.z=-100;//白點消失,讓它的z值往後
score+=10;
}
}
Gameover();
}
void Gameover(){//時間到時顯示的畫面
if(time<60){
fill(255, 255, 255);
rect(200,200,400,400);
fill(255, 0, 0);
textSize(60);
text("SCORE:"+score, 230, 450);
text("TIME OUT", 250, 350);
t=false;
}else{
time--;
}
}
二、期末作品processing程式碼(加了連動arduino的程式)
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
myPort = new Serial(this, portName, 9600);//加在void setup()裡
//void draw()裡,用while包住原本案件上下左右的程式,當硬體有感應才執行
while(myPort.available()>0){ // If data is available
val = myPort.read();// read it and store it in val
for (PVector dot : dots) {
if(t&&dot.x-userX<40&&dot.x-userX>=0&&userY<dot.y+10&&dot.y-10<userY){
if (val==48) {//相對arduino的數字,十進位是0
userX+=0.2;//right
}
}
else if(t&&dot.x-userX>-40&&dot.x-userX<=0&&userY<dot.y+10&&dot.y-10<userY){
if (val==49) {//相對arduino的數字,十進位是1
userX-=0.2;//left
}
}
else if(t&&dot.y-userY>-40&&dot.y-userY<=0&&userX<dot.x+10&&dot.x-10<userX){
if (val==50) {//相對arduino的數字,十進位是2
userY-=0.2;//up
}
}
else if(t&&dot.y-userY<40&&dot.y-userY>=0&&userX<dot.x+10&&dot.x-10<userX){
if (val==51) {//相對arduino的數字,十進位是3
userY+=0.2;//down
}
}
}
}
三、Arduino的程式碼
void setup() {
// put your setup code here, to run once:
Seria.begin(9600);
pinMode(1,INPUT_PULLUP);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorVa1=digitalRead(1);
int sensorVa2=digitalRead(2);
int sensorVa3=digitalRead(3);
int sensorVa4=digitalRead(4);
if(sensorVa1==LOW) Serial.println('0');//連數字1,控制右
if(sensorVa2==LOW) Serial.println('1');//連數字2,控制左
if(sensorVa3==LOW) Serial.println('2');//連數字3,控制上
if(sensorVa4==LOW) Serial.println('3');//連數字4,控制下
}
沒有留言:
張貼留言