[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/games/kblocks
From:       Mauricio Piacentini <piacentini () kde ! org>
Date:       2008-04-01 2:59:26
Message-ID: 1207018766.883059.24508.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 792429 by piacentini:

Big update, result of a three day vacation :) Added:
KScoreDialog, KGameDifficulty, display of points, pot extraction
More to come before the move to kdereview.



 A             Messages.sh  
 M  +72 -1     kblocks.cpp  
 M  +8 -1      kblocks.h  
 M  +6 -0      kblocks.kcfg  
 M  +36 -4     kblocksscene.cpp  
 M  +5 -0      kblocksscene.h  
 M  +4 -1      kblocksui.rc  
 M  +1 -0      kblocksview.h  
 M  +1 -1      settings.kcfgc  


--- trunk/playground/games/kblocks/kblocks.cpp #792428:792429
@@ -25,12 +25,16 @@
 #include <KIcon>
 #include <KDE/KScoreDialog>
 #include <KDE/KGameThemeSelector>
+#include <KDE/KStatusBar>
 #include <KLocale>
 #include <KToggleAction>
 #include <KActionCollection>
 
 #include <QPixmapCache>
+#include <QCloseEvent>
 
+#include <time.h>
+
 #include "settings.h"
 
 /**
@@ -46,12 +50,18 @@
     view = new KBlocksView( this );
     setCentralWidget( view );
     
+    qsrand(time(0));
+    setAutoSaveSettings();
+    
     QAction *action = KStandardGameAction::gameNew(view, SLOT(newGame()), \
actionCollection());  actionCollection()->addAction("newGame", action);
     
     m_pauseAction = KStandardGameAction::pause(this, SLOT(pauseGame()), \
actionCollection());  actionCollection()->addAction("pauseGame", m_pauseAction);
-
+    
+    action = KStandardGameAction::highscores(this, SLOT(showHighscore()), \
actionCollection()); +    actionCollection()->addAction("showHighscores", action);
+    
     action = KStandardGameAction::quit(this, SLOT(close()), actionCollection());
     actionCollection()->addAction("quit", action);
     
@@ -86,6 +96,26 @@
     movedown->setIcon(KIcon("arrow-down"));
     movedown->setShortcuts( KShortcut( Qt::Key_Down ) );
     connect(movedown, SIGNAL(triggered(bool)), view, SLOT(moveDown()));
+    
+    statusBar()->insertItem( i18n("Points: 0 - Lines: 0 - Level: 0"), 0 );
+    connect(view->getSceneObject(), SIGNAL(scoreChanged(int,int,int)), this,  \
SLOT(onScoreChanged(int,int,int))); +    connect(view->getSceneObject(), \
SIGNAL(isHighscore(int,int)), this,  SLOT(onIsHighscore(int,int))); +    
+    KGameDifficulty::init(this, this, \
SLOT(levelChanged(KGameDifficulty::standardLevel))); +    \
KGameDifficulty::setRestartOnChange(KGameDifficulty::RestartOnChange); +    \
KGameDifficulty::addStandardLevel(KGameDifficulty::Easy); +    \
KGameDifficulty::addStandardLevel(KGameDifficulty::Medium); +    \
KGameDifficulty::addStandardLevel(KGameDifficulty::Hard); +    
+    //restore difficulty from settings, need to read it first
+    Settings::self()->readConfig();
+    int difficulty = Settings::difficulty();
+    if ((difficulty < KGameDifficulty::Easy) || (difficulty > KGameDifficulty::Hard) \
) +      //unexpected, but use a default
+      KGameDifficulty::setLevel(KGameDifficulty::Easy);
+    else
+      KGameDifficulty::setLevel((KGameDifficulty::standardLevel) (difficulty));
+    
     setupGUI();
 }
 
@@ -94,11 +124,40 @@
     delete view;
 }
 
+void KBlocks::close()
+{
+  Settings::self()->writeConfig();
+}
+
+void KBlocks::closeEvent(QCloseEvent *event)
+{
+  close();
+  event->accept();
+}
+
 void KBlocks::pauseGame()
 {
   view->pauseGame(m_pauseAction->isChecked());
 }
 
+void KBlocks::showHighscore()
+{
+  KScoreDialog ksdialog(KScoreDialog::Name | KScoreDialog::Level | \
KScoreDialog::Score, this); +  \
ksdialog.setConfigGroup(KGameDifficulty::levelString()); +  ksdialog.exec();
+}
+
+void KBlocks::onIsHighscore(int points, int level)
+{
+  KScoreDialog ksdialog( KScoreDialog::Name | KScoreDialog::Level | \
KScoreDialog::Score, this ); +  \
ksdialog.setConfigGroup(KGameDifficulty::levelString()); +  KScoreDialog::FieldInfo \
info; +  info[KScoreDialog::Score].setNum( points );
+  info[KScoreDialog::Level].setNum( level );
+  if ( ksdialog.addScore( info ) )
+    ksdialog.exec();
+}
+
 void KBlocks::configureSettings()
 {
   if ( KConfigDialog::showDialog("settings") ) {
@@ -115,5 +174,17 @@
 
 }
 
+void KBlocks::onScoreChanged(int points, int lines, int level)
+{
+  statusBar()->changeItem( i18n("Points: %1 - Lines: %2 - Level: %3", points, lines, \
level), 0 ); +}
 
+void KBlocks::levelChanged(KGameDifficulty::standardLevel)
+{
+  //Scene reads the difficulty level for us
+  view->newGame();
+}
+
+
+
 #include "kblocks.moc"
--- trunk/playground/games/kblocks/kblocks.h #792428:792429
@@ -12,6 +12,7 @@
 #define _KBLOCKS_H
 
 #include <KXmlGuiWindow>
+#include <KGameDifficulty>
 
 #include "kblocksview.h"
 
@@ -33,9 +34,15 @@
      * Default Destructor */
       ~KBlocks();
   private slots:
+      void close();
       void configureSettings();
       void pauseGame();
-
+      void showHighscore();
+      void onScoreChanged(int points, int lines, int level);
+      void onIsHighscore(int points, int level);
+      void levelChanged(KGameDifficulty::standardLevel);
+  protected:
+      virtual void closeEvent(QCloseEvent *);
     private:
       KBlocksView* view;
       QAction*  m_pauseAction;
--- trunk/playground/games/kblocks/kblocks.kcfg #792428:792429
@@ -9,4 +9,10 @@
       <label>The graphical theme to be used.</label>
     </entry>
   </group>
+  <group name="Preferences">
+    <entry name="Difficulty" type="Int">
+      <label>The difficulty level.</label>
+      <default>0</default>
+    </entry>
+  </group>
 </kcfg>
--- trunk/playground/games/kblocks/kblocksscene.cpp #792428:792429
@@ -13,6 +13,7 @@
 #include <QtDebug>
 #include <KStandardDirs>
 #include <KDE/KGamePopupItem>
+#include <KDE/KGameDifficulty>
 
 #include "settings.h"
 
@@ -138,6 +139,8 @@
           showMessage( end, 4000 );
           stepTimer.stop();
           gameState=Game_Finished;
+          //check if we have a highscore, but allow player to see the game over \
first +          QTimer::singleShot(1500, this, SLOT(checkHighscore())); 
         } else {
           //we hit something, stop and detach
           freezePiece(piece);
@@ -187,10 +190,29 @@
   inLockPosition = false;
   updateInterval = INITIAL_UPDATE_INTERVAL;
   stepTimer.setInterval(updateInterval);
+  KGameDifficulty::standardLevel level = KGameDifficulty::level();
+  Settings::setDifficulty((int) level);
+  switch(KGameDifficulty::level())
+  {
+    case KGameDifficulty::Easy:
+      //nothing to do
+      break;
+    case KGameDifficulty::Medium:
+      for (short a=0; a<5; a++) levelUp();
+      break;
+    case KGameDifficulty::Hard:
+      for (short a=0; a<10; a++) levelUp();
+      break;
+    default:
+      //unsupported
+      break;
+  }
   gameState=Game_Active;
     //Fire the first piece in two seconds
   releaseTimer.start(2000);
   QTimer::singleShot(500, this, SLOT(greetPlayer())); 
+  KGameDifficulty::setRunning(true);
+  emit scoreChanged(currentPoints, currentRemovedLines, currentLevel);
 }
 
 void KBlocksScene::greetPlayer()
@@ -199,6 +221,11 @@
    showMessage( start, 2000 ); 
 }
 
+void KBlocksScene::checkHighscore()
+{
+  emit isHighscore(currentPoints, currentLevel);
+}
+
 void KBlocksScene::pauseGame(bool pause, bool fromUI)
 {
   //Only work for paused, suspended and active states
@@ -216,7 +243,7 @@
       if (gameState==Game_Active) stepTimer.start();
       if (gameState==Game_Paused) stepTimer.stop();
     }
-    return;
+    goto setStatusAndExit;
   }
   
   if ((gameState==Game_Paused)&&!pause)  {
@@ -233,6 +260,10 @@
     //inconsistency, restore state and log
     kDebug()<<"Inconsistent Game State at pauseGame:"<<gameState<<pause;
   }
+setStatusAndExit:
+  //Set KGameDifficulty state, for prompting user on restart
+  if (gameState==Game_Active) KGameDifficulty::setRunning(true);
+  else KGameDifficulty::setRunning(false);
 }
 
 void KBlocksScene::levelUp()
@@ -308,7 +339,7 @@
   //Adjust the position of the blocks so that the piece is nicely centered in the \
preview area  centerPiecePreview(nextPiece);
   
-  FadeAnimator * fadeInAnim = new FadeAnimator(nextPiece->children(), 200, \
QTimeLine::Forward, false); +  FadeAnimator * fadeInAnim = new \
FadeAnimator(nextPiece->children(), 100, QTimeLine::Forward, false);  animators << \
fadeInAnim;  connect(fadeInAnim, SIGNAL(finished(QObject *)), \
SLOT(animationFinished(QObject *)) );  }
@@ -352,7 +383,7 @@
   connect(fadeInAnim, SIGNAL(finished(QObject *)), SLOT(animationFinished(QObject \
*)) );  
   //FadeOut animator also removes blocks from scene and deletes them when it is done \
                :)
-  FadeAnimator * fadeOutAnim = new FadeAnimator(nextPiece->children(), 200, \
QTimeLine::Backward, true); +  FadeAnimator * fadeOutAnim = new \
FadeAnimator(nextPiece->children(), 100, QTimeLine::Backward, true);  animators << \
fadeOutAnim;  connect(fadeOutAnim, SIGNAL(finished(QObject *)), \
SLOT(animationFinished(QObject *)) );  foreach(Block * block, nextPiece->children()){
@@ -634,7 +665,8 @@
   }
   //scoreArea->setPlainText(QString("%1").arg(currentPoints));
   //levelArea->setPlainText(QString("%1").arg(currentLevel));
-  kDebug(11000) << "Points:" << currentPoints << "Lines:" << currentRemovedLines << \
"Level:" << currentLevel; +  //kDebug(11000) << "Points:" << currentPoints << \
"Lines:" << currentRemovedLines << "Level:" << currentLevel; +  emit \
scoreChanged(currentPoints, currentRemovedLines, currentLevel);  }
 
 void KBlocksScene::animationFinished(QObject * animation)
--- trunk/playground/games/kblocks/kblocksscene.h #792428:792429
@@ -50,6 +50,10 @@
     explicit KBlocksScene();
     virtual ~KBlocksScene();
     
+  signals:
+    void scoreChanged(int,int,int);
+    void isHighscore(int,int);
+    
   public slots:
     void pauseGame(bool pause, bool fromUI);
     void startGame();
@@ -112,6 +116,7 @@
     void animationFinished(QObject * animation);
     void releasePiece();
     void greetPlayer();
+    void checkHighscore();
     
 protected:
     void mousePressEvent(QGraphicsSceneMouseEvent *keyEvent);
--- trunk/playground/games/kblocks/kblocksui.rc #792428:792429
@@ -1,10 +1,13 @@
 <!DOCTYPE kpartgui>
-<kpartgui name="kblocks" version="1">
+<kpartgui name="kblocks" version="2">
 
 <MenuBar>
   <Menu name="game">
     <Action name="newGame" />
     <Action name="pauseGame" />
+    <Seperator/>
+    <Action name="showHighscores" />
+    <Seperator/>
     <Action name="quit" />
   </Menu>
 </MenuBar>
--- trunk/playground/games/kblocks/kblocksview.h #792428:792429
@@ -21,6 +21,7 @@
   public:
     KBlocksView (QWidget * parent = 0 );
     ~KBlocksView();
+    QObject * getSceneObject() { return (QObject *) m_scene; }
     
   public slots:
     void newGame();
--- trunk/playground/games/kblocks/settings.kcfgc #792428:792429
@@ -2,4 +2,4 @@
 File=kblocks.kcfg
 ClassName=Settings
 Singleton=true
-Mutators=false
+Mutators=true


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic