整个游戏地图大小为WIDTH宽HEIGHT高,游戏地图被分成大小一样的矩形块-我们叫地图块吧,我们要将地图图片也分成块,
地图图片块从左到右从上到下依次一维编号0,1,2,3…28,用一维编号是为了只用一个整形变量就可以表示取不同的地图图片块来表示不同的游戏地图块。地图块和地图图片块都用到二维编号是为了贴图的时候迅速定位到各自的区块。
//file:main.h
#ifndef MAIN
#define MAIN
#define JNUM 17//地图块列数
#define INUM 13//地图块行数
#define PICWIDTH 64//地图块图片宽度
#define PICHEIGHT 64//地图块图片高度
#define CELLWIDTH 64//地图块宽度
#define CELLHEIGHT 64
#define WIDTH 64*17//整个地图宽度
#define HEIGHT 64*13
#include<QDebug>
#include<QImage>
extern QImage blockimage;//声明
#endif // MAIN
//file:main.cpp
#include "mainwindow.h"
#include <QApplication>
QImage blockimage=QImage(":/images/map_block.png");//全局变量
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
//file:mapcell.h
#ifndef MAPCELL_H
#define MAPCELL_H
#include"wanwu.h"
#include"main.h"
#include<fstream>
class Mapcell : public Wanwu
{
public:
Mapcell();
Mapcell(int iIndex,int jIndex,int style=0,float life=200){
this->m_pos.setX(jIndex*CELLWIDTH+CELLWIDTH/2);
this->m_pos.setY(iIndex*CELLHEIGHT+CELLHEIGHT/2);
this->CalculateSphere();
this->m_bDisappear=false;
this->life=life;
this->style=style%28;//map_block.png总共有28小块
}
// 绘图
void Display(QPainter &paint);
// 移动
void Move();
//得到方块样式
int getstyle(){return style;}
//切换样式
int switchstyle(){style++;style=style%28;return style;}
//设置样式
void setstyle(int style){this->style=style;}
private:
//static QImage blockimage;
int style;//方块样式,从图片上依次编号0 ,1,2 ,3,4,5.。。。。
//
void cal(int style,int &i,int &j){//将一维编号变成一行有4列的二维编号i行j列
i=style/4;
j=style%4;
}
void CalculateSphere(){
this->m_rectSphere.setRect(m_pos.x()-CELLWIDTH/2,m_pos.y()-CELLHEIGHT/2,CELLWIDTH,CELLHEIGHT);
}
};
#endif // MAPCELL_H
//file:mapcell.cpp
#include "mapcell.h"
//QImage Mapcell::blockimage=QImage(":/images/map_block.png"); linux
Mapcell::Mapcell()
{
}
void Mapcell::Display(QPainter &paint){
int i,j;
cal(style,i,j);
if(!this->IsDisappear())
//paint.drawImage(m_rectSphere,blockimage,QRect(j*PICWIDTH,i*PICHEIGHT,PICWIDTH,PICHEIGHT));//优化代码,速度飞一般
paint.drawImage(m_rectSphere,QImage(":/images/map_block.png"),QRect(j*PICWIDTH,i*PICHEIGHT,PICWIDTH,PICHEIGHT));
}
void Mapcell::Move(){
}
为了简单我们不考虑地图块的护甲魔抗等等,留个大神去完成吧,有多少攻击就减少多少血
在mapcell.h添加
void downlife(float gongji,int type=0);
在mapcell.c添加
void downlife(float gongji,int type=0)
{if(life>0)
life-=gongji;
if(life<0)
{m_bDisappear=true;life=0;}
}
后面C++(qt)游戏实战项目:坦克大战(二)将完成地图的类。
本文章为作者原创
转载请标明本文地址:http://blog.csdn.net/qq_26046771/article/details/72643740