-rw-r--r-- | noncore/games/snake/.cvsignore | 3 | ||||
-rw-r--r-- | noncore/games/snake/Makefile.in | 159 | ||||
-rw-r--r-- | noncore/games/snake/codes.h | 20 | ||||
-rw-r--r-- | noncore/games/snake/interface.cpp | 224 | ||||
-rw-r--r-- | noncore/games/snake/interface.h | 69 | ||||
-rw-r--r-- | noncore/games/snake/main.cpp | 35 | ||||
-rw-r--r-- | noncore/games/snake/obstacle.cpp | 51 | ||||
-rw-r--r-- | noncore/games/snake/obstacle.h | 30 | ||||
-rw-r--r-- | noncore/games/snake/qpe-snake.control | 9 | ||||
-rw-r--r-- | noncore/games/snake/snake.cpp | 246 | ||||
-rw-r--r-- | noncore/games/snake/snake.h | 64 | ||||
-rw-r--r-- | noncore/games/snake/snake.pro | 11 | ||||
-rw-r--r-- | noncore/games/snake/target.cpp | 77 | ||||
-rw-r--r-- | noncore/games/snake/target.h | 37 |
14 files changed, 1035 insertions, 0 deletions
diff --git a/noncore/games/snake/.cvsignore b/noncore/games/snake/.cvsignore new file mode 100644 index 0000000..edfa921 --- a/dev/null +++ b/noncore/games/snake/.cvsignore @@ -0,0 +1,3 @@ +moc_* +*.moc +Makefile diff --git a/noncore/games/snake/Makefile.in b/noncore/games/snake/Makefile.in new file mode 100644 index 0000000..8179b6f --- a/dev/null +++ b/noncore/games/snake/Makefile.in @@ -0,0 +1,159 @@ +############################################################################# + +####### Compiler, tools and options + +CXX = $(SYSCONF_CXX) $(QT_CXX_MT) +CXXFLAGS= $(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS) +CC = $(SYSCONF_CC) $(QT_C_MT) +CFLAGS = $(SYSCONF_CFLAGS) +INCPATH = -I$(QPEDIR)/include +LFLAGS = $(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT) +LIBS = $(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP) +MOC = $(SYSCONF_MOC) +UIC = $(SYSCONF_UIC) + +####### Target + +DESTDIR = $(QPEDIR)/bin/ +VER_MAJ = 1 +VER_MIN = 0 +VER_PATCH = 0 +TARGET = snake +TARGET1 = lib$(TARGET).so.$(VER_MAJ) + +####### Files + +HEADERS = snake.h \ + target.h \ + obstacle.h \ + interface.h \ + codes.h +SOURCES = snake.cpp \ + target.cpp \ + obstacle.cpp \ + interface.cpp \ + main.cpp +OBJECTS = snake.o \ + target.o \ + obstacle.o \ + interface.o \ + main.o +INTERFACES = +UICDECLS = +UICIMPLS = +SRCMOC = moc_snake.cpp \ + moc_interface.cpp +OBJMOC = moc_snake.o \ + moc_interface.o + + +####### Implicit rules + +.SUFFIXES: .cpp .cxx .cc .C .c + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $< + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $< + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $< + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $< + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $< + +####### Build rules + + +all: $(DESTDIR)$(TARGET) + +$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS) + $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS) + +moc: $(SRCMOC) + +tmake: + tmake snake.pro + +clean: + -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS) + -rm -f *~ core + -rm -f allmoc.cpp + +####### Extension Modules + +listpromodules: + @echo + +listallmodules: + @echo + +listaddonpromodules: + @echo + +listaddonentmodules: + @echo + + +REQUIRES= + +####### Sub-libraries + + +###### Combined headers + + + +####### Compile + +snake.o: snake.cpp \ + snake.h \ + target.h \ + codes.h \ + $(QPEDIR)/include/qpe/resource.h + +target.o: target.cpp \ + target.h \ + codes.h \ + $(QPEDIR)/include/qpe/resource.h + +obstacle.o: obstacle.cpp \ + obstacle.h \ + codes.h \ + $(QPEDIR)/include/qpe/resource.h + +interface.o: interface.cpp \ + interface.h \ + snake.h \ + target.h \ + obstacle.h \ + $(QPEDIR)/include/qpe/resource.h \ + $(QPEDIR)/include/qpe/qpetoolbar.h + +main.o: main.cpp \ + interface.h \ + snake.h \ + target.h \ + obstacle.h \ + $(QPEDIR)/include/qpe/qpeapplication.h + +moc_snake.o: moc_snake.cpp \ + snake.h + +moc_interface.o: moc_interface.cpp \ + interface.h \ + snake.h \ + target.h \ + obstacle.h + +moc_snake.cpp: snake.h + $(MOC) snake.h -o moc_snake.cpp + +moc_interface.cpp: interface.h + $(MOC) interface.h -o moc_interface.cpp + + diff --git a/noncore/games/snake/codes.h b/noncore/games/snake/codes.h new file mode 100644 index 0000000..3b5e4d0 --- a/dev/null +++ b/noncore/games/snake/codes.h @@ -0,0 +1,20 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/const int target_rtti = 1500; +const int obstacle_rtti = 1600; diff --git a/noncore/games/snake/interface.cpp b/noncore/games/snake/interface.cpp new file mode 100644 index 0000000..c9b4931 --- a/dev/null +++ b/noncore/games/snake/interface.cpp @@ -0,0 +1,224 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include "interface.h" + +#include <qpe/resource.h> + +#include <qpe/qpetoolbar.h> +#include <qtoolbutton.h> +#include <qstyle.h> +#include <qapplication.h> +#include <qmessagebox.h> + +SnakeGame::SnakeGame(QWidget* parent, const char* name, WFlags f) : + QMainWindow(parent,name,f), + canvas(232, 258) +{ + setCaption( tr("Snake") ); + QPixmap bg = Resource::loadPixmap("grass"); + canvas.setBackgroundPixmap(bg); + canvas.setUpdatePeriod(100); + snake = 0; + + cv = new QCanvasView(&canvas, this); + + pauseTimer = new QTimer(this); + connect(pauseTimer, SIGNAL(timeout()), this, SLOT(wait()) ); + + setToolBarsMovable( FALSE ); + + QPEToolBar* toolbar = new QPEToolBar( this); + toolbar->setHorizontalStretchable( TRUE ); + + QPixmap newicon = Resource::loadPixmap("ksnake"); + setIcon(newicon); + (void)new QToolButton(newicon, tr("New Game"), 0, + this, SLOT(newGame()), toolbar, "New Game"); + + scorelabel = new QLabel(toolbar); + showScore(0); + scorelabel->setBackgroundMode( PaletteButton ); + scorelabel->setAlignment( AlignRight | AlignVCenter | ExpandTabs ); + toolbar->setStretchableWidget( scorelabel ); + + setFocusPolicy(StrongFocus); + + setCentralWidget(cv); + + welcomescreen(); + gamestopped = true; + waitover = true; +} + +SnakeGame::~SnakeGame() +{ + delete snake; +} + +void SnakeGame::resizeEvent(QResizeEvent *) +{ + QSize s = centralWidget()->size(); + int fw = style().defaultFrameWidth(); + canvas.resize( s.width() - fw - 2, s.height() - fw - 2); +} + +void SnakeGame::welcomescreen() +{ + QCanvasText* title = new QCanvasText(tr("SNAKE!"), &canvas); + title->setColor(yellow); + title->setFont( QFont("times", 18, QFont::Bold) ); + int w = title->boundingRect().width(); + title->move(canvas.width()/2 -w/2, canvas.height()/2-110); + title->show(); + QCanvasPixmapArray* titlearray = new QCanvasPixmapArray(Resource::findPixmap("title")); + QCanvasSprite* titlepic = new QCanvasSprite(titlearray, &canvas); + titlepic->move(canvas.width()/2 - 33, canvas.height()/2-85); + titlepic->show(); + QCanvasText* instr = new QCanvasText(tr("Use the arrow keys to guide the\n" + "snake to eat the mouse. You must not\n" + "crash into the walls, edges or its tail."), + &canvas); + w = instr->boundingRect().width(); + instr->move(canvas.width()/2-w/2, canvas.height()/2-20); + instr->setColor(white); + instr->show(); + QCanvasText* cont = new QCanvasText(tr("Press Any Key To Start"), &canvas); + w = cont->boundingRect().width(); + cont->move(canvas.width()/2-w/2, canvas.height()/2+80); + cont->setColor(yellow); + cont->show(); + +} + +void SnakeGame::newGame() +{ + clear(); + snake = new Snake(&canvas); + connect(snake, SIGNAL(dead()), this, SLOT(gameOver()) ); + connect(snake, SIGNAL(targethit()), this, SLOT(levelUp()) ); + connect(snake, SIGNAL(scorechanged()), this, SLOT(scoreInc()) ); + connect(this, SIGNAL(moveFaster()), snake, SLOT(increaseSpeed()) ); + last = 0; + targetamount = 1; + notargets = 1; + level = 1; + stage = 1; + showScore(0); + gamestopped = false; + waitover = true; + int x = canvas.width()/2 - 70; + x = x - x % 16; + int y = canvas.height()-50; + y = y - y % 16; + (void)new Obstacle(&canvas, x, 32); + (void)new Obstacle(&canvas, x, y); + createTargets(); +} + + +void SnakeGame::showScore(int score) +{ + scorelabel->setText(tr(" Score : %1 ").arg(score) ); +} + + +void SnakeGame::scoreInc() +{ + showScore( snake->getScore() ); +} + +void SnakeGame::levelUp() +{ + notargets--; + if (notargets == 0) { + stage++; + if (stage == 3) { + level++; + emit moveFaster(); + targetamount++; + stage = 0; + } + createTargets(); + } +} + +void SnakeGame::createTargets() +{ + for (int i = 0; i < targetamount; i++) + (void)new Target(&canvas); + notargets = targetamount; +} + +void SnakeGame::clear() +{ + delete snake; + snake = 0; + QCanvasItemList l = canvas.allItems(); + for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { + delete *it; + } +} + +void SnakeGame::gameOver() +{ + int score = snake->getScore(); + QString scoreoutput=""; + scoreoutput.setNum(score); + QCanvasText* gameover = new QCanvasText(tr("GAME OVER!\n Your Score: %1").arg( scoreoutput), &canvas); + + gameover->setZ(100); + gameover->setColor(yellow); + gameover->setFont( QFont("times", 18, QFont::Bold) ); + int w = gameover->boundingRect().width(); + gameover->move(canvas.width()/2 -w/2, canvas.height()/2 -50); + gameover->show(); + gamestopped = true; + waitover = false; + pauseTimer->start(2500); +} + +void SnakeGame::wait() +{ + waitover = true; + pauseTimer->stop(); + QCanvasText* cont = new QCanvasText(tr("Press Any Key to Begin a New Game."), + &canvas); + cont->setZ(100); + cont->setColor(white); + int w = cont->boundingRect().width(); + cont->move(canvas.width()/2 -w/2, canvas.height()/2); + cont->show(); +} + +void SnakeGame::keyPressEvent(QKeyEvent* event) +{ + if (gamestopped) { + if (waitover) + newGame(); + else + return; + } + else { + int newkey = event->key(); + snake->go(newkey); + } +} + diff --git a/noncore/games/snake/interface.h b/noncore/games/snake/interface.h new file mode 100644 index 0000000..30c7f84 --- a/dev/null +++ b/noncore/games/snake/interface.h @@ -0,0 +1,69 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include <qmainwindow.h> +#include <qcanvas.h> +#include <qlabel.h> + +#include "snake.h" +#include "target.h" +#include "obstacle.h" + +// class QCanvas; + +class SnakeGame : public QMainWindow { + Q_OBJECT + +public: + SnakeGame(QWidget* parent=0, const char* name=0, WFlags f=0); + ~SnakeGame(); + + void clear(); + void createTargets(); + void welcomescreen(); + +protected: + virtual void keyPressEvent(QKeyEvent*); + virtual void resizeEvent(QResizeEvent *e); + +signals: + void moveFaster(); + +private slots: + void newGame(); + void gameOver(); + void wait(); + void levelUp(); + void scoreInc(); + +private: + void showScore(int); + QCanvasView* cv; + QLabel* scorelabel; + QCanvas canvas; + QTimer* pauseTimer; + Snake* snake; + int last; + int level; + int stage; + int targetamount; + int notargets; + bool waitover; + bool gamestopped; +}; diff --git a/noncore/games/snake/main.cpp b/noncore/games/snake/main.cpp new file mode 100644 index 0000000..90a93b7 --- a/dev/null +++ b/noncore/games/snake/main.cpp @@ -0,0 +1,35 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include "interface.h" + +#include <qpe/qpeapplication.h> + + +int main(int argc, char **argv) +{ + QPEApplication app(argc,argv); + + SnakeGame* m = new SnakeGame; + QPEApplication::setInputMethodHint( m, QPEApplication::AlwaysOff ); + app.showMainWidget(m); + + return app.exec(); +} diff --git a/noncore/games/snake/obstacle.cpp b/noncore/games/snake/obstacle.cpp new file mode 100644 index 0000000..2d07fe7 --- a/dev/null +++ b/noncore/games/snake/obstacle.cpp @@ -0,0 +1,51 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include "obstacle.h" +#include "codes.h" + +#include <qpe/resource.h> + +Obstacle::Obstacle(QCanvas* canvas, int x, int y) + : QCanvasSprite(0, canvas) +{ + newObstacle(x, y); +} + +void Obstacle::newObstacle(int x, int y) +{ + QCanvasPixmapArray* obstaclearray = new QCanvasPixmapArray(Resource::findPixmap("snake/wall.png")); + + setSequence(obstaclearray); + + move(x, y); + + show(); + canvas()->update(); +} + +int Obstacle::rtti() const +{ + return obstacle_rtti; +} + +Obstacle::~Obstacle() +{ +} diff --git a/noncore/games/snake/obstacle.h b/noncore/games/snake/obstacle.h new file mode 100644 index 0000000..838917f --- a/dev/null +++ b/noncore/games/snake/obstacle.h @@ -0,0 +1,30 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include <qcanvas.h> + +class Obstacle : public QCanvasSprite +{ + +public: + Obstacle(QCanvas*, int x, int y); + ~Obstacle(); + void newObstacle(int x, int y); + int rtti() const; +}; diff --git a/noncore/games/snake/qpe-snake.control b/noncore/games/snake/qpe-snake.control new file mode 100644 index 0000000..489e642 --- a/dev/null +++ b/noncore/games/snake/qpe-snake.control @@ -0,0 +1,9 @@ +Files: bin/snake apps/Games/snake.desktop pics/snake +Priority: optional +Section: qpe/games +Maintainer: Warwick Allison <warwick@trolltech.com> +Architecture: arm +Version: $QPE_VERSION-3 +Depends: qpe-base ($QPE_VERSION) +Description: Game: control the snake + A game for the Qtopia environment. diff --git a/noncore/games/snake/snake.cpp b/noncore/games/snake/snake.cpp new file mode 100644 index 0000000..9f19841 --- a/dev/null +++ b/noncore/games/snake/snake.cpp @@ -0,0 +1,246 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include "snake.h" +#include "target.h" +#include "codes.h" + +#include <qpe/resource.h> + +#include <qregexp.h> + +static int Piecekey[4][4] = { {6, 0, 4, 3 }, {0, 6, 2, 1 }, { 1, 3, 5, 0 }, {2, 4, 0, 5 } }; + +Snake::Snake(QCanvas* c) +{ + canvas = c; + score = 0; + snakelist.setAutoDelete(true); + autoMoveTimer = new QTimer(this); + connect( autoMoveTimer, SIGNAL(timeout()), this, SLOT(moveSnake()) ); + createSnake(); +} + +void Snake::createSnake() +{ + snakeparts = new QCanvasPixmapArray(); + QString s0 = Resource::findPixmap("snake/s0001"); + s0.replace(QRegExp("0001"),"%1"); + snakeparts->readPixmaps(s0, 15); + + grow = 0; + last = Key_Right; + + QCanvasSprite* head = new QCanvasSprite(snakeparts, canvas ); + head->setFrame(7); + snakelist.insert(0, head); + head->show(); + head->move(34, 16); + + QCanvasSprite* body = new QCanvasSprite(snakeparts, canvas ); + body->setFrame(6); + snakelist.append( body ); + body->show(); + body->move(18, 16); + + QCanvasSprite* end = new QCanvasSprite(snakeparts, canvas ); + end->setFrame(11); + snakelist.append( end ); + end->show(); + end->move(2, 16); + + currentdir = right; + speed = 250; + autoMoveTimer->start(speed); + moveSnake(); +} + +void Snake::increaseSpeed() +{ + if (speed > 150) + speed = speed - 5; + autoMoveTimer->start(speed); +} + +void Snake::go(int newkey) +{ + // check key is a direction + if (!( (newkey == Key_Up) || (newkey == Key_Left) || + (newkey == Key_Right) || (newkey == Key_Down) )) + return; + // check move is possible + if ( ((currentdir == left) && ((newkey == Key_Right) || (newkey == Key_Left)) ) || + ((currentdir == right) && ((newkey == Key_Left) || (newkey == Key_Right)) ) || + ((currentdir == up) && ((newkey == Key_Down) || (newkey == Key_Up)) ) || + ((currentdir == down) && ((newkey == Key_Up) || (newkey == Key_Down)) ) ) + return; + else { + Snake::changeHead(newkey); + Snake::moveSnake(); + } +} + +void Snake::move(Direction dir) +{ + autoMoveTimer->start(speed); + int x = 0; + int y = 0; + newdir = dir; + switch (dir) { + case right: x = 16; break; + case left: x = -16; break; + case down: y = 16; break; + case up: y = -16; break; + } + int index = lookUpPiece(currentdir, newdir); + QCanvasSprite* sprite = new QCanvasSprite(snakeparts, canvas ); + sprite->setFrame(index); + snakelist.insert(1, sprite); + sprite->move(snakelist.first()->x(), snakelist.first()->y() ); + + snakelist.first()->moveBy(x, y); + if (grow <= 0) + changeTail(); + else + grow--; + sprite->show(); + + currentdir = dir; +} + +void Snake::changeTail() +{ + snakelist.removeLast(); + + double lastx = snakelist.last()->x(); + double prevx = snakelist.prev()->x(); + int index = 0; + + if ( prevx == lastx ) { //vertical + if ( snakelist.prev()->y() > snakelist.last()->y() ) + index = 13; + else + index = 14; + } else { //horizontal + if (snakelist.prev()->x() > snakelist.last()->x() ) + index = 11; + else + index = 12; + } + + snakelist.last()->setFrame(index); +} + +void Snake::changeHead(int lastkey) +{ + int index = 0; + last = lastkey; + + switch (last) + { + case Key_Up: index = 10; break; + case Key_Left: index = 8; break; + case Key_Right: index = 7; break; + case Key_Down: index = 9; break; + } + + if (index) { + snakelist.first()->setFrame(index); + } +} + +// returns an integer corresponding to a particular type of snake piece +int Snake::lookUpPiece(Direction currentdir, Direction newdir) +{ + return Piecekey[currentdir][newdir]; +} + +void Snake::extendSnake() +{ + grow++; +} + +void Snake::moveSnake() +{ + switch (last) + { + case Key_Up: move(up); break; + case Key_Left: move(left); break; + case Key_Right: move(right); break; + case Key_Down: move(down); break; + } + detectCrash(); +} + +void Snake::detectCrash() +{ + QCanvasSprite* head = snakelist.first(); + QCanvasItem* item; + QCanvasItemList l=head->collisions(FALSE); + for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { + item = *it; + // check if snake hit target + if ( (item->rtti()== 1500 ) && (item->collidesWith(head)) ) { + Target* target = (Target*) item; + target->done(); + emit targethit(); + extendSnake(); + setScore(5); + return; + } + // check if snake hit obstacles + if ( (item->rtti()==1600) && (item->collidesWith(head)) ) { + emit dead(); + autoMoveTimer->stop(); + return; + } + } + //check if snake hit itself + for (uint i = 3; i < snakelist.count(); i++) { + if (head->collidesWith(snakelist.at(i)) ) { + emit dead(); + autoMoveTimer->stop(); + return; + } + } + //check if snake hit edge + if ( (head->x() > canvas->width()-5) || (head->y() > canvas->height()-10) + || (head->x() <2) || (head->y() <-5) ) { + emit dead(); + autoMoveTimer->stop(); + return; + } +} + +void Snake::setScore(int amount) +{ + score = score + amount; + emit scorechanged(); +} + +int Snake::getScore() +{ + return score; +} + +Snake::~Snake() +{ + autoMoveTimer->stop(); +} diff --git a/noncore/games/snake/snake.h b/noncore/games/snake/snake.h new file mode 100644 index 0000000..5725343 --- a/dev/null +++ b/noncore/games/snake/snake.h @@ -0,0 +1,64 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include <qcanvas.h> +#include <qtimer.h> + +class Snake : public QObject +{ + Q_OBJECT + +public: + enum Direction{ left, right, up, down}; + + Snake(QCanvas*); + ~Snake(); + void go(int newkey); + void move(Direction dir); + void changeHead(int last); + void changeTail(); + void detectCrash(); + void createSnake(); + void extendSnake(); + int lookUpPiece(Direction currentdir, Direction newdir); + void setScore(int amount); + int getScore(); + +signals: + void dead(); + void targethit(); + void scorechanged(); + +private slots: + void moveSnake(); + void increaseSpeed(); + +private: + QCanvasPixmapArray* snakeparts; + QList<QCanvasSprite>snakelist; + QTimer* autoMoveTimer; + QCanvas* canvas; + int grow; + int last; + int speed; + int score; + Direction currentdir; + Direction newdir; +}; + diff --git a/noncore/games/snake/snake.pro b/noncore/games/snake/snake.pro new file mode 100644 index 0000000..6dacdbd --- a/dev/null +++ b/noncore/games/snake/snake.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +CONFIG += qt warn_on release +DESTDIR = $(QPEDIR)/bin +HEADERS = snake.h target.h obstacle.h interface.h codes.h +SOURCES = snake.cpp target.cpp obstacle.cpp interface.cpp main.cpp +TARGET = snake +INCLUDEPATH += $(QPEDIR)/include +DEPENDPATH += $(QPEDIR)/include +LIBS += -lqpe + +TRANSLATIONS = ../i18n/de/snake.ts diff --git a/noncore/games/snake/target.cpp b/noncore/games/snake/target.cpp new file mode 100644 index 0000000..a09af69 --- a/dev/null +++ b/noncore/games/snake/target.cpp @@ -0,0 +1,77 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include "target.h" +#include "codes.h" + +#include <qpe/resource.h> + +#include <stdlib.h> + +Target::Target(QCanvas* canvas) + : QCanvasSprite(0, canvas) +{ + mouse = new QCanvasPixmapArray(Resource::findPixmap("snake/mouse")); + setSequence(mouse); + newTarget(); +} + +void Target::newTarget() +{ + static bool first_time = TRUE; + if (first_time) { + first_time = FALSE; + QTime midnight(0, 0, 0); + srand(midnight.secsTo(QTime::currentTime()) ); + } + do { + int x = rand() % (canvas()->width()-10); + x = x - (x % 16) + 2; + int y = rand() % (canvas()->height()-10); + y = y - (y % 16) + 2; + move(x, y); + } while (onTop()); + show(); +} + +bool Target::onTop() +{ + QCanvasItem* item; + QCanvasItemList l= canvas()->allItems(); //collisions(FALSE); + for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { + item = *it; + if (item != this && item->collidesWith(this)) return true; + } + return false; +} + +void Target::done() +{ + delete this; +} + +int Target::rtti() const +{ + return target_rtti; +} + +Target::~Target() +{ +} diff --git a/noncore/games/snake/target.h b/noncore/games/snake/target.h new file mode 100644 index 0000000..a6da37c --- a/dev/null +++ b/noncore/games/snake/target.h @@ -0,0 +1,37 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include <qcanvas.h> +#include <qdatetime.h> + +class Target : public QCanvasSprite +{ + +public: + Target(QCanvas*); + ~Target(); + void newTarget(); + void done(); + void createMouse(); + bool onTop(); + int rtti() const; + +private: + QCanvasPixmapArray* mouse; +}; |