天天看點

C++(qt)遊戲實戰項目:坦克大戰(一)

C++(qt)遊戲實戰項目:坦克大戰(一)

整個遊戲地圖大小為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=,float life=){


        this->m_pos.setX(jIndex*CELLWIDTH+CELLWIDTH/);
        this->m_pos.setY(iIndex*CELLHEIGHT+CELLHEIGHT/);
        this->CalculateSphere();
        this->m_bDisappear=false;
        this->life=life;
        this->style=style%;//map_block.png總共有28小塊

    }
    // 繪圖
    void Display(QPainter &paint);

    // 移動
   void Move();
   //得到方塊樣式
   int getstyle(){return style;}
   //切換樣式
   int switchstyle(){style++;style=style%;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/;
    j=style%;
   }
void CalculateSphere(){
    this->m_rectSphere.setRect(m_pos.x()-CELLWIDTH/,m_pos.y()-CELLHEIGHT/,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=);
           

在mapcell.c添加

void downlife(float gongji,int type=)
{if(life>)
life-=gongji;
if(life<)
{m_bDisappear=true;life=;}
}
           

後面C++(qt)遊戲實戰項目:坦克大戰(二)将完成地圖的類。

本文章為作者原創

轉載請标明本文位址:http://blog.csdn.net/qq_26046771/article/details/72643740

繼續閱讀