| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 721 人关注过本帖
标题:上次发表的一个win32的俄罗斯方块游戏这次把代码发出来
只看楼主 加入收藏
zanzan1986
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:100
专家分:140
注 册:2011-2-22
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
上次发表的一个win32的俄罗斯方块游戏这次把代码发出来
并来这个游戏还是要改进的,改进的方法是用多线程来分配游戏的数据运算与绘画图像动画播放的,现在没时间做了,那位有兴趣的可以照我的代码写出你自己的游戏!!!!
程序代码:
// 俄罗斯方块.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100
//时间ID
#define TIME           0x1
#define YELLO_RECT          0x1
#define RED_RECT            0x2
#define GREEN_RECT          0x3

//自定义消息
#define WM_RANDRECT    WM_USER +1                 //out to rect message
#define WM_STARTGAME   WM_USER + 2                //start a game message
#define WM_COMEOVER    WM_USER + 3                //game to over message
#define WM_OUTRECT     WM_USER + 4                //Out to rect message
#define WM_READYGAME   WM_USER + 5                //ready out to rect message
#define WM_MOVERECT    WM_USER + 6                //start move rect message
#define WM_DRAWRECT    WM_USER + 7                //draw down here rect message
#define WM_NEXTGAME    WM_USER + 8                //Next game
#define WM_LOADRECT    WM_USER + 9                //Load rect
#define WM_NEXTRECT    WM_USER + 10               //Show next rect
#define WM_MOVEPOINT   WM_USER + 11               //Show Flas
#define WM_OUTTEXT     WM_USER + 12

#define VKDOWN                  1                 //keyboard to down message
#define VKUP                    2                 //keyboard to up message
#define VKLEFT                  4                 //keyboard to left
#define VKRIGHT                 8                 //keyboard to right
#define VKSPACE                 16                //keyboard to space

#define ID_SHOWRECT             1                 //out to rect ID
#define ID_NEXTRECT             2                 //next rect ID
// Global Variables:
HINSTANCE hInst;                                  // current instance
TCHAR szTitle[MAX_LOADSTRING];                      // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];              // The title bar text
WNDPROC OldProc;                                  //Old windows proc
// Foward declarations of functions included in this code module:

ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    SHOWRECT(HWND, UINT, WPARAM, LPARAM);        //OutRect Proc Function

typedef struct COUNT
{
    BYTE count;
    WORD x;
    WORD y;
    WORD cy;
}COUNT,*PCOUNT;

//自定义函数
void InitRect(char a[], int size);                              //初始化画面函数
void RandRect(BYTE p[], BYTE &x);                               //随机出方块
void Round(BYTE a[], int size, int x);                          //转动方块函数
int InToRect(HWND hDlg, BYTE a[],const BYTE b[], const int x, const int y); //方块赋值于显示矩阵中

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     // TODO: Place code here.
    MSG msg;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
    }

    return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
    HBITMAP hBitmap;
    HBRUSH  hBrush;
    hBitmap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BK));
    hBrush = CreatePatternBrush(hBitmap);
    DeleteObject(hBitmap);
    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_MY);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = hBrush;                     //(HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = (LPCSTR)IDC_MY;
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^WS_MAXIMIZEBOX,
      CW_USEDEFAULT, 0, 400, 520, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//

int IsToMove(const BYTE  pOutRect[],const BYTE ShowRect[], const int x, const int y, const int type)
{
    int top;
    int flag = 0;
    int i, j;
    switch(type)
    {
    case VKDOWN:                                   //判断能否下落方块
             top = int(pOutRect[16] - 1);
             i = top;
             for(;i >= 0; i--)                    //判断在第几行出现方格
             {
                 flag = 0;
                 for(j = 0; j < pOutRect[16] && !flag; j++)
                 {
                      if(pOutRect[i*pOutRect[16] + j]) flag = 1;
                 }
                 if(flag) break;
                 else top--;
             }            
             if(y + top >= 19) return 0;            //到达底部
             for(j = top; j >= 0; j--)
             {

                    if(y + j < 0) break;
                    flag = j * pOutRect[16];
                    for(int l = 0; l < pOutRect[16]; l++)
                    {
                       if(pOutRect[flag +l] && ShowRect[(y+j+1)*11+x+l])
                       {
                           if(y <= 0 && (x >3 && x < 9))  return VKUP;    //游戏结速
                           return 0;                   //方块下落
                       }
                    }
             }
        return VKDOWN;
    case VKLEFT:                                //判断能否向左移动方块
        top = 0;                                //判断左边是在第几例
        for(i = 0; i < pOutRect[16]; i++)
        {       
            flag = 0;
            for(j = 0; j < pOutRect[16]; j++)
            {
                 if(pOutRect[j*pOutRect[16] + i])
                 {
                    flag = 1;
                    break;
                  }
            }               
            if(flag) break;
            else top++;
        }
        if(x + top -1 >= 0)                 //未到边
        {
            for(i = 0; i < pOutRect[16]; i++)
            {
                flag = i * pOutRect[16] + top;
                for(j = 0; j < pOutRect[16] - top; j++)
                {
                    if(pOutRect[flag + j] && ShowRect[(y + i) * 11 + x + top + j - 1]) return 0;      //不能向左移动了

                }
            }
            return VKLEFT;
        }
        return 0;
    case VKRIGHT:
        top = pOutRect[16] - 1;                                //判断左边是在第几例
        for(i = pOutRect[16] - 1; i >= 0; i--)
        {       
            flag = 0;
            for(j = 0; j < pOutRect[16]; j++)
            {
                 if(pOutRect[j*pOutRect[16] + i])
                 {
                    flag = 1;
                    break;
                  }
            }               
            if(flag) break;
            else top--;
        }
        if(x + top + 1 < 11)                 //未到边
        {
            for(i = top; i >= 0; i--)
            {
                for(j = 0; j < pOutRect[16]; j++)
                {
                    flag = j * pOutRect[16] + i;
                    if(pOutRect[flag] && ShowRect[(y + j) * 11 + x + top + i + 1]) return 0;      //不能向左移动了

                }
            }
            return VKRIGHT;
        }
        return 0;
       
    }
    return 0;
}

void DrawRect(HDC hdc, HDC Memhdc, BYTE a,const int x, const int y)
{
        switch(a)
        {
            case 0:
            BitBlt(hdc,x,y,20,20,Memhdc,20,20,SRCCOPY);
            break;
            case 1:
            BitBlt(hdc,x,y,20,20,Memhdc,40,0,SRCCOPY);
            break;
            case 2:
            BitBlt(hdc,x,y,20,20,Memhdc,0,0,SRCCOPY);
            break;
            case 3:
            BitBlt(hdc,x,y,20,20,Memhdc,20,0,SRCCOPY);
            break;
            case 4:
            BitBlt(hdc,x,y,20,20,Memhdc,0,20,SRCCOPY);
            break;
            case 5:
            BitBlt(hdc,x,y,20,20,Memhdc,0,40,SRCCOPY);
            break;
        }

}

void PlayPoint(WORD& COUNTS, HDC hdc,HDC hdcRect, COUNT a[],BYTE ShowRect[])
{
                              
    int s,i,j; 
    if(!COUNTS) return ;
    SetBkMode(hdc,TRANSPARENT);
    for(j = 0,i = 0x8000; j < 16; j++,i>>=1)                                 //演示动画
    {
        if(i ^ (i & COUNTS)) continue;                   //没动画或帧数为0

        s = (a[j].y - 1) * 11 +a[j].x;
        DrawRect(hdc,hdcRect,ShowRect[s],a[j].x*20,(a[j].y-1)*20);
        s = a[j].y * 11 +a[j].x;
        DrawRect(hdc,hdcRect,ShowRect[s],a[j].x*20,(a[j].y)*20);                   
        if(!(--a[j].count))
        {
            COUNTS ^= i;
            continue;
                        
        }
        TextOut(hdc,a[j].x*20,a[j].cy -( 20 - a[j].count),TEXT("+5"),2);                
    }
}

void PlayRect(WORD& COUNTS, HDC hdc,HDC hdcRect, COUNT b[],BYTE ShowRect[])
{
    int i,j,k,l,r;
    if(!COUNTS) return ;
    k = 1;
    for(j = 0,i = 0x8000; j < 16; j++,i>>=1)                                 //演示动画
    {
        if(b[j].cy < 1) continue;
        if(b[j].cy == 1)
        {
            ShowRect[b[j].y*11+b[j].x] = 0;
            if(b[j].count < 12)
            {
                BitBlt(hdc,b[j].x * 20,b[j].y * 20, 20, 20,hdcRect,(b[j].count % 3) * 20,(b[j].count / 3 + 2)*20,SRCCOPY);   
                b[j].count++;
            }
            else
            { 
                COUNTS ^= i;
                b[j].cy = 0;
                BitBlt(hdc,b[j].x * 20,b[j].y * 20, 20,20, hdcRect,20,20,SRCCOPY);
                l = b[j].x - 1 >= 0 ? b[j].x - 1 : -1;
                r = b[j].x + 1 < 11 ? b[j].x - 1 : -1;
                if(((l == -1) || !ShowRect[(b[j].y - k)*11 + l]) && ((r == -1) || !ShowRect[(b[j].y - k)*11 + r]) && ShowRect[(b[j].y - k)*11+b[j].x])
                {
                    do
                    {
                        ShowRect[(b[j].y - k + 1)*11 + b[j].x] = ShowRect[(b[j].y - k)*11 + b[j].x];
                        k++;
                    }while(ShowRect[(b[j].y - k)*11 + b[j].x]);
                    ShowRect[(b[j].y - k + 1)*11 + b[j].x] = 0;
                }

            }
        }               
        else b[j].cy--;
    }
}

void Play(HWND hDlg, WPARAM wParam,LPARAM lParam, WORD& COUNTS, COUNT a[])
{
    int i;
    int x,y;
    x = LOWORD(lParam);
    y = HIWORD(lParam);
            switch(wParam)
            {
            case 0:
               for(i = 0x8000; !(i ^ ((WORD)i&COUNTS)) && i; i>>=1) ;
               COUNTS |= (WORD)i;
               for(i = 0; i < 16; i ++)
               {
                   if(a[i].count <= 0)
                   {
                      a[i].count = 15;           //15帧动画
                      a[i].x  =  (WORD)x;
                      a[i].cy =  (WORD)y * 20;
                      a[i].y  =  (WORD)y;
                      break;
                   }
               }
               break;
            case 1:
               for(i = 0x8000; !(i ^ ((WORD)i&COUNTS)) && i; i>>=1) ;
               COUNTS |= (WORD)i;
               for(i = 0; i < 16; i ++)
               {
                    if(a[i].cy < 1)
                    {
                       a[i].cy = 240;                          //定时器
                       a[i].count = 0;                         //动画帖数
                       a[i].x = (WORD)x;
                       a[i].y = (WORD)y;
                       SendMessage(GetParent(hDlg),WM_OUTTEXT,a[i].x,a[i].y);
                       break;
                    }
                }
               break;
            case 2:
               for(i = 0,wParam = 0x8000; i < 16; i ++,wParam>>=1)
               {
                    if(a[i].cy > 0)
                    {
                       if(a[i].x == x && a[i].y == y)
                       {
                          a[i].cy =0;                          //定时器
                          //a[i].count = 12;                         //动画帖数
                          COUNTS ^=(WORD)wParam;
                       }
                    }
                }
                break;
            case 3:
               for(i = 0; i < 16; i ++)
               {
                    if(a[i].cy > 0 && a[i].y < (WORD)lParam)
                    {
                        a[i].y++;
                    }
                }
                break;
            }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    PAINTSTRUCT ps;
    HDC hdc;                                     //显示
    HFONT font,Old;                              //字体
    static HWND ShowRect,                        //显示方块子窗口句丙
                NextRect;                        //显示下一个方块子窗口句丙
    static int Lever,                            //关卡
               Point,                            //分数
               cPoint;                           //记数分数
    static char OutRect[16];                            //移动方块和下一个方块
    static BYTE OutWidth;                               //方块的宽度
    int i;

    switch (message)
    {
        case WM_CREATE:                          //创建窗口消息
            srand((int)GetCurrentTime());        //设置随机函数
            ShowRect = CreateWindow(TEXT("static"),TEXT(""),WS_CHILD|WS_VISIBLE,20,60,220,400,hWnd,(HMENU)ID_SHOWRECT,hInst,NULL);           //创健显示方块子静态子窗口
            NextRect = CreateWindow(TEXT("static"),TEXT(""),WS_CHILD|WS_VISIBLE,260,100,80,80,hWnd,(HMENU)ID_NEXTRECT,hInst,NULL);            //创健显示下一个方块静态子窗口

            OldProc = (WNDPROC) SetWindowLong(ShowRect,GWL_WNDPROC,(LONG)SHOWRECT);           //窗口子类化                                        //窗口子类化
            SetWindowLong(NextRect,GWL_WNDPROC,(LONG)SHOWRECT);                               //窗口子类化
            SendMessage(ShowRect,WM_LOADRECT,0,0);
            SendMessage(hWnd,WM_STARTGAME,1,40);
            //发一个开始游戏消息WM_STARTGAME  WPARAM 为关卡  LPARAM  为关卡分数
            break;

        case WM_STARTGAME:                    //开始游戏
            Lever = (int) wParam;   //取得关卡
            if(Lever > 10) Lever = 1;
            Point = lParam;   //取得关卡分数
            cPoint= 0;        //关卡记数分数
            SendMessage(ShowRect,WM_READYGAME,Lever,0);//初始化显示子窗口数据
            SendMessage(ShowRect,WM_MOVERECT,0,0); //发一个刷方块消息
            break;

        case WM_NEXTGAME:                     //下一关游戏
            if(lParam)
            {
               
                MessageBox(NULL,TEXT("进入下一关"),TEXT("提示"),MB_OK);
                Point += 20;
                cPoint = 0;
                SendMessage(hWnd,WM_STARTGAME,Lever+1,Point);
            }
            else
            {
               cPoint += wParam;                 //加分
               if(cPoint >= Point){ KillTimer(ShowRect,1);SendMessage(hWnd,WM_NEXTGAME,0,Lever+1);}    //进入下一关
               if(Lever > 10) Lever = 0;
               InvalidateRect(hWnd,NULL,TRUE);
            }
            break;
        case WM_NEXTRECT:                               //显示下一个方块
            SendMessage(NextRect,WM_NEXTRECT,0,0);
            break;
        case WM_COMEOVER:                     //游戏结速   
            MessageBox(NULL,TEXT("游戏结速"),TEXT("提示"),MB_OK);
            break;

        case WM_OUTTEXT:
            {
                TCHAR Text[30];
                i = wsprintf(Text,"%d -- %d   ",wParam,lParam);
                hdc = GetDC(hWnd);
                TextOut(hdc,260,240,Text,i);
                ReleaseDC(hWnd,hdc);
            }
            break;

        case WM_SETFOCUS:                     //设显示子窗口
             SetFocus(ShowRect);
             break;

        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            SetBkMode(hdc,TRANSPARENT);
            //
            font = CreateFont(30,30,0,0,700,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,0,DEFAULT_QUALITY,0,"XIE");
            Old  =(HFONT)SelectObject(hdc,font);
            SetTextColor(hdc,RGB(255,0,0));
            TextOut(hdc,40,10,TEXT("俄罗斯方块"),10);
            DeleteObject(font);
            SelectObject(hdc,Old);
            {
                TCHAR Text[20];
                i = wsprintf(Text,"关卡:第 %d  关",Lever);
                TextOut(hdc,260,200,Text,i);
                i = wsprintf(Text,"分数:%d/%d",Point,cPoint);
                TextOut(hdc,260,220,Text,i);

            }
            EndPaint(hWnd, &ps);           
            break;
        case WM_DESTROY:           
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
// Mesage handler for about box.

搜索更多相关主题的帖子: 俄罗斯方块 
2012-08-28 22:38
zanzan1986
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:100
专家分:140
注 册:2011-2-22
收藏
得分:0 
程序代码:
LRESULT CALLBACK SHOWRECT(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)          //显示方块窗口
{    
    PAINTSTRUCT ps;
    HDC hdc;
    static  HDC hdcRect;
    static  BYTE ShowRect[220];                                 //显示方块数组
    static  BYTE MoveRect1[17],MoveRect2[17];                   //移动方块数组
    static  BYTE *pOutRect;                                     //显示移方块指针
    static  BYTE cTime,AddTime;                                 //计时变量和加速计时变量方块是否越界标志0为无越界1为右越界2为左越界3这理越界
    static  char cxRect,cyRect;                                 //移动方块的X,Y位置
    static  WORD KeyBoard,COUNTS,COUNTS2;                                      //按键读档
    static  BYTE rect;                                          //出其它方块率  动画计数
    static  COUNT a[16],b[16];                                        //加分动画 碎砖动画
    int i;

    switch (message)
    {
        case WM_LOADRECT:             //加载位图资源消息
            if(ID_SHOWRECT == GetWindowLong(hDlg,GWL_ID))         //加载DDB位图
            {               
               
                hdc    = GetDC(hDlg);
                hdcRect  = CreateCompatibleDC(hdc);               //创建方块设备
                ReleaseDC(hDlg,hdc);
                HBITMAP Bitmap;
                Bitmap = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1));
                SelectObject(hdcRect,Bitmap);
                DeleteObject(Bitmap);
                ReleaseDC(hDlg,hdc);
               
            }
            //rect = 5;
            AddTime = 12;
            return 0;
        case WM_READYGAME:                    //准备移动方块消息
             InitRect((char*)ShowRect,220);
             MoveRect1[16] = 0;
             MoveRect2[16] = 0;
             COUNTS = 0;
             COUNTS2 = 0;
             if(wParam % 2) rect += 5;
             if(wParam > 2)    AddTime = (AddTime -= 2) ? AddTime : 12;
             if(wParam == 1)
             {
                 rect = 20;
                 AddTime = 12;
             }
            return 0;

        case WM_MOVEPOINT:                                             //动画消息
            switch(wParam)
            {
                case 0: Play(hDlg,wParam,lParam,COUNTS,a); break;
                case 1:
                case 2: 
                case 3: Play(hDlg,wParam,lParam,COUNTS2,b); break;
            }
            return 0;
        case WM_MOVERECT:              //开始移动方块
            if(!MoveRect1[16] && !MoveRect2[16])                       //游戏第一次取方块
            {
                pOutRect = MoveRect1;
                RandRect(MoveRect1,MoveRect1[16]);
                RandRect(MoveRect2,MoveRect2[16]);
                for(i = 0; i < 16; i++)                //清除未播放的动画
                {
                    a[i].count = 0;
                    b[i].cy    = 0;
                }
                SetTimer(hDlg,1,42,NULL);                             //设置时间片
            }
            else
            {
                if(!MoveRect1[16])             //换一个方块
                {
                    pOutRect = MoveRect2;
                    RandRect(MoveRect1,MoveRect1[16]);
                }
                else
                {
                    pOutRect = MoveRect1;
                    RandRect(MoveRect2,MoveRect2[16]);
                }
            }
            cxRect = 4;                   //方块X坐标
            cyRect = -pOutRect[16];       //方块Y坐标
            if(rect >= rand()%100)
            {
                i = rand()%pOutRect[16];
                wParam = rand()%2 +4;
                for(int j = 0; j < pOutRect[16]; j++)
                {
                    if((pOutRect[i*pOutRect[16]+j] =  pOutRect[i*pOutRect[16]+j] ? wParam : pOutRect[i*pOutRect[16]+j])) break;
                }
            }
            SendMessage(GetParent(hDlg),WM_NEXTRECT,0,0);        //刷下一个方块
            //显示下一个方块
            return 0;

        case WM_NEXTRECT:
            InvalidateRect(hDlg,NULL,TRUE);
            break;
        case WM_TIMER:                            //计时消息
            if(cTime >= AddTime || KeyBoard)
            {
                wParam = cxRect;                       //取得原坐标
                lParam = cyRect;                       //取得原坐标   
                if(KeyBoard & VKLEFT)                                     //按左键
                {
                    if(IsToMove(pOutRect,ShowRect,cxRect,cyRect,VKLEFT) == VKLEFT)  cxRect--;
                }
                else if(KeyBoard & VKRIGHT)                               //按右键
                {
                    if(IsToMove(pOutRect,ShowRect,cxRect,cyRect,VKRIGHT) == VKRIGHT) cxRect++;
                }
                else if((KeyBoard & VKUP) && cxRect >= 0 )                //按上键
                {
                    int  k = 0,n,j,l;
                    if(pOutRect[16] == 2) n = 2;
                    else
                    {
                       n = pOutRect[16] > 3 ? 1:0;
                       n += 3;
                    }
                    for(i = 0; i < n; i++)
                    {
                        l = (cyRect + i) * 11 + cxRect;
                        for(j = 0; j < n; j++)
                        if(ShowRect[l + j] || cxRect + j >= 11)
                        {
                            k = 1;
                        }
                        if(k) break;
                    }
                    if(!k) Round(pOutRect,pOutRect[16],90);  //转动方块
                }
                else if(KeyBoard & VKDOWN) cTime = 12;                   //按下键

                if(cTime >= AddTime)                            //时间片到
                {
                    cTime = 0;
                    i = IsToMove(pOutRect,ShowRect,cxRect,cyRect,VKDOWN);
                    if(i == VKDOWN) cyRect++;
                    else if(!i)                //到达底部
                    {
                        i = InToRect(hDlg,ShowRect,pOutRect,cxRect,cyRect);
                        if(i)
                        {
                            InvalidateRect(hDlg,NULL,TRUE);
                            SendMessage(GetParent(hDlg),WM_NEXTGAME,i,0);
                        }
                        else  SendMessage(hDlg,WM_DRAWRECT,wParam,lParam);         //重绘方块
                        pOutRect[16] = 0;
                        SendMessage(hDlg,WM_MOVERECT,0,0);         //重新刷方块
                        KeyBoard = 0;
                        return 0;
                    }
                    else if(i == VKUP)
                    {
                       SendMessage(hDlg,WM_DRAWRECT,wParam,lParam);
                       KillTimer(hDlg,1);
                       SendMessage(GetParent(hDlg),WM_COMEOVER,0,0);
                    }                   
                }
                SendMessage(hDlg,WM_DRAWRECT,wParam,lParam);           
            }
            KeyBoard = 0;               
            if(COUNTS || COUNTS2)
            { 
                hdc = GetDC(hDlg);
                PlayPoint(COUNTS,hdc,hdcRect,a,ShowRect);
                PlayRect(COUNTS2,hdc,hdcRect,b,ShowRect);
                ReleaseDC(hDlg,hdc);
            }
            cTime++;
            return 0;
           
        case WM_DRAWRECT:
            hdc = GetDC(hDlg);
            {
                int i,j,l;   
                    
                for( i = 0; i < pOutRect[16] && lParam + i < 20; i++)                           //清除原有的方块
                {
                     if((int) lParam + i < 0) continue;                        //判断方块是否进入显示区
                     l = ((int)lParam + i) * 11 + (int) wParam;
                   
                     for( j = 0; j < pOutRect[16]; j++)      //清除原有的方块
                     {
                            if((int) wParam + j >= 0 && (int) wParam + j < 11 )
                            {
                                DrawRect(hdc,hdcRect,ShowRect[l+j],(wParam+j)*20,(lParam+i)*20);
                               
                            }
                     }

                }
               
                for( i = 0; i < pOutRect[16] && cyRect + i < 20; i++)                           //绘画当前的方块
                {
                     if(cyRect + i < 0)   continue;                         //绘新的方块
                     l = i * pOutRect[16];
                     for(j = 0; j < pOutRect[16]; j++)
                     {
                              if(cxRect + j < 0 || cxRect + j > 11) continue;
                              if(pOutRect[l+j]) DrawRect(hdc,hdcRect,pOutRect[l+j],(j+ cxRect) * 20,(i + cyRect) * 20);
                     }
                }
            }
            ReleaseDC(hDlg,hdc);
            return 0;

        case WM_PAINT:
            hdc = BeginPaint(hDlg, &ps);
            if(ID_SHOWRECT == GetWindowLong(hDlg,GWL_ID))
            {
                int i,j;
                for(i = 0; i < 20; i++)
                for(j = 0; j < 11; j++)
                {
                     DrawRect(hdc,hdcRect,ShowRect[i*11+j],j*20,i*20);
                }
                if(pOutRect[16])
                {
                    for(i = 0; i < pOutRect[16] && cyRect + i < 20; i++)
                    {
                       if(cyRect + i < 0) continue;
                       for(j = 0; j < pOutRect[16] && cxRect + j < 11; j++)
                       {
                                if(cxRect + j < 0) continue;
                                DrawRect(hdc,hdcRect,pOutRect[i*pOutRect[16]+j],(cxRect+j)*20,(cyRect+i)*20);
                        }
                     }

                }

            }
            else
            {
                StretchBlt(hdc,0,0,80,80,hdcRect,20,20,20,20,SRCCOPY);
                BYTE *p = (pOutRect == MoveRect1 ? MoveRect2:MoveRect1);
                    for(int i = 0; i < p[16]; i++)
                    for(int j = 0; j < p[16]; j++)
                    {
                        DrawRect(hdc,hdcRect,p[i*p[16]+j],j*20,i*20);
                    }
            }

            EndPaint(hDlg,&ps);
            return 0;
        case WM_KEYDOWN:
            if(!pOutRect[16]) return 0;
            switch(wParam)                      //方向键
            {
                case VK_LEFT:
                        KeyBoard |= VKLEFT;
                    break;
                case VK_RIGHT:
                        KeyBoard |= VKRIGHT;
                    break;
                case VK_UP:
                        KeyBoard |= VKUP;
                    break;
                case VK_DOWN:
                    KeyBoard |= VKDOWN;
                    break;
                case VK_SPACE:
                    MessageBeep(0);
                    wParam = cxRect;
                    lParam = cyRect;
                    do
                    {
                       i = IsToMove(pOutRect,ShowRect,cxRect,cyRect,VKDOWN);
                       if(i == VKDOWN) cyRect++;
                    }while(i == VKDOWN);
                    SendMessage(hDlg,WM_DRAWRECT,wParam,lParam);
                    break;
                case VK_RETURN:
                    MessageBeep(0);
                    break;
            }
              return 0;
        case WM_DESTROY:
            if(ID_SHOWRECT == GetWindowLong(hDlg,GWL_ID))
            {
                DeleteDC(hdcRect);
                KillTimer(hDlg,1);
            }
           
            break;

    }
    return CallWindowProc(OldProc,hDlg,message,wParam,lParam);
}

void InitRect(CHAR a[], int size)                           //初始化数组函数
{
    for(int i = 0; i < size; i++) a[i] = 0;
}

void RandRect(BYTE p[], BYTE &x)                                                //随机出方块
{
    int a,b,i;                                                     
    a = rand()%7;                                                //方块类型
    b = rand()%3;                                                //转动角度                                               
    i = rand()%3+1;          
    InitRect((char*)p,16);                                      //方块颜色
    switch(a)
    {
       case 0:x = 2; break;
       case 1:
       case 2:                                                 //传递长度
       case 3:
       case 4:
       case 5:
           x = 3;
           break;
       case 6:
           x = 4;
           break;
    }
    switch(a)
    {
        case 0:    
            p[0] = p[1] = p[2] = p[3] = i;
            break;
        case 1:  
            p[0] = p[3] = p[4] = p[7] = i;
            break;
        case 2:                                         //初始化
            p[1] = p[4] = p[6] = p[7] = i;
            break;
        case 3:                                       //初始化
            p[4] = p[6] = p[7] = p[8] = i;
            break;
        case 4:
            p[2] = p[4] = p[5] = p[7] = i;
            break;
        case 5:
            p[0] = p[1] = p[2] = p[5] = i;
            break;
        case 6:   
            p[12] = p[13] = p[14] = p[15] = i;
            break;
        default:MessageBox(NULL,"刷方块出错!!","ss",MB_OK); x = 0; break;

    }
    switch(b)
    {
        case 0:
            break;
        case 1:
            Round(p,x,90);
            break;
        case 2:
            Round(p,x,180);
            break;
        case 3:
            Round(p,x,270);
            break;
    }
}

void Round(BYTE a[], int size, int x)               //转动方块函数
{
    BYTE b[16];
    int i,j;
    if(size == 2) return ;
    if(size == 4)
    {

        if(a[15])
        {
            a[0] = a[15];
            a[15]= 0;
            a[4] = a[14];
            a[14]= 0;
            a[8] = a[13];
            a[13]= 0;
        }
        else
        {
            a[13] = a[0];
            a[0]  = 0;
            a[14] = a[4];
            a[4]  = 0;
            a[15] = a[8];
            a[8]  = 0;
        }
        return ;
    }
    int l = size - 1;
    InitRect((char*)b,16);
    switch(x)
    {
        case 0:
            return ;
        case 90:
            for(i = 0; i < size; i++)
            for(j = 0; j < size; j++)
                if(a[i*size+j]) b[j*size+ l -i] = a[i*size+j];
            break;
        case 180:
            for(i = 0; i < size; i++)
            for(j = 0; j < size; j++)
                if(a[i*size+j]) b[(l-i)*size+l-j] = a[i*size+j];
            break;
        case 270:
            for(i = 0; i < size; i++)
            for(j = 0; j < size; j++)
                if(a[i*size+j]) b[(l-j)*size+i] = a[i*size+j];
            break;
        default:
            return ;

    }
    for(i = 0; i < size * size; i++) a[i] = b[i];
}

int InToRect(HWND hDlg, BYTE a[],const BYTE b[], const int x,const int y)                    //方块赋值于显示矩阵中
{

    int n,m,i,j,l,xx,yy;;
    //bool off;
    DWORD Param,pp;
    pp = -1;
    int aa[] = {0,0,0,0};                
    for(i = 0; i < b[16]; i++)
    {
        m = (y+i)*11;
        for(int j = 0; j < b[16]; j++)
        {
           n = i*b[16]+j;
           if(b[n] == 5)
           {

                Param = MAKELONG((x+j),(y+i));
                pp = (y+i);
           }
           if(b[n]) a[m + j + x] = b[n];
        }
    }
    l = i = 0;
    for(yy = y; yy < y + b[16] && yy < 20; yy++)
    {
        for(xx = 0; xx < 11; xx++)
        {
             if(!a[yy*11+ xx]) break;
        }
        if(xx == 11)                              //消除一行
        {
            l++;
            if(!i)                //计算有多少行空格
            {
                  n = 20;
                  for(i = 0; i < n; i++)
                  for(j = 0; j < 11; j++)
                  {
                      if(a[i*11+j]) n = i--;
                  }
            }
            SendMessage(hDlg,WM_MOVEPOINT,3,yy);
            for(m = 0; m < 11; m++)                      //计算星星分数
            {
                if(a[yy*11+m] == 4)
                {
                    SendMessage(GetParent(hDlg),WM_NEXTGAME,5,0);      //加5分
                    SendMessage(hDlg,WM_MOVEPOINT,0,MAKELONG(m,yy));               //演示动画
                }
                if(a[yy*11+m] == 5)
                {
                     SendMessage(hDlg,WM_MOVEPOINT,2,MAKELONG(m,yy));
                }
            }
            for(int u = yy - 1,t = yy; u >= i; u--,t--)
            for(int f = 0; f < 11; f++)
            {
                a[t*11+f] = a[u*11+f];
                if(u == i) a[u*11+f] = 0;          //如果是最后一行则消除
            }
            i--;
        }
        else if(pp != -1)
        {
                if(yy == pp)
                SendMessage(hDlg,WM_MOVEPOINT,1,Param);               //演示动画

        }
    }
    if(l < 4) return l;
    return l*4;
}
2012-08-28 22:38
快速回复:上次发表的一个win32的俄罗斯方块游戏这次把代码发出来
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.059627 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved