這次的期末作品是拿我們的期中作品來改良的,把原本用滑鼠移動的海綿寶寶改用Arduino的按鍵來坐上下左右的移動
Arduino程式碼:
int buttonPin5 = 5;
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonState5 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
pinMode(buttonPin5, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState5 = digitalRead(buttonPin5);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (buttonState5 == HIGH) {
//tone(8, 131, 300);
Serial.write('A');
}
else if (buttonState5 == LOW) {
//Serial.write('Z');
}
if (buttonState2== HIGH) {
//tone(8, 147 , 300);
Serial.print('S');
}
else if (buttonState2 == LOW) {
//Serial.write('X');
}
if (buttonState3 == HIGH) {
//tone(8, 165 , 300);
Serial.print('D');
}
else if (buttonState3 == LOW) {
//Serial.write('C');
}
if (buttonState4 == HIGH) {
//tone(8, 175 , 300);
Serial.print('F');
}
else if (buttonState4 == LOW) {
//Serial.write('V');
}
if(buttonState2 == LOW&&buttonState3 == LOW&&buttonState4 == LOW&&buttonState5 == LOW){
Serial.write('H');
}
delay(500);
}
processing程式碼:
import processing.serial.*;
Serial myPort;
int popox=0,popoy=0,popoy1=0,popoy2=0,popoy3=0,popoy4=0,popoy5=0;
int fishx=0,fishy=0;
int score=600;
int lasttime=60;
import ddf.minim.*;
AudioPlayer player,wate,cheers;
Minim minim;
PImage popo;
//PImage finger;
PImage bobo;
PImage back;
PImage fish;
void setup()
{
size(800,600);
myPort = new Serial(this, "COM3", 9600);
fish=loadImage("fish.png"); //游動的魚
bobo=loadImage("bobo.png"); //海綿寶寶
//finger=loadImage("finger.png"); //手指
popo=loadImage("popo.png"); //泡泡
back=loadImage("back.jpeg"); //背景圖
minim=new Minim(this);
//wate=minim.loadFile("wate.wav", 2048);
player=minim.loadFile("file.mp3"); //背景音樂
player.play();
}
int val;
int b_x=400,b_y=300;
int w=5,s=5,a=5,d=5;
void draw()
{
if(myPort.available()>0){
val = myPort.read();//第三個重點:如果讀到數字就拿出來用
println(val);
}
image(back,0,0);
//image(bobo,250,100);
popoy-=3; //泡泡移動量
image(popo,popox+300,popoy+600,100,100); //泡泡坐標,大小
if (b_x > popox+300 && b_x< popox+400 && b_y<popoy+700 && b_y>popoy+600) //判斷滑鼠是否點到泡泡的範圍
{
popoy=600; //泡泡回到下面再跑一次
score-=10; //碰到泡泡減十分
wate=minim.loadFile("wate.wav"); //戳到泡泡時播放音效
wate.play();
}
if (b_x > popox+200 && b_x< popox+300 && b_y<popoy1+600 && b_y>popoy1+500)
{
popoy1=500;
score-=10;
wate=minim.loadFile("wate.wav");
wate.play();
}
if (b_x > popox+100 && b_x< popox+200 && b_y<popoy2+800 && b_y>popoy2+700)
{
popoy2=700;
score-=10;
wate=minim.loadFile("wate.wav");
wate.play();
}
if (b_x > popox+500 && b_x< popox+600 && b_y<popoy3+750 && b_y>popoy3+650)
{
popoy3=600;
score-=10;
wate=minim.loadFile("wate.wav");
wate.play();
}
if (b_x > popox+400 && b_x< popox+500 && b_y<popoy4+950 && b_y>popoy4+850)
{
popoy4=600;
score-=10;
wate=minim.loadFile("wate.wav");
wate.play();
}
if (b_x > popox+600 && b_x< popox+700 && b_y<popoy5+1050 && b_y>popoy5+950)
{
popoy5=700;
score-=10;
wate=minim.loadFile("wate.wav");
wate.play();
}
if(popoy+600<0) //判斷泡泡是否跑出視窗
{
popoy=0; //泡泡回到原點再跑一次
}
popoy1-=2;
image(popo,popox+200,popoy1+500,100,100);
if(popoy1+500<0)
{
popoy1=0;
}
popoy2-=3;
image(popo,popox+100,popoy2+700,100,100);
if(popoy2+700<0)
{
popoy2=0;
}
popoy3-=5.5;
image(popo,popox+500,popoy3+650,100,100);
if(popoy3+650<0)
{
popoy3=0;
}
popoy4-=2;
image(popo,popox+400,popoy4+850,100,100);
if(popoy4+850<0)
{
popoy4=0;
}
popoy5-=3;
image(popo,popox+600,popoy5+950,100,100);
if(popoy5+950<0)
{
popoy5=0;
}
image(bobo,b_x,b_y,60,60);
fishx++;
image(fish,fishx,fishy+200);
{
if(fishx>800) //判斷魚是否跑出視窗
{
fishx=0;
fishy=250;
}
}
world();
if(val=='A'){
b_x-=4;
}
if(val=='S'){
b_x+=4;
}
if(val=='F'){
b_y-=4;
}
if(val=='D'){
b_y+=4;
}
if(val=='H'){
b_y+=0;
b_x+=0;
}
if(b_x<0)
{
b_x=0;
}
if(b_x>745)
{
b_x=745;
}
if(b_y<0)
{
b_y=0;
}
if(b_y>540)
{
b_y=540;
}
}
void world()
{
textFont(createFont("Georgia", 48));
fill(255);
text("score:"+score, 20, 550); //分數
textFont(createFont("Georgia", 48));
fill(255);
text("Time:"+lasttime, 250, 550); //計時器
lasttime=60-millis()/1000;
if(lasttime==0) //當時間=0,秀出得分
{
stop();
textFont(createFont("Georgia", 80));
background(51,102,51);
fill(255,255,255);
text("Game Over",200,300);
text("Your score:"+score,180,400);
cheers=minim.loadFile("cheers.wav");
cheers.play();
}
}
心得:
這次的期末作業我們覺得沒有做得很好,Demo到後面的時候有點乾
按鍵道具的部分也是用課堂上的案件,並沒有花心思在其他道具上,但在Arduino的程式上,我們花了不少時間去研究,也學到了不少的東西.
沒有留言:
張貼留言