From 11a1b82636ab75cb9d002712a3e0f353ad6b2579 Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 7 Apr 2011 04:02:55 +0200 Subject: add some sounds --- pacman-c++/actor.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++--- pacman-c++/actor.h | 11 +++++++++++ pacman-c++/mainwidget.cpp | 28 ++++++++++++++++++++++++--- pacman-c++/mainwidget.h | 17 +++++++++++------ pacman-c++/pacman.qrc | 6 ++++++ pacman-c++/pixmapitem.h | 4 ++-- pacman-c++/point.h | 2 +- 7 files changed, 101 insertions(+), 15 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index b6de49c..7358756 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -2,6 +2,7 @@ #include "animationmanager.h" #include #include +#include #include static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) @@ -13,9 +14,8 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local) { m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); - // DON'T set any pixmap here. we've a pixmap in the animation - //setPixmap(m_pix); - // higher player "over" lower player + /* DON'T set any pixmap here. we've a pixmap in the animation */ + /* higher player "over" lower player */ setZValue(m_color * 10); /* setup icon for player */ @@ -46,6 +46,17 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) m_eating.append(setupEatingAnimation(Actor::Up)); m_eating.append(setupEatingAnimation(Actor::Down)); + /* setup player */ + m_player = new Phonon::MediaObject(this); + Phonon::AudioOutput *audio_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); + Phonon::createPath(m_player, audio_output); + connect(m_player, SIGNAL(finished()), this, SLOT(enqueue())); + + /* preload sounds */ + m_sounds.append(new QFile(":/sound/wakawaka")); + m_sounds.append(new QFile(":/sound/die")); + m_sounds.append(new QFile(":/sound/eatingcherry")); + /* make the picture showing the current direction visible */ m_images[m_direction]->setVisible(true); } @@ -103,6 +114,13 @@ void Actor::move(Actor::Movement direction) if (isMoving()) return; + if (m_local && m_player->state() != Phonon::PlayingState) + { + m_player->stop(); + m_player->setCurrentSource(m_sounds[0]); + m_player->play(); + } + /* stop current animation */ if (direction != m_direction) { @@ -162,3 +180,27 @@ bool Actor::isMoving() { return (m_moving->state() == QAbstractAnimation::Running); } + +void Actor::enqueue() +{ + if (isMoving()) + m_player->enqueue(m_sounds[0]); +} + +void Actor::die() +{ + if (!m_local) + return; + m_player->stop(); + m_player->setCurrentSource(m_sounds[1]); + m_player->play(); +} + +void Actor::eatingCherry() +{ + if (!m_local) + return; + m_player->stop(); + m_player->setCurrentSource(m_sounds[2]); + m_player->play(); +} diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 68bf06f..36b2a49 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h @@ -6,10 +6,14 @@ #include #include #include +#include +#include class Actor : public PixmapItem { +Q_OBJECT + public: enum Movement { None = 0, @@ -28,6 +32,11 @@ public: bool isLocal(); void move(Movement direction); bool isMoving(); + void die(); + void eatingCherry(); + +private slots: + void enqueue(); private: QPixmap m_pix; @@ -39,6 +48,8 @@ private: QList m_images; QList m_eating; QParallelAnimationGroup *m_moving; + Phonon::MediaObject *m_player; + QList m_sounds; }; #endif // ACTOR_H diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index d0b2ad7..73612af 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -1,11 +1,13 @@ #include "mainwidget.h" - #include "actor.h" #include "block.h" #include "bonuspoint.h" #include "point.h" #include "constants.h" +#include +#include + // temporary Transmission::map_t createDummyMap() { @@ -204,7 +206,7 @@ Transmission::map_t createDummyMap() } MainWidget::MainWidget() - : m_currentKey(0) + : m_currentKey(0), m_running(false) { visualMap.resize(Constants::map_size.width); for (int i=0; iplay(); +#endif } void MainWidget::createGui() @@ -321,7 +332,7 @@ void MainWidget::updateMap(const Transmission::map_t& map) if (actor == NULL) { //qDebug() << "new actor of col" << color; - actor = new Actor(color); + actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing m_actors[color] = actor; m_scene->addItem(actor); actor->setPos(mapPositionToCoord(x, y)); @@ -403,6 +414,9 @@ Transmission::field_t MainWidget::translateKey(int key) void MainWidget::keyPressEvent(QKeyEvent* event) { + if (!m_running) + return; + QWidget::keyPressEvent(event); m_currentKey = translateKey(event->key()); @@ -436,6 +450,9 @@ void MainWidget::keyPressEvent(QKeyEvent* event) void MainWidget::keyReleaseEvent(QKeyEvent* event) { + if (!m_running) + return; + QWidget::keyReleaseEvent(event); Transmission::field_t releasedKey = translateKey(event->key()); if (releasedKey == m_currentKey) @@ -446,3 +463,8 @@ void MainWidget::keyReleaseEvent(QKeyEvent* event) m_currentKey = Transmission::direction_none; } } + +void MainWidget::startGame() +{ + m_running = true; +} diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index b15427c..6adcba2 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -1,11 +1,10 @@ #ifndef MAINWIDGET_H #define MAINWIDGET_H -#include -#include - #include "constants.h" #include "pixmapitem.h" +#include +#include class Actor; @@ -24,12 +23,15 @@ protected: private: void createGui(); - void updateMap(const Transmission::map_t& map); - void updateScore(); + bool isRunning(); - QVector< QVector< PixmapItem* > > visualMap; +private slots: + void startGame(); + +private: + QVector< QVector > visualMap; // data conversion QPoint mapPositionToCoord(unsigned int x, unsigned int y); @@ -46,6 +48,9 @@ private: // translate Qt::Key to our key format Transmission::field_t translateKey(int); + + // game running + bool m_running; }; #endif // MAINWIDGET_H diff --git a/pacman-c++/pacman.qrc b/pacman-c++/pacman.qrc index 6de5cc0..3752ebe 100644 --- a/pacman-c++/pacman.qrc +++ b/pacman-c++/pacman.qrc @@ -12,4 +12,10 @@ pics/bonuspoints.png pics/points.png + + sound/waka_waka.ogg + sound/die.ogg + sound/eating_cherry.ogg + sound/intro.ogg + diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index 35a6440..e576d77 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h @@ -1,5 +1,5 @@ -#ifndef __PIXMAPITEM__H__ -#define __PIXMAPITEM__H__ +#ifndef PIXMAPITEM__H +#define PIXMAPITEM__H #include #include diff --git a/pacman-c++/point.h b/pacman-c++/point.h index 847daed..1b5863a 100644 --- a/pacman-c++/point.h +++ b/pacman-c++/point.h @@ -1,5 +1,5 @@ #ifndef POINT_H -#define SPOINT_H +#define POINT_H #include "pixmapitem.h" -- cgit v1.2.3