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

interface.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "interface.h"
00022 #include "man.h"
00023 
00024 #include <opie2/oresource.h>
00025 
00026 #include <qtopia/qpeapplication.h>
00027 
00028 #include <qtoolbar.h>
00029 #include <qtoolbutton.h>
00030 
00031 ParaShoot::ParaShoot(QWidget* parent, const char* name, WFlags f) :
00032     QMainWindow(parent,name,f),
00033     canvas( 232, 258 ),
00034     cannon(NULL),
00035     base(NULL),
00036     gamestopped( true ),
00037     waitover( false ),
00038     fanfare("level_up"),
00039     score(0)
00040 {
00041     QPEApplication::grabKeyboard();
00042     QPEApplication::setInputMethodHint(this, QPEApplication::AlwaysOff );
00043 
00044     updatespeed = 80;
00045     canvas.setAdvancePeriod(80);
00046 
00047     pb = new QCanvasView(&canvas, this);
00048     pb->setFocus();
00049 
00050     setToolBarsMovable( FALSE );
00051 
00052     QToolBar* toolbar = new QToolBar(this);
00053     toolbar->setHorizontalStretchable( TRUE );
00054 
00055     setCaption( tr("ParaShoot") );
00056     QToolButton *btn = new QToolButton( Opie::Core::OResource::loadPixmap("new", Opie::Core::OResource::SmallIcon),
00057                                         tr("New Game"), 0, this, SLOT(newGame()), toolbar, "New Game");
00058     btn->setUsesBigPixmap( qApp->desktop()->size().width() > 330 );
00059 
00060     levelscore = new QLabel(toolbar);
00061     levelscore->setBackgroundMode( PaletteButton );
00062     levelscore->setAlignment( AlignRight | AlignVCenter | ExpandTabs );
00063     toolbar->setStretchableWidget( levelscore );
00064     showScore(0,0);
00065 
00066     setCentralWidget(pb);
00067 
00068     autoDropTimer = new QTimer(this);
00069     connect (autoDropTimer, SIGNAL(timeout()), this, SLOT(play()) );
00070 
00071     pauseTimer = new QTimer(this);
00072     connect(pauseTimer, SIGNAL(timeout()), this, SLOT(wait()) );
00073 
00074     setFocusPolicy(StrongFocus);
00075 }
00076 
00077 
00078 void ParaShoot::resizeEvent(QResizeEvent *)
00079 {
00080     QSize s = centralWidget()->size();
00081     int fw = style().defaultFrameWidth();
00082     canvas.resize( s.width() - fw - 2, s.height() - fw - 2);
00083 
00084     QImage bgimage = Opie::Core::OResource::loadImage("parashoot/sky");
00085     QPixmap bgpixmap;
00086 
00087     bgpixmap.convertFromImage(bgimage.smoothScale(canvas.width(),
00088         canvas.height()), QPixmap::Auto);
00089     canvas.setBackgroundPixmap(bgpixmap);
00090 
00091     if (base) {
00092         base->reposition();
00093     }
00094 
00095     if (cannon) {
00096         cannon->reposition();
00097     }
00098 }
00099 
00100 void ParaShoot::focusOutEvent (QFocusEvent *)
00101 {
00102     if (!gamestopped)
00103         canvas.setAdvancePeriod(-1);
00104 }
00105 
00106 void ParaShoot::focusInEvent (QFocusEvent *)
00107 {
00108     if (!gamestopped)
00109         canvas.setAdvancePeriod(updatespeed);
00110 }
00111 
00112 void ParaShoot::showScore( int score, int level )
00113 {
00114     levelscore->setText(tr("     Level: %1       Score: %2   ").arg(score).arg(level) );
00115 }
00116 
00117 
00118 void ParaShoot::newGame()
00119 {
00120     clear();
00121     if (pauseTimer->isActive())
00122         pauseTimer->stop();
00123     clear();
00124     Man::setManCount(0);
00125     score = 0;
00126     Bullet::setShotCount(0);
00127     Bullet::setNobullets(0);
00128     nomen = 2;
00129     Bullet::setLimit(nomen);
00130     level = 0;
00131     updatespeed = 80;
00132     showScore(0,0);
00133     gamestopped = false;
00134     Helicopter::deleteAll();
00135     waitover = true;
00136     base = new Base(&canvas);
00137     cannon = new Cannon(&canvas);
00138     connect( cannon, SIGNAL(score(int)), this, SLOT(increaseScore(int)));
00139     autoDropTimer->start(100);
00140 }
00141 
00142 
00143 void ParaShoot::clear()
00144 {
00145    autoDropTimer->stop();
00146 //   QCanvasItem* item;
00147    QCanvasItemList l = canvas.allItems();
00148    for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
00149         delete *it;
00150    }
00151 }
00152 
00153 void ParaShoot::gameOver()
00154 {
00155     QCanvasItem* item;
00156     QCanvasItemList l = canvas.allItems();
00157     for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
00158          item = *it;
00159          if ((item->rtti()==1500) || (item->rtti()==1600) || item->rtti()==1900)
00160          item->setAnimated(false);
00161     }
00162     autoDropTimer->stop();
00163     Helicopter::silenceAll();
00164 
00165     int shots = Bullet::getShotCount();
00166 
00167     int shotsFired = cannon->shotsFired();
00168     if ( shotsFired == 0 )
00169         shotsFired = 1;
00170     QCanvasText* gameover = new QCanvasText(
00171                      tr( "       GAME OVER!\n"
00172                          "       Your Score:  %1\n"
00173                          " Parachuters Killed: %2\n"
00174                          "        Accuracy: %3% " ).arg(score).arg(shots).arg(shots * 100 / shotsFired ),
00175                                      &canvas);
00176     gameover->setColor(red);
00177     gameover->setFont( QFont("times", 16, QFont::Bold) );
00178     gameover->move((canvas.width() - gameover->boundingRect().width()) / 2,
00179         (canvas.height() - gameover->boundingRect().height()) / 2);
00180     gameover->setZ(500);
00181     gameover->show();
00182     gamestopped = true;
00183     waitover = false;
00184     pauseTimer->start(3000);
00185 }
00186 
00187 void ParaShoot::wait()
00188 {
00189    waitover = true;
00190    pauseTimer->stop();
00191 }
00192 
00193 void ParaShoot::play()
00194 {
00195      if (Man::getManCount() < nomen ) {
00196           new Man(&canvas);
00197      }
00198      if (Base::baseDestroyed()) {
00199           gameOver();
00200           return;
00201      }
00202 }
00203 
00204 void ParaShoot::increaseScore(int x)
00205 {
00206     score += x;
00207     if ( score / 150 != (score-x) / 150 )
00208        levelUp();
00209     showScore(level,score);
00210 }
00211 
00212 void ParaShoot::levelUp()
00213 {
00214     level++;
00215     int stage = level % 3;
00216     switch(stage) {
00217       case 0:
00218         nomen++;
00219         Bullet::setLimit(nomen);
00220         fanfare.play();
00221         break;
00222       case 1:
00223         new Helicopter(&canvas);
00224         break;
00225       case 2:
00226         moveFaster();
00227         fanfare.play();
00228         break;
00229       default: return;
00230     }
00231 }
00232 
00233 void ParaShoot::moveFaster()
00234 {
00235    if (updatespeed > 50)
00236        updatespeed = updatespeed-5;
00237    else
00238        updatespeed = updatespeed-3;
00239    canvas.setAdvancePeriod(updatespeed);
00240 }
00241 
00242 void ParaShoot::keyPressEvent(QKeyEvent* event)
00243 {
00244     if (gamestopped) {
00245         if (waitover)
00246             newGame();
00247         else
00248             return;
00249     } else {
00250         switch(event->key()) {
00251           case Key_Up:
00252           case Key_F1:
00253           case Key_F9:
00254           case Key_Space:
00255               if ( cannon )
00256                   cannon->shoot();
00257             break;
00258         case Key_Left:{
00259               if (cannon )
00260                   cannon->pointCannon(Cannon::Left);
00261             lastcannonkey=Key_Left;
00262             break;
00263         }
00264         case Key_Right:{
00265               if ( cannon )
00266                   cannon->pointCannon(Cannon::Right);
00267             lastcannonkey=Key_Right;
00268             break;
00269         }
00270           default:
00271             return;
00272         }
00273     }
00274 }
00275 
00276 void ParaShoot::keyReleaseEvent(QKeyEvent* event)
00277 {
00278     if ( cannon && lastcannonkey == event->key() )
00279         cannon->pointCannon(Cannon::NoDir);
00280 }

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