~kubuntu-members/kapman/4.11

« back to all changes in this revision

Viewing changes to game.cpp

  • Committer: Thomas Gallinari
  • Date: 2008-08-19 19:34:28 UTC
  • Revision ID: git-v1:702a65698c721ccf8ca7cff7035276ccc74308b6
Adding sounds to the game with Phonon... Latency problems, maybe use several MediaObject ?

svn path=/trunk/playground/games/kapman/; revision=849579

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
const int Game::FPS = 40;
26
26
 
27
27
Game::Game(KGameDifficulty::standardLevel p_difficulty) : m_isCheater(false), m_lives(3), m_points(0), m_level(1), m_nbEatenGhosts(0) {
28
 
 
 
28
        // Create the MediaObject
 
29
        m_media = createPlayer(Phonon::GameCategory);
29
30
        // Tells the KGameDifficulty singleton that the game is not running
30
31
        KGameDifficulty::setRunning(false);
31
32
        // Initialize the characters speed considering the difficulty level
81
82
}
82
83
 
83
84
Game::~Game() {
 
85
        delete m_media;
84
86
        delete m_timer;
85
87
        delete m_bonusTimer;
86
88
        delete m_maze;
209
211
        }
210
212
}
211
213
 
 
214
void Game::playSound(const QString& p_sound) {
 
215
        if (m_media->currentSource().fileName() != p_sound) {
 
216
                m_media->setCurrentSource(p_sound);
 
217
        }
 
218
        m_media->play();
 
219
}
 
220
 
212
221
void Game::keyPressEvent(QKeyEvent* p_event) {
213
222
        // At the beginning or when paused, we start the timer when an arrow key is pressed
214
223
        if ((p_event->key() == Qt::Key_Up || p_event->key() == Qt::Key_Down || p_event->key() ==  Qt::Key_Left || p_event->key() == Qt::Key_Right) && !m_timer->isActive()) {
280
289
}
281
290
 
282
291
void Game::kapmanDeath() {
 
292
        playSound(KStandardDirs::locate("sound", "kapman/gameover.ogg"));
283
293
        m_lives--;
284
294
        m_kapman->die();
285
295
        emit(dataChanged(LivesInfo));
316
326
 
317
327
        // If the eaten element is a ghost, win 200 * number of eaten ghosts since the energizer was eaten
318
328
        if (p_element->getType() == Element::GHOST) {
 
329
                playSound(KStandardDirs::locate("sound", "kapman/ghost.ogg"));
319
330
                // Get the position of the ghost
320
 
                // A TESTER (le ghost a peut-être déjà bougé)
321
331
                qreal xPos = p_element->getX();
322
332
                qreal yPos = p_element->getY();
323
 
 
324
333
                // Add points to the score
325
334
                wonPoints = p_element->getPoints() * m_nbEatenGhosts;
326
 
 
327
 
                // Sends to the scene the number of points to display and its position 
 
335
                // Send to the scene the number of points to display and its position 
328
336
                emit(pointsToDisplay(wonPoints, xPos, yPos));
329
337
        }
330
338
        // Else you just win the value of the element
342
350
        }
343
351
        // If the eaten element is an energyzer we change the ghosts state
344
352
        if (p_element->getType() == Element::ENERGYZER) {
 
353
                playSound(KStandardDirs::locate("sound", "kapman/energizer.ogg"));
345
354
                for (int i = 0; i < m_ghosts.size(); i++) {
346
355
                        if(m_ghosts[i]->getState() != Ghost::EATEN) {
347
356
                                m_ghosts[i]->setState(Ghost::PREY);
350
359
                // Reset the number of eaten ghosts
351
360
                m_nbEatenGhosts = 0;
352
361
                emit(elementEaten(p_element->getX(), p_element->getY()));
353
 
        }
354
 
        else if (p_element->getType() == Element::PILL) {
 
362
        } else if (p_element->getType() == Element::PILL) {
 
363
                playSound(KStandardDirs::locate("sound", "kapman/pill.ogg"));
355
364
                emit(elementEaten(p_element->getX(), p_element->getY()));
356
 
        }
357
 
        else if (p_element->getType() == Element::BONUS) {
 
365
        } else if (p_element->getType() == Element::BONUS) {
 
366
                playSound(KStandardDirs::locate("sound", "kapman/bonus.ogg"));
358
367
                // Get the position of the Bonus
359
 
                // A TESTER (le ghost a peut-être déjà bougé)
360
368
                qreal xPos = p_element->getX();
361
369
                qreal yPos = p_element->getY();
362
370
 
375
383
}
376
384
 
377
385
void Game::nextLevel() {
 
386
        playSound(KStandardDirs::locate("sound", "kapman/levelup.ogg"));
378
387
        // Increment the level
379
388
        m_level++;
380
389
        // Initialize the maze items