00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "interface.h"
00022
00023 #include <opie2/oresource.h>
00024
00025 #include <qtoolbar.h>
00026 #include <qtoolbutton.h>
00027
00028 SnakeGame::SnakeGame(QWidget* parent, const char* name, WFlags f) :
00029 QMainWindow(parent,name,f),
00030 canvas(232, 258)
00031 {
00032 setCaption( tr("Snake") );
00033 QPEApplication::setInputMethodHint(this, QPEApplication::AlwaysOff );
00034 QPixmap bg = Opie::Core::OResource::loadPixmap("snake/grass");
00035 canvas.setBackgroundPixmap(bg);
00036 canvas.setUpdatePeriod(100);
00037 snake = 0;
00038
00039 cv = new QCanvasView(&canvas, this);
00040
00041 pauseTimer = new QTimer(this);
00042 connect(pauseTimer, SIGNAL(timeout()), this, SLOT(wait()) );
00043
00044 setToolBarsMovable( FALSE );
00045
00046 QToolBar* toolbar = new QToolBar( this);
00047 toolbar->setHorizontalStretchable( TRUE );
00048
00049 QPixmap newicon = Opie::Core::OResource::loadPixmap("ksnake");
00050 setIcon(newicon);
00051 (void)new QToolButton(newicon, tr("New Game"), 0,
00052 this, SLOT(newGame()), toolbar, "New Game");
00053
00054 scorelabel = new QLabel(toolbar);
00055 showScore(0);
00056 scorelabel->setBackgroundMode( PaletteButton );
00057 scorelabel->setAlignment( AlignRight | AlignVCenter | ExpandTabs );
00058 toolbar->setStretchableWidget( scorelabel );
00059
00060 setFocusPolicy(StrongFocus);
00061
00062 setCentralWidget(cv);
00063
00064 QTimer::singleShot( 16, this, SLOT(welcomescreen()) );
00065 gamestopped = true;
00066 waitover = true;
00067 }
00068
00069 SnakeGame::~SnakeGame()
00070 {
00071 delete snake;
00072 }
00073
00074 void SnakeGame::resizeEvent(QResizeEvent *)
00075 {
00076 QSize s = centralWidget()->size();
00077 int fw = style().defaultFrameWidth();
00078 canvas.resize( s.width() - fw - 2, s.height() - fw - 2);
00079 }
00080
00081 void SnakeGame::welcomescreen()
00082 {
00083 QCanvasText* title = new QCanvasText(tr("SNAKE!"), &canvas);
00084 title->setColor(yellow);
00085 title->setFont( QFont("times", 18, QFont::Bold) );
00086 int w = title->boundingRect().width();
00087 title->move(canvas.width()/2 -w/2, canvas.height()/2-110);
00088 title->show();
00089 QCanvasPixmapArray* titlearray = new QCanvasPixmapArray(Opie::Core::OResource::findPixmap("snake/title"));
00090 QCanvasSprite* titlepic = new QCanvasSprite(titlearray, &canvas);
00091 titlepic->move(canvas.width()/2 - 33, canvas.height()/2-85);
00092 titlepic->show();
00093 QCanvasText* instr = new QCanvasText(tr("Use the arrow keys to guide the\n"
00094 "snake to eat the mouse. You must not\n"
00095 "crash into the walls, edges or its tail."),
00096 &canvas);
00097 w = instr->boundingRect().width();
00098 instr->move(canvas.width()/2-w/2, canvas.height()/2-20);
00099 instr->setColor(white);
00100 instr->show();
00101 QCanvasText* cont = new QCanvasText(tr("Press any key to start"), &canvas);
00102 w = cont->boundingRect().width();
00103 cont->move(canvas.width()/2-w/2, canvas.height()-20);
00104 cont->setColor(yellow);
00105 cont->show();
00106
00107 }
00108
00109 void SnakeGame::newGame()
00110 {
00111 clear();
00112 snake = new Snake(&canvas);
00113 connect(snake, SIGNAL(dead()), this, SLOT(gameOver()) );
00114 connect(snake, SIGNAL(targethit()), this, SLOT(levelUp()) );
00115 connect(snake, SIGNAL(scorechanged()), this, SLOT(scoreInc()) );
00116 connect(this, SIGNAL(moveFaster()), snake, SLOT(increaseSpeed()) );
00117 last = 0;
00118 targetamount = 1;
00119 notargets = 1;
00120 level = 1;
00121 stage = 1;
00122 showScore(0);
00123 gamestopped = false;
00124 waitover = true;
00125 int y = canvas.height()-50;
00126 (void)new Obstacle(&canvas, 32);
00127 (void)new Obstacle(&canvas, y);
00128 createTargets();
00129 }
00130
00131
00132 void SnakeGame::showScore(int score)
00133 {
00134 scorelabel->setText(tr(" Score : %1 ").arg(score) );
00135 }
00136
00137
00138 void SnakeGame::scoreInc()
00139 {
00140 showScore( snake->getScore() );
00141 }
00142
00143 void SnakeGame::levelUp()
00144 {
00145 notargets--;
00146 if (notargets == 0) {
00147 stage++;
00148 if (stage == 3) {
00149 level++;
00150 emit moveFaster();
00151 targetamount++;
00152 stage = 0;
00153 }
00154 createTargets();
00155 }
00156 }
00157
00158 void SnakeGame::createTargets()
00159 {
00160 for (int i = 0; i < targetamount; i++)
00161 (void)new Target(&canvas);
00162 notargets = targetamount;
00163 }
00164
00165 void SnakeGame::clear()
00166 {
00167 delete snake;
00168 snake = 0;
00169 QCanvasItemList l = canvas.allItems();
00170 for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
00171 delete *it;
00172 }
00173 }
00174
00175 void SnakeGame::gameOver()
00176 {
00177 int score = snake->getScore();
00178 QString scoreoutput="";
00179 scoreoutput.setNum(score);
00180 QCanvasText* gameover = new QCanvasText(tr("GAME OVER!\n Your Score: %1").arg( scoreoutput), &canvas);
00181
00182 gameover->setZ(100);
00183 gameover->setColor(yellow);
00184 gameover->setFont( QFont("times", 18, QFont::Bold) );
00185 int w = gameover->boundingRect().width();
00186 gameover->move(canvas.width()/2 -w/2, canvas.height()/2 -50);
00187 gameover->show();
00188 gamestopped = true;
00189 waitover = false;
00190 pauseTimer->start(1500);
00191 }
00192
00193 void SnakeGame::wait()
00194 {
00195 waitover = true;
00196 pauseTimer->stop();
00197 QCanvasText* cont = new QCanvasText(tr("Press any key to begin a new game."),
00198 &canvas);
00199 cont->setZ(100);
00200 cont->setColor(white);
00201 int w = cont->boundingRect().width();
00202 cont->move(canvas.width()/2 -w/2, canvas.height()/2);
00203 cont->show();
00204 }
00205
00206 void SnakeGame::keyPressEvent(QKeyEvent* event)
00207 {
00208 if (gamestopped) {
00209 if (waitover)
00210 newGame();
00211 else
00212 return;
00213 }
00214 else {
00215 int newkey = event->key();
00216 snake->go(newkey);
00217 }
00218 }
00219