作業一: 製作期末作品_爆爆王
第一步 : 連接搖桿裝置

第二步 : 編寫程式 : 連接搖桿,移動遊戲角色。
// Arduino pin numbers
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 = 1; // analog pin connected to Y output
const int SW2_pin = 3;
const int X2_pin = 2;
const int Y2_pin = 3;
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
pinMode(SW2_pin, INPUT);
digitalWrite(SW2_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.write(digitalRead(2));
Serial.write(analogRead(A0)/4);
Serial.write(analogRead(A1)/4);
Serial.write(digitalRead(3));
Serial.write(analogRead(A2)/4);
Serial.write(analogRead(A3)/4);
delay(150);
}
import processing.serial.*;///
Serial myPort;///
import ddf.minim.*;
Minim minim;
AudioPlayer player;
void setup(){
size(850,500);
wall=loadImage("1.png");
wall2=loadImage("2.png");
wall3=loadImage("3.png");
play1=loadImage("play1.png");
play2=loadImage("play2.png");
water=loadImage("water1.png");
minim=new Minim(this);
player = minim.loadFile("music1.mp3",2048);
player.play();
myPort=new Serial(this,"COM6",115200); ///
/*sound=new SoundFile(this,"music1.mp3");
sound.play();
sound.loop();*/
}
PImage wall,wall2,wall3;
PImage play1;
PImage play2;
PImage water;
int stage=0;
//int gameover=0;
int userJ=9,userI=5;
float userX,userY;
int waterN=0;
int []waterJ=new int[100];
int []waterI=new int[100];
int []waterS=new int[100];
int []waterT=new int[100];
int map[][]={{2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2},
{1,2,0,2,0,0,0,3,2,0,0,0,3,0,2,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,2,0,2,0,0,0,3,3,0,0,0,3,0,3,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,0,1,0,1,0,0,3,3,0,0,0,2,0,2,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,0,2,0,1,0,0,3,2,0,0,0,2,0,2,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1}};
int user2J=3,user2I=5;
float user2X,user2Y;
int oldX=128, oldY=128;
int old2X=128, old2Y=128;
//int t=0,x,y,pin2;
void draw(){
if(myPort.available()>0){
/* t=myPort.read();
if(t=='w') pin2=myPort.read();
else if(t=='x') x=myPort.read();
else if(t=='y') y=myPort.read();*/
int pin2=myPort.read();
int x=myPort.read();
int y=myPort.read();
if(x>200 && map[userI][userJ+1]==0 && oldX<200) userJ++;
if(x<50 && map[userI][userJ-1]==0 && oldX>50) userJ--;
if(y>200 && map[userI+1][userJ]==0 && oldY<200) userI++;
if(y<50 && map[userI-1][userJ]==0 && oldY>50) userI--;
if(pin2==0){
waterI[waterN]=userI; waterJ[waterN]=userJ;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
int pin3=myPort.read();
int x1=myPort.read();
int y1=myPort.read();
if(x1>200 && map[user2I][user2J+1]==0 && old2X<200) user2J++;
if(x1<50 && map[user2I][user2J-1]==0 && old2X>50) user2J--;
if(y1>200 && map[user2I+1][user2J]==0 && old2Y<200) user2I++;
if(y1<50 && map[user2I-1][user2J]==0 && old2Y>50) user2I--;
if(pin3==0){
waterI[waterN]=user2I; waterJ[waterN]=user2J;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
oldX=x; oldY=y;
old2X=x1; old2Y=y1;
}
background(#80BF47);
noStroke();
for(int i=0;i<10;i++){
for(int j=0;j<17;j++){
if((j-i)%2 ==0) fill(#AFE380);
else fill(#80BF47);
rect(j*50,i*50,50,50);
if(map[i][j]==1)
image(wall,j*50,i*50,50,50);
if(map[i][j]==2)
image(wall2,j*50,i*50,50,50);
if(map[i][j]==3)
image(wall3,j*50,i*50,50,50);
/*fill(#7E610F);
rect(j*50,i*50,50,50);*/
}
}
image(play1,userJ*50-10,userI*50-10,70,70);
image(play2,user2J*50-25,user2I*50-25,90,90);
/* fill(255,0,0); ellipse(userJ*50+25,userI*50+25,50,50); // User
fill(0,0,255); ellipse(user2J*50+25,user2I*50+25,50,50); // User2*/
for(int i=0; i<waterN; i++){
if(waterT[i]>0){
image(water,waterJ[i]*50-25,waterI[i]*50-25,100,100);
/*fill(#9EE7F5); ellipse(waterJ[i]*50+25,waterI[i]*50+25,30,30);*/
waterT[i]--;
if(waterT[i]==0) waterT[i]=-60;
}
//if(gameover==1) background(255,0,0);
//if(gameover==2) background(0,0,255);
if(stage==1){
background(0,0,0);
fill(#FF2121); textSize(80);
text("Red Game Over",130,200);
}
if(stage==2){
background(0,0,0);
fill(#212AFF); textSize(80);
text("Blue Game Over",130,200);
}
if(waterT[i]<0){
for(int k=0;k<2;k++){
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]+k>=0 && waterJ[i]+k<=16 && (map[waterI[i]][waterJ[i]+k]==2 || map[waterI[i]][waterJ[i]+k]==1)) break;
fill(255); rect((waterJ[i]+k)*50,(waterI[i])*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]-k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]-k, user2J,user2I)==0) stage=2;
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]+k>=0 && waterJ[i]+k<=16 && map[waterI[i]][waterJ[i]+k]==3)
map[waterI[i]][waterJ[i]+k]=0;
}
for(int k=0;k<2;k++){
if(waterI[i]+k>=0 && waterI[i]+k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && (map[waterI[i]+k][waterJ[i]]==2 || map[waterI[i]+k][waterJ[i]]==1)) break;
fill(255); rect((waterJ[i])*50,(waterI[i]+k)*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]-k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]-k, user2J,user2I)==0) stage=2;
if(waterI[i]+k>=0 && waterI[i]+k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && map[waterI[i]+k][waterJ[i]]==3)
map[waterI[i]+k][waterJ[i]]=0;
}
for(int k=0;k<2;k++){
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]-k>=0 && waterJ[i]-k<=16 && (map[waterI[i]][waterJ[i]-k]==2 || map[waterI[i]][waterJ[i]-k]==1)) break;
fill(255); rect((waterJ[i]-k)*50,(waterI[i])*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]+k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]+k, user2J,user2I)==0) stage=2;
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]-k>=0 && waterJ[i]-k<=16 && map[waterI[i]][waterJ[i]-k]==3)
map[waterI[i]][waterJ[i]-k]=0;
}
for(int k=0;k<2;k++){
if(waterI[i]-k>=0 && waterI[i]-k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && (map[waterI[i]-k][waterJ[i]]==2 || map[waterI[i]-k][waterJ[i]]==1)) break;
fill(255); rect((waterJ[i])*50,(waterI[i]-k)*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]+k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]+k, user2J,user2I)==0) stage=2;
if(waterI[i]-k>=0 && waterI[i]-k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && map[waterI[i]-k][waterJ[i]]==3)
map[waterI[i]-k][waterJ[i]]=0;
}
waterT[i]++;
}
}
}
void keyPressed(){
if(keyCode==LEFT && map[userI][userJ-1]==0) userJ--;
if(keyCode==RIGHT && map[userI][userJ+1]==0) userJ++;
if(keyCode==UP && map[userI-1][userJ]==0) userI--;
if(keyCode==DOWN && map[userI+1][userJ]==0) userI++;
if(key=='0'){
waterI[waterN]=userI; waterJ[waterN]=userJ;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
if(keyCode=='A'&& map[user2I][user2J-1]==0) user2J--;
if(keyCode=='D'&& map[user2I][user2J+1]==0) user2J++;
if(keyCode=='W'&& map[user2I-1][user2J]==0) user2I--;
if(keyCode=='S'&& map[user2I+1][user2J]==0) user2I++;
if(key==' '){
waterI[waterN]=user2I; waterJ[waterN]=user2J;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
}
void mousePressed(){
if(stage==1 || stage==2) stage=0;
}
- Arduino程式碼 :
// Arduino pin numbers
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 = 1; // analog pin connected to Y output
const int SW2_pin = 3;
const int X2_pin = 2;
const int Y2_pin = 3;
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
pinMode(SW2_pin, INPUT);
digitalWrite(SW2_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.write(digitalRead(2));
Serial.write(analogRead(A0)/4);
Serial.write(analogRead(A1)/4);
Serial.write(digitalRead(3));
Serial.write(analogRead(A2)/4);
Serial.write(analogRead(A3)/4);
delay(150);
}
- Processing 程式碼 :
import processing.serial.*;///
Serial myPort;///
import ddf.minim.*;
Minim minim;
AudioPlayer player;
void setup(){
size(850,500);
wall=loadImage("1.png");
wall2=loadImage("2.png");
wall3=loadImage("3.png");
play1=loadImage("play1.png");
play2=loadImage("play2.png");
water=loadImage("water1.png");
minim=new Minim(this);
player = minim.loadFile("music1.mp3",2048);
player.play();
myPort=new Serial(this,"COM6",115200); ///
/*sound=new SoundFile(this,"music1.mp3");
sound.play();
sound.loop();*/
}
PImage wall,wall2,wall3;
PImage play1;
PImage play2;
PImage water;
int stage=0;
//int gameover=0;
int userJ=9,userI=5;
float userX,userY;
int waterN=0;
int []waterJ=new int[100];
int []waterI=new int[100];
int []waterS=new int[100];
int []waterT=new int[100];
int map[][]={{2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2},
{1,2,0,2,0,0,0,3,2,0,0,0,3,0,2,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,2,0,2,0,0,0,3,3,0,0,0,3,0,3,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,0,1,0,1,0,0,3,3,0,0,0,2,0,2,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,0,2,0,1,0,0,3,2,0,0,0,2,0,2,0,1},
{2,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,2},
{1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1}};
int user2J=3,user2I=5;
float user2X,user2Y;
int oldX=128, oldY=128;
int old2X=128, old2Y=128;
//int t=0,x,y,pin2;
void draw(){
if(myPort.available()>0){
/* t=myPort.read();
if(t=='w') pin2=myPort.read();
else if(t=='x') x=myPort.read();
else if(t=='y') y=myPort.read();*/
int pin2=myPort.read();
int x=myPort.read();
int y=myPort.read();
if(x>200 && map[userI][userJ+1]==0 && oldX<200) userJ++;
if(x<50 && map[userI][userJ-1]==0 && oldX>50) userJ--;
if(y>200 && map[userI+1][userJ]==0 && oldY<200) userI++;
if(y<50 && map[userI-1][userJ]==0 && oldY>50) userI--;
if(pin2==0){
waterI[waterN]=userI; waterJ[waterN]=userJ;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
int pin3=myPort.read();
int x1=myPort.read();
int y1=myPort.read();
if(x1>200 && map[user2I][user2J+1]==0 && old2X<200) user2J++;
if(x1<50 && map[user2I][user2J-1]==0 && old2X>50) user2J--;
if(y1>200 && map[user2I+1][user2J]==0 && old2Y<200) user2I++;
if(y1<50 && map[user2I-1][user2J]==0 && old2Y>50) user2I--;
if(pin3==0){
waterI[waterN]=user2I; waterJ[waterN]=user2J;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
oldX=x; oldY=y;
old2X=x1; old2Y=y1;
}
background(#80BF47);
noStroke();
for(int i=0;i<10;i++){
for(int j=0;j<17;j++){
if((j-i)%2 ==0) fill(#AFE380);
else fill(#80BF47);
rect(j*50,i*50,50,50);
if(map[i][j]==1)
image(wall,j*50,i*50,50,50);
if(map[i][j]==2)
image(wall2,j*50,i*50,50,50);
if(map[i][j]==3)
image(wall3,j*50,i*50,50,50);
/*fill(#7E610F);
rect(j*50,i*50,50,50);*/
}
}
image(play1,userJ*50-10,userI*50-10,70,70);
image(play2,user2J*50-25,user2I*50-25,90,90);
/* fill(255,0,0); ellipse(userJ*50+25,userI*50+25,50,50); // User
fill(0,0,255); ellipse(user2J*50+25,user2I*50+25,50,50); // User2*/
for(int i=0; i<waterN; i++){
if(waterT[i]>0){
image(water,waterJ[i]*50-25,waterI[i]*50-25,100,100);
/*fill(#9EE7F5); ellipse(waterJ[i]*50+25,waterI[i]*50+25,30,30);*/
waterT[i]--;
if(waterT[i]==0) waterT[i]=-60;
}
//if(gameover==1) background(255,0,0);
//if(gameover==2) background(0,0,255);
if(stage==1){
background(0,0,0);
fill(#FF2121); textSize(80);
text("Red Game Over",130,200);
}
if(stage==2){
background(0,0,0);
fill(#212AFF); textSize(80);
text("Blue Game Over",130,200);
}
if(waterT[i]<0){
for(int k=0;k<2;k++){
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]+k>=0 && waterJ[i]+k<=16 && (map[waterI[i]][waterJ[i]+k]==2 || map[waterI[i]][waterJ[i]+k]==1)) break;
fill(255); rect((waterJ[i]+k)*50,(waterI[i])*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]-k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]-k, user2J,user2I)==0) stage=2;
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]+k>=0 && waterJ[i]+k<=16 && map[waterI[i]][waterJ[i]+k]==3)
map[waterI[i]][waterJ[i]+k]=0;
}
for(int k=0;k<2;k++){
if(waterI[i]+k>=0 && waterI[i]+k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && (map[waterI[i]+k][waterJ[i]]==2 || map[waterI[i]+k][waterJ[i]]==1)) break;
fill(255); rect((waterJ[i])*50,(waterI[i]+k)*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]-k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]-k, user2J,user2I)==0) stage=2;
if(waterI[i]+k>=0 && waterI[i]+k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && map[waterI[i]+k][waterJ[i]]==3)
map[waterI[i]+k][waterJ[i]]=0;
}
for(int k=0;k<2;k++){
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]-k>=0 && waterJ[i]-k<=16 && (map[waterI[i]][waterJ[i]-k]==2 || map[waterI[i]][waterJ[i]-k]==1)) break;
fill(255); rect((waterJ[i]-k)*50,(waterI[i])*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]+k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]+k, user2J,user2I)==0) stage=2;
if(waterI[i]>=0 && waterI[i]<=9 && waterJ[i]-k>=0 && waterJ[i]-k<=16 && map[waterI[i]][waterJ[i]-k]==3)
map[waterI[i]][waterJ[i]-k]=0;
}
for(int k=0;k<2;k++){
if(waterI[i]-k>=0 && waterI[i]-k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && (map[waterI[i]-k][waterJ[i]]==2 || map[waterI[i]-k][waterJ[i]]==1)) break;
fill(255); rect((waterJ[i])*50,(waterI[i]-k)*50,50,50);
if(dist(waterJ[i]+k,waterI[i], userJ,userI)==0) stage=1;
if(dist(waterJ[i],waterI[i]+k, userJ,userI)==0) stage=1;
if(dist(waterJ[i]+k,waterI[i], user2J,user2I)==0) stage=2;
if(dist(waterJ[i],waterI[i]+k, user2J,user2I)==0) stage=2;
if(waterI[i]-k>=0 && waterI[i]-k<=9 && waterJ[i]>=0 && waterJ[i]<=16 && map[waterI[i]-k][waterJ[i]]==3)
map[waterI[i]-k][waterJ[i]]=0;
}
waterT[i]++;
}
}
}
void keyPressed(){
if(keyCode==LEFT && map[userI][userJ-1]==0) userJ--;
if(keyCode==RIGHT && map[userI][userJ+1]==0) userJ++;
if(keyCode==UP && map[userI-1][userJ]==0) userI--;
if(keyCode==DOWN && map[userI+1][userJ]==0) userI++;
if(key=='0'){
waterI[waterN]=userI; waterJ[waterN]=userJ;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
if(keyCode=='A'&& map[user2I][user2J-1]==0) user2J--;
if(keyCode=='D'&& map[user2I][user2J+1]==0) user2J++;
if(keyCode=='W'&& map[user2I-1][user2J]==0) user2I--;
if(keyCode=='S'&& map[user2I+1][user2J]==0) user2I++;
if(key==' '){
waterI[waterN]=user2I; waterJ[waterN]=user2J;
waterS[waterN]=5; waterT[waterN]=120;
waterN++;
}
}
void mousePressed(){
if(stage==1 || stage==2) stage=0;
}
沒有留言:
張貼留言