VIDEO
程式碼:
processing:
import processing.serial.*;
Serial myPort;
int i,d1=0,d2=0,d3=0,d4=0;
int score=0, lastTime=60;
int[] value = new int[50000];
color[] c=new color[4];
int a,count=0;
int tmp=0;
int startt;
int val;
void setup(){
size(500,600);
myPort = new Serial(this,
"COM4", 9600);
c[0]=color(255,0,0);
c[1]=color(0,255,0);
c[2]=color(0,0,255);
c[3]=color(255,255,255);
for(i=0;i<10000;i++)
{
a=int(random(0,3.99));
/*
if(i%25==0)
fill(255,0,0);
else if(i%30==0)
fill(0,255,0);
else if(i%40==0)
fill(0,0,255);
else
fill(255,255,255);
*/
count%=50000;
value[count++]=a;
}
}
void draw(){
background(255,200,100);
if(myPort.available()>0){
val = myPort.read();// 第三個重點: 如果讀到數字就拿出來用
//println("123");
}
textSize(30);
fill(100,0,50);
text("Time:"+lastTime, 50,80);
text("Score:"+score, 0,220);
if(lastTime==0){
stop();
textFont(createFont("3",
60));
background(100,100,100);
fill(255,255,255);
text("Game Over
",120,250);
text("Your
score:"+score,0,350);
}
lastTime=30-millis()/1000;
i=200;
for(int
k=startt;k<startt+20;k++){
fill(c[value[k]]);
rect(i,100,80,80);
i+=20;
}
fill(255,200,100);
rect(0,100,200,80);
if(d1==1){
fill(128,128,128);
}else{
fill(255,0,0);
}
rect(60,400,80,80);
if(d2==1){
fill(128,128,128);
}else{
fill(0,255,0);
}
rect(360,400,80,80);
if(d3==1){
fill(128,128,128);
}else{
fill(0,0,255);
}
rect(260,400,80,80);
if(d4==1){
fill(128,128,128);
}else{
fill(255,255,255);
}
rect(160,400,80,80);
if(val=='A'){
d1=1;
if(value[startt]==0){
startt++;
score+=10;
}
}
else if(val=='S'){
d2=1;if(value[startt]==1){
startt++;
score+=10;
}
}
else if(val=='D'){
d3=1;if(value[startt]==2)
{
startt++;
score+=10;
}
}else if(val=='F'){
d4=1;if(value[startt]==3)
{ startt++;
score+=10;
}
}
else if(val=='H'){
d1=0;
d2=0;
d3=0;
d4=0;
}
}
void keyPressed(){
if(key=='4'){
d1=1;
if(value[startt]==0){
startt++;
score+=10;
}
}else if(key=='5'){
d2=1;if(value[startt]==1){
startt++;
score+=10;
}
}else if(key=='r'){
d3=1;if(value[startt]==2)
{
startt++;
score+=10;
}
}else if(key=='t'){
d4=1;if(value[startt]==3)
{ startt++;
score+=10;
}
}
}
void keyReleased(){
if(key=='4'){
d1=0;
}else if(key=='5'){
d2=0;
}else if(key=='r'){
d3=0;
}else if(key=='t'){
d4=0;
}
}
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() {,tgy
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);
}
心得:
在眾多的課程當中我覺得這是最需要創意且最有難度的一堂課,要會打程式要會麵包板還需要很多靈感來完成作品,而這堂課除了要感謝老師也要感謝助教、學長還有同學的幫忙,讓我們這組能順利的打完程式碼。雖然這堂課困難重重,但我又比別人多會了一樣的東西。
組員分工:
程式碼:朱席寬
電路板:林品妤
找題材:朱席寬
期末上台報告:林品妤