草稿111

peekmessage(用于接受信息)

exmessage(专门用于鼠标操作)

#include<graphics.h>
#include<string>
#include<vector>
using namespace std;
const int WINDOW_WIDTH = 1280;
const int WINDOW_HEIGHT = 720;//封装了一个动画播放
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是图形的意思,图形列表
};//输出图片函数#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 Enemy
{
public:Enemy(){loadimage(&img_shadow, _T("img/shadow_enemy.png"));anim_left = new Animation(_T("img/enemy_left_%d.png"), 6, 45);anim_right = new Animation(_T("img/enemy_right_%d.png"), 6, 45);//生成敌人边界enum class SpawnEdge{Up = 0,Down,Left,Right};//将敌人放置在地图外边界出的随机位置SpawnEdge egde = (SpawnEdge)(rand() % 4);switch (egde){case SpawnEdge::Up:position.x = rand() % WINDOW_WIDTH;position.y = -FRAME_HEIGHT;break;case SpawnEdge::Down:position.x = rand() % WINDOW_WIDTH;position.y = WINDOW_HEIGHT;break;case SpawnEdge::Left:position.x = -FRAME_WIDTH;position.y = rand() % WINDOW_HEIGHT;break;case SpawnEdge::Right:position.x = WINDOW_WIDTH;position.y = rand() % WINDOW_HEIGHT;break;default:break;}}bool CheckBulletCollison(const Bullet& bullet){return false;}bool CheckPlayerCollison(const Player& player){return false;}void move(const Player& player){const POINT& player_position = player.GetPosition();int dir_x = player_position.x - position.x;int dir_y = player_position.y - position.y;double len_dir = sqrt(dir_x * dir_x + dir_y + dir_y);if (len_dir != 0){double normalize_x = dir_x / len_dir;double normalize_y = dir_y / len_dir;position.x += (int)(SPEED * normalize_x);position.y += (int)(SPEED * normalize_y);}if (dir_x < 0)facing_left = true;else if (dir_x > 0)facing_left = false;}void Draw(int delta){int pos_shadow_x = position.x + (FRAME_WIDTH / 2 - SHADOW_WIDTH / 2);int pos_shadow_y = position.y + FRAME_HEIGHT - 8;putimage_alpha(pos_shadow_x, pos_shadow_y, &img_shadow);if (facing_left)anim_left->play(position.x, position.y, delta);elseanim_right->play(position.x, position.y, delta);}~Enemy(){delete anim_left;delete anim_right;}private:const int SPEED = 2;const int FRAME_WIDTH = 80;const int FRAME_HEIGHT = 80;const int SHADOW_WIDTH = 48;bool is_move_right = false;bool is_move_left = false;
private:IMAGE img_shadow;Animation* anim_left;Animation* anim_right;POINT position = { 0, 0 };bool facing_left = false;
};//子弹类
class Bullet
{
public:POINT position = { 0, 0 };public:Bullet() = default;~Bullet() = default;void Draw() const{setlinecolor(RGB(255, 155, 50));setfillcolor(RGB(200, 75, 10));fillcircle(position.x, position.y, RADIUS);}private:const int RADIUS = 10;//圆的半径
};
//玩家类
class Player
{
public:Player(){loadimage(&img_shadow, _T("img/shadow_player.png"));anim_left = new Animation(_T("img/player_left_%d.png"), 6, 45);anim_right = new Animation(_T("img/player_right_%d.png"), 6, 45);}~Player(){delete anim_left;//释放空间delete anim_right;}void ProcessEvent(const ExMessage& msg)//处理玩家的操作信息{switch (msg.message){case 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;}break;case 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;}break;}}//处理玩家的移动void Move(){int dir_x = is_move_right - is_move_left;int dir_y = is_move_up - is_move_down;double len_dir = sqrt(dir_x * dir_x + dir_y + dir_y);if (len_dir != 0){double normalize_x = dir_x / len_dir;double normalize_y = dir_y / len_dir;position.x += (int)(SPEED * normalize_x);position.y += (int)(SPEED * normalize_y);}//防止玩家走出迷宫if (position.x < 0)position.x = 0;if (position.y < 0)position.y = 00;if (position.x + FRAME_WIDTH > WINDOW_WIDTH) position.x = WINDOW_WIDTH - FRAME_WIDTH;if (position.y + FRAME_HEIGHT > WINDOW_HEIGHT) position.y = WINDOW_HEIGHT - FRAME_HEIGHT;}//处理玩家的图形输出void Draw(int delta){int pos_shadow_x = position.x + (FRAME_WIDTH / 2 - SHADOW_WIDTH / 2);int pos_shadow_y = position.y + FRAME_HEIGHT - 8;putimage_alpha(pos_shadow_x, pos_shadow_y, &img_shadow);static bool facing_left = false;int dir_x = is_move_right - is_move_left;if (dir_x < 0)facing_left = true;else if (dir_x > 0)facing_left = false;if (facing_left)anim_left->play(position.x, position.y, delta);elseanim_right->play(position.x, position.y, delta);}private:const int SPEED = 3;const int FRAME_HEIGHT = 80;//玩家图片高度const int FRAME_WIDTH = 80;//玩家图片宽度const int SHADOW_WIDTH = 32;//阴影宽度
private:IMAGE img_shadow;Animation* anim_left;Animation* anim_right;POINT position = { 500,500 };//初始化玩家的位置bool is_move_up = false;bool is_move_down = false;bool is_move_left = false;bool is_move_right = false;
};
int main()
{initgraph(1280, 720);bool runing = true;Player player;ExMessage msg;IMAGE img_background;vector<Enemy*> enemy_list;loadimage(&img_background, _T("img/background.png"));BeginBatchDraw();while (runing){DWORD start_time = GetTickCount();while (peekmessage(&msg)){player.ProcessEvent(msg);}player.Move();cleardevice();putimage(0, 0, &img_background);player.Draw(1000 / 144);FlushBatchDraw();DWORD end_time = GetTickCount();DWORD delta_time = end_time - start_time;if (delta_time < 1000 / 144){Sleep(1000 / 144 - delta_time);}}bool is_move_up = false;bool is_move_down = false;bool is_move_left = false;bool is_move_right = false;return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/519144.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LoadRunner VS RunnerGo:主流性能测试工具对比谁更胜一筹?

LoadRunner作为性能测试工具的开拓者&#xff0c;测试人员应该都听过&#xff0c;可能也用过&#xff0c;相比较后起之秀Jmeter&#xff0c;使用场景更趋于企业级的性能测试&#xff0c;不太适合个人使用。 RunnerGo呢&#xff0c;是一款基于Go语言、国产自研的测试平台。它支…

NCDA设计大赛中设定画命题解读

一年一度的未来设计师全国高校数字艺术设计大赛&#xff08;NCDA&#xff09;正在如火如荼的进行中&#xff0c;各高校的大学生和指导老师们也都在着手准备中。今天我们就特地来说说它的数字绘画命题之一的设定画选项&#xff0c;为了使大家更好地参加本次比赛&#xff0c;本文…

CTP-API开发系列之三:柜台系统简介

CTP-API开发系列之三&#xff1a;柜台系统简介 CTP-API开发系列之三&#xff1a;柜台系统简介中国金融市场结构---交易所柜台系统通用柜台系统极速柜台系统主席与次席 CTP柜台系统CTP组件名称对照表CTP柜台系统程序包CTP柜台系统架构图 CTP-API开发系列之三&#xff1a;柜台系统…

2.5K Star,打造个性化博客平台

2.5K Star&#xff0c;打造个性化博客平台 Hi&#xff0c;骚年&#xff0c;我是大 G&#xff0c;公众号「GitHub 指北」会推荐 GitHub 上有趣有用的项目&#xff0c;一分钟 get 一个优秀的开源项目&#xff0c;挖掘开源的价值&#xff0c;欢迎关注。 导语 在当今的信息时代&a…

3.7练习题解

一共五道题&#xff1a; 1. PERKET&#xff1a; 观察容易发现n的值很小&#xff0c;所以我们可以考虑使用dfs的方法进行解答&#xff0c;首先我们可以考虑一共有n种配料&#xff0c;那么我们就可以考虑到可以选择1到n种配料数目&#xff0c;然后基于这个思路我们再对其进行判断…

任务调度新境界:探秘ScheduledExecutorService的异步魔力

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 任务调度新境界&#xff1a;探秘ScheduledExecutorService的异步魔力 前言ScheduledExecutorService的基本概念基本概念&#xff1a;为何它是 Java 中任务调度的首选工具&#xff1a;基本用法&#xf…

MySQL-查询SQL语句的执行过程:连接器->查询缓存(8就没了)->分析器->优化器->执行器->返回结果

MySQL-查询SQL语句的执行过程&#xff1a;连接器->查询缓存<8就没了>->分析器->优化器->执行器->返回结果 查询SQL语句的执行过程1、主要步骤2、实用案例 查询SQL语句的执行过程 1、主要步骤 在MySQL中&#xff0c;一条查询SQL语句的执行过程非常复杂且…

day14_异常

今日内容 零、 复习昨日 一、日期类 二、异常 零、 复习昨日 1为什么要重写toString Object类toString返回的是对象名字地址,无意义子类重写toString() 返回的对象属性内容 2为什么要重写equals Object类equals判断是对象的地址值是否相等,无意义子类重写equals,为了判断对象的…

如何写一份简单的产品说明书,教程奉上

如果你是一位新晋产品经理&#xff0c;或者正在研发新产品&#xff0c;并且心中惴惴不安因为未知的产品说明书制作环节&#xff0c;那么今天你就来对地方了。本篇文章将教你如何创建一份简单明了的产品说明书。让我们开始吧&#xff01; 首先&#xff0c;明确产品说明书的目标。…

Leetcoder Day42| 动态规划part09 打家劫舍问题

198.打家劫舍 你是一个专业的小偷&#xff0c;计划偷窃沿街的房屋。每间房内都藏有一定的现金&#xff0c;影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统&#xff0c;如果两间相邻的房屋在同一晚上被小偷闯入&#xff0c;系统会自动报警。 给定一个代表每个房…

从新手到大师:顶级PPT学习网站推荐,让你的幻灯片脱颖而出!

介绍&#xff1a;PowerPoint&#xff0c;简称PPT&#xff0c;是微软公司开发的一款演示程序&#xff0c;也是Microsoft Office套件的重要组成部分之一。 PPT允许用户通过幻灯片的形式创建和展示信息&#xff0c;这些幻灯片可以包含文本、图形、图表、视频、音频等多种元素。用户…

Linux-网络-011

1网络协议模型 1.1【OSI】协议模型 1.1.1应用层 实际发送的数据应用层:HTTP 超文本传输协议HTTPS FTP 文件传输协议TFTP 简单文本传输协议SMTP 邮件传输协议MQTT TELNET ..1.1.2表示层 发送的数据是否加密1.1.3会话层 是否建立会话连接1.1.4传输层 数据…