• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            l

            成都手游碼農一枚
            隨筆 - 32, 文章 - 0, 評論 - 117, 引用 - 0
            數據加載中……

            [C++]俄羅斯方塊源碼

            寫的很爛請別在意- -!
            暑假無聊時寫的。

            /***************************************************
            Filename:ELSFK.h
            Anthor:shly
            E-mail:xlq1989@vip.qq.com
            LastUpdate:2009.8.7
            **************************************************
            */


            #pragma once

            #include 
            "stdafx.h"
            #include 
            <cassert>
            //----------------------Data---------------------------
            struct SquareData{
                
            static const int S_KIND = 7;
                
            static const int S_MAX  = 16;
                
            static const int S_START= 6;
                
            static int         sE[S_KIND];
                
            static int         sData[S_KIND][S_MAX];
            }
            ;
            int SquareData::sE[SquareData::S_KIND] = {4333332};
            int SquareData::sData[SquareData::S_KIND][SquareData::S_MAX] = 
            {//0為格子不顯示 非0則顯示
                {0100010001000100},
                
            {2002200200000000},
                
            {0030330300000000},
                
            {4400400400000000},
                
            {0550500500000000},
                
            {0606660000000000},
                
            {7777000000000000}
            }
            ;
            //----------------------Matrix--------------------------
            //游戲區域
            class Matrix{
            protected:
                
            int x,y;  int* pM;
            public:
                Matrix(
            int _x, int _y)
                         :x(_x),y(_y)
                
            {
                    pM 
            = new int[x*y];
                    assert(pM);
                    
            for(int i = 0; i<x*y; ++i) pM[i] = 0;
                }

                Matrix(
            int* _t, int _x, int _y)
                         :x(_x),y(_y)
                
            {
                    pM 
            = new int[x*y];
                    assert(pM);
                    
            for(int i = 0; i<x*y; ++i) pM[i] = _t[i];
                }

                
            virtual ~Matrix(){ delete [] pM;}
            public:
                
            //common functions
                int GetX() const {return x;}
                
            int GetY() const {return y;}
                
            int*GetM() const {return pM;}
                
            void MatrixCombin(Matrix& _m,int _x, int _y, bool zero = false);
                
            bool MatrixIntersect(Matrix& _m, int _x, int _y);
                
            bool MatrixYAll(int _y, bool zero = false);
            public:
                
            //use in the game
                void MatrixDeleteAndMove(int _y);
            }
            ;
            void Matrix::MatrixCombin(Matrix& _m, int _x, int _y, bool zero)
            {
                
            int Ex = _m.GetX(), Ey = _m.GetY();
                
            for(int i = 0; i<Ey; ++i)
                    
            for(int j =0;  j<Ex; ++j)
                    
            {
                        
            if(_m.GetM()[i*Ex+j]){
                            
            if(_x+j<0 || _x+j>=|| _y+i<0 || _y+i>=y ) continue;
                        zero
            ?pM[(_y+i)*x+_x+j] = 0:pM[(_y+i)*x+_x+j] = 
                            (pM[(_y
            +i)*x+_x+j]>_m.GetM()[i*Ex+j]?pM[(_y+i)*x+_x+j]:_m.GetM()[i*Ex+j]);
                        }

                    }

            }

            bool Matrix::MatrixIntersect(Matrix& _m, int _x, int _y)
            {
                
            int Ex = _m.GetX(), Ey = _m.GetY();
                
            for(int i = 0; i<Ey; ++i)
                    
            for(int j =0;  j<Ex; ++j)
                    
            {
                        
            if(_m.GetM()[i*Ex+j]){
                            
            if(_x+j<0 || _x+j>=||
                               _y
            +i<0 || _y+i>=|| pM[(_y+i)*x+_x+j] ) 
                                
            return true;
                        }

                    }

                
            return false;
            }

            bool Matrix::MatrixYAll(int _y, bool zero)
            {
                
            if(_y<0 || _y>=y) return false;
                
            for(int i = 0; i<x; ++i)
                    
            if( zero?pM[_y*x+i]:!pM[_y*x+i])
                        
            return false;
                
            return true;
            }

            void Matrix::MatrixDeleteAndMove(int _y)
            {
                
            if(_y<0 || _y>=y) return;
                
            for(int i = _y; i>0--i){
                    
            for(int j = 0; j<x; ++j)
                        pM[i
            *x+j] = pM[(i-1)*x+j];
                    
            //use in the game
                    if(MatrixYAll(i,true)) break;
                }

            }


            //------------------------FKMatrix-------------------------
            //方塊
            class FKMatrix:public Matrix{
            protected:
                
            int sx, sy;
            public:
                FKMatrix(
            int index):Matrix(SquareData::sData[index],
                                  SquareData::sE[index],
                                  SquareData::sE[index]),
                                  sx(SquareData::S_START),sy(
            0)
                
            {}
                
            int GetSX() const {return sx;}
                
            int GetSY() const {return sy;}
                
            void SetSX(int _x) {sx = _x;}
                
            void SetSY(int _y) {sy = _y;}
                
            void FKMatrixTransform(Matrix& _m);
            }
            ;

            void FKMatrix::FKMatrixTransform(Matrix& _m)
            {
                
            int i = 0,E = x;
                
            int* tM = new int[E*E];
                assert(pM);
                
            int* t = pM;
                
            for(int j = 0; j<E; ++j)
                    
            for(int l = E - 1; l>=0--l, ++i)
                        tM[i] 
            = pM[l*E+j];
                
            //use in the game
                pM = tM;
                
            if(_m.MatrixIntersect(*this,sx,sy)){
                    pM 
            = t;
                    delete [] tM;
                }
            else delete [] t;
            }




            // ELSFK.cpp : 定義應用程序的入口點。
            //
            #include "stdafx.h"
            #include 
            "ELSFK.h"
            #include 
            "resource.h"
            #include 
            <ctime>

            // 全局變量:
            Matrix matGame(10,18);
            FKMatrix
            * matFK = NULL;

            bool   bAgain = true;
            int        score = 0;
            int        level = 1;
            const int DES = 20;
            const int SCREEN_WIDTH = 308;
            const int SCREEN_HEIGHT = 408;

            #define FKBeginUpdate(__g,__f) __g.MatrixCombin(*__f,__f->GetSX(),__f->GetSY(),true);
            #define FKEndUpdate(__g,__f) __g.MatrixCombin(*__f,__f->GetSX(),__f->GetSY(),false);\
                    InvalidateRect(hWnd,NULL,
            false);

            //{{{--------------------------編譯器生成代碼--------------------------
            #define MAX_LOADSTRING 100
            // 全局變量:
            HINSTANCE hInst;                                // 當前實例
            TCHAR szTitle[MAX_LOADSTRING];                    // 標題欄文本
            TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口類名

            // 此代碼模塊中包含的函數的前向聲明:
            ATOM                MyRegisterClass(HINSTANCE hInstance);
            BOOL                InitInstance(HINSTANCE, 
            int);
            LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
            INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

            int APIENTRY _tWinMain(HINSTANCE hInstance,
                                 HINSTANCE hPrevInstance,
                                 LPTSTR    lpCmdLine,
                                 
            int       nCmdShow)
            {
                UNREFERENCED_PARAMETER(hPrevInstance);
                UNREFERENCED_PARAMETER(lpCmdLine);
                MSG msg;
                HACCEL hAccelTable;
                LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
                LoadString(hInstance, IDC_ELSFK, szWindowClass, MAX_LOADSTRING);
                MyRegisterClass(hInstance);
                
            if (!InitInstance (hInstance, nCmdShow)){return FALSE;}
                hAccelTable 
            = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ELSFK));
                
            while (GetMessage(&msg, NULL, 00))
                
            {
                    
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
                    
            {
                        TranslateMessage(
            &msg);
                        DispatchMessage(
            &msg);
                    }

                }

                
            return (int) msg.wParam;
            }

            ATOM MyRegisterClass(HINSTANCE hInstance)
            {
                WNDCLASSEX wcex;
                wcex.cbSize 
            = sizeof(WNDCLASSEX);
                wcex.style            
            = CS_HREDRAW | CS_VREDRAW;
                wcex.lpfnWndProc    
            = WndProc;
                wcex.cbClsExtra        
            = 0;
                wcex.cbWndExtra        
            = 0;
                wcex.hInstance        
            = hInstance;
                wcex.hIcon            
            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ELSFK));
                wcex.hCursor        
            = LoadCursor(NULL, IDC_ARROW);
                wcex.hbrBackground    
            = (HBRUSH)(COLOR_WINDOW+1);
                wcex.lpszMenuName    
            = MAKEINTRESOURCE(IDC_ELSFK);
                wcex.lpszClassName    
            = szWindowClass;
                wcex.hIconSm        
            = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
                
            return RegisterClassEx(&wcex);
            }

            BOOL InitInstance(HINSTANCE hInstance, 
            int nCmdShow)
            {
               HWND hWnd;
               hInst 
            = hInstance; // 將實例句柄存儲在全局變量中
               hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MAXIMIZEBOX,
                  
            400,100,SCREEN_WIDTH,SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
               
            if (!hWnd)return FALSE;}
               ShowWindow(hWnd, nCmdShow);
               UpdateWindow(hWnd);
               
            return TRUE;
            }

            //--------------------------編譯器生成代碼------------------------}}}


            LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
            {
                
            int wmId, wmEvent;
                PAINTSTRUCT ps;
                HDC hdc;

                
            switch (message)
                
            {
                
            //{{{---------------------菜單----------------------
                case WM_COMMAND:
                    wmId    
            = LOWORD(wParam);
                    wmEvent 
            = HIWORD(wParam);
                    
            // 分析菜單選擇:
                    switch (wmId)
                    
            {
                    
            case IDM_ABOUT:
                        DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                        
            break;
                    
            case IDM_EXIT:
                        DestroyWindow(hWnd);
                        
            break;
                    
            default:
                        
            return DefWindowProc(hWnd, message, wParam, lParam);
                    }

                    
            break;
                
            //---------------------菜單----------------------}}}
                case WM_PAINT:
                    
            {
                        hdc 
            = BeginPaint(hWnd, &ps);
                        HBITMAP tmpBmp 
            = CreateCompatibleBitmap(hdc,SCREEN_WIDTH,SCREEN_HEIGHT);
                        HDC tmpDC 
            = CreateCompatibleDC(hdc);
                        SelectObject(tmpDC,tmpBmp);
                        
            //---------------------背景-------------------------
                        {
                            RECT r1 
            = {0,0,(matGame.GetX())*DES,(matGame.GetY())*DES};
                            RECT r2 
            = {(matGame.GetX())*DES,0,SCREEN_WIDTH,(matGame.GetY())*DES};
                            FillRect(tmpDC,
            &r1,(HBRUSH)GetStockObject(BLACK_BRUSH));
                            FillRect(tmpDC,
            &r2,(HBRUSH)GetStockObject(DKGRAY_BRUSH));
                        }

                        
            //--------------------游戲區------------------------
                        for(int i = 0; i<matGame.GetY(); ++i)
                            
            for(int j = 0; j<matGame.GetX(); ++j)
                            
            {
                                
            int id = matGame.GetM()[i*matGame.GetX()+j];
                                
            if(!id) continue;
                                HBRUSH hBrush 
            = CreateSolidBrush(RGB(id*35,255-id*35,id*20+150));
                                HBRUSH hOld 
            =(HBRUSH) SelectObject(hdc,hBrush);
                                RECT r 
            = {j*DES,i*DES,j*DES+DES-1,i*DES+DES-1};
                                FillRect(tmpDC,
            &r,hBrush);
                                SelectObject(hdc,hOld);
                                DeleteObject(hBrush);
                            }

                        
            //---------------------邊欄-------------------------
                        SetBkMode(tmpDC,TRANSPARENT);
                        SetTextColor(tmpDC,RGB(
            200,50,75));
                        
            char cScr[20];
                        
            int l = wsprintfA(cScr,"得分: %d",score);
                        TextOutA(tmpDC,matGame.GetX()
            *DES+2,SCREEN_HEIGHT/2-60,cScr,l);

                        SetTextColor(tmpDC,RGB(
            200,200,0));
                            l 
            = wsprintfA(cScr,"等級: %d",level);
                        TextOutA(tmpDC,matGame.GetX()
            *DES+2,SCREEN_HEIGHT/2,cScr,l);

                        BitBlt(hdc,
            0,0,SCREEN_WIDTH,SCREEN_HEIGHT,tmpDC,0,0,SRCCOPY);
                        DeleteDC(tmpDC);
                        DeleteObject(tmpBmp);
                        EndPaint(hWnd, 
            &ps);
                        
            break;
                    }

                
            case WM_TIMER:
                    SendMessage(hWnd,WM_KEYDOWN,VK_DOWN,
            0);
                    
            break;
                
            case WM_KEYDOWN:
                    
            {
                        FKBeginUpdate(matGame,matFK);
            //開始更新
                        int sx = matFK->GetSX(), sy = matFK->GetSY();
                        
            int x = matGame.GetX(), y = matGame.GetY();
                        
            switch(wParam)
                        
            {
                        
            case VK_SPACE:
                        
            case VK_UP:
                            matFK
            ->FKMatrixTransform(matGame); break;
                        
            case VK_DOWN:
                            
            if(matGame.MatrixIntersect(*matFK,sx,sy+1))
                                bAgain 
            = true;
                            
            else 
                                matFK
            ->SetSY(sy+1);
                            
            break;
                        
            case VK_LEFT:
                            
            if(matGame.MatrixIntersect(*matFK,sx-1,sy)) break;
                            matFK
            ->SetSX(sx-1);
                            
            break;
                        
            case VK_RIGHT:
                            
            if(matGame.MatrixIntersect(*matFK,sx+1,sy)) break;
                            matFK
            ->SetSX(sx+1);
                            
            break;
                        
            default:break;
                        }

                        FKEndUpdate(matGame,matFK);
            //結束更新
                        if(bAgain)
                        
            {
                            
            int count = 0;
                            
            for(int i = 0; i<matFK->GetY(); ++i)
                                    
            if(matGame.MatrixYAll(sy+i,false)){
                                        matGame.MatrixDeleteAndMove(sy
            +i);
                                        
            ++count;
                                    }

                                    score 
            += count*count*2;
                                    level 
            = score/200+1;
                            
            if(matFK->GetSY()-count<=0){
                                KillTimer(hWnd,
            1);
                                MessageBox(NULL,L
            "Game Over!",L"Sys",MB_OK);
                                SendMessage(hWnd,WM_DESTROY,
            0,0);
                                
            return 0;
                            }

                            delete matFK; bAgain 
            = false;
                            matFK 
            = new FKMatrix(rand()%7);
                            FKEndUpdate(matGame,matFK);
                        }
             
                        
            break;
                    }

                
            //----------------------初始和結束處理------------------------
                case WM_CREATE:
                    srand(time(NULL));
                    matFK 
            = new FKMatrix(rand()%7);
                    bAgain 
            = false;
                    FKEndUpdate(matGame,matFK);
                    SetTimer(hWnd,
            1,500,NULL);
                    
            break;
                
            case WM_DESTROY:
                    PostQuitMessage(
            0);
                    delete matFK;
                    
            break;
                
            default:
                    
            return DefWindowProc(hWnd, message, wParam, lParam);
                }

                
            return 0;
            }



            // “關于”框的消息處理程序。
            INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
            {
                UNREFERENCED_PARAMETER(lParam);
                
            switch (message)
                
            {
                
            case WM_INITDIALOG:
                    
            return (INT_PTR)TRUE;

                
            case WM_COMMAND:
                    
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
                    
            {
                        EndDialog(hDlg, LOWORD(wParam));
                        
            return (INT_PTR)TRUE;
                    }

                    
            break;
                }

                
            return (INT_PTR)FALSE;
            }

            posted on 2009-09-01 00:55 l1989 閱讀(7933) 評論(2)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: [C++]俄羅斯方塊源碼  回復  更多評論   

            分析的不錯啊!
            2009-09-01 16:13 | 99讀書人

            # re: [C++]俄羅斯方塊源碼  回復  更多評論   

            厲害。
            2009-09-03 13:41 | vanlin
            久久精品国产亚洲av日韩| 久久人人爽人人澡人人高潮AV| 亚洲αv久久久噜噜噜噜噜| 精品少妇人妻av无码久久| 久久精品视频免费| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 日韩精品久久久久久久电影蜜臀| 久久精品午夜一区二区福利 | 一本一本久久a久久综合精品蜜桃| 久久精品国产第一区二区三区 | 久久激情亚洲精品无码?V| 久久精品中文无码资源站| 国产精品久久一区二区三区| 久久久久97国产精华液好用吗| 伊人久久综合无码成人网| 国产精品99久久久久久猫咪| 国内精品久久久久久久久电影网| 欧美精品一区二区精品久久| 亚洲中文字幕无码久久2020| 精品久久久久久99人妻| 国产美女久久精品香蕉69| 亚洲伊人久久综合中文成人网| 久久综合狠狠色综合伊人| 少妇内射兰兰久久| 一级a性色生活片久久无少妇一级婬片免费放| 99久久婷婷国产综合亚洲| 久久乐国产综合亚洲精品| 国内精品久久久久影院网站| 久久精品国产免费一区| 婷婷久久香蕉五月综合加勒比| 国产精品99久久久精品无码| 美女久久久久久| 欧洲国产伦久久久久久久| 国产精品亚洲美女久久久| 9191精品国产免费久久| 久久―日本道色综合久久| 久久99热精品| 久久青草国产手机看片福利盒子| AV无码久久久久不卡网站下载| 久久亚洲精品人成综合网| 亚洲狠狠婷婷综合久久久久 |