给游戏新增加敌人,利用cllidingItem()方法表示两个物体是否相交。
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-19T23:17:02
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tutorial5
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp \
myrect.cpp \
bullet.cpp \
enemy.cpp
HEADERS += \
myrect.h \
bullet.h \
enemy.h
#ifndef BULLET_H
#define BULLET_H
#include <QGraphicsRectItem>
#include <QObject>
class Bullet : public QObject, public QGraphicsRectItem {
Q_OBJECT
public:
Bullet();
public slots:
void move();
};
#endif // BULLET_H
#ifndef ENEMY_H
#define ENEMY_H
#include <QGraphicsRectItem>
#include <QObject>
class Enemy : public QObject, public QGraphicsRectItem {
Q_OBJECT
public :
Enemy();
public slots:
void move();
};
#endif // ENEMY_H
#ifndef MYRECT_H
#define MYRECT_H
#include <QGraphicsRectItem>
#include <QObject>
class MyRect : public QObject, public QGraphicsRectItem {
Q_OBJECT
public:
void keyPressEvent(QKeyEvent *event);
public slots:
void spawn();
};
#endif // MYRECT_H
#include "bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "enemy.h"
#include <typeinfo>
Bullet::Bullet() : QObject(), QGraphicsRectItem()
{
// draw the rect
setRect(0, 0, 10, 50);
// connect
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),this, SLOT(move()));
timer->start(50);
}
void Bullet::move()
{
// if bullet collides with enemy, destroy both
QList<QGraphicsItem *> colliding_items = collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; ++i) {
if (typeid(*(colliding_items[i])) == typeid(Enemy)) {
// remove them both
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
// delete them both
delete colliding_items[i];
delete this;
return ;
}
}
// move bullet up
setPos(x(), y() - 10);
if (pos().y() + rect().height() < 0) {
scene()->removeItem(this);
delete this;
}
}
#include "enemy.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include <stdlib.h>
#include <QDebug>
Enemy::Enemy() : QObject(), QGraphicsRectItem() {
// set random position
int random_number = rand() % 700;
setPos(random_number, 0);
//draw the rect
setRect(0, 0, 100, 100);
// connect
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50);
}
void Enemy::move() {
// move enmey down
setPos(x(), y() + 5);
if (pos().y() + rect().height() < 0) {
scene()->removeItem(this);
delete this;
}
}
#include "myrect.h"
#include <QGraphicsScene>
#include <QKeyEvent>
#include "enemy.h"
#include "bullet.h"
#include <QDebug>
void MyRect::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Left) {
if (pos().x() > 0)
setPos(x()-10, y());
} else if (event->key() == Qt::Key_Right) {
if (pos().x() + 100 < 800)
setPos(x()+10, y());
} else if (event->key() == Qt::Key_Space) {
Bullet *bullet = new Bullet();
bullet->setPos(x(), y());
scene()->addItem(bullet);
}
}
void MyRect::spawn() {
// create an enemy
Enemy *enemy = new Enemy();
scene()->addItem(enemy);
}
#include <QApplication>
#include "myrect.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
/*
* tutorial topics:
* collidingItems()
* QTimer and coordinate systems
*/
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// create a scene
QGraphicsScene *scene = new QGraphicsScene();
// create an item to add to the scene
MyRect *player = new MyRect();
player->setRect(0, 0, 100, 100);
// add the item to the scene
scene->addItem(player);
// make rect focusable
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();
// create a view to visualize the scene
QGraphicsView *view = new QGraphicsView(scene);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// show the view
view->show();
view->setFixedSize(800, 600);
scene->setSceneRect(0, 0, 800, 600);
player->setPos(view->width()/2,
view->height() - player->rect().height());
// spawn enemies
QTimer *timer = new QTimer();
QObject::connect(timer, SIGNAL(timeout()), player, SLOT(spawn()));
timer->start(2000);
return a.exec();
}