|
1.jpg (87.55 KB, 下載次數: 114)
下載附件
2020-2-3 17:00 上傳
單片機源程序如下:
- #include "t6963c.h"
- #include "typedef.h"
- #include "key.h"
- #define X_START 5
- #define Y_START 0
- #define MIN_SLOW_SPEED 300
- #define BX_START 30
- #define BY_START 15
- #define Nothing 100
- //全局數據
- code uchar Game_Char[]={0x27,0x41,0x4d,0x45};
- code uchar Over_Char[]={0x2f,0x56,0x45,0x52};
-
- static uchar xx,yy; //方塊的位置
- static uint Game_Score=0;
- static uchar xdata Platform[14][21]; //游戲平臺數據
- static uchar This_shape; //當前形狀
- static uchar Next_shape=0;
- static uint Game_Speed=MIN_SLOW_SPEED; //等級速度,正常情況,方塊下降的速度
- static uchar Game_Stop=1;
- static uchar Game_Level=0;
- /////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////
- /////////方塊形狀的定義//////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////
- struct POINT{
- uchar x;
- uchar y;
- };
- struct SHAPE{
- struct POINT point[4];
- uchar next; //下一個形狀
- }
- xdata shape[19]={
- { 1,0,0,1,1,1,2,1,1 },
- { 1,0,1,1,2,1,1,2,2 },
- { 0,0,1,0,2,0,1,1,3 },
- { 1,0,0,1,1,1,1,2,0 },
-
- { 1,0,2,0,1,1,1,2,5 },
- { 0,0,1,0,2,0,2,1,6 },
- { 2,0,2,1,2,2,1,2,7 },
- { 0,0,0,1,1,1,2,1,4 },
- { 1,0,2,0,2,1,2,2,9 },
- { 2,0,0,1,1,1,2,1,10 },
- { 1,0,1,1,1,2,2,2,11 },
- { 0,0,1,0,2,0,0,1,8 },
- { 0,0,0,1,1,1,1,2,13 },
- { 1,0,2,0,0,1,1,1,12 },
- { 2,0,1,1,2,1,1,2,15 },
- { 0,0,1,0,1,1,2,1,14 },
- { 1,0,1,1,1,2,1,3,17 },
- { 0,1,1,1,2,1,3,1,16 },
- { 1,0,2,0,1,1,2,1,18 },
-
- };
- ////////////////////////////////////////////////////////////////////////////
- //**************************************************************************
- //= 函數原型:void Init_GamePlatform()
- //= 功 能: 初始化游戲平臺
- //= 參 數: 無
- //= 返 回 值: 無
- //= 函數性質:公有函數
- //= 注 意:
- //***************************************************************************
- void Show_score(uchar);
- void Init_GamePlatform()
- {
- uchar i;
- uchar j;
- uchar N_Hanzi;
- Wr_line(1,33,13,64,1 ); //初始化游戲平臺邊界 畫游戲區域
- Wr_line(1,33,14,64,1 ); //畫上橫線
- Wr_line(0,33,15,100,1);
- Wr_line(0,34,15,100,1); //畫左豎線
- Wr_line(1,33,115,64,1);
- Wr_line(1,33,116,64,1); //畫下橫線
- Wr_line(0,95,15,100,1);
- Wr_line(0,96,15,100,1); //畫右豎線
- //--------------------------------------------------------
- for(i=1;i<13;i++) //游戲平臺數據清零
- {
- for(j=0;j<20;j++)
- {
- Platform[i][j]=0;
- }
- }
- for(i=1;i<13;i++)
- {
- Platform[i][20]=1; //游戲平臺最下面一行的每一個方塊位置為1,作為下邊界
- }
- for(j=0;j<20;j++) //游戲平臺左右方塊位置置1,作為左右邊界
- {
- Platform[0][j]=1;
- Platform[13][j]=1;
- }
- //---------------------------------------------------------
- N_Hanzi=0;
- for(j=3;j<=12;j=j+2) //輸入“馮燕輝制作”漢字
- {
- hanzhi(1,j,N_Hanzi,1);
- N_Hanzi++; //指向下一個漢字
- }
- hanzhi(13,3,6,1); //輸入“分”漢字
- hanzhi(13,8,8,1); //輸入“級”漢字
- Show_score(0); //顯示初始分數
- Show_num(13,11,Game_Level); //顯示初始等級水平
- Game_Speed=MIN_SLOW_SPEED/(Game_Level+1); //根據水平確定速度
- //------------------------------------------------------------------
- Game_Score=0;
- xx=X_START ;
- yy=Y_START ;
- }
- ////////////////////////Init_Game/////////////////////
- void Init_Game()
- {
- Game_Stop=1;
- Init_GamePlatform();
- hanzhi(7,3,9,1); //輸入“按”漢字
- Show_num(7,6,7); //顯示7
- hanzhi(7,8,10,1); //輸入“開”漢字
- hanzhi(7,10,11,1); //輸入“始”漢字
- }
- //**************************************************************************
- //= 函數原型:void XiaoFengKuai(uchar x,uchar y,uchar mode)
- //= 功 能: 顯示一個小方塊
- //= 參 數: 小方塊的橫x,堅坐標y,mode=1:顯示小方塊,mode=0:刪除小方塊
- //= 返 回 值:
- //= 函數性質:私有函數
- //= 注 意:
- //***************************************************************************
- void XiaoFengKuai(uchar x,uchar y,bit mode)
- {
- uchar x1=5*x+BX_START; //將方塊在平臺的位置轉化成LCD的點坐標(地址轉換)
- uchar y1=5*y+BY_START;
- uchar i;
- if(mode==1)
- {
- for(i=0;i<5;i++)
- {
- Point(x1+i,y1,1); //畫一條橫線
- }
- y1+=4;
- for(i=0;i<5;i++)
- {
- Point(x1+i,y1,1); //畫第二條橫線
- }
- for(i=0;i<5;i++)
- {
- Point(x1,y1-i,1); //畫第1條堅線
- }
- x1+=4;
- for(i=0;i<5;i++)
- {
- Point(x1,y1-i,1); //畫第2條堅線
- }
- y1-=4;
- for(i=0;i<5;i++)
- {
- Point(x1-i,y1+i,1); //畫斜線
- }
- }
- else
- {
- for(i=0;i<5;i++)
- {
- Point(x1+i,y1,0); //畫一條橫線
- }
- y1+=4;
- for(i=0;i<5;i++)
- {
- Point(x1+i,y1,0); //畫第二條橫線
- }
- for(i=0;i<5;i++)
- {
- Point(x1,y1-i,0); //畫第1條堅線
- }
- x1+=4;
- for(i=0;i<5;i++)
- {
- Point(x1,y1-i,0); //畫第2條堅線
- }
- y1-=4;
- for(i=0;i<5;i++)
- {
- Point(x1-i,y1+i,0); //畫斜線
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- ////////////////左沖突檢測//////////////////////////////////////////////
- bit Left_Anti()
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- if(Platform[xx+shape[This_shape].point[i].x-1][yy+shape[This_shape].point[i].y]==1)
- return 1;
- }
- return 0;
- }
- ////////////////右沖突檢測///////////////////////////////////////////
- bit Right_Anti()
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- if(Platform[xx+shape[This_shape].point[i].x+1][yy+shape[This_shape].point[i].y]==1)
- return 1;
- }
- return 0;
- }
- ////////////////////////////////////////下沖突檢測//////////////////////////
- bit Bottom_Anti()
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- if(Platform[xx+shape[This_shape].point[i].x][yy+shape[This_shape].point[i].y+1]==1)
- return 1;
- }
- return 0;
- }
- //////////////////////////////////改變形狀時產生的沖突檢測////////////////////
- bit Change_Shape_Anti()
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- if(Platform[xx+shape[shape[This_shape].next].point[i].x][yy+shape[shape[This_shape].next].point[i].y]==1)
- return 1; //檢測一個形狀的沖突情況
- }
- return 0;
- }
- //////////////////////////////////產生一個隨機數,返回一個隨機數///////////////
- uchar Random()
- {
- static uchar m;
- m+=49;
- return (m%19);
- }
- //////////////計分函數,參數為 消行行數n///////////////////////////////////////
- void Show_score(uchar n)
- {
- Game_Score=Game_Score+10*n;
- if(Game_Score<10)
- {
- Show_num(13,6,Game_Score%10); //顯示個位
- }
- else if(Game_Score<100)
- {
- Show_num(14,6,Game_Score%10);
- Show_num(13,6,Game_Score/10%100); //顯示個位,十位
- }
- else if(Game_Score<1000)
- {
- Show_num(14,6,Game_Score%10);
- Show_num(13,6,Game_Score/10%10);
- Show_num(12,6,Game_Score/100%10); //顯示個位 ,十位,百位
- }
- else
- {
- Show_num(15,6,Game_Score%10);
- Show_num(14,6,Game_Score/10%10);
- Show_num(13,6,Game_Score/100%10); //顯示個位 ,十位,百位,千位
- Show_num(12,6,Game_Score/1000);
- }
- if(Game_Score%1000==0)
- {
- if(Game_Score>0)
- {
- Game_Level++;
- if(Game_Level==10)
- {
- Game_Stop=1;
- hanzhi(7,5,12,1); // 輸出“太棒了"
- hanzhi(7,7,13,1);
- hanzhi(7,9,14,1);
-
- }
- Show_num(13,11,Game_Level); //顯示水平
-
- }
- }
- }
- //**************************************************************************
- //= 函數原型:void Undisplay_line()
- //= 功 能: 消除行
- //= 參 數: 無
- //= 返 回 值: 無
- //= 函數性質:私有函數
- //= 注 意:
- //***************************************************************************
- void UnDisplay_line()
- {
- uchar Del_Line; //標識要刪除的行
- uchar Del_Line_Num=0; //標識刪除的行數
- uchar i,j,k;
- bit HavePoint; //標識一行中是否有空白點
- for(i=0;i<4;i++)
- {
- for(j=1;j<13;j++)
- {
- if(Platform[j][yy+i]==0)
- break; //如果這一行中有一個為空,則退出這一行的循環
- else if(j==12)
- {
- Del_Line=yy+i; //確定要刪除的行
- if(Del_Line<20)
- {
- Del_Line_Num++; //計算共刪除的行數
- for(k=1;k<13;k++)
- {
- XiaoFengKuai(k,Del_Line,0); //刪除行
- Platform[k][Del_Line]=0; //平臺數據清零
- }
- while(1) //下移
- {
- HavePoint=0;
- for(k=1;k<13;k++)
- {
- if(Platform[k][Del_Line-1]==1)
- {
- HavePoint=1; //標識這一行有要下移的點
- XiaoFengKuai(k,Del_Line-1,0); //刪除小方塊
- Platform[k][Del_Line-1]=0; //平臺數據清零
- XiaoFengKuai(k,Del_Line,1) ; //將小方塊下移
- Platform[k][Del_Line]=1; //平臺數據置1,表明此位置已被占用
- }
- }
- if(HavePoint==0) break; //沒有要下移的行,退出本循環
- Del_Line--; //下移上一行
- }
- }
- }
- }
- }
- if(Del_Line_Num)
- {
- Show_score(Del_Line_Num); //刷新分數顯示
- }
- }
-
- //**************************************************************************
- //= 函數原型:void Show_shape(uchar x1,uchar y1,uchar Tshape,bit mode)
- //= 功 能: 顯示一個方塊形狀或刪除一個方塊形狀
- //= 參 數: (x1,y1)為顯示位置,Tshape為顯示的形狀,mode=1為顯示,mode=0不顯示
- //= 返 回 值:
- //= 函數性質:私有函數
- //= 注 意:
- //***************************************************************************
- void Show_shape(uchar x1,uchar y1,uchar Tshape,bit mode)
- {
- if(mode==1)
- {
- XiaoFengKuai(x1+shape[Tshape].point[0].x,y1+shape[Tshape].point[0].y,1); //顯示形狀
- XiaoFengKuai(x1+shape[Tshape].point[1].x,y1+shape[Tshape].point[1].y,1);
- XiaoFengKuai(x1+shape[Tshape].point[2].x,y1+shape[Tshape].point[2].y,1);
- XiaoFengKuai(x1+shape[Tshape].point[3].x,y1+shape[Tshape].point[3].y,1);
- }
- else
- {
- XiaoFengKuai(x1+shape[Tshape].point[0].x,y1+shape[Tshape].point[0].y,0); //刪除形狀
- XiaoFengKuai(x1+shape[Tshape].point[1].x,y1+shape[Tshape].point[1].y,0);
- XiaoFengKuai(x1+shape[Tshape].point[2].x,y1+shape[Tshape].point[2].y,0);
- XiaoFengKuai(x1+shape[Tshape].point[3].x,y1+shape[Tshape].point[3].y,0);
- }
- }
- //**************************************************************************
- //= 函數原型:void Fangkuai_down()
- //= 功 能: 方塊下降處理
- //= 參 數:
- //= 返 回 值:
- //= 函數性質:公有函數
- //= 注 意:
- //***************************************************************************
- static uint DSpeed=MIN_SLOW_SPEED; //標識下降速度
- static uint Now_Speed=MIN_SLOW_SPEED; //當前速度
- void Fangkuai_down()
- {
- uchar i;
- static bit New_shape=1; //標識是否要產生新形狀
- if(Game_Stop==1) return;
- if(New_shape==1)
- {
- New_shape=0;
- xx=X_START;
- yy=Y_START;
- This_shape=Next_shape; //當前方塊等于預方塊
- Show_shape(15,18,Next_shape,0); // 產生一下個方塊前,將預方塊刪除
- Next_shape=Random(); //產生下一個方塊
- Show_shape(xx,yy,This_shape,1); //顯示當前方塊
- Show_shape(15,18,Next_shape,1); //預顯示下一個方塊
- if(Bottom_Anti())
- {
- Game_Stop=1;
- Show_Image(35,15,94,114,0); //清屏
- char_wr(6,6,Game_Char,0,4); //顯示Game
- char_wr(6,8,Over_Char,0,4); //顯示over
- return;
- }
- }
- else
- {
- if(DSpeed==0)
- {
- DSpeed=Now_Speed; //確定方塊下落的速度
- if(Bottom_Anti())
- {
- New_shape=1;//產生新的形狀
- for(i=0;i<4;i++)
- {
- Platform[xx+shape[This_shape].point[i].x][yy+shape[This_shape].point[i].y]=1;//寫入平臺
-
- }
- UnDisplay_line();//消行計分
- return;
- }
- else
- {
- Show_shape(xx,yy,This_shape,0); //刪除當前形狀
- yy++;
- Show_shape(xx,yy,This_shape,1); //顯示形狀(形狀下移一個位置)
- return;
- }
- }
- else
- {
- DSpeed--;
-
- }
-
- }
- }
- //**************************************************************************
- //= 函數原型:void Fangkuai_Control()
- //= 功 能: 方塊游戲控制
- //= 參 數:
- //= 返 回 值:
- //= 函數性質:公有函數
- //= 注 意:
- //***************************************************************************
- #define Move_Left 4
- #define Move_Right 6
- #define Add_Speed 5
- #define Change_Shape 8
- #define Game_Star 7
- #define Game_Pause 9
- void Fangkuai_Control()
- {
-
- switch(Key) //消息處理
- {
- case Move_Left:
- {
- Key=Nothing; //信息已被處理,拋棄它
- if(!Left_Anti())
- {
- Show_shape(xx,yy,This_shape,0); //刪除當前形狀
- xx--;
- Show_shape(xx,yy,This_shape,1); //顯示移動后的形狀
- }
- }break;
- case Move_Right:
- {
- Key=Nothing; //信息已被處理,拋棄它
- if(!Right_Anti())
- {
- Show_shape(xx,yy,This_shape,0); //刪除當前形狀
- xx++;
- Show_shape(xx,yy,This_shape,1); //顯示移動后的形狀
- }
- }break;
- case Add_Speed:
- {
- Key=Nothing; //信息已被處理,拋棄它
- if(Game_Stop==1)
- {
- if(Game_Level==0)
- {
- Game_Level=9;
- Show_num(13,11,9); //顯示等級水平
- Game_Speed=MIN_SLOW_SPEED/(Game_Level+1); //根據水平確定速度
- }
- else
- {
- Game_Level--;
- Show_num(13,11,Game_Level); //顯示等級水平
- Game_Speed=MIN_SLOW_SPEED/(Game_Level+1); //根據水平確定速度
- }
- }
- else
- {
- Now_Speed=1; //調整位置后,加速下降
- DSpeed=Now_Speed;
- }
- }break;
- case Change_Shape:
- {
- Key=Nothing; //信息已被處理,拋棄它
- if(Game_Stop==1)
- {
- if(Game_Level==9)
- {
- Game_Level=0;
- Show_num(13,11,0); //顯示等級水平
- Game_Speed=MIN_SLOW_SPEED/(Game_Level+1); //根據水平確定速度
- }
- else
- {
- Game_Level++;
- Show_num(13,11,Game_Level); //顯示等級水平
- Game_Speed=MIN_SLOW_SPEED/(Game_Level+1); //根據水平確定速度
- }
- }
- else
- {
- if(!Change_Shape_Anti())
- {
- Show_shape(xx,yy,This_shape,0); //刪除當前形狀
- This_shape=shape[This_shape].next;
- Show_shape(xx,yy,This_shape,1); //顯示變化后的形狀
- }
- }
- }break;
- case Game_Star:
- {
- Key=Nothing; //信息已被處理,拋棄它
- ClrGraphic();
- Show_Image(35,15,94,114,0);
- Init_GamePlatform();
- Game_Stop=0;
- }break;
- case Game_Pause:
- {
- Key=Nothing; //信息已被處理,拋棄它
- Game_Stop=!Game_Stop;
- }
- default:Now_Speed=Game_Speed;
- }
- }
復制代碼 |
-
2.jpg
(20.1 KB, 下載次數: 170)
下載附件
2020-2-3 17:00 上傳
-
-
俄羅斯方塊.rar
2020-2-3 17:00 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
129.36 KB, 下載次數: 11, 下載積分: 黑幣 -5
|