幸存者游戏(类)

#include <iostream>

#include <graphics.h>

#include <stdio.h>

#include <conio.h>

#include <vector>

#include <string>

using namespace std;

int idx_player_anim = 0;

const int player_anim_num = 6;//这里要把动画帧数定位const int才能放入数组括号中,int不行

IMAGE img_player_left[player_anim_num];

IMAGE img_player_right[player_anim_num];

POINT player_pos = { 500,500 };

int player_speed = 10;

#pragma comment(lib,"MSIMG32.LIB")//putimage不能处理透明度,要弄个能处理透明度的putimage函数

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

}

class Animation

{

public:

Animation(LPCTSTR path, int num, int interval)//构造函数实现loadimage

{

interval_ms = interval;

TCHAR path_file[256];

for (size_t i = 0; i < num; i++)

{

_stprintf_s(path_file, path, i);

IMAGE* frame = new IMAGE();

loadimage(frame, path_file);

frame_list.push_back(frame);

}

}

~Animation()

{

for (size_t i = 0; i < frame_list.size(); i++)

delete frame_list[i];

}

void play(int x, int y, int delta)//实现putiamge

{

timer += delta;

if (timer > interval_ms)

{

idx_frame = (idx_frame + 1) % player_anim_num;

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;

};//别漏;

Animation anim_left_player(T("img/player_left%d.png"), 6, 45);

Animation anim_right_player(T("img/player_right%d.png"), 6, 45);

int main()

{

initgraph(1280, 720);

IMAGE img_background;

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

bool running = true;

bool is_up = false;

bool is_down = false;

bool is_left = false;

bool is_right = false;

bool turn_left = true;

bool turn_right = false;

ExMessage msg;

BeginBatchDraw();

while (running)

{

DWORD start_time = GetTickCount();

while (peekmessage(&msg))

{

if (msg.message == WM_KEYDOWN)

{

switch (msg.vkcode)

{

case VK_UP:

is_up = true;

break;

case VK_DOWN:

is_down = true;

break;

case VK_LEFT:

is_left = true;

turn_left = true;

turn_right = false;

break;

case VK_RIGHT:

is_right = true;

turn_left = false;

turn_right = true;

break;

}

}

if (msg.message == WM_KEYUP)

{

switch (msg.vkcode)

{

case VK_UP:

is_up = false;

break;

case VK_DOWN:

is_down = false;

break;

case VK_LEFT:

is_left = false;

break;

case VK_RIGHT:

is_right = false;

break;

}

}

}

if (is_up) player_pos.y -= player_speed;

if (is_down) player_pos.y += player_speed;

if (is_left) player_pos.x -= player_speed;

if (is_right) player_pos.x += player_speed;

cleardevice();

static int counter = 0;

if (++counter % 5 == 0)

idx_player_anim++;

idx_player_anim = idx_player_anim % player_anim_num;

putimage_alpha(0, 0, &img_background);

if(turn_left) anim_left_player.play(player_pos.x, player_pos.y, 10);

if(turn_right)anim_right_player.play(player_pos.x, player_pos.y, 10);

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;

}

相关推荐
2501_944424127 小时前
Flutter for OpenHarmony游戏集合App实战之贪吃蛇食物生成
android·开发语言·flutter·游戏·harmonyos
点云SLAM8 小时前
C++内存泄漏检测之Windows 专用工具(CRT Debug、Dr.Memory)和Linux 专业工具(ASan 、heaptrack)
linux·c++·windows·asan·dr.memory·c++内存泄漏检测·c++内存管理
LuiChun8 小时前
Docker Compose 容器服务查询与文件查看操作指南(Windows Docker Desktop 版)【一】
linux·运维·windows·docker·容器
无限进步_10 小时前
【C++】大数相加算法详解:从字符串加法到内存布局的思考
开发语言·c++·windows·git·算法·github·visual studio
lusananan10 小时前
Transformer为何一统天下?深度解析RNN、CNN的局限与注意力机制的崛起
人工智能·游戏
love530love11 小时前
彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错
人工智能·windows·python·whisper·win_comfyui
习惯就好zz11 小时前
[实战笔记] 从 Qt 5.12.9 跨越到 Qt 6.x 完美迁移指南 (Windows + VS)
windows·qt·msvc·qt5·qt6·迁移
2501_9444241211 小时前
Flutter for OpenHarmony游戏集合App实战之俄罗斯方块七种形状
android·开发语言·flutter·游戏·harmonyos
石像鬼₧魂石11 小时前
Windows Server 2003 域控制器靶机搭建与渗透环境配置手册
linux·windows·学习
抠头专注python环境配置12 小时前
解决“No module named ‘tensorflow‘”报错:从导入失败到环境配置成功
人工智能·windows·python·tensorflow·neo4j