基于Windows API的小游戏制作

编程入门 行业动态 更新时间:2024-10-17 14:15:08

基于Windows API的<a href=https://www.elefans.com/category/jswz/34/1769974.html style=小游戏制作"/>

基于Windows API的小游戏制作

目标

使用Visual C++ 仿造植物大战僵尸的模式构造一个有基本布景,游戏消息处理,物理现象处理等基本功能的游戏。在完成基本功能的基础上实现游戏的娱乐性和可玩性,丰富游戏机制,完善游戏规则。

基本概念

由于C++程序编译后的文件是可直接运行的机器码,并非Java等语言编译后生成的中间码,所以VC++程序运行速度要较优越。Windows API是Windows操作系统提供的动态链接函数库(.DLL文件),包含了windows内核及所有应用程序所需要的功能。并且操作简单

具体操作实现

Windows API的程序架构,主程序文件"canvas.cpp",由以下几个函数组成: WinMain(主程序,程序起始点),WinProc(自定义函数,处理程序消息),MyRegisterClass(自定义函数,注册窗口类别),Init(自定义函数,初始化)

项目源代码如下

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>    //随机数种子用

#include "mmsystem.h"//导入声音头文件
#pragma comment(lib,"winmm.lib")//导入声音头文件库


struct BULLET                                                                //子弹结构
{
    int x,y;
    bool exist;
};
struct MONSTER                                                               //僵尸结构
{
    int x,y;
    int blood;
    bool exist;
};
struct STONE
{
    int x,y;
    bool exist;
};
HINSTANCE hInst;                                                            //各种变量初始化
HBITMAP dra[6],bg[2],pl[5],bullet,k[2],over,win,life[5],stone;
HDC        hdc,mdc,bufdc,buffdc;
HWND    hWnd;
DWORD    tPre,tNow;
int        x,y;
int     num,bcount=300;
int     count=0;
BULLET  b[300];
STONE    s;
MONSTER m[6];
int     q[5]={-50,20,110,200,300},qi=rand()%5,wi=rand()%5,ei=rand()%5,ri=rand()%5,ti=rand()%5;

ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
void                MyPaint(HDC hdc);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    MSG msg;

    MyRegisterClass(hInstance);

    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    while( msg.message!=WM_QUIT )
    {
        if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
        {
            tNow = GetTickCount();

            if(tNow-tPre >= 100)
                MyPaint(hdc);
        }
    }

    return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    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            = NULL;
    wcex.hCursor        = NULL;
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;
    wcex.lpszClassName    = "canvas";
    wcex.hIconSm        = NULL;

    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    char filename[20] = "";
    HBITMAP bmp;
    hInst = hInstance;

    hWnd = CreateWindow("canvas", "菜园保卫战" , WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

    MoveWindow(hWnd,10,10,640,480,true);
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    hdc = GetDC(hWnd);
    mdc = CreateCompatibleDC(hdc);
    bufdc = CreateCompatibleDC(hdc);
    buffdc = CreateCompatibleDC(hdc);
    bmp = CreateCompatibleBitmap(hdc,640,480);

    SelectObject(mdc,bmp);
    
    k[0]=(HBITMAP)LoadImage(NULL,"k0.bmp",IMAGE_BITMAP,700,150,LR_LOADFROMFILE);
    k[1]=(HBITMAP)LoadImage(NULL,"k1.bmp",IMAGE_BITMAP,700,150,LR_LOADFROMFILE);
    life[0]=(HBITMAP)LoadImage(NULL,"life1.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
    life[1]=(HBITMAP)LoadImage(NULL,"life2.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
    life[2]=(HBITMAP)LoadImage(NULL,"life3.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
    life[3]=(HBITMAP)LoadImage(NULL,"life4.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
    life[4]=(HBITMAP)LoadImage(NULL,"life5.bmp",IMAGE_BITMAP,310,60,LR_LOADFROMFILE);
    dra[0] = (HBITMAP)LoadImage(NULL,"dra1.bmp",IMAGE_BITMAP,800,300,LR_LOADFROMFILE);
    dra[1] = (HBITMAP)LoadImage(NULL,"dra2.bmp",I

更多推荐

基于Windows API的小游戏制作

本文发布于:2024-02-26 06:19:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1701585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小游戏   Windows   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!