当前位置: 首页 > news >正文

黄网网站 推荐最近新闻头条最新消息

黄网网站 推荐,最近新闻头条最新消息,郑州高端定制网站,WordPress是不是免费的前言: 根据前面的项目框架,搭建游戏的运行场景...... 1.0 框架预览 基于该框架首先实现游戏的运行场景 2.0 图片文件 创建图片文件,本次项目使用easyx作为图形库文件,在easyx中想要显示图片,需要有一张图片和图片的掩码…

前言:


根据前面的项目框架,搭建游戏的运行场景......


1.0 框架预览


基于该框架首先实现游戏的运行场景


2.0 图片文件


创建图片文件,本次项目使用easyx作为图形库文件,在easyx中想要显示图片,需要有一张图片和图片的掩码文件,创建image.h文件和image.cpp文件用于图片设置,具体创建效果如下所示。创建的头文件中做了函数的声明,包含函数的x,y轴的坐标和图片的地址,掩码图片的地址。


#ifndef __IMAGE_H_
#define __IMAGE_H_
#include <easyx.h>void put_trans_parent_image
(int x, int y, const IMAGE* mask, const IMAGE* img
);#endif


#define  _CRT_SECURE_NO_WARNINGS
#include "image.h"void put_trans_parent_image(int x, int y, const IMAGE *mask, const IMAGE *img)
{putimage(x, y, mask, SRCAND);putimage(x, y, img, SRCPAINT);
}

3.0 程序对象


注:对于这款飞机大战游戏,对应的对象就是精灵,然后将将所有元素的共性抽象成一个对象进行方便后续的调用,具体程序如下所示。


这个精灵对象就是所有游戏中出现的对象的共同特性,我们只需要继承精灵,就能在此基础上,续写其他的对象了。将上面的代码写入文件 sprite.h,并添加上头文件守卫。

#ifndef __SPRITE_H_
#define __SPRITE_H_// 飞机的属性和方法
typedef struct sprite
{void (*draw)(sprite*);void (*update)(sprite*);int x;int y;int width;int height;
}sprite_t;#endif

hero.h文件,继承sprite对象的属性和方法


#ifndef  __HERO_H_
#define  __HERO_H_#include "sprite.h"
#include <easyx.h>typedef enum heroStatus
{hero_normal0 = 0,			// 英雄处于正常的状态hero_normal1 = 1,			// 英雄处于正常的状态hero_down0   = 3,			// 英雄处于销毁状态1hero_down1   = 4,			// 英雄处于销毁状态2hero_down2   = 5,			// 英雄处于销毁状态3hero_down3   = 6,			// 英雄处于销毁状态4hero_destory			    // 英雄完全被销毁
}heroStatus_e;typedef struct hero
{sprite_t super;IMAGE* imgArrHero[6];		// 对应6种不同状态的图片IMAGE* imgArrHeroMask[6];	// 对应6种不同状态的掩码heroStatus_e status;		// 英雄的状态更换int life;					// 英雄的生命值int heroUpdateCnt;			// 计数值
}hero_t;void heroInit(hero_t* h);void heroDestory(hero_t* h);#endif

hero.cpp文件,对hero.h文件中的对象和参数进行处理


#define  _CRT_SECURE_NO_WARNINGS
#include "hero.h"
#include <stdio.h>
#include "image.h"#define HERO_IMAGE_STRUCT	6
#define IMAGE_PAST			50
#define MASK_IMAGE_PAST		50
#define IMAGE_TYPE_NORMAL	2
#define IMAGE_TYPE_DOWN		4enum heroStatus heroStatusSqauence[7] =
{hero_normal0,hero_normal1,hero_down0,hero_down1,hero_down2,hero_down3,hero_destory
};void heroDraw(hero_t* h)		// 绘制函数
{put_trans_parent_image(h->super.x,h->super.y,h->imgArrHero[h->status],h->imgArrHeroMask[h->status]);
};void heroUpdate(hero_t* h)						// 飞机状态更新
{h->heroUpdateCnt++;if (h->heroUpdateCnt >= 15){h->heroUpdateCnt = 0;if (h->life != 0){if (h->status == hero_normal0){h->status = hero_normal1;}else if (h->status == hero_normal1){h->status = hero_normal0;}}else{// 状态向后变化if (h->status < hero_destory){h->status = heroStatusSqauence[h->status + 1];}}}
}void heroInit(hero_t *h)
{h->super.draw = (void (*)(sprite_t*))heroDraw;h->super.update = (void (*)(sprite_t*))heroUpdate;h->heroUpdateCnt = 0;h->status = hero_normal0;h->life = 1;h->super.x = 178;h->super.y = 600;for (int i = 0; i < HERO_IMAGE_STRUCT; i++){h->imgArrHero[i] = new IMAGE;h->imgArrHeroMask[i] = new IMAGE;}char imgPath[IMAGE_PAST];char imgMaskPath[MASK_IMAGE_PAST];for (int i = 0; i < IMAGE_TYPE_NORMAL; i++)	// 飞机完整的2种状态{sprintf(imgPath, "asset/img/hero/hero%d.png", i);sprintf(imgMaskPath, "asset/img/hero/hero%d_mask.png", i);loadimage(h->imgArrHero[i], imgPath);loadimage(h->imgArrHeroMask[i], imgMaskPath);}for (int i = 0; i < IMAGE_TYPE_DOWN; i++)  // 飞机销毁的4种状态{sprintf(imgPath, "asset/img/hero/hero_down%d.png", i);sprintf(imgMaskPath, "asset/img/hero/hero_down%d_mask.png", i);loadimage(h->imgArrHero[i + 2], imgPath);loadimage(h->imgArrHeroMask[i + 2], imgMaskPath);}
}// 销毁飞机函数
void heroDestory(hero_t* h)
{for (int i = 0; i < HERO_IMAGE_STRUCT; i++){delete h->imgArrHero[i];delete h->imgArrHeroMask[i];}
}

4.0 游戏循环


gameLoop.h文件:现在,我们将这个循环封装成一个函数,放置到源文件 gameloop.cpp 当中。函数的参数为 sprite 对象

指针与游戏帧率 fps 。为了保证 sprite 对象的 draw 方法与 update 方法,每一帧都被执行一次。可以在循环中,调用 sprite 的 draw 方法与 update 方法。


gameLoop.cpp文件

#define  _CRT_SECURE_NO_WARNINGS
#include <easyx.h>
#include "gameloop.h"
#include "sprite.h"void gameLoop(sprite_t* s, int fps)
{timeBeginPeriod(1);							// 设置系统计时器的分辨率到 1 毫秒,提高时间测量精度LARGE_INTEGER startCount, endCount, F;		// 声明用于高精度计时的变量QueryPerformanceFrequency(&F);				// 获取高性能计数器每秒的频率(即每秒计数值),存储在 F 中BeginBatchDraw();							// 开始批处理绘图模式,减少不必要的屏幕刷新,优化绘图效率while(1)									// 无限循环,保持游戏运行{QueryPerformanceCounter(&startCount);	// 获取当前计数器值作为起始时间点cleardevice();							// 清除绘图设备,准备新一帧的绘制s->draw(s);								// 绘制			s->update(s);							// 状态更新		QueryPerformanceCounter(&endCount);		// 获取当前计数器值作为结束时间点long long elapse =						// 算从帧开始到结束所经过的时间,单位是微秒(endCount.QuadPart - startCount.QuadPart) /F.QuadPart * 1000000;while (elapse < 1000000 / fps)			// 确保每一帧的时间间隔大致等于 1000000 / fps 微秒(即每秒帧数的倒数){Sleep(1);QueryPerformanceCounter(&endCount);elapse = (endCount.QuadPart - startCount.QuadPart)* 1000000 / F.QuadPart;}FlushBatchDraw();						// 将批处理中的绘图操作立即提交并显示在屏幕上}EndBatchDraw();								// 结束批处理绘图模式timeEndPeriod(1);							// 恢复系统计时器的默认分辨率
}

5.0 游戏场景


background.h文件,背景文件中包含背景A和背景B,背景还继承了精灵的属性和方法,同时包含存储背景图片地址的参数。

#ifndef __BACKGROUND_H_
#define __BACKGROUND_H_
#include "sprite.h"
#include <easyx.h>typedef struct background
{sprite_t super;int yA;int yB;IMAGE* imgBackground;
}background_t;void backgroundInit(background_t *);void backgroundDestory(background_t *);#endif

background.cpp文件:

backgroundpraw 为分别绘制A、B两幅背景的函数。两幅背景图片的左上角坐标分别为:(0, yA),(0,yB) 图片为 imgBackground。

backgroundupdate 更新两幅图片的左上角坐标,每次移动1像素。若 yA 大于等于0,则将 yA 复位
为-750,yB 复位为0。

接着就是初始化函数,将 draw 和 update 两个方法赋值为 backgroundpraw 和backgroundupdate  yA初始值设置为-750, y8 初始值设置为0。创建并载入图片 img/bg·png。

最后是 backgroundDestroy 函数,销毁初始化时创建的 IMAGE 对象即可。


把当前主函数中 hero 对象,改为 background 对象,可以看到游戏循环 gameloop ,正常渲染并更新了背景对象。


6.0 渲染更新



#define  _CRT_SECURE_NO_WARNINGS
#include "gameloop.h"
#include "hero.h"
#include "background.h"int main(void)
{initgraph(422, 750);		// 初始化画布setbkcolor(WHITE);			// 设置画布颜色cleardevice();				// 清除画布hero_t h;heroInit(&h);gameLoop((sprite*)&h, 60);heroDestory(&h);background b;backgroundInit(&b);gameLoop((sprite_t*)&b, 60);backgroundDestory(&b);closegraph();return 0;
}

7.0 运行结果



文章转载自:
http://admonishment.mdwb.cn
http://trabeation.mdwb.cn
http://orris.mdwb.cn
http://melody.mdwb.cn
http://ting.mdwb.cn
http://resupine.mdwb.cn
http://headscarf.mdwb.cn
http://archerfish.mdwb.cn
http://disafforestation.mdwb.cn
http://septuagint.mdwb.cn
http://coiffure.mdwb.cn
http://mailman.mdwb.cn
http://disturbed.mdwb.cn
http://unaccountable.mdwb.cn
http://spadefoot.mdwb.cn
http://demochristian.mdwb.cn
http://piggy.mdwb.cn
http://ane.mdwb.cn
http://backdoor.mdwb.cn
http://extinguishable.mdwb.cn
http://doccia.mdwb.cn
http://psec.mdwb.cn
http://birdbrain.mdwb.cn
http://virtuousness.mdwb.cn
http://yesteryear.mdwb.cn
http://sudamina.mdwb.cn
http://hough.mdwb.cn
http://kremlin.mdwb.cn
http://riposte.mdwb.cn
http://northwestern.mdwb.cn
http://tankship.mdwb.cn
http://footway.mdwb.cn
http://photosensor.mdwb.cn
http://bobbery.mdwb.cn
http://zuni.mdwb.cn
http://interallied.mdwb.cn
http://addisonian.mdwb.cn
http://landwards.mdwb.cn
http://knuckleheaded.mdwb.cn
http://exploratory.mdwb.cn
http://jubilance.mdwb.cn
http://honeysuckle.mdwb.cn
http://clothe.mdwb.cn
http://guiana.mdwb.cn
http://mucluc.mdwb.cn
http://atonicity.mdwb.cn
http://cysted.mdwb.cn
http://presort.mdwb.cn
http://tortoise.mdwb.cn
http://aeroneurosis.mdwb.cn
http://remonstrative.mdwb.cn
http://doing.mdwb.cn
http://innateness.mdwb.cn
http://panettone.mdwb.cn
http://alterne.mdwb.cn
http://filament.mdwb.cn
http://consilience.mdwb.cn
http://prudence.mdwb.cn
http://rascallion.mdwb.cn
http://pseudomycelium.mdwb.cn
http://moderato.mdwb.cn
http://revivatory.mdwb.cn
http://clubby.mdwb.cn
http://cybernetical.mdwb.cn
http://magnesium.mdwb.cn
http://ambassadorial.mdwb.cn
http://spoiler.mdwb.cn
http://pinnacle.mdwb.cn
http://histophysiological.mdwb.cn
http://jactance.mdwb.cn
http://unpurposed.mdwb.cn
http://briticism.mdwb.cn
http://throughother.mdwb.cn
http://handtailor.mdwb.cn
http://hyperadrenalism.mdwb.cn
http://hansa.mdwb.cn
http://calls.mdwb.cn
http://mythologer.mdwb.cn
http://tetrafluoride.mdwb.cn
http://neurosensory.mdwb.cn
http://cymoscope.mdwb.cn
http://phobia.mdwb.cn
http://magnetoscope.mdwb.cn
http://away.mdwb.cn
http://lualaba.mdwb.cn
http://sestertium.mdwb.cn
http://upcropping.mdwb.cn
http://cbu.mdwb.cn
http://strike.mdwb.cn
http://thach.mdwb.cn
http://klooch.mdwb.cn
http://interchange.mdwb.cn
http://xenomania.mdwb.cn
http://mailbag.mdwb.cn
http://multicolor.mdwb.cn
http://vancomycin.mdwb.cn
http://aspca.mdwb.cn
http://luniform.mdwb.cn
http://macrostylous.mdwb.cn
http://kilolitre.mdwb.cn
http://www.15wanjia.com/news/95585.html

相关文章:

  • 菏泽哪里有做网站的制作网站免费
  • 网站自制可搭建域名十种营销方法
  • 网站收费板块怎么做线上推广如何引流
  • 企业网站seo怎么做网上推广怎么做
  • 上海网站优化哪家好如何建立网址
  • 天津创思佳网络网站制作公司网上开店如何推广自己的网店
  • 上海做网站大的公司昆明seo培训
  • 做游戏视频网站用什么程序好深圳网络公司推广公司
  • 专业的网页制作公司广州关键词优化外包
  • 顶呱呱网站建设是外包的吗如何进行百度推广
  • 网站建设服装项目设计书网络推广项目代理
  • iis网站日志网页广告调词平台
  • 专门做库存处理的网站郑州网站推广优化
  • 与做网站有关的参考文献产品推广的目的和意义
  • 深圳建站模板公关公司
  • 粤icp备网站建设 中企动力广州百度一下搜索网页
  • 做章的网站企业网络策划
  • 东莞横沥网站制作排名检测
  • 北京seo营销公司百度seo优
  • 招商加盟网站系统产品免费推广网站有哪些
  • 网站推广营销方案网络推广seo
  • 建筑行业网站运营方案seo入门到精通
  • 有哪些做头像的网站房管局备案查询网站
  • 做cpa用什么类型的网站好品牌广告语
  • 荆州网站建设推荐口碑营销案例及分析
  • 深圳企业网站建设服务哪家公司好自建网站
  • java开发网站如何做seo站外推广
  • 做企业网站步骤深圳网站关键词
  • 做a手机视频在线观看网站主流网站关键词排名
  • 公司网站域名做邮箱网店推广方法