標籤:

Arduino UNO 俄羅斯方塊

const int P_D=2;const int P_C=3;const int P_B=4;const int P_A=5;const int P_G=6;const int P_DI=7;const int P_CLK=8;const int P_LAT=9;const int I_LEFT=11;const int I_RIGHT=13;const int I_MID=12;const int I_OUT=10;const int D_LEFT=101;const int D_RIGHT=102;const int D_SPIN=103;const int D_DOWN=104;const int D_NO=105;const int D_ERR=106;bool startscene[16][16]={ {1,0,1,0,1,1,1,0,1,0,0,0,1,0,0,0}, {1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,1,1,0,1,1,1,0,1,0,0,0,1,0,0,0}, {1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,0,0,0,1,0,0,0,1,0,1,1,1,0}, {1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0}, {1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0}, {1,1,1,0,0,0,0,1,0,1,0,0,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,0,0,1,0,0,0,1,1,1,0,0,1,0}, {1,0,0,1,0,1,0,0,0,1,0,0,1,0,1,0}, {1,1,1,0,0,1,0,0,0,1,0,0,1,0,1,0}, {1,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0}, {1,0,1,1,0,1,1,1,0,1,1,1,0,0,1,0}};bool gamescene[16][16]{ {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,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1, 0, 0 } }};bool gameover=false;void setup() { Serial.begin(9600); for(int i=2;i<=9;i++) pinMode(i,OUTPUT); for(int i=10;i<=12;i++) pinMode(i,INPUT); pinMode(13,INPUT_PULLUP);}int nextc;long stime;int cubex=0,cubey=0;int score=0;void resetGame(){ nextc=millis()%7; score=0; cubex=0; cubey=0; for(int i=0;i<16;i++) { for(int j=0;j<16;j++) { if(j<8) { gamescene[i][j]=0; gamedata[i][j]=0; } else if(j==8) gamescene[i][j]=1; } }}long presstime;void loop() { while(true) { EPrint(startscene); if(getInputPress()==D_DOWN && millis()-presstime>=200) { gameover=false; resetGame(); stime=millis(); presstime=stime; break; } } CreateACube(); while(!gameover) { PutOnScreen(); EPrint(gamescene); if(millis()-stime>=1000) { stime=millis(); CubeFall(); } if(millis()-presstime>=130) { switch(getInputPress()) { case D_LEFT: CubeLeft();presstime=millis();break; case D_RIGHT: CubeRight();presstime=millis();break; case D_SPIN: CubeSpin();presstime=millis();break; case D_DOWN: CubeDown();presstime=millis();break; default: break; } } } GameOver(); while(true) { EPrint(gamescene); if(getInputPress()==D_DOWN && (millis()-presstime)>200) { presstime=millis(); return; } }}void GameOver(){ for(int i=0;i<16;i++) { for(int j=0;j<9;j++) { gamescene[i][j]=!gamescene[i][j]; } }}void CubeLeft(){ for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(gamedata[i+cubex][j+cubey]==1 && (j+cubey==0 || gamedata[i+cubex][j+cubey-1]==2) ) { return; } } } for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(gamedata[i+cubex][j+cubey-1]==0 && gamedata[i+cubex][j+cubey]==1) { gamedata[i+cubex][j+cubey]=0; gamedata[i+cubex][j+cubey-1]=1; } } } cubey--;}void CubeRight(){ for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(gamedata[i+cubex][j+cubey]==1 && (j+cubey==7 || gamedata[i+cubex][j+cubey+1]==2) ) { return; } } } for(int i=3;i>=0;i--) { for(int j=3;j>=0;j--) { if(gamedata[i+cubex][j+cubey+1]==0 && gamedata[i+cubex][j+cubey]==1) { gamedata[i+cubex][j+cubey]=0; gamedata[i+cubex][j+cubey+1]=1; } } } cubey++;}void CubeDown(){ while(true) { for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if( (gamedata[i+cubex][j+cubey]==1) && ((i+cubex==15)||gamedata[i+cubex+1][j+cubey]==2) ) { FallOK(); CreateACube(); return; } } } for(int i=15;i>=0;i--) { for(int j=7;j>=0;j--) { if(gamedata[i][j]==1) { int a=gamedata[i][j]; gamedata[i][j]=gamedata[i+1][j]; gamedata[i+1][j]=a; } } } cubex++; }}void FallOK(){ for(int i=0;(i<4 && i+cubex<16);i++) { for(int j=0;( j<4 && j+cubey<8 );j++) { if(gamedata[i+cubex][j+cubey]==1) { gamedata[i+cubex][j+cubey]=2; } } } ClearLines();}void ClearLines(){ int linenum=0; for(int i=15;i>=0;i--) { bool linecanclear=true; for(int j=7;j>=0;j--) { if(gamedata[i][j]!=2) { linecanclear=false; break; } } if(linecanclear) { ClearLine(i); linenum++; i++; } } score+=(linenum*linenum); if(score>=1000000)gameover=true; Serial.println(score);}void ClearLine(int line){ for(int i=0;i<8;i++) { gamedata[line][i]=0; } for(int i=line;i>=1;i--) { for(int j=0;j<8;j++) { int tem=gamedata[i][j]; gamedata[i][j]=gamedata[i-1][j]; gamedata[i-1][j]=tem; } }}void CubeFall(){ for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if( (gamedata[i+cubex][j+cubey]==1) && ((i+cubex==15)||gamedata[i+cubex+1][j+cubey]==2) ) { FallOK(); CreateACube(); return; } } } for(int i=15;i>=0;i--) { for(int j=7;j>=0;j--) { if(gamedata[i][j]==1) { int a=gamedata[i][j]; gamedata[i][j]=gamedata[i+1][j]; gamedata[i+1][j]=a; } } } cubex++;}void PutOnScreen(){ for(int i=0;i<16;i++) { for(int j=0;j<8;j++) { if(gamedata[i][j]!=0) { gamescene[i][j]=1; } else { gamescene[i][j]=0; } } } for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { gamescene[i+1][j+9]=squ[nextc][i][j]; } } for(int i=15;i>=6;i--) { for(int j=15;j>=9;j--) { int b=15-j; int l=15-i; if(score%power10(b+1)/power10(b)>l) { gamescene[i][j]=1; } else { gamescene[i][j]=0; } } }}int power10(int n){ int ret=1; for(int i=0;i<n;i++) { ret*=10; } return ret;}void CreateACube(){ int r=nextc; nextc=millis()%7; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(gamedata[i][j+2]==2) { gameover=true; return; } gamedata[i][j+2]=squ[r][i][j]; } } cubex=0; cubey=2; int sp=millis()%3; while(sp--)CubeSpin();}void CubeSpin(){ if(!CanSpin())return; int tem[4][4] { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { tem[i][j]=gamedata[3-j+cubex][i+cubey]; } } for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { gamedata[i+cubex][j+cubey]=tem[i][j]; } }}bool CanSpin(){ if(cubey<0 || cubey>4 || cubex<0 ||cubex>12)return false; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(gamedata[i+cubex][j+cubey]==2)return false; } } return true;}int getInputPress(){ int leftread=digitalRead(I_LEFT); int rightread=digitalRead(I_RIGHT); int spinread=digitalRead(I_MID); int downread=digitalRead(I_OUT); int number=((leftread==LOW)+(rightread!=LOW)+(spinread==LOW)+(downread==LOW)); if(number<1)return D_NO; if(number>=2)return D_ERR; if(leftread==LOW)return D_LEFT; if(rightread!=LOW)return D_RIGHT; if(spinread==LOW)return D_SPIN; if(downread==LOW)return D_DOWN; return D_NO;}void EPrint(bool arr[][16]){ for(int i=0;i<16;i++) { digitalWrite(P_G,HIGH); Send(arr[i]); digitalWrite(P_LAT,HIGH); delayMicroseconds(1); digitalWrite(P_LAT,LOW); delayMicroseconds(1); ChooseLine(i); digitalWrite(P_G,LOW); delayMicroseconds(100); }}void Send(bool arr[]){ digitalWrite(P_CLK,LOW); delayMicroseconds(1); digitalWrite(P_LAT,LOW); delayMicroseconds(1); for(int i=0;i<16;i++) { EWrite(P_DI,!arr[15-i]); delayMicroseconds(1); digitalWrite(P_CLK,HIGH); delayMicroseconds(1); digitalWrite(P_CLK,LOW); delayMicroseconds(1); }}void ChooseLine( int m){ switch(m) { case 0: digitalWrite(P_D, LOW);digitalWrite(P_C, LOW);digitalWrite(P_B, LOW);digitalWrite(P_A, LOW); break; case 1: digitalWrite(P_D, LOW);digitalWrite(P_C, LOW);digitalWrite(P_B, LOW);digitalWrite(P_A, HIGH); break; case 2: digitalWrite(P_D, LOW);digitalWrite(P_C, LOW);digitalWrite(P_B, HIGH);digitalWrite(P_A, LOW); break; case 3: digitalWrite(P_D, LOW);digitalWrite(P_C, LOW);digitalWrite(P_B, HIGH);digitalWrite(P_A, HIGH); break; case 4: digitalWrite(P_D, LOW);digitalWrite(P_C, HIGH);digitalWrite(P_B, LOW);digitalWrite(P_A, LOW); break; case 5: digitalWrite(P_D, LOW);digitalWrite(P_C, HIGH);digitalWrite(P_B, LOW);digitalWrite(P_A, HIGH); break; case 6: digitalWrite(P_D, LOW);digitalWrite(P_C, HIGH);digitalWrite(P_B, HIGH);digitalWrite(P_A, LOW); break; case 7: digitalWrite(P_D, LOW);digitalWrite(P_C, HIGH);digitalWrite(P_B, HIGH);digitalWrite(P_A, HIGH); break; case 8: digitalWrite(P_D, HIGH);digitalWrite(P_C, LOW);digitalWrite(P_B, LOW);digitalWrite(P_A, LOW); break; case 9: digitalWrite(P_D, HIGH);digitalWrite(P_C, LOW);digitalWrite(P_B, LOW);digitalWrite(P_A, HIGH); break; case 10: digitalWrite(P_D, HIGH);digitalWrite(P_C, LOW);digitalWrite(P_B, HIGH);digitalWrite(P_A, LOW); break; case 11: digitalWrite(P_D, HIGH);digitalWrite(P_C, LOW);digitalWrite(P_B, HIGH);digitalWrite(P_A, HIGH); break; case 12: digitalWrite(P_D, HIGH);digitalWrite(P_C, HIGH);digitalWrite(P_B, LOW);digitalWrite(P_A, LOW); break; case 13: digitalWrite(P_D, HIGH);digitalWrite(P_C, HIGH);digitalWrite(P_B, LOW);digitalWrite(P_A, HIGH); break; case 14: digitalWrite(P_D, HIGH);digitalWrite(P_C, HIGH);digitalWrite(P_B, HIGH);digitalWrite(P_A, LOW); break; case 15: digitalWrite(P_D, HIGH);digitalWrite(P_C, HIGH);digitalWrite(P_B, HIGH);digitalWrite(P_A, HIGH); break; default: break; }}void EWrite(int wpin,bool what){ if(what) { digitalWrite(wpin,HIGH); } else { digitalWrite(wpin,LOW); }}

推薦閱讀:

Github 加V認證
放學快走,你的電腦在實驗室自己喊啪嗒!
如何後台掛爾雅通識課?
從零開始手敲次世代遊戲引擎(卌)
如何學習編程語言?

TAG:編程 |