zoukankan      html  css  js  c++  java
  • WIN32打网球

    //头文件header.h

    #ifndef _HEADER
    #define _HEADER
    #include <windows.h>
    #include <stdio.h>
    #define KEYDOWN(vk_code)((GetAsyncKeyState(vk_code)& 0x8000)? 1: 0)
    #define KEYUP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000)? 0: 1)
    
    HPEN white_pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
    HPEN black_pen = CreatePen(PS_SOLID, 1, RGB(100, 100, 100));
    
    HBRUSH yellow_brush= CreateSolidBrush(RGB(255, 255, 0));
    HBRUSH black_brush= CreateSolidBrush(RGB(100, 100, 100));
    HBRUSH white_brush= CreateHatchBrush(HS_CROSS, RGB(255, 255, 255));
    HBRUSH bk_brush = CreateSolidBrush(RGB(137, 156, 205));
    HBRUSH arnd_brush = CreateSolidBrush(RGB(66, 143, 165));
    HBRUSH field_brush = CreateSolidBrush(RGB(125, 177, 104));
    
    POINT NPCPOINT[4] = {
        {120,120} ,
        {650,120} ,
        {650,200} ,
        {120,200} };
    
    POINT MANPOINT[4] = {
        {600,400} ,
        {640,400} ,
        {640,480} ,
        {600,480} };
    
    POINT NETPOINT[17] = {
        {125,250} ,
        {128,250} ,
        {135,250} ,
        {140,251} ,
        {180,252} ,
        {200,255} ,
        {300,255} ,
        {550,252} ,
        {672,251} ,
        {675,250} ,
        {675,300} ,
        {600,296} ,
        {540,298} ,
        {500,295} ,
        {300,297} ,
        {175,300} ,
        {125,300} };
    
    POINT BKPOINT[6] = { 
        {0   , 0} ,
        {800 , 0} ,
        {800,450} ,
        {630,130} ,
        {170,130} ,
        {0  ,450} };
    
    POINT AROUNDPOINT[6] = {
        {0  ,450} ,
        {170,130} ,
        {630,130} ,
        {800,450} ,
        {800,600} ,
        {0  ,600} };
    
    POINT FIELDPOINT[4] = {
        {200,150} ,
        {600,150} ,
        {775,550} ,
        { 25,550} };
    
    #endif

    //主程序

    #include "header.h"
    
    long FAR PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {    
        switch(message)
        {
        case WM_CREATE:
            break;
        case WM_CLOSE: //从右上角关闭触发
            {
                int result=MessageBox(hWnd, L"Are you sure to Quit?", L"alert",
                    MB_YESNO|MB_ICONQUESTION);
                if(result == IDYES)
                {
                    return DefWindowProc(hWnd,message,wParam,lParam);
                }
                else
                {
                    return 0;
                }
            }break;
        case WM_DESTROY:  //销毁窗口
            PostQuitMessage(0);
            return 0;
        case WM_KEYDOWN:   //按键信息 
            switch(wParam)
            {
            case VK_ESCAPE:
                PostMessage(hWnd, WM_QUIT, 0, 0);
                break;
            }
            break;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);// 返回给windows 处理//默认事件
    }
    
    
    //画背景
    void DrawBkground(HWND hWnd)
    {
        HDC hdc;
        hdc = GetDC(hWnd);
        SelectObject(hdc, black_pen);
        SelectObject(hdc, bk_brush);
        Polygon(hdc,BKPOINT,6);    
        SelectObject(hdc, black_pen);
        SelectObject(hdc, bk_brush);
        ReleaseDC(hWnd, hdc);
    }
    //画场地围边
    void DrawAround(HWND hWnd)
    {
        HDC hdc;
        hdc = GetDC(hWnd);
        SelectObject(hdc, black_pen);
        SelectObject(hdc, arnd_brush);
    
        Polygon(hdc,AROUNDPOINT,6);    
    
        SelectObject(hdc, black_pen);
        SelectObject(hdc, arnd_brush);
        ReleaseDC(hWnd, hdc);
    }
    //画场地
    void DrawField(HWND hWnd)
    {
        HDC hdc;
        hdc = GetDC(hWnd);
    
        SelectObject(hdc, black_pen);
        SelectObject(hdc, field_brush);
    
        Polygon(hdc,FIELDPOINT,4);    
    
        MoveToEx(hdc,400,150,NULL);
        LineTo(hdc, 400, 550);
        MoveToEx(hdc, 130, 300, NULL);
        LineTo(hdc, 665, 300);
        MoveToEx(hdc, 24, 551, NULL);
        LineTo(hdc, 775, 551);
    
        SelectObject(hdc, black_pen);
        SelectObject(hdc, field_brush);
        ReleaseDC(hWnd, hdc);
    }
    
    //画球网
    void DrawNet(HWND hWnd)
    {
        HDC hdc;
        hdc = GetDC(hWnd);
    
        SelectObject(hdc, white_pen);
        SelectObject(hdc, white_brush);
        SetBkMode(hdc, TRANSPARENT);
    
        Polygon(hdc,NETPOINT,17);    
    
        SelectObject(hdc, white_pen);
        SelectObject(hdc, white_brush);
        ReleaseDC(hWnd, hdc);
    }
    void DrawNPC(HWND hWnd)
    {
        HDC hdc;
        hdc = GetDC(hWnd);
    
        SelectObject(hdc,white_pen);
        SelectObject(hdc,black_brush);
    
        Polygon(hdc, NPCPOINT,4);
    
        SelectObject(hdc,white_pen);
        SelectObject(hdc,black_brush);
        ReleaseDC(hWnd, hdc);
    }
    
    void DrawMan(HWND hWnd)
    {
        HDC hdc;
        hdc = GetDC(hWnd);
    
        SelectObject(hdc,white_pen);
        SelectObject(hdc,black_brush);
    
        Polygon(hdc, MANPOINT,4);
    
        SelectObject(hdc,white_pen);
        SelectObject(hdc,black_brush);
        ReleaseDC(hWnd, hdc);
    }
    // win32主函数 
    int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmLine, int nCmdShow)
    {
        MSG msg;        //定义一个消息的对象
        HWND hWnd;      //窗口句柄
        WNDCLASS wc; //定义窗口类
        HDC hdc;
    
        //创建和设置窗口类
        wc.style  = CS_HREDRAW| CS_VREDRAW; //支持水平和垂直重绘
        wc.lpfnWndProc = WindowProc;  //定义相应信息的处理函数
        wc.cbClsExtra =0;           //附加内存空间
        wc.cbWndExtra =0;           //附加内存空间
        wc.hInstance =hInstance;  //窗口的实例化句柄
        wc.hIcon =;     //窗口的图标
        wc.hCursor =NULL;   //设置窗口鼠标的形状
        wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); //背景刷及背景颜色
        wc.lpszMenuName =NULL; //窗口是否有菜单
        wc.lpszClassName =L"create window"; //窗口的类名称 全文必须一致
        ////
        RegisterClass(&wc); //注册窗口句柄
        hWnd =CreateWindowEx(WS_EX_TOPMOST, //窗口总显示在顶部
            L"create window", //窗口类名
            L"Gary", //窗口的标题
            WS_OVERLAPPEDWINDOW, //窗口的风格
            0, // 窗口左上角的x位置
            0, // 窗口左上角的y位置
            800,//GetSystemMetrics(SM_CXSCREEN), 宽度初始化 
            600,//GetSystemMetrics(SM_CYSCREEN), 高度初始化 
            NULL,  //父窗口句柄
            NULL,  //窗口菜单句柄
            hInstance, //窗口实例句柄
            NULL);  //附加信息
        if(! hWnd) //判断窗口是否建立成功
        {
            return FALSE;
        }
    
        ShowWindow(hWnd, nCmdShow); //显示窗口
        UpdateWindow(hWnd); //更新窗口
        //    ShowCursor(FALSE);
        hdc= GetDC(hWnd);
    
        char buffer[80];
        const short INITHEIGHT = 85;
        short ball_x=600;
        short ball_y=420;
        short ball_height=INITHEIGHT;
        short ball_size=20;
        short ball_xv=-2;
        short ball_yv=2;
        short count=0; //落地次数
        short ball_speed=1; //球弹跳速度
        bool direct = true; // 方向标志,true为向下落,false为向上升
        short bat_x=600;
        short bat_y=600;
        short strength=80;//击球力度,值越大,球平行飞行的距离越长 
        bool alive = true;
    
        srand(GetTickCount());
    
        while(alive)
        {
            DWORD start_time = GetTickCount();
            //一般来说,GetMessage 被设计用于高效的从消息队列中获取消息,
            //如果队列中没有消息,那么导致线程处于睡眠状态,而PeekMessage不会导致睡眠
            //它马上返回0;
            if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
                //接收系统信息(&msg 为MSG 类型的信息结构体,NULL是窗口句柄, 
                //0,0表示接受所有窗口的消息
            {
                if(msg.message == WM_QUIT)
                    break;
                TranslateMessage(&msg); //转换信息
                DispatchMessage(&msg);  //调用回调函数
            }
            else
            {
                if(KEYDOWN(VK_LEFT) &&(MANPOINT[0].x > 20) )
                {
                    MANPOINT[0].x -= 12;
                    MANPOINT[1].x -= 12;
                    MANPOINT[2].x -= 12;
                    MANPOINT[3].x -= 12;
                }
    
                if(KEYDOWN(VK_RIGHT) &&(MANPOINT[0].x < 740) )
                {
                    MANPOINT[0].x += 12;
                    MANPOINT[1].x += 12;
                    MANPOINT[2].x += 12;
                    MANPOINT[3].x += 12;
                }
    
                if(KEYDOWN(VK_UP) &&(MANPOINT[0].y > 350) )
                {
                    MANPOINT[0].y -= 12;
                    MANPOINT[1].y -= 12;
                    MANPOINT[2].y -= 12;
                    MANPOINT[3].y -= 12;
                }
    
                if(KEYDOWN(VK_DOWN) &&(MANPOINT[0].y < 470) )
                {
                    MANPOINT[0].y += 12;
                    MANPOINT[1].y += 12;
                    MANPOINT[2].y += 12;
                    MANPOINT[3].y += 12;
                }
                //渲染部分            
                DrawBkground(hWnd);
                DrawAround(hWnd);
                DrawField(hWnd);
                DrawNPC(hWnd);
    
                if(ball_y >=270)
                {
                    DrawNet(hWnd);
                    ball_size = 20;
                }
    
                SelectObject(hdc, black_pen);            
                //画影子
                SelectObject(hdc, black_brush);
                Ellipse(hdc, ball_x, ball_y+ball_height, 
                    ball_x+ball_size-3, ball_y+ball_height+ball_size-5);
                //画球
                SelectObject(hdc, yellow_brush);
                Ellipse(hdc,ball_x,ball_y,ball_x+ball_size,ball_y+ball_size);
                SelectObject(hdc, black_pen);
                SelectObject(hdc, yellow_brush);
                if(ball_y <270)
                {
                    DrawNet(hWnd);
                    ball_size = 18;
                }
    
                DrawMan(hWnd);
    
                ball_x += ball_xv;
                ball_y += ball_yv;        
                if(direct)
                {    
                    strength -=3;
                    if( strength <=0)
                    {
                        ball_height -= ball_speed++;
                        strength=0;
                    }
    
                    if(ball_height <= 0)
                    {
                        ball_height=0;
                        count++;
                        if(ball_xv<0)
                        {
                            ball_xv++;    
                        }
                        else
                        {
                            ball_xv--;
                        }
    
                        if(ball_yv<0)
                        {
                            ball_yv++;    
                        }
                        else
                        {
                            ball_yv--;
                        }
    
                        direct = !direct;
    
                    }
                }
                else
                {
                    ball_speed -= 2;
                    ball_height += ball_speed;
    
                    if(ball_speed <= 0)
                    {
                        direct = !direct; 
                    }
    
                }
                if(count>=2)
                {
                    alive = false;
                    MessageBox(hWnd, L"Game Over!", L"message:", MB_OK);
                }
    
                //画球结束
    
                //碰撞检测
                if(ball_x > MANPOINT[0].x-10 
                    && ball_x < MANPOINT[1].x 
                    && ball_y > MANPOINT[0].y-10
                    && ball_y < MANPOINT[3].y
                    && ball_yv> 0 )
                {
                    strength = 80+ rand()%10;
                    ball_height = INITHEIGHT;
    
                    if(rand()%2==0)
                    {                    
                        ball_xv = -1*rand()%6;
                    }
                    else
                    {
                        ball_xv = rand()%3;
                    }
    
                    ball_yv = -1*(rand()%8 + 4);
                    count = 0;
                }
                else
                {
                    if( ball_x > NPCPOINT[0].x-10 
                        && ball_x < NPCPOINT[1].x
                        && ball_y > NPCPOINT[0].y-10
                        && ball_y < NPCPOINT[3].y
                        && ball_yv< 0)
                    {
                        strength = 50+rand()%40;
                        ball_height = INITHEIGHT;
                        if(rand()%2==0)
                        {
                            ball_xv = -1*rand()%3;
                        }
                        else
                        {
                            ball_xv = rand()%3;
                        }
    
                        ball_yv = rand()%8 + 4;
                        count = 0;
                    }
                }
    
                while(GetTickCount()- start_time < 43);
            }
        }
        DeleteObject(white_pen);
        DeleteObject(black_pen);
        DeleteObject(yellow_brush);
        DeleteObject(black_brush);
        DeleteObject(white_brush);
        DeleteObject(bk_brush);
        DeleteObject(arnd_brush);
        DeleteObject(field_brush);
    
        ReleaseDC(hWnd, hdc);
        UnregisterClass(L"create window", wc.hInstance);
        //注销窗口
        return 1;
    }

    其中我自己添加了一个“刀.ico”图标,

    不添加直接赋值为空 wc.hIcon = NULL;

  • 相关阅读:
    JAVA实现微信支付功能
    avue设置表格显示图片
    职工管理系统----删除职工
    职工管理系统---显示职工
    职工管理系统---读文件
    职工管理系统---写文件
    职工管理系统-------添加职工
    职工管理系统-----实现职工类
    职工管理系统-------实现退出功能
    职工管理系统-------菜单功能
  • 原文地址:https://www.cnblogs.com/WinonaJia/p/8670785.html
Copyright © 2011-2022 走看看