summaryrefslogtreecommitdiffstats
path: root/pacman-c++
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2011-04-03 17:01:08 +0200
committermanuel <manuel@mausz.at>2011-04-03 17:01:08 +0200
commit11e767ea030cbdab3f0374f61efc3a813746fad5 (patch)
tree8351c8b61092874286f9f78f96ad8b5d86ca66e6 /pacman-c++
parent818383fec4f220a2410177b58518797e81d8eab3 (diff)
downloadfoop-11e767ea030cbdab3f0374f61efc3a813746fad5.tar.gz
foop-11e767ea030cbdab3f0374f61efc3a813746fad5.tar.bz2
foop-11e767ea030cbdab3f0374f61efc3a813746fad5.zip
extreme cleanup
Diffstat (limited to 'pacman-c++')
-rw-r--r--pacman-c++/actor.cpp7
-rw-r--r--pacman-c++/actor.h30
-rw-r--r--pacman-c++/main.cpp269
-rw-r--r--pacman-c++/pacman.pro7
-rw-r--r--pacman-c++/pixmapitem.cpp32
-rw-r--r--pacman-c++/pixmapitem.h3
6 files changed, 62 insertions, 286 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp
new file mode 100644
index 0000000..860f490
--- /dev/null
+++ b/pacman-c++/actor.cpp
@@ -0,0 +1,7 @@
1#include "actor.h"
2
3Actor::Actor(Type type)
4 : PixmapItem("google-pacman-sprite"), m_type(type), m_direction(Actor::None)
5{
6 setSprite(2, 2, 16, 16);
7}
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h
new file mode 100644
index 0000000..753c920
--- /dev/null
+++ b/pacman-c++/actor.h
@@ -0,0 +1,30 @@
1#ifndef ACTOR_H
2#define ACTOR_H
3
4#include "pixmapitem.h"
5
6class Actor
7 : public PixmapItem
8{
9public:
10 enum Movement {
11 None = 0,
12 Left,
13 Right,
14 Up,
15 Down
16 };
17
18 enum Type {
19 Player1 = 1,
20 Player2,
21 Player3,
22 };
23
24 Actor(Type type);
25private:
26 Type m_type;
27 Movement m_direction;
28};
29
30#endif // ACTOR_H
diff --git a/pacman-c++/main.cpp b/pacman-c++/main.cpp
index 5fa9c1c..ac55036 100644
--- a/pacman-c++/main.cpp
+++ b/pacman-c++/main.cpp
@@ -1,286 +1,23 @@
1#include "actor.h"
1#include "pixmapitem.h" 2#include "pixmapitem.h"
2#include <QtCore> 3#include <QtCore>
3#include <QtGui> 4#include <QtGui>
4 5
5//![15]
6class StateSwitchEvent: public QEvent
7{
8public:
9 StateSwitchEvent()
10 : QEvent(Type(StateSwitchType))
11 {
12 }
13
14 StateSwitchEvent(int rand)
15 : QEvent(Type(StateSwitchType)),
16 m_rand(rand)
17 {
18 }
19
20 enum { StateSwitchType = QEvent::User + 256 };
21
22 int rand() const { return m_rand; }
23
24private:
25 int m_rand;
26};
27//![15]
28
29//![16]
30class QGraphicsRectWidget : public QGraphicsWidget
31{
32public:
33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *,
34 QWidget *)
35 {
36 painter->fillRect(rect(), Qt::blue);
37 }
38};
39//![16]
40
41class StateSwitchTransition: public QAbstractTransition
42{
43public:
44 StateSwitchTransition(int rand)
45 : QAbstractTransition(),
46 m_rand(rand)
47 {
48 }
49
50protected:
51//![14]
52 virtual bool eventTest(QEvent *event)
53 {
54 return (event->type() == QEvent::Type(StateSwitchEvent::StateSwitchType))
55 && (static_cast<StateSwitchEvent *>(event)->rand() == m_rand);
56 }
57//![14]
58
59 virtual void onTransition(QEvent *) {}
60
61private:
62 int m_rand;
63};
64
65//![10]
66class StateSwitcher : public QState
67{
68 Q_OBJECT
69public:
70 StateSwitcher(QStateMachine *machine)
71 : QState(machine), m_stateCount(0), m_lastIndex(0)
72 { }
73//![10]
74
75//![11]
76 virtual void onEntry(QEvent *)
77 {
78 int n;
79 while ((n = (qrand() % m_stateCount + 1)) == m_lastIndex)
80 { }
81 m_lastIndex = n;
82 machine()->postEvent(new StateSwitchEvent(n));
83 }
84 virtual void onExit(QEvent *) {}
85//![11]
86
87//![12]
88 void addState(QState *state, QAbstractAnimation *animation) {
89 StateSwitchTransition *trans = new StateSwitchTransition(++m_stateCount);
90 trans->setTargetState(state);
91 addTransition(trans);
92 trans->addAnimation(animation);
93 }
94//![12]
95
96private:
97 int m_stateCount;
98 int m_lastIndex;
99};
100
101//![13]
102QState *createGeometryState(QObject *w1, const QRect &rect1,
103 QObject *w2, const QRect &rect2,
104 QObject *w3, const QRect &rect3,
105 QObject *w4, const QRect &rect4,
106 QState *parent)
107{
108 QState *result = new QState(parent);
109 result->assignProperty(w1, "geometry", rect1);
110 result->assignProperty(w2, "geometry", rect2);
111 result->assignProperty(w3, "geometry", rect3);
112 result->assignProperty(w4, "geometry", rect4);
113
114 return result;
115}
116//![13]
117
118int main(int argc, char **argv) 6int main(int argc, char **argv)
119{ 7{
120 QApplication app(argc, argv); 8 QApplication app(argc, argv);
121 9
122#if 0
123 QWidget window;
124 QPalette palette;
125 palette.setBrush(QPalette::Window, Qt::black);
126 window.setPalette(palette);
127
128 QPushButton *button1 = new QPushButton("A", &window);
129 button1->setObjectName("button1");
130
131 QPushButton *button2 = new QPushButton("B", &window);
132 button2->setObjectName("button2");
133
134 QPushButton *button3 = new QPushButton("C", &window);
135 button3->setObjectName("button3");
136
137 QPushButton *button4 = new QPushButton("D", &window);
138 button4->setObjectName("button4");
139
140#else
141//![1]
142 //QGraphicsRectWidget *button1 = new QGraphicsRectWidget;
143 QGraphicsRectWidget *button2 = new QGraphicsRectWidget;
144 QGraphicsRectWidget *button3 = new QGraphicsRectWidget;
145 QGraphicsRectWidget *button4 = new QGraphicsRectWidget;
146 button2->setZValue(1);
147 button3->setZValue(2);
148 button4->setZValue(3);
149
150 //QPixmap pixmap(":/pacman10-hp-sprite-2");
151
152 QGraphicsScene scene(0, 0, 300, 300); 10 QGraphicsScene scene(0, 0, 300, 300);
153 scene.setBackgroundBrush(Qt::black); 11 scene.setBackgroundBrush(Qt::black);
154 12
155 PixmapItem *button1 = new PixmapItem("google-pacman-sprite", &scene); 13 Actor *actor1 = new Actor(Actor::Player1);
156 button1->setSprite(2, 2, 16, 16); 14 scene.addItem(actor1);
157 15
158 //scene.addItem(button2);
159 //scene.addItem(button3);
160 //scene.addItem(button4);
161//![1]
162 QGraphicsView window(&scene); 16 QGraphicsView window(&scene);
163 window.setFrameStyle(0); 17 window.setFrameStyle(0);
164 window.setAlignment(Qt::AlignLeft | Qt::AlignTop); 18 window.setAlignment(Qt::AlignLeft | Qt::AlignTop);
165 window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 19 window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
166 window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 20 window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
167#endif
168
169#if 0
170//![2]
171 QStateMachine machine;
172
173 QState *group = new QState();
174 group->setObjectName("group");
175 QTimer timer;
176 timer.setInterval(1250);
177 timer.setSingleShot(true);
178 QObject::connect(group, SIGNAL(entered()), &timer, SLOT(start()));
179//![2]
180
181//![3]
182 QState *state1;
183 QState *state2;
184 QState *state3;
185 QState *state4;
186 QState *state5;
187 QState *state6;
188 QState *state7;
189
190 state1 = createGeometryState(button1, QRect(100, 0, 50, 50),
191 button2, QRect(150, 0, 50, 50),
192 button3, QRect(200, 0, 50, 50),
193 button4, QRect(250, 0, 50, 50),
194 group);
195//![3]
196 state2 = createGeometryState(button1, QRect(250, 100, 50, 50),
197 button2, QRect(250, 150, 50, 50),
198 button3, QRect(250, 200, 50, 50),
199 button4, QRect(250, 250, 50, 50),
200 group);
201 state3 = createGeometryState(button1, QRect(150, 250, 50, 50),
202 button2, QRect(100, 250, 50, 50),
203 button3, QRect(50, 250, 50, 50),
204 button4, QRect(0, 250, 50, 50),
205 group);
206 state4 = createGeometryState(button1, QRect(0, 150, 50, 50),
207 button2, QRect(0, 100, 50, 50),
208 button3, QRect(0, 50, 50, 50),
209 button4, QRect(0, 0, 50, 50),
210 group);
211 state5 = createGeometryState(button1, QRect(100, 100, 50, 50),
212 button2, QRect(150, 100, 50, 50),
213 button3, QRect(100, 150, 50, 50),
214 button4, QRect(150, 150, 50, 50),
215 group);
216 state6 = createGeometryState(button1, QRect(50, 50, 50, 50),
217 button2, QRect(200, 50, 50, 50),
218 button3, QRect(50, 200, 50, 50),
219 button4, QRect(200, 200, 50, 50),
220 group);
221//![4]
222 state7 = createGeometryState(button1, QRect(0, 0, 50, 50),
223 button2, QRect(250, 0, 50, 50),
224 button3, QRect(0, 250, 50, 50),
225 button4, QRect(250, 250, 50, 50),
226 group);
227 group->setInitialState(state1);
228//![4]
229
230//![5]
231 QParallelAnimationGroup animationGroup;
232 QSequentialAnimationGroup *subGroup;
233
234 QPropertyAnimation *anim = new QPropertyAnimation(button4, "geometry");
235 anim->setDuration(1000);
236 anim->setEasingCurve(QEasingCurve::OutElastic);
237 animationGroup.addAnimation(anim);
238//![5]
239
240//![6]
241 subGroup = new QSequentialAnimationGroup(&animationGroup);
242 subGroup->addPause(100);
243 anim = new QPropertyAnimation(button3, "geometry");
244 anim->setDuration(1000);
245 anim->setEasingCurve(QEasingCurve::OutElastic);
246 subGroup->addAnimation(anim);
247//![6]
248
249 subGroup = new QSequentialAnimationGroup(&animationGroup);
250 subGroup->addPause(150);
251 anim = new QPropertyAnimation(button2, "geometry");
252 anim->setDuration(1000);
253 anim->setEasingCurve(QEasingCurve::OutElastic);
254 subGroup->addAnimation(anim);
255
256 subGroup = new QSequentialAnimationGroup(&animationGroup);
257 subGroup->addPause(200);
258 anim = new QPropertyAnimation(button1, "geometry");
259 anim->setDuration(1000);
260 anim->setEasingCurve(QEasingCurve::OutElastic);
261 subGroup->addAnimation(anim);
262
263//![7]
264 StateSwitcher *stateSwitcher = new StateSwitcher(&machine);
265 stateSwitcher->setObjectName("stateSwitcher");
266 group->addTransition(&timer, SIGNAL(timeout()), stateSwitcher);
267 stateSwitcher->addState(state1, &animationGroup);
268 stateSwitcher->addState(state2, &animationGroup);
269//![7]
270 stateSwitcher->addState(state3, &animationGroup);
271 stateSwitcher->addState(state4, &animationGroup);
272 stateSwitcher->addState(state5, &animationGroup);
273 stateSwitcher->addState(state6, &animationGroup);
274//![8]
275 stateSwitcher->addState(state7, &animationGroup);
276//![8]
277
278//![9]
279 machine.addState(group);
280 machine.setInitialState(group);
281 machine.start();
282//![9]
283#endif
284 21
285 window.resize(300, 300); 22 window.resize(300, 300);
286 window.show(); 23 window.show();
diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro
index 976ad61..a94121d 100644
--- a/pacman-c++/pacman.pro
+++ b/pacman-c++/pacman.pro
@@ -1,5 +1,6 @@
1SOURCES += main.cpp \ 1SOURCES += main.cpp \
2 pixmapitem.cpp 2 pixmapitem.cpp \
3HEADERS += pixmapitem.h 3 actor.cpp
4HEADERS += pixmapitem.h \
5 actor.h
4RESOURCES += pacman.qrc 6RESOURCES += pacman.qrc
5
diff --git a/pacman-c++/pixmapitem.cpp b/pacman-c++/pixmapitem.cpp
index 9c2f941..64ac594 100644
--- a/pacman-c++/pixmapitem.cpp
+++ b/pacman-c++/pixmapitem.cpp
@@ -2,43 +2,43 @@
2#include <QPainter> 2#include <QPainter>
3 3
4PixmapItem::PixmapItem(const QString &fileName, QGraphicsItem *parent) 4PixmapItem::PixmapItem(const QString &fileName, QGraphicsItem *parent)
5 : QGraphicsObject(parent), m_x(0), m_y(0) 5 : QGraphicsObject(parent), m_x(0), m_y(0)
6{ 6{
7 m_pix = ":/" + fileName; 7 m_pix = ":/" + fileName;
8 m_width = m_pix.width(); 8 m_width = m_pix.width();
9 m_height = m_pix.height(); 9 m_height = m_pix.height();
10} 10}
11 11
12PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) 12PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene)
13 : QGraphicsObject(), m_x(0), m_y(0) 13 : QGraphicsObject(), m_x(0), m_y(0)
14{ 14{
15 m_pix = ":/" + fileName; 15 m_pix = ":/" + fileName;
16 m_width = m_pix.width(); 16 m_width = m_pix.width();
17 m_height = m_pix.height(); 17 m_height = m_pix.height();
18 scene->addItem(this); 18 scene->addItem(this);
19} 19}
20 20
21void PixmapItem::setSprite(int x, int y, int width, int height) 21void PixmapItem::setSprite(int x, int y, int width, int height)
22{ 22{
23 m_x = x; 23 m_x = x;
24 m_y = y; 24 m_y = y;
25 m_width = width; 25 m_width = width;
26 m_height = height; 26 m_height = height;
27} 27}
28 28
29QSizeF PixmapItem::size() const 29QSizeF PixmapItem::size() const
30{ 30{
31 return QSizeF(m_width, m_height); 31 return QSizeF(m_width, m_height);
32} 32}
33 33
34QRectF PixmapItem::boundingRect() const 34QRectF PixmapItem::boundingRect() const
35{ 35{
36 return QRectF(QPointF(0, 0), size()); 36 return QRectF(QPointF(0, 0), size());
37} 37}
38 38
39void PixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) 39void PixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
40{ 40{
41 painter->drawPixmap(0, 0, m_pix, m_x, m_y, m_width, m_height); 41 painter->drawPixmap(0, 0, m_pix, m_x, m_y, m_width, m_height);
42} 42}
43 43
44 44
diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h
index 4ea3748..a94c251 100644
--- a/pacman-c++/pixmapitem.h
+++ b/pacman-c++/pixmapitem.h
@@ -4,7 +4,8 @@
4#include <QtGui/QGraphicsObject> 4#include <QtGui/QGraphicsObject>
5#include <QtGui/QGraphicsScene> 5#include <QtGui/QGraphicsScene>
6 6
7class PixmapItem : public QGraphicsObject 7class PixmapItem
8 : public QGraphicsObject
8{ 9{
9public: 10public:
10 PixmapItem(const QString &fileName, QGraphicsItem *parent = 0); 11 PixmapItem(const QString &fileName, QGraphicsItem *parent = 0);