#include "cannon.h"
#include <QPainter>
#include <QPalette>
#include <QFrame>
#include <QGridLayout>
#include <QPixmap>
#include <QPaintEvent>
#include <math.h>
#include <QDateTime>
#include <stdlib.h>
CannonField::CannonField(QWidget *parent, const char *name)
:QWidget(parent,name)
{
//QPalette palette;
//QPixmap pixmap(":/6.png");
//palette.setBrush(QPalette::Background,QBrush(pixmap));
//this->setPalette(palette);
gameEnded = false;
newTarget();
ang=45;
f=0;
timerCount=0;
autoShootTimer = new QTimer(this,"movement handler");
connect(autoShootTimer,SIGNAL(timeout()),this,SLOT(moveShot()));
shoot_ang=0;
shoot_f=0;
//setPalette(QPalette(QColor(250,250,200)));
/*QFrame *frame = new QFrame;
QPixmap pixmap(":/6.png");
QPalette palette;
palette.setBrush(frame->backgroundRole(),QBrush(pixmap));
frame->setPalette(palette);
//frame->setMask(pixmap.mask()); //可以将图片中透明部分显示为透明的
//frame->setAutoFillBackground(true);
//frame->show();
QGridLayout *glayout;
glayout = new QGridLayout;
glayout->addWidget(frame);*/
}
void CannonField::newTarget()
{
static bool first_time = true;
if(first_time)
{
first_time=false;
QTime midnight(0,0,0);
srand(midnight.secsTo(QTime::currentTime()));
}
QRegion r(targetRect());
target = QPoint(200+rand()%190,35+rand()%255);
repaint(r.unite(targetRect()));
}
void CannonField::shoot()
{
if(isShooting())
return;
//if(autoShootTimer->isActive())
//return;
timerCount=0;
shoot_ang=ang;
shoot_f=f;
autoShootTimer->start(50);
emit canShoot(FALSE);
}
void CannonField::setGameOver()
{
if(gameEnded)
return;
if(isShooting())
autoShootTimer->stop();
gameEnded=true;
repaint();
}
void CannonField::restartGame()
{
if(isShooting())
autoShootTimer->stop();
gameEnded=false;
repaint();
emit canShoot(TRUE);
}
void CannonField::moveShot()
{
QRegion r(shotRect());
timerCount++;
QRect shotR=shotRect();
if(shotR.intersects(targetRect()))
{
autoShootTimer->stop();
emit hit();
emit canShoot(TRUE);
}
else if(shotR.x()>width()||shotR.y()>height())
{
autoShootTimer->stop();
emit missed();
emit canShoot(TRUE);
}
else
r=r.unite(QRegion(shotR));
repaint(r);
}
void CannonField::setAngle(int degrees)
{
if(degrees<5)
degrees=5;
else if(degrees>70)
degrees=70;
if(ang==degrees)
return;
ang=degrees;
repaint(cannonRect(),FALSE);//FALSE参数说明在一个绘画事件发送到窗口部件之前指定的矩形将不会被擦去
emit angleChanged(ang);
}
void CannonField::setForce(int newton)
{
if(newton<0)
newton=0;
if(f==newton)
return;
f=newton;
emit forceChanged(f);
}
void CannonField::paintEvent(QPaintEvent *e)
{
QRect updateR = e->rect();
QPainter p(this);
if(gameEnded)
{
p.setPen(Qt::black);
p.setFont(QFont("Courier",48,QFont::Bold));
p.drawText(rect(),Qt::AlignCenter,"Game Over!");
}
if(updateR.intersects(cannonRect()))
paintCannon(&p);
if(autoShootTimer->isActive()&&updateR.intersects(shotRect()))
paintShot(&p);
if(updateR.intersects(targetRect()))
paintTarget(&p);
/*
if(!e->rect().intersects(cannonRect()))
return;
QRect cr = cannonRect();
QPixmap pix(cr.size());
pix.fill(this,cr.topLeft());
QPainter p(&pix);
p.translate(0,pix.height()-1);
//QString s="Angle = " + QString ::number(ang);
//QPainter p(this);
//p.drawText(200,200,s);
p.setBrush(Qt::blue);
p.setPen(Qt::NoPen);
//p.translate(0,rect().bottom());
p.drawPie(QRect(-35,-35,70,70),0,90*16);
p.rotate(-ang);
p.drawRect(QRect(33,-4,15,8));
p.end();
p.begin(this);
p.drawPixmap(cr.topLeft(),pix);
*/
}
void CannonField::paintTarget(QPainter *p)
{
p->setBrush(Qt::red);
p->setPen(Qt::black);
p->drawRect(targetRect());
}
QRect CannonField::targetRect() const
{
QRect r(0,0,20,10);
r.moveCenter(QPoint(target.x(),height()-1-target.y()));
return r;
}
const QRect barrelRect(33,-4,15,8);
void CannonField::paintCannon(QPainter *p)
{
//if(!p->rect().intersects(cannonRect()))
//return;
QRect cr = cannonRect();
QPixmap pix(cr.size());
pix.fill(this,cr.topLeft());
QPainter tmp(&pix);
tmp.translate(0,pix.height()-1);
tmp.setBrush(Qt::blue);
tmp.setPen(Qt::NoPen);
tmp.drawPie(QRect(-35,-35,70,70),0,90*16);
//tmp.drawPie(0,0,35,35,0,16*90);
tmp.rotate(-ang);
tmp.drawRect(QRect(33,-4,15,8));
tmp.end();
//tmp->begin(this);
p->drawPixmap(cr.topLeft(),pix);
}
void CannonField::paintShot(QPainter *p)
{
p->setBrush(Qt::black);
p->setPen(Qt::NoPen);
p->drawRect(shotRect());
}
QRect CannonField::shotRect() const
{
const double gravity=2.0;
const double PI=acos(-1);
double time = timerCount/4.0;
double velocity = shoot_f+10;
double radians = shoot_ang*PI/180.0;
double velx = velocity*cos(radians);
double vely = velocity*sin(radians)-0.5*gravity*time*time;
double x0 = (barrelRect.right()+5)*cos(radians);
double y0 = (barrelRect.right()+5)*sin(radians);
double x = x0+velx*time;
double y = y0+vely*time;
QRect r = QRect(0,0,6,6);
r.moveCenter(QPoint(qRound(x),height()-1-qRound(y)));
return r;
}
QRect CannonField::cannonRect() const
{
QRect r(0,0,50,50);
r.moveBottomLeft(rect().bottomLeft());
return r;
}
QSizePolicy CannonField::sizePolicy() const
{
return QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
}
QT射击小游戏
5星 · 超过95%的资源 需积分: 9 116 浏览量
2013-10-13
14:26:16
上传
评论 4
收藏 1.79MB RAR 举报
vip哲
- 粉丝: 1
- 资源: 1
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页