关于C语言俄罗斯方块的基本实现!

来源:互联网 发布:科技大数据分析宣传片 编辑:程序博客网 时间:2024/05/01 02:01

 俄罗斯基本功能实现,中心思想就是:利用画图函数RectTang来画不同的图形,不断的改变其坐标,并且用画刷重绘!

#include <windows.h>#include <stdio.h>#define TIMER_SEC 1#define SUDU 1#define LEFT 0#define RIGHT 12int i=0,XIA=20;int j=0;int k=0;int buju[21][13]={0}; int time=500;     //定时器时间 typedef struct sharp{ int x; int y; int x1; int y1; int x2; int y2; int x3; int y3; int s; }sharp;sharp ss;sharp s0[4]={{5,1,6,1,6,2,6,0,1},//5,1,6,1,6,2,6,0,1 {5,1,6,1,7,1,6,0,1},//5,1,6,1,7,1,6,0,1{7,1,6,1,6,0,6,2,1},{5,1,6,1,7,1,6,2,1},};sharp s00[4]={{5,1,6,1,6,2,6,0,1}, {5,1,6,1,7,1,6,0,1},{7,1,6,1,6,0,6,2,1},{5,1,6,1,7,1,6,2,1},};sharp *s=s0;//定义变量void xiaotuxing(HDC hdc, sharp *s);    //消除图形 void huatuxing(HDC hdc,sharp *s);    //画图形 void huafangkuai(HDC hdc,int x,int y,int color);//画小方块 void huachutuxing(HDC hdc,sharp *s);   //显示出图形 void init_dipan();        //初始化底盘 void bianxing(HDC hdc);       //变换图形状态 void left(HDC hdc);        //按下左键 void right(HDC hdc);       //按下右键 void down(HDC hdc);        //按下下键时 void times(HDC hdc);       //定时函数 void huabianjiemian(HDC hdc1);     //界面函数 LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/*  Declare Windows procedure  */ /*  Make the class name into a global variable  */char szClassName[ ] = "WindowsApp";int WINAPIWinMain (HINSTANCE hThisInstance,         HINSTANCE hPrevInstance,         LPSTR lpszArgument,         int nFunsterStil){    HWND hwnd;               /* This is the handle for our window */    MSG messages;            /* Here messages to the application are saved */    WNDCLASSEX wincl;        /* Data structure for the windowclass */    /* The Window structure */    wincl.hInstance = hThisInstance;    wincl.lpszClassName = szClassName;    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */    wincl.cbSize = sizeof (WNDCLASSEX);    /* Use default icon and mouse-pointer */    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);    wincl.lpszMenuName = NULL;                 /* No menu */    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */    wincl.cbWndExtra = 0;                      /* structure or the window instance */    /* Use Windows's default color as the background of the window */    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);    /* Register the window class, and if it fails quit the program */    if (!RegisterClassEx (&wincl))        return 0;    /* The class is registered, let's create the program*/    hwnd = CreateWindowEx (           0,                   /* Extended possibilites for variation */           szClassName,         /* Classname */           "eluosi",   /* Title Text */           WS_OVERLAPPEDWINDOW, /* default window */           CW_USEDEFAULT,       /* Windows decides the position */           CW_USEDEFAULT,       /* where the window ends up on the screen */           384,                 /* The programs width */           422,                 /* and height in pixels */           HWND_DESKTOP,        /* The window is a child-window to desktop */           NULL,                /* No menu */           hThisInstance,       /* Program Instance handler */           NULL                 /* No Window Creation data */           );    /* Make the window visible on the screen */    ShowWindow (hwnd, nFunsterStil); SetTimer(hwnd,TIMER_SEC,time,NULL);   //设置定时器  init_dipan();         //初始化地盘     /* Run the message loop. It will run until GetMessage() returns 0 */    while (GetMessage (&messages, NULL, 0, 0))    {        /* Translate virtual-key messages into character messages */        TranslateMessage(&messages);        /* Send message to WindowProcedure */        DispatchMessage(&messages);    }    /* The program return-value is 0 - The value that PostQuitMessage() gave */    return messages.wParam;}/*  This function is called by the Windows function DispatchMessage()  */LRESULT CALLBACKWindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ PAINTSTRUCT ps; HDC hdc,hdc1; RECT rt; char szHello[]="hello,胡涂婷"; char *c; hdc = GetDC(hwnd); int j;     switch (message)                  /* handle the messages */    {       case WM_PAINT:   hdc1 = BeginPaint(hwnd, &ps);   /* TODO: Add any drawing code here... */   huabianjiemian(hdc1);   GetClientRect(hwnd, &rt);   DrawText(hdc1, szHello, strlen(szHello), &rt, DT_CENTER);   EndPaint(hwnd, &ps);          break;        case WM_DESTROY:            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */            break;        case WM_KEYDOWN:         switch(wParam)         {          case VK_SPACE:                 bianxing(hdc);  //当按下空格时     break;         case VK_LEFT:       left(hdc);     //当按下左键时       break;    case VK_RIGHT:     right(hdc);   //当按下右键时         break;     case VK_DOWN:   //当按下下键时      down(hdc);    break;       case VK_UP:     MessageBox(NULL,"da","",MB_OK);    break;           }                  break;             case WM_TIMER:          switch(wParam)          {           case TIMER_SEC:        times(hdc); //定时器函数      break;          }                 default:                      /* for messages that we don't deal with */            return DefWindowProc (hwnd, message, wParam, lParam);    }           return 0;    ReleaseDC(hwnd,hdc);}  void xiaotuxing(HDC hdc, sharp *s){   HPEN hPen, hOldPen;        //画笔 HBRUSH hBrush, hOldBrush;  //画刷  hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));    //画笔 hOldPen = (HPEN)SelectObject(hdc, hPen);  hBrush = CreateSolidBrush(RGB(255, 255, 255)); //实心画刷   hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);    huatuxing(hdc,s); DeleteObject(hPen);                //删除新笔  DeleteObject(hBrush);              //删除新刷     }void huachutuxing(HDC hdc,sharp *s){   HPEN hPen, hOldPen;        //画笔  HBRUSH hBrush, hOldBrush;  //画刷  hPen = CreatePen(PS_SOLID, 1, RGB(150, 110, 210));    //画笔    hOldPen = (HPEN)SelectObject(hdc, hPen);    hBrush = CreateSolidBrush(RGB(150, 160, 160)); //实心画刷    hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);    huatuxing(hdc,s);    DeleteObject(hPen);                //删除新笔   DeleteObject(hBrush);              //删除新刷}void huatuxing(HDC hdc,sharp *s){     huafangkuai(hdc,s->x,s->y,0);  huafangkuai(hdc,s->x1,s->y1,0);  huafangkuai(hdc,s->x2,s->y2,0);  huafangkuai(hdc,s->x3,s->y3,0);}void huafangkuai(HDC hdc,int x,int y,int color) //画方块 { const int beginx = 25; const int beginy = 25; const int fsize = 17.5; int screenx = beginx + x*fsize; int screeny = beginy + y*fsize; Rectangle(hdc,screenx,screeny,screenx+fsize,screeny+fsize);}void init_dipan(){ int f; for(f=0;f<13;f++) {  buju[21][f]=1;   //初始化底座为1,不可越界! }  // buju[20][13]=0;}void bianxing(HDC hdc){     if(i>3)      ;      else           {       if(s[i].x<=LEFT||s[i].x1<=LEFT||s[i].x2<=LEFT||       s[i].x3<=LEFT||s[i].x>=RIGHT||s[i].x1>=RIGHT||s[i].x2>=RIGHT||s[i].x3>=RIGHT||       s[i].y>=XIA||s[i].y1>=XIA||s[i].y2>=XIA||s[i].y3>=XIA)  //检查是否越界        ;       else       {       for(j=0;j<4;j++)       {                  xiaotuxing(hdc,&s[j]); //清除先前的       }      huachutuxing(hdc,&s[i]); //绘出现在的       i++;       if(i>3)       i=0;     }    }                }void left(HDC hdc){  if(s[i].x<=LEFT||s[i].x1<=LEFT||s[i].x2<=LEFT||s[i].x3<=LEFT) //检查是否越左界    ;   else    {   for(j=0;j<4;j++)   {         xiaotuxing(hdc,&s[j]);   }             //擦出先前的                int q1;      for(q1=0;q1<4;q1++)      {    s[q1].x=s[q1].x-SUDU;    s[q1].x1=s[q1].x1-SUDU;    s[q1].x2=s[q1].x2-SUDU;    s[q1].x3=s[q1].x3-SUDU;      }    huachutuxing(hdc,&s[i]);  //绘出后来的                     } }void right(HDC hdc){ if(s[i].x>=RIGHT||s[i].x1>=RIGHT||s[i].x2>=RIGHT||s[i].x3>=RIGHT)//检查是否越右界  ; else {   for(j=0;j<4;j++) {          xiaotuxing(hdc,&s[j]); }              int q2;    for(q2=0;q2<4;q2++)   {   s[q2].x=s[q2].x+SUDU;   s[q2].x1=s[q2].x1+SUDU;   s[q2].x2=s[q2].x2+SUDU;   s[q2].x3=s[q2].x3+SUDU;   }   huachutuxing(hdc,&s[i]);  //绘出后来的  }}void times(HDC hdc){          if(buju[s[i].y+1][s[i].x] == 1 || buju[s[i].y1+1][s[i].x1] == 1 ||         buju[s[i].y2+1][s[i].x2] == 1 || buju[s[i].y3+1][s[i].x3] == 1)         {           buju[s[i].y][s[i].x]=1;          buju[s[i].y1][s[i].x1]=1;          buju[s[i].y2][s[i].x2]=1;          buju[s[i].y3][s[i].x3]=1;          int i,j,l=0,a,b;          for(i=0;i<=20;i++)          {           for(j=0;j<=RIGHT;j++)           {            if(buju[i][j] == 1)             l++;           }           if(l == RIGHT + 1)            {             for(a=0;a<=RIGHT;a++)             {               HPEN hPen, hOldPen;        //画笔          HBRUSH hBrush, hOldBrush;  //画刷           hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));    //画笔          hOldPen = (HPEN)SelectObject(hdc, hPen);           hBrush = CreateSolidBrush(RGB(255, 255, 255)); //实心画刷            hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);             huafangkuai(hdc,a,i,1);             DeleteObject(hPen);                //删除新笔           DeleteObject(hBrush);              //删除新刷             } //清除那一行                           int n,m=i;;                                for(n=i-1;n>=0;n--,m--)           {            for(j=0;j<=RIGHT;j++)            {            buju[j]=buju[n][j];                 }           }     //依次向下移动!映射数组!                                              //不知道为什么每次消除之后数组被清零                      for(n=i-1;n>=0;n--)           {            for(j=0;j<=RIGHT;j++)            {             if(buju[n][j] == 1)             {              huafangkuai(hdc,j,n,1);             }            }           }  //消除上面的 ,往下移动!            MessageBox(NULL,"da","",MB_OK);           for(n=i-1;n>=0;n--)           {                        for(j=0;j<=RIGHT;j++)            {             if(buju[n][j] == 1)             {              HPEN hPen, hOldPen;        //画笔             HBRUSH hBrush, hOldBrush;  //画刷               hPen = CreatePen(PS_SOLID, 1, RGB(150, 110, 210));    //画笔               hOldPen = (HPEN)SelectObject(hdc, hPen);               hBrush = CreateSolidBrush(RGB(150, 160, 160)); //实心画刷               hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);               huafangkuai(hdc,j,n+1,1);               DeleteObject(hPen);                //删除新笔              DeleteObject(hBrush);              //删除新刷                           }            }           }  //显示出来                                                                                      }           else            l=0;          }          ss=s[i];         huachutuxing(hdc,&ss);         for(i=0;i<4;i++)         s[i]=s00[i];         }        else     {      for(j=0;j<4;j++)      {                   xiaotuxing(hdc,&s[j]);      }          int q=0;          for(q=0;q<4;q++)          {               s[q].y=s[q].y+SUDU;              s[q].y1=s[q].y1+SUDU;                            s[q].y2=s[q].y2+SUDU;              s[q].y3=s[q].y3+SUDU;          }        huachutuxing(hdc,&s[i]);                              }         }void huabianjiemian(HDC hdc1){   MoveToEx (hdc1, 255, 24, NULL) ;//画线           //画线    LineTo (hdc1, 255, 382) ;      MoveToEx (hdc1, 24, 24, NULL) ; //画线           //画线    LineTo (hdc1, 24, 382) ;      MoveToEx (hdc1, 24, 24, NULL) ; //画线           //画线    LineTo (hdc1, 255, 24) ;   MoveToEx (hdc1, 24, 382, NULL) ;//画线           //画线    LineTo (hdc1, 255, 382) ;}void down(HDC hdc){     if(buju[s[i].y+1][s[i].x] == 1 || buju[s[i].y1+1][s[i].x1] == 1 ||         buju[s[i].y2+1][s[i].x2] == 1 || buju[s[i].y3+1][s[i].x3] == 1)   //检查是否越底界      {      ;     }     else       {         for(j=0;j<4;j++)        {                     xiaotuxing(hdc,&s[j]);        }                  //擦出先前的           for(j=0;j<4;j++)          {         s[j].y=s[j].y+SUDU;         s[j].y1=s[j].y1+SUDU;         s[j].y2=s[j].y2+SUDU;         s[j].y3=s[j].y3+SUDU;          }        huachutuxing(hdc,&s[i]);  //绘出后来的        }}


 

原创粉丝点击