#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;
}