用STC單片機DIY 3D立體數字滑塊推盤游戲 3d Puzzle 4x4x4 串口版 全球首發!
傳統的2D數字滑塊推盤現已有百年歷史,現3D立體數字推盤正式于2016年誕生!
由4個平面推盤組成,只有一層有空區,其余的沒有。
平面移動方法與傳統推盤一致,立體移動方法是層之間進行移動。
可移植到數碼管進行顯示,共需要128個數碼管進行顯示,只用IO口驅動大量數碼管是不可能的,而且動態掃描亮度影響非常大,所以要用到74系列芯片或者其他LED數碼管驅動芯片進行驅動。
還原技巧以及算法比傳統的更加復雜,可能會難倒世界頂級拼圖還原高手!
3D數字推盤由于機械式設計非常復雜,采用單片機和數碼管和按鍵實現了電子式設計。
若發現建議意見以及BUG等問題請在下面回復,謝謝合作!
以下源碼用Keil4編譯后直接燒寫到單片機內即可運行,根據晶振頻率修改串口位延時時間即可。
- #include "reg51.h"
- #include "intrins.h"
- sbit rxd=P3^0; //數據接收口
- sbit txd=P3^1; //數據發送口
- unsigned char l1[16]={ //推盤層1
- 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
- };
- unsigned char l2[16]={ //推盤層2
- 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
- };
- unsigned char l3[16]={ //推盤層3
- 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47
- };
- unsigned char l4[16]={ //推盤層4
- 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
- };
- unsigned char code xyd[64] = { //拼圖板移動方向與0交換坐標數據,請勿修改否則數字會無法正常移動。
- 0x04, 0x10, 0x01, 0x10, 0x05, 0x10, 0x02, 0x00, 0x06, 0x10, 0x03, 0x01, 0x07, 0x10, 0x10, 0x02,
- 0x08, 0x00, 0x05, 0x10, 0x09, 0x01, 0x06, 0x04, 0x0A, 0x02, 0x07, 0x05, 0x0B, 0x03, 0x10, 0x06,
- 0x0C, 0x04, 0x09, 0x10, 0x0D, 0x05, 0x0A, 0x08, 0x0E, 0x06, 0x0B, 0x09, 0x0F, 0x07, 0x10, 0x0A,
- 0x10, 0x08, 0x0D, 0x10, 0x10, 0x09, 0x0E, 0x0C, 0x10, 0x0A, 0x0F, 0x0D, 0x10, 0x0B, 0x10, 0x0E
- };
- sfr AUXR = 0x8E; //定義AUXR寄存器
- sfr INT_CLKO = 0x8F; //定義下降沿中斷寄存器
- //------------------------軟串口驅動程序------------------------
- /*
- void init_uart(){ //初始化串口 使用硬件下降沿中斷必須初始化
- AUXR |= 0x80; //設置AUXR寄存器
- INT_CLKO |= 0x40; //開串口中斷 (允許串口接收數據)
- EA = 1; //啟用I/O口外部中斷功能
- }
- */
- void delay_uart(){ //延時時間1除以波特率秒 1/9600秒
- unsigned char a,b;
- for(b=114;b>0;b--)
- for(a=1;a>0;a--);
- }
- void txd_data(unsigned char i){ //發送數據
- unsigned char j=1;//控制移位
- unsigned char k=8;//控制循環次數
- delay_uart();//延時時間
- txd=0; //發送起始位,低電平
- while(k--){ //下面循環8次
- delay_uart();//延時時間
- txd=(i&j)/j; //發送數據
- j<<=1; //左移1位,發送下1位數據
- }
- delay_uart();//延時時間
- txd=1; //發送停止位
- }
- unsigned char rxd_data(){ //接收數據
- unsigned char j=1;//控制移位
- unsigned char k=8;//控制循環次數
- unsigned char d=0;//存放數據
- while(rxd == 1); //等待起始位低電平
- while(k--){//下面循環8次
- delay_uart(); //延時時間
- d|=(rxd*j); //存入數據
- j<<=1; //左移1位,接收下1位數據
- }
- delay_uart();//延時時間
- if(rxd == 1){ //停止位為高電平
- return d; //返回數據
- } else {
- return 0; //無效數據 返回0
- }
- }
- void send_text(unsigned char *text){ //發送字符串
- for(;*text!=0;text++){ //遇到停止符0結束發送
- txd_data(*text); //發送數據
- }
- }
- void send_enter(){ //發送換行
- txd_data(0x0D);
- txd_data(0x0A);
- }
- //------------------------3D推盤驅動程序------------------------
- //0的坐標:0~15 移動方向:1234 表示上下左右
- unsigned char get_adj_coo(unsigned char zero_adj,unsigned char mov_dir){ //獲取相鄰坐標
- return xyd[zero_adj*4+mov_dir-1];
- }
- unsigned char get_0_lay(){ //獲取0所在的層數 返回1~4
- unsigned char add=0;
- for(add=0;add<16;add++){
- if(l1[add] == 0){
- return 1;
- }
- if(l2[add] == 0){
- return 2;
- }
- if(l3[add] == 0){
- return 3;
- }
- if(l4[add] == 0){
- return 4;
- }
- }
- return 0;
- }
- unsigned char get_0_pla(){ //獲取0的平面坐標 返回0~15
- unsigned char lay=0;
- unsigned char add=0; //當前掃描的坐標
- lay=get_0_lay(); //獲取0所在的層數
- for(add=0;add<16;add++){
- if(lay == 1){
- if(l1[add] == 0){
- return add;
- }
- }
- if(lay == 2){
- if(l2[add] == 0){
- return add;
- }
- }
- if(lay == 3){
- if(l3[add] == 0){
- return add;
- }
- }
- if(lay == 4){
- if(l4[add] == 0){
- return add;
- }
- }
- }
- return 0;
- }
- //平面交換數字位置 層數:1~4 坐標A 坐標B
- void pec(unsigned char s,unsigned char a,unsigned char b){
- unsigned char ad=0; //坐標A數據
- unsigned char bd=0; //坐標B數據
- if(a > 15 || b > 15) {
- return;
- }
- if(s == 1){ad=l1[a];bd=l1[b];l1[a]=bd;l1[b]=ad;return;}
- if(s == 2){ad=l2[a];bd=l2[b];l2[a]=bd;l2[b]=ad;return;}
- if(s == 3){ad=l3[a];bd=l3[b];l3[a]=bd;l3[b]=ad;return;}
- if(s == 4){ad=l4[a];bd=l4[b];l4[a]=bd;l4[b]=ad;return;}
- return;
- }
- void up_mov(){ //拼圖上移
- unsigned char lay0=0;
- unsigned char pla0=0;
- unsigned char adj=0;
- lay0=get_0_lay(); //獲取0的坐標所在層數
- pla0=get_0_pla(); //獲取0的平面坐標
- adj=get_adj_coo(pla0,1); //獲取相鄰坐標
- pec(lay0,pla0,adj); //平面交換數字位置
- }
- void down_mov(){ //拼圖下移
- unsigned char lay0=0;
- unsigned char pla0=0;
- unsigned char adj=0;
- lay0=get_0_lay(); //獲取0的坐標所在層數
- pla0=get_0_pla(); //獲取0的平面坐標
- adj=get_adj_coo(pla0,2); //獲取相鄰坐標
- pec(lay0,pla0,adj); //平面交換數字位置
- }
- void left_mov(){ //拼圖左移
- unsigned char lay0=0;
- unsigned char pla0=0;
- unsigned char adj=0;
- lay0=get_0_lay(); //獲取0的坐標所在層數
- pla0=get_0_pla(); //獲取0的平面坐標
- adj=get_adj_coo(pla0,3); //獲取相鄰坐標
- pec(lay0,pla0,adj); //平面交換數字位置
- }
- void right_mov(){ //拼圖右移
- unsigned char lay0=0;
- unsigned char pla0=0;
- unsigned char adj=0;
- lay0=get_0_lay(); //獲取0的坐標所在層數
- pla0=get_0_pla(); //獲取0的平面坐標
- adj=get_adj_coo(pla0,4); //獲取相鄰坐標
- pec(lay0,pla0,adj); //平面交換數字位置
- }
- void in_mov(){ //拼圖里移
- unsigned char lay0=0; //0的坐標所在層數
- unsigned char pla0=0; //0的平面坐標
- unsigned char add=0; //累加計數
- lay0=get_0_lay(); //獲取0的坐標所在層數
- pla0=get_0_pla(); //獲取0的平面坐標
- if(lay0 < 4){
- if(lay0 == 3){l3[pla0]=l4[pla0];l4[pla0]=0;}
- if(lay0 == 2){l2[pla0]=l3[pla0];l3[pla0]=0;}
- if(lay0 == 1){l1[pla0]=l2[pla0];l2[pla0]=0;}
- }
- }
- void out_mov(){ //拼圖外移
- unsigned char lay0=0; //0的坐標所在層數
- unsigned char pla0=0; //0的平面坐標
- unsigned char add=0; //累加計數
- lay0=get_0_lay(); //獲取0的坐標所在層數
- pla0=get_0_pla(); //獲取0的平面坐標
- if(lay0 > 1){
- if(lay0 == 4){l4[pla0]=l3[pla0];l3[pla0]=0;}
- if(lay0 == 3){l3[pla0]=l2[pla0];l2[pla0]=0;}
- if(lay0 == 2){l2[pla0]=l1[pla0];l1[pla0]=0;}
- }
- }
- void send_puzzle_l1(){
- unsigned char a=0;
-
- send_text("┏━┳━┳━┳━┓");send_enter(); //發送頂部表格框架
- for(a=0;a<16;a++){
- send_text("┃");
- if(l1[a] == 0){ //讀取到的數字為0則發送2個空格
- txd_data(0x20);
- txd_data(0x20);
- } else { //不為0則發送數字
- txd_data((l1[a]/10)|0x30); //發送數字十位
- txd_data((l1[a]%10)|0x30); //發送數字個位
- }
-
- if(a==3 || a==7 || a==11){ //讀取到第3 7 11個數字
- send_text("┃"); //發送分隔符
- send_enter();
- send_text("┣━╋━╋━╋━┫"); //發送中間表格框架
- send_enter();
- }
- }
- send_text("┃");
- send_enter();
- send_text("┗━┻━┻━┻━┛"); //發送底部表格框架
- }
- void send_puzzle_l2(){
- unsigned char a=0;
-
- send_text("┏━┳━┳━┳━┓");send_enter(); //發送頂部表格框架
- for(a=0;a<16;a++){
- send_text("┃");
- if(l2[a] == 0){ //讀取到的數字為0則發送2個空格
- txd_data(0x20);
- txd_data(0x20);
- } else { //不為0則發送數字
- txd_data((l2[a]/10)|0x30); //發送數字十位
- txd_data((l2[a]%10)|0x30); //發送數字個位
- }
-
- if(a==3 || a==7 || a==11){ //讀取到第3 7 11個數字
- send_text("┃"); //發送分隔符
- send_enter();
- send_text("┣━╋━╋━╋━┫"); //發送中間表格框架
- send_enter();
- }
- }
- send_text("┃");
- send_enter();
- send_text("┗━┻━┻━┻━┛"); //發送底部表格框架
- }
- void send_puzzle_l3(){
- unsigned char a=0;
-
- send_text("┏━┳━┳━┳━┓");send_enter(); //發送頂部表格框架
- for(a=0;a<16;a++){
- send_text("┃");
- if(l3[a] == 0){ //讀取到的數字為0則發送2個空格
- txd_data(0x20);
- txd_data(0x20);
- } else { //不為0則發送數字
- txd_data((l3[a]/10)|0x30); //發送數字十位
- txd_data((l3[a]%10)|0x30); //發送數字個位
- }
-
- if(a==3 || a==7 || a==11){ //讀取到第3 7 11個數字
- send_text("┃"); //發送分隔符
- send_enter();
- send_text("┣━╋━╋━╋━┫"); //發送中間表格框架
- send_enter();
- }
- }
- send_text("┃");
- send_enter();
- send_text("┗━┻━┻━┻━┛"); //發送底部表格框架
- }
- void send_puzzle_l4(){
- unsigned char a=0;
-
- send_text("┏━┳━┳━┳━┓");send_enter(); //發送頂部表格框架
- for(a=0;a<16;a++){
- send_text("┃");
- if(l4[a] == 0){ //讀取到的數字為0則發送2個空格
- txd_data(0x20);
- txd_data(0x20);
- } else { //不為0則發送數字
- txd_data((l4[a]/10)|0x30); //發送數字十位
- txd_data((l4[a]%10)|0x30); //發送數字個位
- }
-
- if(a==3 || a==7 || a==11){ //讀取到第3 7 11個數字
- send_text("┃"); //發送分隔符
- send_enter();
- send_text("┣━╋━╋━╋━┫"); //發送中間表格框架
- send_enter();
- }
- }
- send_text("┃");
- send_enter();
- send_text("┗━┻━┻━┻━┛"); //發送底部表格框架
- }
- void send_3dpuzzle(){ //通過串口發送拼圖數據
- txd_data(0x0C);
- send_text("3D立體數字滑塊推盤游戲 4x4x4 串口版 WASD控制上下左右 Q里 E外,全球首發! ");
- send_enter();
- send_text("第一層(最里層):");send_enter();
- send_puzzle_l1();send_enter();send_enter();
- send_text("第二層:");send_enter();
- send_puzzle_l2();send_enter();send_enter();
- send_text("第三層:");send_enter();
- send_puzzle_l3();send_enter();send_enter();
- send_text("第四層(最外層):");send_enter();
- send_puzzle_l4();send_enter();send_enter();
- }
- void exint4() //interrupt 16
- { //P3.0下降沿中斷 (串口接收到數據)
- unsigned char dat=0; //數據存放變量
- // INT_CLKO &= 0xBF; //關閉中斷,避免重復觸發中斷程序。
- dat=rxd_data(); //串口接收數據。
- if(dat == 0x57 || dat == 0x77) up_mov(); //W 上
- if(dat == 0x41 || dat == 0x61) left_mov(); //A 左
- if(dat == 0x53 || dat == 0x73) down_mov(); //S 下
- if(dat == 0x44 || dat == 0x64) right_mov(); //D 右
- if(dat == 0x51 || dat == 0x71) in_mov(); //Q 里
- if(dat == 0x45 || dat == 0x65) out_mov(); //E 外
- send_3dpuzzle();
- // INT_CLKO |= 0x40; //開啟中斷,允許下次接收數據。
- }
- void main(){ //入口函數
- //init_uart();
- send_3dpuzzle(); //發送拼圖數據
- while(1){
- if(rxd == 0){ //串口收到低電平
- exint4(); //處理數據
- }
- }
- }
復制代碼
|