From 818383fec4f220a2410177b58518797e81d8eab3 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 3 Apr 2011 16:28:52 +0200 Subject: basic pacman qt setup --- .gitignore | 9 + pacman-c++/main.cpp | 293 +++++++++++++++++++++++++++++++ pacman-c++/pacman.pro | 5 + pacman-c++/pacman.qrc | 5 + pacman-c++/pics/pacman10-hp-sprite-2.png | Bin 0 -> 6857 bytes pacman-c++/pixmapitem.cpp | 44 +++++ pacman-c++/pixmapitem.h | 22 +++ pacman-google/src/js/pacman10-hp.3.js | 19 +- 8 files changed, 394 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 pacman-c++/main.cpp create mode 100644 pacman-c++/pacman.pro create mode 100644 pacman-c++/pacman.qrc create mode 100644 pacman-c++/pics/pacman10-hp-sprite-2.png create mode 100644 pacman-c++/pixmapitem.cpp create mode 100644 pacman-c++/pixmapitem.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c901713 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +pacman-c++/pacman.pro.user +pacman-c++/pacman +pacman-c++/Makefile +pacman-c++/qrc_* +pacman-c++/*.o +pacman-c++/*.moc +pacman-build-debug +pacman-build-release +.*.swp diff --git a/pacman-c++/main.cpp b/pacman-c++/main.cpp new file mode 100644 index 0000000..5fa9c1c --- /dev/null +++ b/pacman-c++/main.cpp @@ -0,0 +1,293 @@ +#include "pixmapitem.h" +#include +#include + +//![15] +class StateSwitchEvent: public QEvent +{ +public: + StateSwitchEvent() + : QEvent(Type(StateSwitchType)) + { + } + + StateSwitchEvent(int rand) + : QEvent(Type(StateSwitchType)), + m_rand(rand) + { + } + + enum { StateSwitchType = QEvent::User + 256 }; + + int rand() const { return m_rand; } + +private: + int m_rand; +}; +//![15] + +//![16] +class QGraphicsRectWidget : public QGraphicsWidget +{ +public: + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, + QWidget *) + { + painter->fillRect(rect(), Qt::blue); + } +}; +//![16] + +class StateSwitchTransition: public QAbstractTransition +{ +public: + StateSwitchTransition(int rand) + : QAbstractTransition(), + m_rand(rand) + { + } + +protected: +//![14] + virtual bool eventTest(QEvent *event) + { + return (event->type() == QEvent::Type(StateSwitchEvent::StateSwitchType)) + && (static_cast(event)->rand() == m_rand); + } +//![14] + + virtual void onTransition(QEvent *) {} + +private: + int m_rand; +}; + +//![10] +class StateSwitcher : public QState +{ + Q_OBJECT +public: + StateSwitcher(QStateMachine *machine) + : QState(machine), m_stateCount(0), m_lastIndex(0) + { } +//![10] + +//![11] + virtual void onEntry(QEvent *) + { + int n; + while ((n = (qrand() % m_stateCount + 1)) == m_lastIndex) + { } + m_lastIndex = n; + machine()->postEvent(new StateSwitchEvent(n)); + } + virtual void onExit(QEvent *) {} +//![11] + +//![12] + void addState(QState *state, QAbstractAnimation *animation) { + StateSwitchTransition *trans = new StateSwitchTransition(++m_stateCount); + trans->setTargetState(state); + addTransition(trans); + trans->addAnimation(animation); + } +//![12] + +private: + int m_stateCount; + int m_lastIndex; +}; + +//![13] +QState *createGeometryState(QObject *w1, const QRect &rect1, + QObject *w2, const QRect &rect2, + QObject *w3, const QRect &rect3, + QObject *w4, const QRect &rect4, + QState *parent) +{ + QState *result = new QState(parent); + result->assignProperty(w1, "geometry", rect1); + result->assignProperty(w2, "geometry", rect2); + result->assignProperty(w3, "geometry", rect3); + result->assignProperty(w4, "geometry", rect4); + + return result; +} +//![13] + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + +#if 0 + QWidget window; + QPalette palette; + palette.setBrush(QPalette::Window, Qt::black); + window.setPalette(palette); + + QPushButton *button1 = new QPushButton("A", &window); + button1->setObjectName("button1"); + + QPushButton *button2 = new QPushButton("B", &window); + button2->setObjectName("button2"); + + QPushButton *button3 = new QPushButton("C", &window); + button3->setObjectName("button3"); + + QPushButton *button4 = new QPushButton("D", &window); + button4->setObjectName("button4"); + +#else +//![1] + //QGraphicsRectWidget *button1 = new QGraphicsRectWidget; + QGraphicsRectWidget *button2 = new QGraphicsRectWidget; + QGraphicsRectWidget *button3 = new QGraphicsRectWidget; + QGraphicsRectWidget *button4 = new QGraphicsRectWidget; + button2->setZValue(1); + button3->setZValue(2); + button4->setZValue(3); + + //QPixmap pixmap(":/pacman10-hp-sprite-2"); + + QGraphicsScene scene(0, 0, 300, 300); + scene.setBackgroundBrush(Qt::black); + + PixmapItem *button1 = new PixmapItem("google-pacman-sprite", &scene); + button1->setSprite(2, 2, 16, 16); + + //scene.addItem(button2); + //scene.addItem(button3); + //scene.addItem(button4); +//![1] + QGraphicsView window(&scene); + window.setFrameStyle(0); + window.setAlignment(Qt::AlignLeft | Qt::AlignTop); + window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); +#endif + +#if 0 +//![2] + QStateMachine machine; + + QState *group = new QState(); + group->setObjectName("group"); + QTimer timer; + timer.setInterval(1250); + timer.setSingleShot(true); + QObject::connect(group, SIGNAL(entered()), &timer, SLOT(start())); +//![2] + +//![3] + QState *state1; + QState *state2; + QState *state3; + QState *state4; + QState *state5; + QState *state6; + QState *state7; + + state1 = createGeometryState(button1, QRect(100, 0, 50, 50), + button2, QRect(150, 0, 50, 50), + button3, QRect(200, 0, 50, 50), + button4, QRect(250, 0, 50, 50), + group); +//![3] + state2 = createGeometryState(button1, QRect(250, 100, 50, 50), + button2, QRect(250, 150, 50, 50), + button3, QRect(250, 200, 50, 50), + button4, QRect(250, 250, 50, 50), + group); + state3 = createGeometryState(button1, QRect(150, 250, 50, 50), + button2, QRect(100, 250, 50, 50), + button3, QRect(50, 250, 50, 50), + button4, QRect(0, 250, 50, 50), + group); + state4 = createGeometryState(button1, QRect(0, 150, 50, 50), + button2, QRect(0, 100, 50, 50), + button3, QRect(0, 50, 50, 50), + button4, QRect(0, 0, 50, 50), + group); + state5 = createGeometryState(button1, QRect(100, 100, 50, 50), + button2, QRect(150, 100, 50, 50), + button3, QRect(100, 150, 50, 50), + button4, QRect(150, 150, 50, 50), + group); + state6 = createGeometryState(button1, QRect(50, 50, 50, 50), + button2, QRect(200, 50, 50, 50), + button3, QRect(50, 200, 50, 50), + button4, QRect(200, 200, 50, 50), + group); +//![4] + state7 = createGeometryState(button1, QRect(0, 0, 50, 50), + button2, QRect(250, 0, 50, 50), + button3, QRect(0, 250, 50, 50), + button4, QRect(250, 250, 50, 50), + group); + group->setInitialState(state1); +//![4] + +//![5] + QParallelAnimationGroup animationGroup; + QSequentialAnimationGroup *subGroup; + + QPropertyAnimation *anim = new QPropertyAnimation(button4, "geometry"); + anim->setDuration(1000); + anim->setEasingCurve(QEasingCurve::OutElastic); + animationGroup.addAnimation(anim); +//![5] + +//![6] + subGroup = new QSequentialAnimationGroup(&animationGroup); + subGroup->addPause(100); + anim = new QPropertyAnimation(button3, "geometry"); + anim->setDuration(1000); + anim->setEasingCurve(QEasingCurve::OutElastic); + subGroup->addAnimation(anim); +//![6] + + subGroup = new QSequentialAnimationGroup(&animationGroup); + subGroup->addPause(150); + anim = new QPropertyAnimation(button2, "geometry"); + anim->setDuration(1000); + anim->setEasingCurve(QEasingCurve::OutElastic); + subGroup->addAnimation(anim); + + subGroup = new QSequentialAnimationGroup(&animationGroup); + subGroup->addPause(200); + anim = new QPropertyAnimation(button1, "geometry"); + anim->setDuration(1000); + anim->setEasingCurve(QEasingCurve::OutElastic); + subGroup->addAnimation(anim); + +//![7] + StateSwitcher *stateSwitcher = new StateSwitcher(&machine); + stateSwitcher->setObjectName("stateSwitcher"); + group->addTransition(&timer, SIGNAL(timeout()), stateSwitcher); + stateSwitcher->addState(state1, &animationGroup); + stateSwitcher->addState(state2, &animationGroup); +//![7] + stateSwitcher->addState(state3, &animationGroup); + stateSwitcher->addState(state4, &animationGroup); + stateSwitcher->addState(state5, &animationGroup); + stateSwitcher->addState(state6, &animationGroup); +//![8] + stateSwitcher->addState(state7, &animationGroup); +//![8] + +//![9] + machine.addState(group); + machine.setInitialState(group); + machine.start(); +//![9] +#endif + + window.resize(300, 300); + window.show(); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + return app.exec(); +} + +#include "main.moc" diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro new file mode 100644 index 0000000..976ad61 --- /dev/null +++ b/pacman-c++/pacman.pro @@ -0,0 +1,5 @@ +SOURCES += main.cpp \ + pixmapitem.cpp +HEADERS += pixmapitem.h +RESOURCES += pacman.qrc + diff --git a/pacman-c++/pacman.qrc b/pacman-c++/pacman.qrc new file mode 100644 index 0000000..c209878 --- /dev/null +++ b/pacman-c++/pacman.qrc @@ -0,0 +1,5 @@ + + + pics/pacman10-hp-sprite-2.png + + diff --git a/pacman-c++/pics/pacman10-hp-sprite-2.png b/pacman-c++/pics/pacman10-hp-sprite-2.png new file mode 100644 index 0000000..7354b9d Binary files /dev/null and b/pacman-c++/pics/pacman10-hp-sprite-2.png differ diff --git a/pacman-c++/pixmapitem.cpp b/pacman-c++/pixmapitem.cpp new file mode 100644 index 0000000..9c2f941 --- /dev/null +++ b/pacman-c++/pixmapitem.cpp @@ -0,0 +1,44 @@ +#include "pixmapitem.h" +#include + +PixmapItem::PixmapItem(const QString &fileName, QGraphicsItem *parent) + : QGraphicsObject(parent), m_x(0), m_y(0) +{ + m_pix = ":/" + fileName; + m_width = m_pix.width(); + m_height = m_pix.height(); +} + +PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) + : QGraphicsObject(), m_x(0), m_y(0) +{ + m_pix = ":/" + fileName; + m_width = m_pix.width(); + m_height = m_pix.height(); + scene->addItem(this); +} + +void PixmapItem::setSprite(int x, int y, int width, int height) +{ + m_x = x; + m_y = y; + m_width = width; + m_height = height; +} + +QSizeF PixmapItem::size() const +{ + return QSizeF(m_width, m_height); +} + +QRectF PixmapItem::boundingRect() const +{ + return QRectF(QPointF(0, 0), size()); +} + +void PixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + painter->drawPixmap(0, 0, m_pix, m_x, m_y, m_width, m_height); +} + + diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h new file mode 100644 index 0000000..4ea3748 --- /dev/null +++ b/pacman-c++/pixmapitem.h @@ -0,0 +1,22 @@ +#ifndef __PIXMAPITEM__H__ +#define __PIXMAPITEM__H__ + +#include +#include + +class PixmapItem : public QGraphicsObject +{ +public: + PixmapItem(const QString &fileName, QGraphicsItem *parent = 0); + PixmapItem(const QString &fileName, QGraphicsScene *scene); + void setSprite(int x, int y, int width, int height); + QSizeF size() const; + QRectF boundingRect() const; + void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); +private: + QPixmap m_pix; + int m_x, m_y; + int m_width, m_height; +}; + +#endif diff --git a/pacman-google/src/js/pacman10-hp.3.js b/pacman-google/src/js/pacman10-hp.3.js index fe3ef91..604c64d 100644 --- a/pacman-google/src/js/pacman10-hp.3.js +++ b/pacman-google/src/js/pacman10-hp.3.js @@ -1803,10 +1803,12 @@ function () { return [c, b] }; E.prototype.b = function () { + // this calculates the sprite position this.k(); var b = [0, 0]; b = g.gameplayMode == 8 || g.gameplayMode == 14 ? [0, 3] : this.ghost ? this.r() : this.s(); if (this.elBackgroundPos[0] != b[0] || this.elBackgroundPos[1] != b[1]) { + console.log("changeElementBkPos: b=" + b) this.elBackgroundPos[0] = b[0]; this.elBackgroundPos[1] = b[1]; b[0] *= 16; @@ -1857,6 +1859,7 @@ function () { b.style.backgroundImage = "url(src/pacman10-hp-sprite-2.png)"; b.style.backgroundPosition = -c + "px " + -d + "px"; b.style.backgroundRepeat = "no-repeat" + console.log("prepareElement: id=" + b.id + ", bgpos=", b.style.backgroundPosition) } else { b.style.overflow = "hidden"; c = "display: block; position: relative; left: " + -c + "px; top: " + -d + "px"; @@ -1865,10 +1868,15 @@ function () { }; g.changeElementBkPos = function (b, c, d, f) { if (f) { + console.log("changeElementBkPos: c=" + c + ", d=" + d) c = g.getCorrectedSpritePos(c); d = g.getCorrectedSpritePos(d) + console.log("changeElementBkPos: c=" + c + ", d=" + d) + } + if (g.useCss) { + b.style.backgroundPosition = -c + "px " + -d + "px"; + console.log("changeElementBkPos: id=" + b.id + ", bgpos=", b.style.backgroundPosition) } - if (g.useCss) b.style.backgroundPosition = -c + "px " + -d + "px"; else if (b.childNodes[0]) { b.childNodes[0].style.left = -c + "px"; b.childNodes[0].style.top = -d + "px" @@ -2366,7 +2374,12 @@ function () { for (var b = g.playerCount; b < g.playerCount + 4; b++) g.actors[b].B() }; g.moveActors = function () { - for (var b in g.actors) g.actors[b].move() + for (var b in g.actors) + { + if (g.actors[b].ghost) + continue; + g.actors[b].move() + } }; g.ghostDies = function (b, c) { g.playSound("eating-ghost", 0); @@ -3197,4 +3210,4 @@ function () { g.prepareSound() }; g.init(); -}(); \ No newline at end of file +}(); -- cgit v1.2.3