2023/3/7的草稿,怕数据丢失
#include<graphics.h>
#include<iostream>
#include<string>
#include<vector>//使用这个为图片动态的分配内存
using namespace std;//减少透明度的,用于输出png文件
#pragma comment(lib, "MSIMG32.LIB")
inline void putimage_alpha(int x, int y, IMAGE* img)
{int w = img->getheight();int h = img->getwidth();AlphaBlend(GetImageHDC(NULL), x, y, w, h,GetImageHDC(img), 0, 0, w, h, { AC_SRC_OVER,0,225,AC_SRC_ALPHA });
}//封装了一个动画播放
class Animation
{
public:Animation(LPCTSTR path, int num, int interval)//图片所来源的路径,图片的数量,图片播放之间的间隔 LP的含义使长指针, C的含义是const ,{interval_ms = interval;TCHAR path_file[256];//TCHAR就是相当于根据我们系统在自己分配的字符串类型 这个自定义的含义是道路的文件for (int i = 0; i < num; i++)//通过一个for循环将所有我们下载的图片都加载到一个图片数组中{_stprintf_s(path_file, path, i);//将文件名字打印在loadimage中IMAGE* frame = new IMAGE();//定义一个图片指针 并且开辟这个图片指针loadimage(frame, path_file);//加载这个图片frame_list.push_back(frame);//有点类似于出队,或者出栈,就是数组向后移的意思}}~Animation()//析构函数{for (int i = 0; i < frame_list.size(); i++)delete frame_list[i];}void play(int x, int y, int delta)//动画播放{timer += delta;if (timer >= interval_ms){idx_frame = (idx_frame + 1) % frame_list.size();timer = 0;}putimage_alpha(x, y, frame_list[idx_frame]);}
private:int timer = 0; //动画计时器int idx_frame = 0; //动画帧索引int interval_ms = 0;vector<IMAGE*> frame_list;//frame是图形的意思,图形列表
};//当前的动
int idx_current_anim = 0;//表示播放的总数 anim代表动的意思
const int PLAYER_ANIM_NUM = 6;//玩家图片的数据
const int PLATER_WIDTH = 80;
const int PLAYER_HEIGHT = 80;
const int SHADOW_WIDTH = 32;//窗口大小
const int WINDOW_WIDTH = 1280;
const int WINDOW_HIGHT = 720;//用来存放玩家动画的图片数组
IMAGE img_player_left[PLAYER_ANIM_NUM];
IMAGE img_player_right[PLAYER_ANIM_NUM];//放玩家的初始位置
POINT player_pos = { 500,500 };//POINT就使相当于一个点const int PLAYER_SPEED = 3;//加载玩家动画函数
//void loadAnimation()
//{
// for (int 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 (int 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());
// }
//}//绘制玩家动画函数,所以我觉得这个ainm有绘制的意思
Animation anim_left_player(_T("img/player_left_%d.png"), 6, 45);
Animation anim_right_player(_T("img/player_right_%d.png"), 6, 45);IMAGE img_shadow;//绘制玩家
void drawplayer(int delta,int dir_x)
{int pos_shadow_x = player_pos.x + (PLATER_WIDTH / 2 - SHADOW_WIDTH / 2);int pos_shadow_y = player_pos.y + PLAYER_HEIGHT - 8;putimage_alpha(pos_shadow_x, pos_shadow_y, &img_shadow);static bool facing_left = false;if (dir_x < 0)facing_left = true;else if (dir_x > 0)facing_left = false;if (facing_left)anim_left_player.play(player_pos.x, player_pos.y, delta);elseanim_right_player.play(player_pos.x, player_pos.y, delta);
}
//主函数
int main()
{initgraph(WINDOW_WIDTH, WINDOW_HIGHT);bool runing = true;//void loadAnimation();ExMessage msg;//获取数据IMAGE img_background;loadimage(&img_background, _T("img/background.png"));loadimage(&img_shadow, _T("img/shadow_player.png"));bool is_move_up = false;bool is_move_down = false;bool is_move_right = false;bool is_move_left = false;BeginBatchDraw();//加载玩家的图片//loadAnimation();while (runing){DWORD s_t = 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_RIGHT:is_move_right = true;break;case VK_LEFT:is_move_left = 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_RIGHT:is_move_right = false;break;case VK_LEFT:is_move_left = false;break;}}}if (is_move_up) player_pos.y -= PLAYER_SPEED;if (is_move_down)player_pos.y += PLAYER_SPEED;if (is_move_right)player_pos.x += PLAYER_SPEED;if (is_move_left)player_pos.x -= PLAYER_SPEED;static int counter = 0;if (++counter % 5 == 0)//每5个游戏帧播放一个动画帧{idx_current_anim++;}//使动画循环播放idx_current_anim %= PLAYER_ANIM_NUM;int dir_x = is_move_right - is_move_left;int dir_y = is_move_down - is_move_up;double len_dir = sqrt(dir_x * dir_x + dir_y * dir_y);if (len_dir != 0){double normalized_x = dir_x / len_dir;double normalized_y = dir_y / len_dir;player_pos.x += (int)(PLAYER_SPEED * normalized_x);player_pos.y += (int)(PLAYER_SPEED * normalized_y);}if (player_pos.x < 0)player_pos.x = 0;if (player_pos.y < 0)player_pos.y = 0;if(player_pos.x + PLATER_WIDTH > WINDOW_WIDTH)player_pos.x = WINDOW_WIDTH - PLATER_WIDTH;if (player_pos.y + PLAYER_HEIGHT > WINDOW_HIGHT)player_pos.y = WINDOW_HIGHT - PLAYER_HEIGHT;cleardevice();putimage(0, 0, &img_background);//putimage_alpha(player_pos.x, player_pos.y, &img_player_left[idx_current_anim]);drawplayer(1000 / 400, is_move_right - is_move_left);//优化过程,包括缓冲区,减少cpu占存FlushBatchDraw();DWORD e_t = GetTickCount();DWORD d_t = s_t - e_t;if (d_t < 1000 / 144){Sleep(1000 / 144 - d_t);}}EndBatchDraw();return 0;
}