提瓦特幸存者1

#include <iostream>
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <string>

using namespace std;

int idx_current_anim = 0;
const int PLAYER_ANIM_NUM = 6;

IMAGE img_player_left[PLAYER_ANIM_NUM];
IMAGE img_player_right[PLAYER_ANIM_NUM];

POINT player_pos = { 500,500 };
int PLAYER_SPEED = 5;


#pragma comment(lib,"MSIMG32.LIB")
inline void putimage_alpha(int x, int y, IMAGE* img)//有透明度信息的图片打开
{
    int w = img->getwidth();
    int h = img->getheight();
    AlphaBlend(GetImageHDC(NULL), x, y, w, h, GetImageHDC(img), 0, 0, w, h, { AC_SRC_OVER,0,255,AC_SRC_ALPHA });
}


void LoadAnimation()
{
    for (size_t i = 0; i < PLAYER_ANIM_NUM; i++)
    {
        wstring path = L"img/player_left_" + to_wstring(i) + L".png";
        loadimage(&img_player_left[i], path.c_str());
    }
    for (size_t i = 0; i < PLAYER_ANIM_NUM; i++)
    {
        wstring path = L"img/player_right_" + to_wstring(i) + L".png";
        loadimage(&img_player_right[i], path.c_str());
    }
}

int _tmain(int argc, _TCHAR* argv[])//主函数
{
    initgraph(1280, 720);
    ExMessage msg;
    IMAGE img_background;

    bool is_move_up = false;
    bool is_move_down = false;
    bool is_move_right = false;
    bool is_move_left = false;

    LoadAnimation();
    loadimage(&img_background, _T("img/background.png"));

    bool running = true;

    BeginBatchDraw();//批量绘制
    while (running)
    {
        DWORD start_time = GetTickCount();//动态延时

        while (peekmessage(&msg))
        {
            if (msg.message == WM_KEYDOWN)
            {
                switch (msg.vkcode)
                {
                case VK_UP:
                    is_move_up = true;
                    break;
                case VK_DOWN:
                    is_move_down = true;
                    break;
                case VK_LEFT:
                    is_move_left = true;
                    break;
                case VK_RIGHT:
                    is_move_right = true;
                    break;
                }
            }
            else if (msg.message == WM_KEYUP)
            {
                switch (msg.vkcode)
                {
                case VK_UP:
                    is_move_up = false;
                    break;
                case VK_DOWN:
                    is_move_down = false;
                    break;
                case VK_LEFT:
                    is_move_left = false;
                    break;
                case VK_RIGHT:
                    is_move_right = false;
                    break;
                }
            }

        }
        if (is_move_up) player_pos.y -= PLAYER_SPEED;
        if (is_move_down) player_pos.y += PLAYER_SPEED;
        if (is_move_left) player_pos.x -= PLAYER_SPEED;
        if (is_move_right) player_pos.x += PLAYER_SPEED;

        

        static int counter = 0;
        if (++counter % 5 == 0)
        {
            idx_current_anim++;
        }

        idx_current_anim = idx_current_anim % PLAYER_ANIM_NUM;//使动画循环播放


        cleardevice();

        putimage_alpha(0, 0, &img_background);
        putimage_alpha(player_pos.x, player_pos.y, &img_player_left[idx_current_anim]);

        FlushBatchDraw();

        DWORD end_time = GetTickCount();
        DWORD delta_time = end_time - start_time;
        if (delta_time < 1000 / 144)
        {
            Sleep(1000 / 144 - delta_time);
        }


    }
    EndBatchDraw();
    

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值