| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
Reworld,下班在家制作游戏,1500万奖金等你拿以码会友 以友辅仁
共有 974 人关注过本帖
标题:游戏小demo
只看楼主 加入收藏
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:1335
专家分:1179
注 册:2006-12-17
结帖率:100%
  已结贴   问题点数:20  回复次数:9   
游戏小demo
#include <windows.h>
#include <tchar.h>

#pragma comment(lib,"winmm.lib")
#pragma  comment(lib,"Msimg32.lib")

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"[游戏]程序核心框架"

struct SwordBullets
{
    int x,y;
    bool exist;
};

HDC g_hdc=NULL,g_mdc=NULL,g_bufdc=NULL;
HBITMAP g_hSwordMan=NULL,g_hSwordBlade=NULL,g_hBackGround=NULL;
DWORD g_tPre=0,g_tNow=0;
int g_iX=0,g_iY=0,g_iXnow=0,g_iYnow=0;
int g_iBGOffset=0,g_iBulletNum=0;
SwordBullets Bullet[30];

LRESULT CALLBACK    WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
BOOL Game_Init(HWND hwnd);   
VOID Game_Paint( HWND hwnd);   
BOOL Game_CleanUp(HWND hwnd );

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
{
    WNDCLASSEX WNDCLASS={0};
    WNDCLASS.cbClsExtra=0;
    WNDCLASS.cbSize=sizeof(WNDCLASSEX);
    WNDCLASS.cbWndExtra=0;
    WNDCLASS.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
    WNDCLASS.hCursor=LoadCursor(NULL,IDC_ARROW);
    WNDCLASS.hIcon=(HICON)::LoadImage(NULL,L"icon.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
    WNDCLASS.hInstance=hInstance;
    WNDCLASS.lpfnWndProc=WndProc;
    WNDCLASS.lpszClassName=L"ForTheDreamOfGameDevelop";
    WNDCLASS.lpszMenuName=NULL;
    WNDCLASS.style=CS_HREDRAW|CS_VREDRAW;

    if( !RegisterClassEx(&WNDCLASS) )
        return -1;

    HWND hwnd=CreateWindow(L"ForTheDreamOfGameDevelop",WINDOW_TITLE,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);

    MoveWindow(hwnd,250,80,WINDOW_WIDTH,WINDOW_HEIGHT,true);
    ShowWindow(hwnd,nShowCmd);
    UpdateWindow(hwnd);

    if(!Game_Init(hwnd))
    {
        MessageBox(hwnd,L"资源初始化失败",L"消息窗口",0);
        return FALSE;
    }

    PlaySound(L"仙剑三·原版战斗3.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);

    MSG msg={0};
    while(msg.message!=WM_QUIT)
    {
        if(PeekMessage(&msg,0,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            g_tNow=GetTickCount();
            if(g_tNow-g_tPre>=5)
                Game_Paint(hwnd);
        }
    }

    UnregisterClass(L"ForTheDreamOfGameDevelop",WNDCLASS.hInstance);
    return 0;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch(message)
    {
    case WM_KEYDOWN:
        switch(wParam)
        {
        case VK_ESCAPE:
            DestroyWindow(hwnd);
            PostQuitMessage(0);
            break;
        }   
        break;

    case WM_LBUTTONDOWN:
        for(int i=0;i<30;i++)
        {
            if(!Bullet[i].exist)
            {
                Bullet[i].x=g_iXnow;
                Bullet[i].y=g_iYnow+30;
                Bullet[i].exist=true;
                g_iBulletNum++;
                break;
            }
        }

    case WM_MOUSEMOVE:
        g_iX=LOWORD(lParam);
        if(g_iX>WINDOW_WIDTH-317)
            g_iX=WINDOW_WIDTH-317;
        else if(g_iX<0)
            g_iX=0;
        g_iY=HIWORD(lParam);
        if(g_iY>WINDOW_HEIGHT-283)
            g_iY=WINDOW_HEIGHT-283;
        else if(g_iY<-200)
            g_iY=-200;
        break;

    case WM_DESTROY:
        Game_CleanUp(hwnd);
        PostQuitMessage(0);
        break;

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


BOOL Game_Init(HWND hwnd)
{
    HBITMAP bmp;

    g_hdc=GetDC(hwnd);
    g_mdc=CreateCompatibleDC(g_hdc);
    g_bufdc=CreateCompatibleDC(g_hdc);
    bmp=CreateCompatibleBitmap(g_hdc,WINDOW_WIDTH,WINDOW_HEIGHT);

    g_iX=300;
    g_iY=100;
    g_iXnow=300;
    g_iYnow=100;

    SelectObject(g_mdc,bmp);
    g_hSwordMan=(HBITMAP)LoadImage(NULL,L"swordman.bmp",IMAGE_BITMAP,317,283,LR_LOADFROMFILE);
    g_hSwordBlade=(HBITMAP)LoadImage(NULL,L"swordblade.bmp",IMAGE_BITMAP,100,26,LR_LOADFROMFILE);
    g_hBackGround=(HBITMAP)LoadImage(NULL,L"bg.bmp",IMAGE_BITMAP,WINDOW_WIDTH,WINDOW_HEIGHT,LR_LOADFROMFILE);

    POINT pt,lt,rb;
    RECT rect;

    pt.x=300;
    pt.y=100;
    ClientToScreen(hwnd,&pt);
    SetCursorPos(pt.x,pt.y);

    ShowCursor(false);

    GetClientRect(hwnd,&rect);
    lt.x=rect.left;
    lt.y=rect.top;
    rb.x=rect.right;
    rb.y=rect.bottom;
    ClientToScreen(hwnd,&lt);
    ClientToScreen(hwnd,&rb);
    rect.left=lt.x;
    rect.top=lt.y;
    rect.right=rb.x;
    rect.bottom=rb.y;
    ClipCursor(&rect);

    Game_Paint(hwnd);
    return TRUE;
}

VOID Game_Paint(HWND hwnd)
{
    SelectObject(g_bufdc,g_hBackGround);
    BitBlt(g_mdc,0,0,g_iBGOffset,WINDOW_HEIGHT,g_bufdc,WINDOW_WIDTH-g_iBGOffset,0,SRCCOPY);
    BitBlt(g_mdc,g_iBGOffset,0,WINDOW_WIDTH-g_iBGOffset,WINDOW_HEIGHT,g_bufdc,0,0,SRCCOPY);

    wchar_t str[20]={};

    if(g_iXnow<g_iX)
    {
        g_iXnow+=10;
        if(g_iXnow>g_iX)
            g_iXnow=g_iX;
    }
    else
    {
        g_iXnow-=10;
        if(g_iXnow<g_iX)
            g_iXnow=g_iX;
    }

    if(g_iYnow<g_iY)
    {
        g_iYnow+=10;
        if(g_iYnow>g_iY)
            g_iYnow=g_iY;
    }
    else
    {
        g_iYnow-=10;
        if(g_iYnow<g_iY)
            g_iYnow=g_iY;
    }

    SelectObject(g_bufdc,g_hSwordMan);
    TransparentBlt(g_mdc,g_iXnow,g_iYnow,317,283,g_bufdc,0,0,317,283,RGB(0,0,0));

    SelectObject(g_bufdc,g_hSwordBlade);
    if(g_iBulletNum!=0)
        for(int i=0;i<30;i++)
            if(Bullet[i].exist)
            {
                TransparentBlt(g_mdc,Bullet[i].x-70,Bullet[i].y+100,100,33,g_bufdc,0,0,100,26,RGB(0,0,0));

                Bullet[i].x-=10;
                if(Bullet[i].x<0)
                {
                    g_iBulletNum--;
                    Bullet[i].exist=false;
                }
            }

            HFONT hFont;
            hFont=CreateFont(20,0,0,0,0,0,0,0,GB2312_CHARSET,0,0,0,0,TEXT("微软雅黑"));
            SelectObject(g_mdc,hFont);
            SetBkMode(g_mdc,TRANSPARENT);
            SetTextColor(g_mdc,RGB(255,255,0));

            swprintf_s(str,L"鼠标X坐标为%d ",g_iX);
            TextOut(g_mdc,0,0,str,wcslen(str));
            swprintf_s(str,L"鼠标Y坐标为%d ",g_iY);
            TextOut(g_mdc,0,20,str,wcslen(str));

            BitBlt(g_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,g_mdc,0,0,SRCCOPY);

            g_tPre=GetTickCount();

            g_iBGOffset+=5;
            if(g_iBGOffset==WINDOW_WIDTH)
                g_iBGOffset=0;
}

BOOL Game_CleanUp(HWND hwnd)
{
    DeleteObject(g_hBackGround);
    DeleteDC(g_bufdc);
    DeleteDC(g_mdc);
    ReleaseDC(hwnd,g_hdc);
    return TRUE;
}

照着书打的,看看怎么样

附件: 您没有浏览附件的权限,请 登录注册
搜索更多相关主题的帖子: str int NULL HWND return 
2019-06-14 17:19
无涯汪洋
Rank: 2
等 级:论坛游民
帖 子:15
专家分:34
注 册:2019-3-30
  得分:20 
版主,为啥直接复制粘贴后在运行出错
附件: 您没有浏览附件的权限,请 登录注册
2019-06-15 17:01
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:1335
专家分:1179
注 册:2006-12-17
  得分:0 
回复 2楼 无涯汪洋
用vs2010

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2019-06-15 17:22
无涯汪洋
Rank: 2
等 级:论坛游民
帖 子:15
专家分:34
注 册:2019-3-30
  得分:0 
嗯,谢谢!
2019-06-16 22:37
大大同学
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2019-7-2
  得分:0 
顶一下
2019-07-02 12:11
fighting910
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-6-12
  得分:0 
版主看的哪本书呀
2019-07-25 22:44
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:1335
专家分:1179
注 册:2006-12-17
  得分:0 
windows游戏编程之从零开始

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2019-07-26 12:15
spiritualman
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-8-2
  得分:0 
回复 2楼 无涯汪洋
编译器版本低了
建议下载2013以上的版本
2019-08-02 21:41
Drunk
Rank: 1
等 级:新手上路
帖 子:8
专家分:5
注 册:2019-8-14
  得分:0 
顶一下
2019-08-17 12:29
lap27510
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-9-7
  得分:0 
学习学习了
2019-09-13 15:18
快速回复:游戏小demo
数据加载中...
 
   





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

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