Windows编程——API(伪)打砖块

Posted ViKyanite.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows编程——API(伪)打砖块相关的知识,希望对你有一定的参考价值。

//windows.h文件中包含应用程序中所需的数据类型和数据结构的定义
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define PI acos(-1)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口函数说明
//---------以下初始化窗口类--------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
    HWND hwnd;
    MSG Msg;
    WNDCLASS wndclass;
    char lpszClassName[] = "窗口";     //窗口类名
    char lpszTitle[] = "My_Windows";    //窗口标题名

    //窗口类的定义
    wndclass.style = CS_HREDRAW | CS_VREDRAW;                  //窗口类型为默认类型
    wndclass.lpfnWndProc = WndProc;      //窗口处理函数为WndProc
    wndclass.cbClsExtra = 0;             //窗口类无扩展
    wndclass.cbWndExtra = 0;             //窗口实例无扩展
    wndclass.hInstance = hInstance;     //当前实例句柄

    wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);//窗口的最小化图标为默认图标
    wndclass.hCursor = LoadCursor( NULL, IDC_ARROW); //窗口 采用箭头光标
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //窗口 背景为 白色
    wndclass.lpszMenuName = NULL ; //窗口 中无菜单
    wndclass.lpszClassName = lpszClassName; //窗口类名为“窗口示例”
//-----------以下进行窗口类的注册
    if (!RegisterClass(&wndclass))//如果注册失败则发出警吿声音
    {
        MessageBeep (0);
        return FALSE;
    }

//创建窗口
    hwnd = CreateWindow(
                        lpszClassName, //窗口类名
                        lpszTitle,      //窗口实例的标题名
                        WS_OVERLAPPEDWINDOW, //窗口 的风格
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,  //窗口左上角坐标为默认值
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,  //窗口的髙和宽为默认值
                        NULL,           //此窗口无父窗口
                        NULL,           //此窗口无主菜单
                        hInstance,      //创建此窗口应用程序的当前句柄
                        NULL            //不使用该值
                        );



    ShowWindow( hwnd, nCmdShow);        //显示窗口
    UpdateWindow(hwnd);                 //绘制用户区
    while( GetMessage(&Msg, NULL, 0, 0)) //消息循环
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;                  //程序终止时将信息返回系统
}

struct rectangle{
    int l, r, u, d;
    int isTouched;
}rec[5][10], toMove;

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    HDC hDC, hdc_old;
    HBITMAP hBmp;
    RECT rect;
    HBRUSH hBrush;
    PAINTSTRUCT Ps;
    TEXTMETRIC tm;
    HFONT hf;
    HPEN hPen;
    SIZE Sz;
    COLORREF color[7] = {RGB(255,0,0), RGB(255,165,0), RGB(255,255,0), RGB(0,255,0), RGB(0,255,255), RGB(0,0,255), RGB(139,0,255)};
    int temp, disx, xx, yy;
    static int x = 400, y= 250;
    static int vx = 3 , vy = 5;
    static int r = 17;
    static int dis = 18;
    static int i, j;
    static bool isFailed = 0;
    HBITMAP hBkBmp;
    char str[] = "GAME OVER";
    switch(message){
    case WM_ERASEBKGND:
        return 1;
    case WM_TIMER:
        InvalidateRect(hWnd, NULL, 1);
        return 0;
    case WM_CREATE:
    //建立计时器,每2秒发出wm_timer的消息
        SetTimer(hWnd, 1, 25, NULL);
        GetClientRect (hWnd, &rect) ;
        disx = (rect.right - rect.left)/10;
        xx = rect.left;
        yy = rect.top;
        for(j = 0; j < 10; j++){
            for(i = 0; i < 5; i++){
                rec[i][j].l = xx + j * disx;
                rec[i][j].r = xx + (j+1) * disx-2;
                rec[i][j].u = yy + i*40;
                rec[i][j].d = yy + (i+1)*40-2;
                rec[i][j].isTouched = 1;
            }
        }
        toMove.l = (rect.left+rect.right)/2;
        toMove.r = (rect.left+rect.right)/2+140;
        toMove.d = rect.bottom;
        toMove.u = rect.bottom-30;
        return 0;


        case WM_KEYDOWN:
            GetClientRect (hWnd, &rect);
            if(wParam == VK_LEFT){
                if(toMove.l - dis >= rect.left){
                    toMove.l -= dis;
                    toMove.r -= dis;
                }
                else {
                    toMove.l = rect.left;
                    toMove.r = rect.left +140;
                }
            }
            if(wParam == VK_RIGHT){
                if(toMove.r + dis <= rect.right){
                        toMove.r += dis;
                        toMove.l += dis;
                }
                else{
                    toMove.r = rect.right;
                    toMove.l = rect.right -140;
                }
            }
            return 0;

        case WM_PAINT:
            hdc_old = BeginPaint(hWnd, &Ps);
            hDC = CreateCompatibleDC(hdc_old);
            GetClientRect(hWnd,&rect);
            hBmp = CreateCompatibleBitmap(hdc_old,rect.right,rect.bottom);
            SelectObject(hDC,hBmp);

            if(isFailed){
                HFONT hF;    //定义字体句柄
                hF=CreateFont(             //创建自定义字体
                            100,            //字体的高度
                            0,        //由系统根据高宽比选取字体最佳宽度值
                            0,            //文本的倾斜度为0,表示水平
                            0,            //字体的倾斜度为0
                            FW_HEAVY,        //字体的粗度,FW_HEAVY为最粗
                            0,            //非斜体字
                            0,            //无下划线
                            0,            //无删除线
                            ANSI_CHARSET,    //表示所用的字符集为ANSI_CHARSET
                            OUT_DEFAULT_PRECIS,        //输出精度为缺省精度
                            CLIP_DEFAULT_PRECIS,        //剪裁精度为缺省精度
                            DEFAULT_QUALITY,        //输出质量为缺省值
                            DEFAULT_PITCH|FF_DONTCARE,//字间距和字体系列使用缺省值
                            "粗体字" );         //字体名称

                SetTextColor(hDC,RGB(255,0,0));//设置字体的颜色为红色
                SelectObject(hDC,hF);        //选入字体
                DrawText(hDC, str, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                DeleteObject(hF);
            }

            if(x >= toMove.l && x <= toMove.r && y+r > toMove.u){
                vy *= -1;
            }

            if(x + r >= rect.right){
                x = rect.right - r;
                vx *= -1;
            }
            else if(x - r <= rect.left){
                x = rect.left + r;
                vx *= -1;
            }

            if(y + r >= rect.bottom){
                y = rect.bottom - r;
                vy = 0;
                vx = 0;
                isFailed = 1;
            }
            else if(y - r <= rect.top){
                y= rect.top + r;
                vy *= -1;
            }
            hBrush = CreateSolidBrush(RGB(0,255,0));
            SelectObject(hDC, hBrush);
            x = x + vx;
            y = y + vy;
            Ellipse(hDC,x-r,y-r,x+r,y+r);   //画圆
            DeleteObject(hBrush);
            for(j = 0; j < 10; j++){
                for(i = 0; i < 5; i++)
                    if(rec[i][j].isTouched){

                    if((x + r >= rec[i][j].l && x + r < rec[i][j].r)){
                        if(y >= rec[i][j].u && y <= rec[i][j].d){
                            rec[i][j].isTouched = 0;
                            x = rec[i][j].l - r;
                            vx *= -1;
                        }
                    }
                    else if((x - r <= rec[i][j].r && x - r > rec[i][j].l)){
                        if(y >= rec[i][j].u && y <= rec[i][j].d){
                            rec[i][j].isTouched = 0;
                            x = rec[i][j].r + r;
                            vx *= -1;
                        }
                    }

                    if(y + r >= rec[i][j].u && y +r <= rec[i][j].d){
                        if(x >= rec[i][j].l && x <= rec[i][j].r){
                            rec[i][j].isTouched = 0;
                            y = rec[i][j].u - r;
                            vy *= -1;
                        }
                    }
                    else if(y - r <= rec[i][j].d && y -r >= rec[i][j].u){
                        if(x >= rec[i][j].l && x <= rec[i][j].r){
                            rec[i][j].isTouched = 0;
                            y = rec[i][j].d + r;
                            vy *= -1;
                        }
                    }
                }
            }
            //画出砖块
            for(j = 0; j < 10; j++)
                for(i = 0; i < 5; i++){
                    if(rec[i][j].isTouched){
                        hBrush = CreateSolidBrush(color[i]);
                        SelectObject(hDC, hBrush);
                        Rectangle(hDC, rec[i][j].l, rec[i][j].u, rec[i][j].r, rec[i][j].d);
                        DeleteObject(hBrush);
                    }
            }



            hBrush=CreateSolidBrush(RGB(0,0,255));
            SelectObject(hDC, hBrush);
            Rectangle(hDC, toMove.l, toMove.u, toMove.r, toMove.d);
    BitBlt(hdc_old,0,0,rect.right,rect.bottom,hDC,0,0,SRCCOPY);
    DeleteObject(hBmp);
    DeleteDC(hDC);
    ReleaseDC(hWnd, hDC); //释放
    EndPaint(hWnd, &Ps); //结束缓制
    return 0;

        case WM_KEYUP:
            InvalidateRect(hWnd, NULL,1);
            return 0;

        case WM_DESTROY:
            InvalidateRect(hWnd, NULL,1);
            PostQuitMessage(0);
            return 0;

        default:
            return  DefWindowProc(hWnd,message,wParam,lParam);
        }
        return 0;
}

 

以上是关于Windows编程——API(伪)打砖块的主要内容,如果未能解决你的问题,请参考以下文章

原生javascript面向对象开发儿时经典打砖块小游戏

打砖块代码

GDI打砖块游戏

一百多行js代码实现打砖块小游戏

P1174 打砖块

803. 打砖块