发射子弹
单独的子弹类根据坦克属性生成相应的子弹对象。

//file bullet.h
#ifndef BULLET_H
#define BULLET_H#include"wanwu.h"
#include"tank.h"
class Bullet : public Wanwu
{
public:Bullet(const Tank &tank);// 绘图void Display(QPainter &paint) ;// 移动void Move() ;//protected:void CalculateSphere();
private:int style;int group;//子弹所在组};#endif // BULLET_H
//file bullet.cpp
#include "bullet.h"
#include"status.h"
Bullet::Bullet(const Tank &tank)
{m_dir=tank.m_dir;m_pos=tank.m_pos;style=tank.style/2;m_step=tank.m_step+10;wuli=tank.wuli;fashu-tank.fashu;group=tank.group;m_bDisappear=false;
CalculateSphere();
}void Bullet::Display(QPainter &paint){if(m_bDisappear==true)return;switch(m_dir){case UP:paint.drawImage(m_rectSphere,*glo.bulletimage,QRect(2*PICBULLETWIDTH,style*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//身体//paint.drawImage(xm_rectSphere,*glo.bulletimage,QRect(2*PICBULLETWIDTH,(style*2+1)*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//炮塔break;case DOWN:paint.drawImage(m_rectSphere,*glo.bulletimage,QRect(0*PICBULLETWIDTH,style*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//身体//paint.drawImage(xm_rectSphere,*glo.bulletimage,QRect(0*PICBULLETWIDTH,(style*2+1)*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//炮塔break;case LEFT:paint.drawImage(m_rectSphere,*glo.bulletimage,QRect(1*PICBULLETWIDTH,style*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//身体//paint.drawImage(xm_rectSphere,*glo.bulletimage,QRect(1*PICBULLETWIDTH,(style*2+1)*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//炮塔break;case RIGHT:paint.drawImage(m_rectSphere,*glo.bulletimage,QRect(3*PICBULLETWIDTH,style*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//身体//paint.drawImage(xm_rectSphere,*glo.bulletimage,QRect(3*PICBULLETWIDTH,(style*2+1)*PICBULLETHEIGHT,PICBULLETWIDTH,PICBULLETHEIGHT));//炮塔break;}}void Bullet::Move(){switch(m_dir){case UP:m_pos.setY(m_pos.y()-m_step);break;case DOWN:m_pos.setY(m_pos.y()+m_step);break;case LEFT:m_pos.setX(m_pos.x()-m_step);break;case RIGHT:m_pos.setX(m_pos.x()+m_step);break;}CalculateSphere();//子弹是否与地图块碰撞for(int i=0;i<INUM;i++)for(int j=0;j<JNUM;j++)if(glo.gamemap->getcell(i,j))if(!glo.gamemap->getcell(i,j)->ischuantou1())if(IsBoom(*glo.gamemap->getcell(i,j))){glo.gamemap->getcell(i,j)->downlife(wuli,0);m_bDisappear=true;*glo.status=Status(*glo.gamemap->getcell(i,j));}//子弹是否击中敌对坦克for(int i=0;i<glo.badtanks.count();i++)if(glo.badtanks.at(i)&&glo.badtanks.at(i)->group!=this->group&&IsBoom(*glo.badtanks.at(i))){m_bDisappear=true;//子弹消失glo.badtanks.at(i)->downlife(wuli);*glo.status=Status(*glo.badtanks.at(i));//将打中的对象的血量显示出来}if(glo.player->group!=this->group&&IsBoom(*glo.player)){m_bDisappear=true;//子弹消失glo.player->downlife(wuli);*glo.status=Status(*glo.player);//将打中的对象的血量显示出来}//子弹出界if(m_pos.x()<0||m_pos.x()>WIDTH||m_pos.y()>HEIGHT||m_pos.y()<0)m_bDisappear=true;//子弹消失}void Bullet::CalculateSphere(){this->m_rectSphere.setRect(m_pos.x()-BULLETWIDTH/2,m_pos.y()-BULLETHEIGHT/2,BULLETWIDTH,BULLETHEIGHT);}

坦克拥有子弹,我们可以用包含关系来管理子弹,坦克发射子弹就是新建一个子弹对象插入到子弹链表中
坦克类更改如下

#ifndef TANK_H
#define TANK_H
#include"wanwu.h"
#include"main.h"
#include"gamemap.h"
#include"bullet.h"
class Tank : public Wanwu
{
protected:QList<Bullet*> bullets;static int steps[8];static float lifes[8];static float wulis[8];static float fashus[8];static float hujias[8];static float mokangs[8];static int gongjijianges[8];int  group;//坦克所在组// 计算势力范围virtual void CalculateSphere();
public:friend class Bullet;Tank();Tank(int iIndex,int jIndex,Dir dir=UP,int style=0,int group=1);// 绘图void Display(QPainter &paint);// 移动void Move();//void Move1();//fangzhi si xun huan//设置移动状态为开void startmove(){ismove=true;}//设置移动状态为关void stopmove(){ismove=false;}//void fire();void startfire();void stopfire();//设置方向void setdir(Dir dir){m_dir=dir;}//坦克下一步 是否和地图块碰撞bool nextsiboom();private:int style;bool ismove;bool isfire;int gongjijiange;};#endif // TANK_H
#include "tank.h"
int Tank::steps[8]={4,8,8,16,16,32,32,64};
float Tank::lifes[8]={200,400,600,800,900,1100,1300,1500};
float Tank::wulis[8]={20,40,80,160,160,320,320,640};
float Tank::fashus[8]={20,40,80,160,160,320,320,640};
float Tank::hujias[8]={50,70,90,110,130,150,170,190};
float Tank::mokangs[8]={50,70,90,110,130,150,170,190};
int Tank::gongjijianges[8]={8,7,6,5,4,3,2,2};
Tank::Tank()
{this->m_pos.setX(10*CELLWIDTH+CELLWIDTH/2);this->m_pos.setY(8*CELLHEIGHT+CELLHEIGHT/2);this->wuli=200;this->group=0;//0玩家组,1敌人组m_step=16;gongjijiange=3;this->style=0;ismove=false;m_dir=UP;isfire=false;m_bDisappear=false;life=1000;CalculateSphere();
}Tank::Tank(int iIndex,int jIndex,Dir dir,int style,int group){this->m_pos.setX(jIndex*CELLWIDTH+CELLWIDTH/2);this->m_pos.setY(iIndex*CELLHEIGHT+CELLHEIGHT/2);this->m_dir=dir;this->style=style;this->group=group;isfire=false;wuli=wulis[style];fashu=fashus[style];hujia=hujias[style];mokang=mokangs[style];m_step=steps[style];gongjijiange=gongjijianges[style];m_bDisappear=false;life=lifes[style];CalculateSphere();
}void Tank::Display(QPainter &paint){for(int i=0;i<bullets.count();i++)if(bullets.at(i)&&!bullets.at(i)->IsDisappear())bullets.at(i)->Display(paint);else if(bullets.at(i)){delete bullets.at(i);//回收new出来的对象空间bullets.removeAt(i);//将对象指针从链表删除i--;}QRect xm_rectSphere=m_rectSphere;if(m_bDisappear)return;switch(m_dir){case UP:paint.drawImage(m_rectSphere,*glo.tankimage,QRect(2*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(2*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔break;case DOWN:paint.drawImage(m_rectSphere,*glo.tankimage,QRect(0*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(0*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔break;case LEFT:paint.drawImage(m_rectSphere,*glo.tankimage,QRect(1*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(1*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔break;case RIGHT:paint.drawImage(m_rectSphere,*glo.tankimage,QRect(3*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(3*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔break;}}
void Tank::Move()
{for(int i=0;i<bullets.count();i++)bullets.at(i)->Move();if(m_bDisappear)return ;if(nextsiboom())return;if(ismove==true){switch(m_dir){case UP:m_pos.setY(m_pos.y()-m_step);break;case DOWN:m_pos.setY(m_pos.y()+m_step);break;case LEFT:m_pos.setX(m_pos.x()-m_step);break;case RIGHT:m_pos.setX(m_pos.x()+m_step);break;}CalculateSphere();qDebug("move on");}
qDebug("move off");
}void Tank::Move1()//和move一样,为了避免死循环
{if(ismove==true){switch(m_dir){case UP:m_pos.setY(m_pos.y()-m_step);break;case DOWN:m_pos.setY(m_pos.y()+m_step);break;case LEFT:m_pos.setX(m_pos.x()-m_step);break;case RIGHT:m_pos.setX(m_pos.x()+m_step);break;}CalculateSphere();qDebug("yuce ");}
qDebug("move off");
}bool Tank::nextsiboom(){Tank tmp=*this;tmp.Move1();//qDebug("%d",tmp.m_rectSphere.right());//qDebug("%d",glo.gamemap->getcell(1,4)->m_rectSphere.left());//是否与地图块碰撞for(int i=0;i<INUM;i++)for(int j=0;j<JNUM;j++)if(glo.gamemap->getcell(i,j)&&!glo.gamemap->getcell(i,j)->ischuantou()&&tmp.IsBoom(*glo.gamemap->getcell(i,j))){qDebug("-----------boom-------"); return true;}qDebug("---------------");//是否与地图边界碰撞if(tmp.m_rectSphere.left()<0||tmp.m_rectSphere.right()>WIDTH||tmp.m_rectSphere.bottom()>HEIGHT||tmp.m_rectSphere.top()<0)return true;//return false;
}void Tank::fire(){if(m_bDisappear)return;
if(isfire==true&&glo.framei%gongjijiange==0){
Bullet *newbullet=new Bullet(*this);
bullets.append(newbullet);
}}void Tank::startfire(){isfire=true;}void Tank::stopfire(){isfire=false;
}void Tank::CalculateSphere(){this->m_rectSphere.setRect(m_pos.x()-TANKWIDTH/2,m_pos.y()-TANKHEIGHT/2,TANKWIDTH,TANKHEIGHT);}

地图块类有些能被子弹穿透,有些能被坦克穿透,因此增加两个属性。外加其他方面更改

#ifndef MAPCELL_H
#define MAPCELL_H#include"wanwu.h"
#include"main.h"
#include<fstream>
class Mapcell : public Wanwu
{public:enum {BLOCKSNUM=13};static float lifes[BLOCKSNUM];static float hujias[BLOCKSNUM];static float mokangs[BLOCKSNUM];static bool chuantous[BLOCKSNUM];static bool chuantous1[BLOCKSNUM];Mapcell();Mapcell(int iIndex, int jIndex, int style=0);//护甲魔抗物理攻击魔法攻击攻击速度移动速度暂时用不到// 绘图void Display(QPainter &paint);// 移动void Move();//得到方块样式int getstyle(){return style;}//切换样式int switchstyle(){style++;style=style%BLOCKSNUM; this->life=lifes[this->style];this->hujia=hujias[this->style];this->mokang=mokangs[this->style];this->chuantou=chuantous[this->style];this->chuantou1=chuantous1[this->style];return style;}//设置样式void setstyle(int style){this->style=style%BLOCKSNUM; this->life=lifes[this->style];this->hujia=hujias[this->style];this->mokang=mokangs[this->style];this->chuantou=chuantous[this->style];this->chuantou1=chuantous1[this->style];}//bool ischuantou(){return chuantou;}//bool ischuantou1(){return chuantou1;}
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);}bool chuantou;//坦克能否穿透方块
bool chuantou1;//子弹能否穿透方块};#endif // MAPCELL_H
#include "mapcell.h"
//QImage Mapcell::blockimage=QImage(":/images/map_block.png"); linux
float Mapcell::lifes[BLOCKSNUM]={30000,3000,2000,1000,800,700,600,20000,0,0,0,0,0};
float Mapcell::hujias[BLOCKSNUM]={1000,500,400,300,200,100,50,800,0,0,0,0,0};
float Mapcell::mokangs[BLOCKSNUM]={1000,500,400,300,200,100,50,800,0,0,0,0,0};
bool Mapcell::chuantous[BLOCKSNUM]={false,false,false,false,false,false,false,false,true,true,true,true,true};
bool Mapcell::chuantous1[BLOCKSNUM]={false,false,false,false,false,false,false,false,true,true,true,true,true};
Mapcell::Mapcell()
{this->m_pos.setX(8*CELLWIDTH+CELLWIDTH/2);this->m_pos.setY(8*CELLHEIGHT+CELLHEIGHT/2);this->CalculateSphere();this->m_bDisappear=false;this->style=0;//map_block.png总共有28小块this->life=lifes[this->style];this->hujia=hujias[this->style];this->mokang=mokangs[this->style];this->chuantou=chuantous[this->style];this->chuantou1=chuantous1[this->style];}Mapcell::Mapcell(int iIndex,int jIndex,int style){//护甲魔抗物理攻击魔法攻击攻击速度移动速度暂时用不到this->m_pos.setX(jIndex*CELLWIDTH+CELLWIDTH/2);this->m_pos.setY(iIndex*CELLHEIGHT+CELLHEIGHT/2);this->CalculateSphere();this->m_bDisappear=false;this->style=style%BLOCKSNUM;//map_block.png总共有28小块this->life=lifes[this->style];this->hujia=hujias[this->style];this->mokang=mokangs[this->style];this->chuantou=chuantous[this->style];this->chuantou1=chuantous1[this->style];
}void Mapcell::Display(QPainter &paint){
int i,j;
cal(style,i,j);
if(!this->IsDisappear())
paint.drawImage(m_rectSphere,*glo.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(){}

增加一个状态类。为了实时记录被子弹打种物体的血量

#ifndef STATUS_H
#define STATUS_H#include"wanwu.h"
class Status
{public:float life;Status(){life=0;}Status(const Wanwu &wanwu);void Display(QPainter &paint);
};#endif // STATUS_H
#include "status.h"
#include<QColor>
Status::Status(const Wanwu &wanwu)
{
life=wanwu.life;
}void Status::Display(QPainter &paint){paint.drawText(500,180,QString("life:")+QString::number(life));}

void MainWindow::keyPressEvent(QKeyEvent *event)函数中加入

else if(event->key()==Qt::Key_J){glo.player->startfire();}

Glo结构体加入

Status *status;

MainWindow::MainWindow(QWidget *parent)里加入

glo.status=new Status();

MainWindow::paintEvent(QPaintEvent *event)函数加入

glo.status->Display(paint);

这样就完成了坦克发射子弹和血量的显示

本文章为作者原创
转载请标明本系列文章地址:http://blog.csdn.net/qq_26046771/article/details/72643740

C++(qt)游戏实战项目:坦克大战(五)相关推荐

  1. stg游戏c语言,坦克大战改版

    <坦克大战改>是一款国内玩家模仿FC经典游戏坦克大战所自制的小游戏,游戏中玩家将会控制坦克保卫基地,一旦基地失守关卡就会失败.游戏支持手柄震动 ,同时包含有关卡编辑器,让玩家能够自由编辑关 ...

  2. 基于QT的C++的坦克大战游戏

    近期培训期间做的基于C++的坦克小游戏,欢迎评论. 首先是主驱动部分:tankclient.h,和tankclient.cpp tankclient.h部分 #ifndef TANKCLIENT_H ...

  3. eclipse 导入项目_JAVA编程实战:坦克大战系列2-坦克如何在eclipse中编写

    游戏中寻找学习JAVA的乐趣之 坦克大战系列2-坦克如何在Eclipse中编写 前言 本篇主要对Robocode在eclipse中如何配置并编写. Eclipse中的配置 通过本身自带的编辑器去写代码 ...

  4. 【Cocos2D-x 3.5实战】坦克大战(1)环境配置

    前言: 最近课比较少,空闲时间比较多,一有时间就东想西想,想着想着就突然想到做手机游戏(android)了,学习下CoCos2d.看了一些CoCos2D的相关文档和教程,觉得是时候实战了,但是苦于没有 ...

  5. 微信小游戏开发之坦克大战(比羊了个羊还好玩系列)

    现在很多公司开始使用游戏化的方式去做产品,让产品呈现给用户时更好玩,以达到增加用户粘性,提升DAU的效果. 同时随着硬件与底层系统的发展,用户的终端对动画的表现能力也越来越强,很多APP以引导用户互动 ...

  6. Unity项目 - 坦克大战3D TankBattle

    目录 游戏原型 项目演示 绘图资源 代码实现 技术探讨 参考来源 游戏原型 游戏玩法:在有界的战场上,玩家将驾驶坦克,代表绿色阵营,与你的队友一起击溃红蓝阵营的敌人,在这场三方大战中夺得胜利! 操作指 ...

  7. VS/Qt C++ 入门项目飞机大战(内含全部源代码,素材,项目工程,项目祥解)可直接运行

    零.说在前面 最近做了个qt/c++的小项目飞机大战,主要是边玩边做,主要讲解一下设计思路,各个模块的实现原理,非常适合初学者拿来练手.需要源码.素材.项目详解.打包软件等整个项目用到的全部内容,可以 ...

  8. 制作项目——坦克大战

    继上一周开始做的模拟飞机大战游戏项目之后,本周一直持续在做坦克大战这个游戏项目上,虽然看似简单的游戏,但真正对于我们这刚学了JS的初学者来讲,还有点挑战,因为里面涉及的javascript代码颇多,具 ...

  9. 【Java游戏开发】坦克大战(附源码+课件+资料)

    本课程讲解了一个坦克大战游戏的详细编写流程,即使你是刚入门java的新手,只要你简单掌握了该游戏所需要的javase基础知识,便可以跟随教程视频完成属于你自己的坦克大战游戏!同时还可以加深和巩固你对面 ...

  10. python游戏开发keydown_教你用python开发游戏 python之坦克大战上

    #coding:utf-8import pygame,sys,timefrom random import randintfrom pygame.locals import *"" ...

最新文章

  1. C语言按两个字节读写二进制文件,C语言 读写二进制文件(示例代码)
  2. 11年瑞纳手动挡值多少钱_三分钟让你知道手中的松石值多少钱
  3. 结构型模式:外观模式(门面模式)
  4. 如果和对方的意见或者事件冲突了怎么办?让步
  5. mac下anaconda安装selenium+PhantomJS
  6. C#正在被人用来做什么?--在CSDN上引发小讨论的帖子
  7. c 包含其他文件_C语言:全局变量在多个c文件中公用的方法!
  8. XP共享拒绝访问,全面解决
  9. 【云图】如何制作中国贪官落马图?
  10. 接口自动化测试(Python+Requests+Unittest)
  11. linux ios文件是否存在,技术|如何在 Linux 中验证 ISO 镜像
  12. 几种降维思想方法总结
  13. 使用C#生成word文件
  14. 第三讲 系统建模与仿真
  15. 在word中快速得到数学公式
  16. matlab 打开access文件,matlab读取Access数据(.mdb文件)
  17. Google SketchUp Cookbook: (Chapter 3) Intersection Edges: Cutting and Trimming
  18. 人机的根本区别:Free will
  19. window自带的计算机应用程序,Win10系统电脑不小心将自带的应用程序卸载了该怎么恢复...
  20. 一万字! 中大形企业网络部署架构(链路聚合 mstp+vrrp ap+ac 防火墙 ospf )

热门文章

  1. 用python写作文_Python3实现写作
  2. druid数据源检测数据库连接有效性testOnBorrow、testOnReturn、testWhileIdle属性原理分析
  3. PyCharm2021安装教程
  4. html制作钢铁侠心脏,心脏术后我变成了“钢铁侠”
  5. CHECK约束在表继承中的使用
  6. 2048的C语言实现
  7. 关于瑞星杀毒软件无法完全卸载、自动重装的无奈,与相应的解决办法
  8. win7网络不显示共享计算机,Win7电脑已开启共享却找不到设备 局域网显示空白该怎么解决...
  9. 论文阅读笔记:A CRITIQUE OF SELF-EXPRESSIVE DEEP SUBSPACE CLUSTERING,自表达深度子空间聚类批判
  10. 【经验分享】Thinkpad(系列均可) E50左Ctrl键失灵问题解决方法