⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 els1.c

📁 在LINUX下采用miniGUI 的API编写的俄罗斯方块程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <sys/types.h>#include <time.h>#include <sys/time.h>#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>#include <minigui/mgext.h>#define IDC_CTRL_els_OK     	600#define IDC_CTRL_els_CANCLE    601#define IDC_els_STATIC1             650#define IDC_els_STATIC11            651#define IDC_els_STATIC2             652#define IDC_els_STATIC21            653#define IDC_els_STATIC3             654#define IDC_els_STATIC31            655#define IDC_els_STATIC4             656#define IDC_TIMER_STATIC            657//#define  SCANCODE_F     START#define BLOCK_LEN 20   /*block size 20*20 */#define H_BLOCK_NUM 9  /*横向9个方格*/#define V_BLOCK_NUM 18 /*纵向18个方格*/#define X0  10#define Y0   5         /*起点坐标(10,5)*/    static int grade=100;static BITMAP fk; static int bitmap[V_BLOCK_NUM];/*  18*9方格区位图,有俄罗斯方块的地方为1*/static int shape[6][4]={    {0x1111,0x0f,0x1111,0x0f},   //line  {0x232,0x131,0x72,0x27},     //T shape  {0x63, 0x36, 0x132,0x231},   //Z shape  {0x17, 0x71, 0x47, 0x74},    //横L  shape  {0x322,0x223,0x311,0x113},   //竖L  shape  {0x33, 0x33, 0x33, 0x33}     //田   shape};static  int temppos[4][2];  /*4个方快,每个方快一对坐标*/ static int newok=0;/******************************************************/unsigned short int randomn(int N){  struct timeval tpstart;  gettimeofday(&tpstart,NULL);  srand(tpstart.tv_usec);  return (unsigned int)(1+(int) (100.0*rand()/(RAND_MAX+1.0)))%N;}void locate_pos( int x, int y, int n,int  N){    int i,k=0,M;         if(N==3)         /*N=3,非直线行*/    {      for(i=0;i<N;i++)      /*求取相对坐标*/       {            M=(n>>(i*4))&0x0f;          switch(M)            {               case 0:      break;              case 1: temppos[k][0]=x+2; temppos[k++][1]=y+i; break;              case 2: temppos[k][0]=x+1; temppos[k++][1]=y+i; break;              case 3: temppos[k][0]=x+1; temppos[k++][1]=y+i; temppos[k][0]=x+2; temppos[k++][1]=y+i; break;              case 4: temppos[k][0]=x; temppos[k++][1]=y+i;   break;              case 6: temppos[k][0]=x; temppos[k++][1]=y+i;   temppos[k][0]=x+1; temppos[k++][1]=y+i;  break;              case 7: temppos[k][0]=x; temppos[k++][1]=y+i;   temppos[k][0]=x+1; temppos[k++][1]=y+i; temppos[k][0]=x+2; temppos[k++][1]=y+i; break;            }        }           }       else         /*N=4,直线形*/    {       if(n==0x0f) {temppos[0][0]=x; temppos[0][1]=y;temppos[1][0]=x+1; temppos[1][1]=y;temppos[2][0]=x+2; temppos[2][1]=y;temppos[3][0]=x+3; temppos[3][1]=y;}     if(n=0x1111) {temppos[0][0]=x; temppos[0][1]=y;temppos[1][0]=x; temppos[1][1]=y+1;temppos[2][0]=x; temppos[2][1]=y+2;temppos[3][0]=x; temppos[3][1]=y+3;}        }}void generate_shape(void){  unsigned short A,B,nextA,nextB, N,M;  A=randomn(6);B=randomn(4);  A?(N=3):(N=4);  locate_pos(3,0,shape[A][B],N);        }void savetobitmap(void){  int i,j,k;  for(i=0;i<4;i++)  {   j=temppos[i][1];k=temppos[i][0];   bitmap[j]|=1<<(H_BLOCK_NUM-1-k);  }}int check_cross(int temp[],int temp1[], int n) //temp横坐标数组,temp1纵坐标数组{ int tempbitmap[V_BLOCK_NUM]; int i,j,k; memset(tempbitmap,0,sizeof(tempbitmap));  for(i=0;i<n;i++)   {     j=temp1[i]; k=temp[i];     tempbitmap[j]|=1<<(H_BLOCK_NUM-1-k);   }    for(i=0;i<V_BLOCK_NUM;i++)             if(tempbitmap[i] & bitmap[i])  break; if(i==V_BLOCK_NUM)  return 1;   else return 0;}void turndown(void){ int i,flag=0; int temp[4],temp1[4]; for(i=0;i<4;i++)           //暂存   temp[i]=temppos[i][1]; for(i=0;i<4;i++)   temp1[i]=temppos[i][0];  //为了传递参数的需要  for(i=0;i<4;i++) {      temp[i]++;    if(temp[i]<V_BLOCK_NUM )  flag=1;    else {flag=0;break;}  }  printf("the cross contion is %d\n",check_cross(temp1,temp,4));if(flag && check_cross(temp1,temp,4))   for(i=0;i<4;i++)  temppos[i][1]=temp[i]; else { newok=0;savetobitmap();}   }void  turnleft(void){ int i,flag=0; int temp[4],temp1[4]; for(i=0;i<4;i++)           //暂存    temp[i]=temppos[i][0]; for(i=0;i<4;i++)   temp1[i]=temppos[i][1];  //为了传递参数的需要   for(i=0;i<4;i++) {      temp[i]--;    if(temp[i]>=0)  flag=1;    else {flag=0;break;}  }if(flag && check_cross(temp,temp1,4))   for(i=0;i<4;i++)  temppos[i][0]=temp[i]; }void  turnright(void){ int i,flag=0; int temp[4],temp1[4]; for(i=0;i<4;i++)           //暂存    temp[i]=temppos[i][0]; for(i=0;i<4;i++)   temp1[i]=temppos[i][1];  //为了传递参数的需要    for(i=0;i<4;i++) {      temp[i]++;    if(temp[i]<H_BLOCK_NUM)  flag=1;    else {flag=0;break;}  }if(flag && check_cross(temp,temp1,4))   for(i=0;i<4;i++)  temppos[i][0]=temp[i]; }void turnchange(void){}/******************************************************/static DLGTEMPLATE DlgCustom ={	WS_BORDER | WS_CAPTION,	WS_EX_NONE,	100, 100, 300,400,	"俄罗斯方块",	0, 0,	6, NULL,	0};static CTRLDATA CtrlCustom[] ={{		CTRL_STATIC,        WS_VISIBLE | SS_LEFT,        200, 150, 100, 25,         IDC_els_STATIC1,         "Scores",        0	},		{		CTRL_SLEDIT,        WS_VISIBLE | ES_RIGHT ,//| WS_BORDER,        200, 175, 80, 25,         IDC_els_STATIC11,         NULL,        0	},		{		CTRL_STATIC,        WS_VISIBLE | SS_LEFT,        200, 210,100, 25,         IDC_els_STATIC2,         "DelLines",        0	},		{		CTRL_SLEDIT,        WS_VISIBLE | ES_RIGHT ,//| WS_BORDER,        200, 235, 80, 25,         IDC_els_STATIC21,         NULL,        0	},		{		CTRL_STATIC,        WS_VISIBLE | SS_LEFT,        200, 270,100, 25,         IDC_els_STATIC3,         "Grade",        0	},		{		CTRL_SLEDIT,        WS_VISIBLE | ES_CENTER ,//| WS_BORDER,        200, 295, 80, 25,         IDC_els_STATIC31,         NULL,        0	},	};/******************************************************************************************/static int Custom_Proc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam){     static PLOGFONT my_font;  //  static BITMAP fk;    static HWND h1;   static char readyok=0,runok=0;   static int delays=5;   switch (message) {         case MSG_INITDIALOG:          memset(bitmap,0,sizeof(bitmap));                    LoadBitmap(HDC_SCREEN,&fk,"fk2.bmp");         // SetWindowBkColor(hwnd,PIXEL_lightwhite);           my_font = CreateLogFont (NULL, "song", "GB2312",                         FONT_WEIGHT_REGULAR, FONT_SLANT_ROMAN, FONT_SETWIDTH_NORMAL,                        FONT_SPACING_CHARCELL, FONT_UNDERLINE_NONE, FONT_STRUCKOUT_NONE,                         22, 0);          SetWindowFont ( GetDlgItem (hWnd, IDC_els_STATIC1), my_font) ;             SetWindowFont ( GetDlgItem (hWnd, IDC_els_STATIC11), my_font) ;             // SetWindowBkColor(GetDlgItem (hWnd, IDC_els_STATIC11),PIXEL_blue);           SetWindowFont ( GetDlgItem (hWnd, IDC_els_STATIC2), my_font) ;            SetWindowFont ( GetDlgItem (hWnd, IDC_els_STATIC21), my_font) ;            // SetWindowBkColor(GetDlgItem (hWnd, IDC_els_STATIC21),PIXEL_yellow);          SetWindowFont ( GetDlgItem (hWnd, IDC_els_STATIC3), my_font) ;            SetWindowFont ( GetDlgItem (hWnd, IDC_els_STATIC31), my_font) ;           // SetWindowBkColor(GetDlgItem (hWnd, IDC_els_STATIC31),PIXEL_lightwhite);                      // randomize();          SetFocusChild(hWnd);   /*把焦点赋予该窗口*/                     break;     case MSG_COMMAND:        switch (wParam)         {                        }        break;      case MSG_KEYDOWN:          switch(LOWORD(wParam))           {             case SCANCODE_F:            if(!readyok && !runok)             {              h1=CreateWindow (CTRL_STATIC, "提示", WS_VISIBLE | WS_BORDER | WS_THICKFRAME | WS_CAPTION , IDC_els_STATIC4,  100, 100, 130, 50, hWnd, 0);              SetTimer(hWnd,IDC_TIMER_STATIC,100);              SetWindowText (h1, "leave 5s,Ready!");              readyok=1;             }            break;                        case SCANCODE_A:             if(runok){ turnleft(); InvalidateRect (hWnd, NULL, TRUE); }             break;            case SCANCODE_S:             if(runok) {turndown(); InvalidateRect (hWnd, NULL, TRUE); }             break;             case SCANCODE_D:             if(runok) {turnright(); InvalidateRect (hWnd, NULL, TRUE); }             break;          }         break;       case MSG_TIMER:         if(readyok && ! runok)          {             if( delays) {delays--; char leaves[]="leave 5s,Ready!"; leaves[6]=delays+'0';  SetWindowText (h1, leaves); }            else  {SetTimer(hWnd,IDC_TIMER_STATIC,grade); DestroyWindow(h1); runok=1;}             //KillTimer(hWnd,IDC_TIMER_STATIC);          }                    if(runok)          {          if(!newok)           {           generate_shape();           newok=1;          }          else  turndown();                    InvalidateRect (hWnd, NULL, TRUE);                   }       break;                           case MSG_PAINT:       {   int i,j;           HDC hdc;           hdc = BeginPaint (hWnd);           Rectangle (hdc, 5, 5, 290, 370);                      MoveTo(hdc,10,5);           LineTo(hdc,10,370);            MoveTo(hdc,190,5);           LineTo(hdc,190,370);           MoveTo(hdc,195,5);           LineTo(hdc,195,370);           MoveTo(hdc,10,365);           LineTo(hdc,190,365);           //SetPenColor(hdc,PIXEL_black);           //locate_pos(3,0,0x223,3);                     if(runok)           {            for(i=0;i<4;i++)                /*求取绝对坐标,并绘制*/            //temppos[i][0]=temppos[i][0]*BLOCK_LEN+X0;temppos[i][1]=temppos[i][1]*BLOCK_LEN+Y0;             FillBoxWithBitmap(hdc,temppos[i][0]*BLOCK_LEN+X0,temppos[i][1]*BLOCK_LEN+Y0,BLOCK_LEN,BLOCK_LEN,&fk);                       for(i=0;i<V_BLOCK_NUM;i++)            for(j=0;j<9;j++)             if((bitmap[i]>>j)&0x01)               FillBoxWithBitmap(hdc,(8-j)*BLOCK_LEN+X0,i*BLOCK_LEN+Y0,BLOCK_LEN,BLOCK_LEN,&fk);            }                    EndPaint (hWnd, hdc);        }        return 0;                     case MSG_CLOSE:         UnloadBitmap(&fk);         EndDialog(hWnd,0);       return 0;    } 	return DefaultDialogProc (hWnd, message, wParam, lParam);}/******************************************************//******************************************************以下为主调函数                                      ******************************************************/int MiniGUIMain (int argc, const char* argv[]){#ifdef _LITE_VERSION	SetDesktopRect(0, 0, 1024, 768);#endif    	if (!InitMiniGUIExt()) 	{        	return 2;    	}    		DlgCustom.controls = CtrlCustom;	DialogBoxIndirectParam (&DlgCustom, HWND_DESKTOP, Custom_Proc, 0L);	MiniGUIExtCleanUp ();		return 0;}#ifndef _LITE_VERSION#include <minigui/dti.c>#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -