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

kbounce.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000 Stefan Schimanski <1Stein@gmx.de>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License,Life or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this program; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include <qlayout.h>
00020 #include <qtimer.h>
00021 #include <qmessagebox.h>
00022 
00023 #include "kbounce.h"
00024 #include "game.h"
00025 
00026 KJezzball::KJezzball(QWidget *p, const char* n, WFlags f) 
00027     : QMainWindow(p,n,f), m_gameWidget( 0 )
00028 {
00029         setCaption(tr("Bounce"));
00030         // setup variables
00031         m_game.level = 1;
00032         m_game.score = 0;
00033         m_state = Idle;
00034 
00035 
00036         menu = menuBar();
00037         game = new QPopupMenu;
00038         game->insertItem(tr("&New game"), this, SLOT(newGame()), Key_N );
00039         game->insertItem(tr("&Pause game"), this, SLOT(pauseGame()), Key_P );
00040         game->insertSeparator();
00041         game->insertItem(tr("&About"), this, SLOT(about()));
00042         menu->insertItem( tr("&Game"), game );
00043 
00044         // create widgets
00045         m_view = new QWidget( this, "m_view" );
00046         setCentralWidget( m_view );
00047 
00048         m_layout = new QGridLayout( m_view );
00049         m_layout->setSpacing( 0 );
00050         m_layout->setMargin( 0 );
00051 
00052         ScoreLabel = new QLabel( m_view, "ScoreLabel" );
00053         ScoreLabel->setText( tr( "Score: 00" ) );
00054         ScoreLabel->setAlignment( int( QLabel::AlignCenter ) );
00055 
00056         m_layout->addWidget( ScoreLabel, 1, 0 );
00057 
00058         LivesLabel = new QLabel( m_view, "LivesLabel" );
00059         LivesLabel->setText( tr( "Lives: 0%" ) );
00060         LivesLabel->setAlignment( int( QLabel::AlignCenter ) );
00061 
00062         m_layout->addWidget( LivesLabel, 1, 2 );
00063 
00064         FilledLabel = new QLabel( m_view, "FilledLabel" );
00065         FilledLabel->setText( tr( "Filled: 00%" ) );
00066         FilledLabel->setAlignment( int( QLabel::AlignCenter ) );
00067 
00068         m_layout->addWidget( FilledLabel, 1, 1 );
00069 
00070         TimeLabel = new QLabel( m_view, "TimeLabel" );
00071         TimeLabel->setText( tr( "Time: 00" ) );
00072         TimeLabel->setAlignment( int( QLabel::AlignCenter ) );
00073 
00074         m_layout->addWidget( TimeLabel, 1, 3 );
00075 
00076         // create timers
00077         m_nextLevelTimer = new QTimer( this, "m_nextLevelTimer" );
00078         connect( m_nextLevelTimer, SIGNAL(timeout()), this, SLOT(switchLevel()) );
00079 
00080         m_gameOverTimer = new QTimer( this, "m_gameOverTimer" );
00081         connect( m_gameOverTimer, SIGNAL(timeout()), this, SLOT(gameOverNow()) );
00082 
00083         m_timer = new QTimer( this, "m_timer" );
00084         connect( m_timer, SIGNAL(timeout()), this, SLOT(second()) );
00085 
00086         // create demo game
00087         createLevel( 1 );
00088 }
00089 
00090 void KJezzball::newGame()
00091 {
00092         // Check for running game
00093         closeGame();
00094         if ( m_state==Idle )
00095         {
00096                 // update displays
00097                 m_game.level = 1;
00098                 m_game.score = 0;
00099 
00100                 setCaption(tr("Bounce Level %1").arg(m_game.level));
00101                 ScoreLabel->setText( tr( "Score: %1" ).arg(m_game.score) );
00102 
00103                 // start new game
00104                 m_state = Running;
00105 
00106                 createLevel( m_game.level );
00107                 startLevel();
00108         }
00109 }
00110 
00111 void KJezzball::about()
00112 {
00113         QMessageBox::information( this, tr("About"),
00114                                   tr("Written by: Stefan Schimanski\n"
00115                                   "Ported by: Martin Imobersteg\n"
00116                                   "\n"
00117                                   "Click to form walls.\n"
00118                                   "Hit space to switch wall direction.\n"
00119                                   "Try to reduce total space by 75%.\n"
00120                                   "\n"
00121                                   "This program is distributed under\n"
00122                                   "the terms of the GPL v2.") );
00123 }
00124 
00125 void KJezzball::closeGame()
00126 {
00127         if ( m_state!=Idle )
00128         {
00129                 stopLevel();
00130                 m_state = Idle;
00131         }
00132 }
00133 
00134 void KJezzball::pauseGame()
00135 {
00136         switch ( m_state )
00137         {
00138                 case Running:
00139                         m_state = Paused;
00140                         m_gameWidget->display( tr("Game paused.\nPress P to continue!") );
00141                         stopLevel();
00142                         break;
00143 
00144                 case Paused:
00145                 case Suspend:
00146                         m_state = Running;
00147                         m_gameWidget->display( QString::null );
00148                         startLevel();
00149                         break;
00150 
00151                 case Idle:
00152                         break;
00153         }
00154 }
00155 
00156 void KJezzball::gameOver()
00157 {
00158         stopLevel();
00159         m_gameOverTimer->start( 100, TRUE );
00160 }
00161 
00162 
00163 void KJezzball::gameOverNow()
00164 {
00165         m_state = Idle;
00166 
00167         QString score;
00168         score.setNum( m_game.score );
00169         QMessageBox::information( this, "Game Over", tr("Game Over!\nScore: %1").arg(score) );
00170 }
00171 
00172 void KJezzball::focusOutEvent( QFocusEvent *ev )
00173 {
00174         if ( m_state==Running )
00175         {
00176                 stopLevel();
00177                 m_state = Suspend;
00178         }
00179 
00180         QMainWindow::focusOutEvent( ev );
00181 }
00182 
00183 void KJezzball::focusInEvent ( QFocusEvent *ev )
00184 {
00185         if ( m_state==Suspend )
00186         {
00187                 startLevel();
00188                 m_state = Running;
00189         }
00190 
00191         QMainWindow::focusInEvent( ev );
00192 }
00193 
00194 void KJezzball::second()
00195 {
00196         m_level.time--;
00197         TimeLabel->setText( tr( "Time: %1" ).arg(m_level.time) );
00198         if ( m_level.time<=0 )
00199         {
00200                 gameOver();
00201         }
00202 }
00203 
00204 void KJezzball::died()
00205 {
00206         m_level.lifes--;
00207         LivesLabel->setText( tr( "Lives: %1" ).arg(m_level.lifes) );
00208         if ( m_level.lifes==0 ) gameOver();
00209 }
00210 
00211 void KJezzball::newPercent( int percent )
00212 {
00213         FilledLabel->setText( tr( "Filled: %1%" ).arg(percent) );
00214         if ( percent>=75 )
00215         {
00216                 m_level.score = m_level.lifes*15 + (percent-75)*2*(m_game.level+5);
00217                 nextLevel();
00218         }
00219 }
00220 
00221 void KJezzball::createLevel( int level )
00222 {
00223         // destroy old game
00224         if ( m_gameWidget ) delete m_gameWidget;
00225 
00226         m_gameWidget = new JezzGame( level+1, m_view, "m_gameWidget" );
00227 
00228         m_gameWidget->show();
00229         m_layout->addMultiCellWidget( m_gameWidget, 0, 0, 0, 3 );
00230         connect( m_gameWidget, SIGNAL(died()), this, SLOT(died()) );
00231         connect( m_gameWidget, SIGNAL(newPercent(int)), this, SLOT(newPercent(int)) );
00232 
00233         // update displays
00234         m_level.lifes = level+1;
00235         LivesLabel->setText( tr( "Lives: %1" ).arg(m_level.lifes) );
00236         FilledLabel->setText( tr( "Filled: 0%" ) );
00237 
00238         m_level.time = (level+2)*30;
00239         TimeLabel->setText( tr( "Time: %1" ).arg(m_level.time) );
00240 
00241         m_level.score = 0;
00242 }
00243 
00244 void KJezzball::startLevel()
00245 {
00246         if ( m_gameWidget )
00247         {
00248                 m_timer->start( 1000 );
00249                 m_gameWidget->start();
00250         }
00251 }
00252 
00253 void KJezzball::stopLevel()
00254 {
00255         if ( m_gameWidget )
00256         {
00257                 m_gameWidget->stop();
00258                 m_timer->stop();
00259         }
00260 }
00261 
00262 void KJezzball::nextLevel()
00263 {
00264         stopLevel();
00265         m_nextLevelTimer->start( 100, TRUE );
00266 }
00267 
00268 void KJezzball::switchLevel()
00269 {
00270         m_game.score += m_level.score;
00271         ScoreLabel->setText( tr( "Score: %1" ).arg(m_game.score) );
00272 
00273         QString score;
00274         score.setNum( m_level.score );
00275 
00276         QString level;
00277         level.setNum( m_game.level );
00278 
00279         QString foo = QString(
00280                         tr("Successfully cleared more than 75%.\n") +
00281                         tr("%1 points: 15 points per life\n").arg(m_level.lifes*15) +
00282                         tr("%1 points: Bonus\n").arg((m_gameWidget->percent()-75)*2*(m_game.level+5)) +
00283                         tr("%1 points: Total score\n").arg(score) +
00284                         tr("On to level %1.\nYou get %2 lives this time!")).arg(m_game.level+1).arg(m_game.level+2);
00285 
00286         QMessageBox::information( this, "Success", foo );
00287 
00288         m_game.level++;
00289 
00290         createLevel( m_game.level );
00291         startLevel();
00292 }
00293 
00294 void KJezzball::keyPressEvent( QKeyEvent *ev )
00295 {
00296         if ( ev->key() == Key_Space ||
00297                         ev->key() == Key_Up ||
00298                         ev->key() == Key_Down ||
00299                         ev->key() == Key_Right ||
00300                         ev->key() == Key_Left )
00301         {
00302                 m_gameWidget->changeCursor();
00303         }
00304         else
00305         {
00306                 ev->ignore();
00307         }
00308 }

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