C++ Qt Game Tutorial 3 - Shooting With the Spacebar

移动矩形,在第一个例子基础上,建立一个矩形类,实现keyPressEvent方法。空格键的地方写了一个子弹(bullet), 里面有一个定时器,不停地向上移动, 利用到了qt 的一个很重要的特性,signal and slot.

#-------------------------------------------------
#
# Project created by QtCreator 2017-01-17T19:56:59
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Totorial2
TEMPLATE = app


SOURCES += main.cpp \
    myrect.cpp \
    bullet.cpp

HEADERS  += \
    myrect.h \
    bullet.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

#include <QTimer>
#include "bullet.h"
Bullet::Bullet()
{
    setRect(0, 0, 10, 50);
    QTimer *timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(move()));
    timer->start(50);
}

void Bullet::move() {
    setPos(x(), y()-10);
}

#ifndef MYRECT_H
#define MYRECT_H

#include <QGraphicsRectItem>

class MyRect : public QGraphicsRectItem {
public:
    void keyPressEvent(QKeyEvent *event);
};

#endif // MYRECT_H

#include "myrect.h"
#include "QGraphicsRectItem"
#include <QKeyEvent>
#include "bullet.h"
#include <QGraphicsScene>

void MyRect::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Left) {
        setPos(x()-10, y());
    } else if (event->key() == Qt::Key_Right) {
        setPos(x()+10, y());
    } else if (event->key() == Qt::Key_Up) {
        setPos(x(), y()-10);
    } else if (event->key() == Qt::Key_Down) {
        setPos(x(), y()+10);
    } else if (event->key() == Qt::Key_Space) {
        Bullet *bullet = new Bullet();
        bullet->setPos(x(), y());
        scene()->addItem(bullet);
    }
}

#include <QApplication>
#include <QGraphicsScene>
#include "myrect.h"
#include <QGraphicsView>

/*
 * Tutorial Topics:
 * -event (keyPressEvent() and QkeyEvent)
 * -event propogation system
 * -QDebug
 */

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // create a scene
    QGraphicsScene *scene = new QGraphicsScene();

    // create an item to put into the scene
    MyRect *rect = new MyRect();
    rect->setRect(0, 0, 100, 100);

    // make rect focusable
    rect->setFlag(QGraphicsItem::ItemIsFocusable);
    rect->setFocus();

    // add the item to the scene
    scene->addItem(rect);

    // add a view to visualize the scene
    QGraphicsView *view = new QGraphicsView(scene);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->show();

    return a.exec();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值