Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

klines.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           klines.cpp  -  description
00003                              -------------------
00004     begin                : Fri May 19 2000
00005     copyright            : (C) 2000 by Roman Merzlyakov
00006     email                : roman@sbrf.barrt.ru
00007     copyright            : (C) 2000 by Roman Razilov
00008     email                : Roman.Razilov@gmx.de
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   (at your option) any later version.                                   *
00017  *                                                                         *
00018  ***************************************************************************/
00019 /* changes
00020 21.05.2000    Roman Razilov     Menu game/Next
00021 */
00022 //
00023 // The implementation of the KLines widget
00024 //
00025 
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 #include <time.h>
00029 
00030 #include <qapplication.h>
00031 
00032 
00033 #include "klines.h"
00034 
00035 
00036 /*
00037    Creates the KLines widget and sets saved options (if any).
00038  */
00039 
00040 KLines::KLines(QWidget *par, const char* n, WFlags fl) : QMainWindow(par,n,fl)
00041 {
00042         time_t t;
00043         time(&t);
00044         srand((unsigned int)t + getpid());
00045 
00046         setCaption(QString("ZLines"));
00047 
00048         mwidget = new MainWidget(this);
00049         setCentralWidget( mwidget );
00050 
00051         lsb = mwidget->GetLsb();
00052         lPrompt = mwidget->GetPrompt();
00053 
00054         menu = menuBar();
00055         game = new QPopupMenu;
00056         edit = new QPopupMenu;
00057 
00058         game->insertItem(tr("&New game"), this, SLOT(stopGame()), CTRL+Key_N );
00059         game->insertSeparator();
00060         game->insertItem(tr("Ne&xt"), this, SLOT(makeTurn()), Key_N );
00061         game->insertSeparator();
00062         idMenuPrompt = game->insertItem( tr("&Show next"), this, SLOT(switchPrompt()), CTRL+Key_P );
00063         game->setCheckable(true);
00064         game->setItemChecked(idMenuPrompt, lPrompt->getState());
00065         game->insertSeparator();
00066         game->insertItem(tr("&Quit"), qApp, SLOT(quit()), CTRL+Key_Q );
00067 
00068         idMenuUndo = edit->insertItem(tr("Und&o"), this, SLOT(undo()), CTRL+Key_Z );
00069 
00070         menu->insertItem( tr("&Game"), game );
00071         menu->insertItem( tr("&Edit"), edit );
00072 
00073         menu->show();
00074 
00075         score = 0;
00076         prev_score = 0;
00077 
00078         mwidget->setMessage(tr("Points: 0"));
00079 
00080         startGame();
00081 }
00082 
00083 /*
00084    Saves the options and destroys the KLines widget.
00085  */
00086 
00087 KLines::~KLines()
00088 {
00089 }
00090 
00091 /*
00092    Resize event of the KLines widget.
00093  */
00094 
00095 void KLines::resizeEvent( QResizeEvent *e )
00096 {
00097         QMainWindow::resizeEvent(e);
00098 }
00099 
00100 void KLines::setMinSize()
00101 {
00102         //  setMinimumSize( gr->wHint(), gr->hHint() + menu->height() + stat->height() +
00103         //      tool->height() );
00104 }
00105 
00106 void KLines::startGame()
00107 {
00108         score = 0;
00109         prev_score = 0;
00110         bUndo=TRUE;
00111 
00112         lsb->clearField();
00113         generateRandomBalls();
00114         placeBalls();
00115         generateRandomBalls();
00116         edit->setItemEnabled(idMenuUndo, FALSE );
00117         updateStat();
00118 }
00119 void KLines::stopGame()
00120 {
00121         debug("Klines::stopGame");
00122         endGame();
00123 }
00124 
00125 void KLines::searchBallsLine()
00126 {
00127 }
00128 
00129 void KLines::generateRandomBalls()
00130 {
00131 
00132         for( int i = 0 ; i < BALLSDROP ; i++ )
00133         {
00134                 nextBalls_undo[i] = nextBalls[i];               
00135                 nextBalls[i] = bUndo ?
00136                         rand() % NCOLORS:
00137                         nextBalls_redo[i];
00138         }
00139         lPrompt->SetBalls(nextBalls);
00140 }
00141 
00142 void KLines::placeBalls()
00143 {
00144         lsb->placeBalls(nextBalls);
00145         debug("exit from placeBalls");
00146 }
00147 
00148 void KLines::undo()
00149 {
00150         debug("Undo");
00151         if (!bUndo)
00152                 return;
00153         for( int i = 0 ; i < BALLSDROP ; i++ )
00154         {
00155                 nextBalls_redo[i] = nextBalls[i];               
00156                 nextBalls[i] = nextBalls_undo[i];
00157         }
00158         lPrompt->SetBalls(nextBalls);
00159         lsb->undo();
00160         switchUndo(FALSE);
00161 }
00162 
00163 void KLines::makeTurn()
00164 {
00165         placeBalls();
00166         generateRandomBalls();
00167         switchUndo(TRUE);
00168 }
00169 
00170 void KLines::addScore(int ballsErased)
00171 {   if(ballsErased >= 5){
00172         score += 2*ballsErased*ballsErased - 20*ballsErased + 60 ;
00173         if( !lPrompt->getState() ) score+= 1;
00174         updateStat();
00175                                                 };
00176 }
00177 
00178 void KLines::updateStat()
00179 {
00180         mwidget->setMessage(tr(" Score: %1 ").arg(score));
00181 }
00182 
00183 void KLines::endGame()
00184 {
00185         startGame();
00186 }
00187 
00188 void KLines::switchPrompt()
00189 {
00190         lPrompt->setPrompt(!lPrompt->getState());
00191         game->setItemChecked(idMenuPrompt, lPrompt->getState());
00192 }
00193 
00194 void KLines::switchUndo(bool bu)
00195 {
00196         bUndo = bu;
00197         edit->setItemEnabled(idMenuUndo, bUndo );
00198 }
00199 
00200 void KLines::help()
00201 {
00202         //  KApplication::getKApplication()->invokeHTMLHelp("", "");
00203 }
00204 
00205 

Generated on Sat Nov 5 16:17:28 2005 for OPIE by  doxygen 1.4.2