summaryrefslogtreecommitdiff
path: root/noncore/games/snake
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/snake
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'noncore/games/snake') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/snake/.cvsignore3
-rw-r--r--noncore/games/snake/Makefile.in159
-rw-r--r--noncore/games/snake/codes.h20
-rw-r--r--noncore/games/snake/interface.cpp224
-rw-r--r--noncore/games/snake/interface.h69
-rw-r--r--noncore/games/snake/main.cpp35
-rw-r--r--noncore/games/snake/obstacle.cpp51
-rw-r--r--noncore/games/snake/obstacle.h30
-rw-r--r--noncore/games/snake/qpe-snake.control9
-rw-r--r--noncore/games/snake/snake.cpp246
-rw-r--r--noncore/games/snake/snake.h64
-rw-r--r--noncore/games/snake/snake.pro11
-rw-r--r--noncore/games/snake/target.cpp77
-rw-r--r--noncore/games/snake/target.h37
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 @@
1moc_*
2*.moc
3Makefile
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 @@
1#############################################################################
2
3####### Compiler, tools and options
4
5 CXX =$(SYSCONF_CXX) $(QT_CXX_MT)
6 CXXFLAGS=$(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS)
7 CC =$(SYSCONF_CC) $(QT_C_MT)
8 CFLAGS =$(SYSCONF_CFLAGS)
9 INCPATH =-I$(QPEDIR)/include
10 LFLAGS =$(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT)
11 LIBS =$(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP)
12 MOC =$(SYSCONF_MOC)
13 UIC =$(SYSCONF_UIC)
14
15####### Target
16
17DESTDIR = $(QPEDIR)/bin/
18VER_MAJ = 1
19VER_MIN = 0
20VER_PATCH = 0
21 TARGET= snake
22TARGET1 = lib$(TARGET).so.$(VER_MAJ)
23
24####### Files
25
26 HEADERS =snake.h \
27 target.h \
28 obstacle.h \
29 interface.h \
30 codes.h
31 SOURCES =snake.cpp \
32 target.cpp \
33 obstacle.cpp \
34 interface.cpp \
35 main.cpp
36 OBJECTS =snake.o \
37 target.o \
38 obstacle.o \
39 interface.o \
40 main.o
41INTERFACES =
42UICDECLS =
43UICIMPLS =
44 SRCMOC =moc_snake.cpp \
45 moc_interface.cpp
46 OBJMOC =moc_snake.o \
47 moc_interface.o
48
49
50####### Implicit rules
51
52.SUFFIXES: .cpp .cxx .cc .C .c
53
54.cpp.o:
55 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
56
57.cxx.o:
58 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
59
60.cc.o:
61 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
62
63.C.o:
64 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
65
66.c.o:
67 $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
68
69####### Build rules
70
71
72all: $(DESTDIR)$(TARGET)
73
74$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS)
75 $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
76
77moc: $(SRCMOC)
78
79tmake:
80 tmake snake.pro
81
82clean:
83 -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS)
84 -rm -f *~ core
85 -rm -f allmoc.cpp
86
87####### Extension Modules
88
89listpromodules:
90 @echo
91
92listallmodules:
93 @echo
94
95listaddonpromodules:
96 @echo
97
98listaddonentmodules:
99 @echo
100
101
102REQUIRES=
103
104####### Sub-libraries
105
106
107###### Combined headers
108
109
110
111####### Compile
112
113snake.o: snake.cpp \
114 snake.h \
115 target.h \
116 codes.h \
117 $(QPEDIR)/include/qpe/resource.h
118
119target.o: target.cpp \
120 target.h \
121 codes.h \
122 $(QPEDIR)/include/qpe/resource.h
123
124obstacle.o: obstacle.cpp \
125 obstacle.h \
126 codes.h \
127 $(QPEDIR)/include/qpe/resource.h
128
129interface.o: interface.cpp \
130 interface.h \
131 snake.h \
132 target.h \
133 obstacle.h \
134 $(QPEDIR)/include/qpe/resource.h \
135 $(QPEDIR)/include/qpe/qpetoolbar.h
136
137main.o: main.cpp \
138 interface.h \
139 snake.h \
140 target.h \
141 obstacle.h \
142 $(QPEDIR)/include/qpe/qpeapplication.h
143
144moc_snake.o: moc_snake.cpp \
145 snake.h
146
147moc_interface.o: moc_interface.cpp \
148 interface.h \
149 snake.h \
150 target.h \
151 obstacle.h
152
153moc_snake.cpp: snake.h
154 $(MOC) snake.h -o moc_snake.cpp
155
156moc_interface.cpp: interface.h
157 $(MOC) interface.h -o moc_interface.cpp
158
159
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/const int target_rtti = 1500;
20const 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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "interface.h"
22
23#include <qpe/resource.h>
24
25#include <qpe/qpetoolbar.h>
26#include <qtoolbutton.h>
27#include <qstyle.h>
28#include <qapplication.h>
29#include <qmessagebox.h>
30
31SnakeGame::SnakeGame(QWidget* parent, const char* name, WFlags f) :
32 QMainWindow(parent,name,f),
33 canvas(232, 258)
34{
35 setCaption( tr("Snake") );
36 QPixmap bg = Resource::loadPixmap("grass");
37 canvas.setBackgroundPixmap(bg);
38 canvas.setUpdatePeriod(100);
39 snake = 0;
40
41 cv = new QCanvasView(&canvas, this);
42
43 pauseTimer = new QTimer(this);
44 connect(pauseTimer, SIGNAL(timeout()), this, SLOT(wait()) );
45
46 setToolBarsMovable( FALSE );
47
48 QPEToolBar* toolbar = new QPEToolBar( this);
49 toolbar->setHorizontalStretchable( TRUE );
50
51 QPixmap newicon = Resource::loadPixmap("ksnake");
52 setIcon(newicon);
53 (void)new QToolButton(newicon, tr("New Game"), 0,
54 this, SLOT(newGame()), toolbar, "New Game");
55
56 scorelabel = new QLabel(toolbar);
57 showScore(0);
58 scorelabel->setBackgroundMode( PaletteButton );
59 scorelabel->setAlignment( AlignRight | AlignVCenter | ExpandTabs );
60 toolbar->setStretchableWidget( scorelabel );
61
62 setFocusPolicy(StrongFocus);
63
64 setCentralWidget(cv);
65
66 welcomescreen();
67 gamestopped = true;
68 waitover = true;
69}
70
71SnakeGame::~SnakeGame()
72{
73 delete snake;
74}
75
76void SnakeGame::resizeEvent(QResizeEvent *)
77{
78 QSize s = centralWidget()->size();
79 int fw = style().defaultFrameWidth();
80 canvas.resize( s.width() - fw - 2, s.height() - fw - 2);
81}
82
83void SnakeGame::welcomescreen()
84{
85 QCanvasText* title = new QCanvasText(tr("SNAKE!"), &canvas);
86 title->setColor(yellow);
87 title->setFont( QFont("times", 18, QFont::Bold) );
88 int w = title->boundingRect().width();
89 title->move(canvas.width()/2 -w/2, canvas.height()/2-110);
90 title->show();
91 QCanvasPixmapArray* titlearray = new QCanvasPixmapArray(Resource::findPixmap("title"));
92 QCanvasSprite* titlepic = new QCanvasSprite(titlearray, &canvas);
93 titlepic->move(canvas.width()/2 - 33, canvas.height()/2-85);
94 titlepic->show();
95 QCanvasText* instr = new QCanvasText(tr("Use the arrow keys to guide the\n"
96 "snake to eat the mouse. You must not\n"
97 "crash into the walls, edges or its tail."),
98 &canvas);
99 w = instr->boundingRect().width();
100 instr->move(canvas.width()/2-w/2, canvas.height()/2-20);
101 instr->setColor(white);
102 instr->show();
103 QCanvasText* cont = new QCanvasText(tr("Press Any Key To Start"), &canvas);
104 w = cont->boundingRect().width();
105 cont->move(canvas.width()/2-w/2, canvas.height()/2+80);
106 cont->setColor(yellow);
107 cont->show();
108
109}
110
111void SnakeGame::newGame()
112{
113 clear();
114 snake = new Snake(&canvas);
115 connect(snake, SIGNAL(dead()), this, SLOT(gameOver()) );
116 connect(snake, SIGNAL(targethit()), this, SLOT(levelUp()) );
117 connect(snake, SIGNAL(scorechanged()), this, SLOT(scoreInc()) );
118 connect(this, SIGNAL(moveFaster()), snake, SLOT(increaseSpeed()) );
119 last = 0;
120 targetamount = 1;
121 notargets = 1;
122 level = 1;
123 stage = 1;
124 showScore(0);
125 gamestopped = false;
126 waitover = true;
127 int x = canvas.width()/2 - 70;
128 x = x - x % 16;
129 int y = canvas.height()-50;
130 y = y - y % 16;
131 (void)new Obstacle(&canvas, x, 32);
132 (void)new Obstacle(&canvas, x, y);
133 createTargets();
134}
135
136
137void SnakeGame::showScore(int score)
138{
139 scorelabel->setText(tr(" Score : %1 ").arg(score) );
140}
141
142
143void SnakeGame::scoreInc()
144{
145 showScore( snake->getScore() );
146}
147
148void SnakeGame::levelUp()
149{
150 notargets--;
151 if (notargets == 0) {
152 stage++;
153 if (stage == 3) {
154 level++;
155 emit moveFaster();
156 targetamount++;
157 stage = 0;
158 }
159 createTargets();
160 }
161}
162
163void SnakeGame::createTargets()
164{
165 for (int i = 0; i < targetamount; i++)
166 (void)new Target(&canvas);
167 notargets = targetamount;
168}
169
170void SnakeGame::clear()
171{
172 delete snake;
173 snake = 0;
174 QCanvasItemList l = canvas.allItems();
175 for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
176 delete *it;
177 }
178}
179
180void SnakeGame::gameOver()
181{
182 int score = snake->getScore();
183 QString scoreoutput="";
184 scoreoutput.setNum(score);
185 QCanvasText* gameover = new QCanvasText(tr("GAME OVER!\n Your Score: %1").arg( scoreoutput), &canvas);
186
187 gameover->setZ(100);
188 gameover->setColor(yellow);
189 gameover->setFont( QFont("times", 18, QFont::Bold) );
190 int w = gameover->boundingRect().width();
191 gameover->move(canvas.width()/2 -w/2, canvas.height()/2 -50);
192 gameover->show();
193 gamestopped = true;
194 waitover = false;
195 pauseTimer->start(2500);
196}
197
198void SnakeGame::wait()
199{
200 waitover = true;
201 pauseTimer->stop();
202 QCanvasText* cont = new QCanvasText(tr("Press Any Key to Begin a New Game."),
203 &canvas);
204 cont->setZ(100);
205 cont->setColor(white);
206 int w = cont->boundingRect().width();
207 cont->move(canvas.width()/2 -w/2, canvas.height()/2);
208 cont->show();
209}
210
211void SnakeGame::keyPressEvent(QKeyEvent* event)
212{
213 if (gamestopped) {
214 if (waitover)
215 newGame();
216 else
217 return;
218 }
219 else {
220 int newkey = event->key();
221 snake->go(newkey);
222 }
223}
224
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include <qmainwindow.h>
21#include <qcanvas.h>
22#include <qlabel.h>
23
24#include "snake.h"
25#include "target.h"
26#include "obstacle.h"
27
28// class QCanvas;
29
30class SnakeGame : public QMainWindow {
31 Q_OBJECT
32
33public:
34 SnakeGame(QWidget* parent=0, const char* name=0, WFlags f=0);
35 ~SnakeGame();
36
37 void clear();
38 void createTargets();
39 void welcomescreen();
40
41protected:
42 virtual void keyPressEvent(QKeyEvent*);
43 virtual void resizeEvent(QResizeEvent *e);
44
45signals:
46 void moveFaster();
47
48private slots:
49 void newGame();
50 void gameOver();
51 void wait();
52 void levelUp();
53 void scoreInc();
54
55private:
56 void showScore(int);
57 QCanvasView* cv;
58 QLabel* scorelabel;
59 QCanvas canvas;
60 QTimer* pauseTimer;
61 Snake* snake;
62 int last;
63 int level;
64 int stage;
65 int targetamount;
66 int notargets;
67 bool waitover;
68 bool gamestopped;
69};
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "interface.h"
22
23#include <qpe/qpeapplication.h>
24
25
26int main(int argc, char **argv)
27{
28 QPEApplication app(argc,argv);
29
30 SnakeGame* m = new SnakeGame;
31 QPEApplication::setInputMethodHint( m, QPEApplication::AlwaysOff );
32 app.showMainWidget(m);
33
34 return app.exec();
35}
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "obstacle.h"
22#include "codes.h"
23
24#include <qpe/resource.h>
25
26Obstacle::Obstacle(QCanvas* canvas, int x, int y)
27 : QCanvasSprite(0, canvas)
28{
29 newObstacle(x, y);
30}
31
32void Obstacle::newObstacle(int x, int y)
33{
34 QCanvasPixmapArray* obstaclearray = new QCanvasPixmapArray(Resource::findPixmap("snake/wall.png"));
35
36 setSequence(obstaclearray);
37
38 move(x, y);
39
40 show();
41 canvas()->update();
42}
43
44int Obstacle::rtti() const
45{
46 return obstacle_rtti;
47}
48
49Obstacle::~Obstacle()
50{
51}
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include <qcanvas.h>
21
22class Obstacle : public QCanvasSprite
23{
24
25public:
26 Obstacle(QCanvas*, int x, int y);
27 ~Obstacle();
28 void newObstacle(int x, int y);
29 int rtti() const;
30};
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 @@
1Files: bin/snake apps/Games/snake.desktop pics/snake
2Priority: optional
3Section: qpe/games
4Maintainer: Warwick Allison <warwick@trolltech.com>
5Architecture: arm
6Version: $QPE_VERSION-3
7Depends: qpe-base ($QPE_VERSION)
8Description: Game: control the snake
9 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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "snake.h"
22#include "target.h"
23#include "codes.h"
24
25#include <qpe/resource.h>
26
27#include <qregexp.h>
28
29static int Piecekey[4][4] = { {6, 0, 4, 3 }, {0, 6, 2, 1 }, { 1, 3, 5, 0 }, {2, 4, 0, 5 } };
30
31Snake::Snake(QCanvas* c)
32{
33 canvas = c;
34 score = 0;
35 snakelist.setAutoDelete(true);
36 autoMoveTimer = new QTimer(this);
37 connect( autoMoveTimer, SIGNAL(timeout()), this, SLOT(moveSnake()) );
38 createSnake();
39}
40
41void Snake::createSnake()
42{
43 snakeparts = new QCanvasPixmapArray();
44 QString s0 = Resource::findPixmap("snake/s0001");
45 s0.replace(QRegExp("0001"),"%1");
46 snakeparts->readPixmaps(s0, 15);
47
48 grow = 0;
49 last = Key_Right;
50
51 QCanvasSprite* head = new QCanvasSprite(snakeparts, canvas );
52 head->setFrame(7);
53 snakelist.insert(0, head);
54 head->show();
55 head->move(34, 16);
56
57 QCanvasSprite* body = new QCanvasSprite(snakeparts, canvas );
58 body->setFrame(6);
59 snakelist.append( body );
60 body->show();
61 body->move(18, 16);
62
63 QCanvasSprite* end = new QCanvasSprite(snakeparts, canvas );
64 end->setFrame(11);
65 snakelist.append( end );
66 end->show();
67 end->move(2, 16);
68
69 currentdir = right;
70 speed = 250;
71 autoMoveTimer->start(speed);
72 moveSnake();
73}
74
75void Snake::increaseSpeed()
76{
77 if (speed > 150)
78 speed = speed - 5;
79 autoMoveTimer->start(speed);
80}
81
82void Snake::go(int newkey)
83{
84 // check key is a direction
85 if (!( (newkey == Key_Up) || (newkey == Key_Left) ||
86 (newkey == Key_Right) || (newkey == Key_Down) ))
87 return;
88 // check move is possible
89 if ( ((currentdir == left) && ((newkey == Key_Right) || (newkey == Key_Left)) ) ||
90 ((currentdir == right) && ((newkey == Key_Left) || (newkey == Key_Right)) ) ||
91 ((currentdir == up) && ((newkey == Key_Down) || (newkey == Key_Up)) ) ||
92 ((currentdir == down) && ((newkey == Key_Up) || (newkey == Key_Down)) ) )
93 return;
94 else {
95 Snake::changeHead(newkey);
96 Snake::moveSnake();
97 }
98}
99
100void Snake::move(Direction dir)
101{
102 autoMoveTimer->start(speed);
103 int x = 0;
104 int y = 0;
105 newdir = dir;
106 switch (dir) {
107 case right: x = 16; break;
108 case left: x = -16; break;
109 case down: y = 16; break;
110 case up: y = -16; break;
111 }
112 int index = lookUpPiece(currentdir, newdir);
113 QCanvasSprite* sprite = new QCanvasSprite(snakeparts, canvas );
114 sprite->setFrame(index);
115 snakelist.insert(1, sprite);
116 sprite->move(snakelist.first()->x(), snakelist.first()->y() );
117
118 snakelist.first()->moveBy(x, y);
119 if (grow <= 0)
120 changeTail();
121 else
122 grow--;
123 sprite->show();
124
125 currentdir = dir;
126}
127
128void Snake::changeTail()
129{
130 snakelist.removeLast();
131
132 double lastx = snakelist.last()->x();
133 double prevx = snakelist.prev()->x();
134 int index = 0;
135
136 if ( prevx == lastx ) { //vertical
137 if ( snakelist.prev()->y() > snakelist.last()->y() )
138 index = 13;
139 else
140 index = 14;
141 } else { //horizontal
142 if (snakelist.prev()->x() > snakelist.last()->x() )
143 index = 11;
144 else
145 index = 12;
146 }
147
148 snakelist.last()->setFrame(index);
149}
150
151void Snake::changeHead(int lastkey)
152{
153 int index = 0;
154 last = lastkey;
155
156 switch (last)
157 {
158 case Key_Up: index = 10; break;
159 case Key_Left: index = 8; break;
160 case Key_Right: index = 7; break;
161 case Key_Down: index = 9; break;
162 }
163
164 if (index) {
165 snakelist.first()->setFrame(index);
166 }
167}
168
169// returns an integer corresponding to a particular type of snake piece
170int Snake::lookUpPiece(Direction currentdir, Direction newdir)
171{
172 return Piecekey[currentdir][newdir];
173}
174
175void Snake::extendSnake()
176{
177 grow++;
178}
179
180void Snake::moveSnake()
181{
182 switch (last)
183 {
184 case Key_Up: move(up); break;
185 case Key_Left: move(left); break;
186 case Key_Right: move(right); break;
187 case Key_Down: move(down); break;
188 }
189 detectCrash();
190}
191
192void Snake::detectCrash()
193{
194 QCanvasSprite* head = snakelist.first();
195 QCanvasItem* item;
196 QCanvasItemList l=head->collisions(FALSE);
197 for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
198 item = *it;
199 // check if snake hit target
200 if ( (item->rtti()== 1500 ) && (item->collidesWith(head)) ) {
201 Target* target = (Target*) item;
202 target->done();
203 emit targethit();
204 extendSnake();
205 setScore(5);
206 return;
207 }
208 // check if snake hit obstacles
209 if ( (item->rtti()==1600) && (item->collidesWith(head)) ) {
210 emit dead();
211 autoMoveTimer->stop();
212 return;
213 }
214 }
215 //check if snake hit itself
216 for (uint i = 3; i < snakelist.count(); i++) {
217 if (head->collidesWith(snakelist.at(i)) ) {
218 emit dead();
219 autoMoveTimer->stop();
220 return;
221 }
222 }
223 //check if snake hit edge
224 if ( (head->x() > canvas->width()-5) || (head->y() > canvas->height()-10)
225 || (head->x() <2) || (head->y() <-5) ) {
226 emit dead();
227 autoMoveTimer->stop();
228 return;
229 }
230}
231
232void Snake::setScore(int amount)
233{
234 score = score + amount;
235 emit scorechanged();
236}
237
238int Snake::getScore()
239{
240 return score;
241}
242
243Snake::~Snake()
244{
245 autoMoveTimer->stop();
246}
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include <qcanvas.h>
21#include <qtimer.h>
22
23class Snake : public QObject
24{
25 Q_OBJECT
26
27public:
28 enum Direction{ left, right, up, down};
29
30 Snake(QCanvas*);
31 ~Snake();
32 void go(int newkey);
33 void move(Direction dir);
34 void changeHead(int last);
35 void changeTail();
36 void detectCrash();
37 void createSnake();
38 void extendSnake();
39 int lookUpPiece(Direction currentdir, Direction newdir);
40 void setScore(int amount);
41 int getScore();
42
43signals:
44 void dead();
45 void targethit();
46 void scorechanged();
47
48private slots:
49 void moveSnake();
50 void increaseSpeed();
51
52private:
53 QCanvasPixmapArray* snakeparts;
54 QList<QCanvasSprite>snakelist;
55 QTimer* autoMoveTimer;
56 QCanvas* canvas;
57 int grow;
58 int last;
59 int speed;
60 int score;
61 Direction currentdir;
62 Direction newdir;
63};
64
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 @@
1 TEMPLATE= app
2 CONFIG += qt warn_on release
3 DESTDIR = $(QPEDIR)/bin
4 HEADERS = snake.h target.h obstacle.h interface.h codes.h
5 SOURCES = snake.cpp target.cpp obstacle.cpp interface.cpp main.cpp
6 TARGET = snake
7INCLUDEPATH += $(QPEDIR)/include
8 DEPENDPATH+= $(QPEDIR)/include
9LIBS += -lqpe
10
11TRANSLATIONS = ../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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "target.h"
22#include "codes.h"
23
24#include <qpe/resource.h>
25
26#include <stdlib.h>
27
28Target::Target(QCanvas* canvas)
29 : QCanvasSprite(0, canvas)
30{
31 mouse = new QCanvasPixmapArray(Resource::findPixmap("snake/mouse"));
32 setSequence(mouse);
33 newTarget();
34}
35
36void Target::newTarget()
37{
38 static bool first_time = TRUE;
39 if (first_time) {
40 first_time = FALSE;
41 QTime midnight(0, 0, 0);
42 srand(midnight.secsTo(QTime::currentTime()) );
43 }
44 do {
45 int x = rand() % (canvas()->width()-10);
46 x = x - (x % 16) + 2;
47 int y = rand() % (canvas()->height()-10);
48 y = y - (y % 16) + 2;
49 move(x, y);
50 } while (onTop());
51 show();
52}
53
54bool Target::onTop()
55{
56 QCanvasItem* item;
57 QCanvasItemList l= canvas()->allItems(); //collisions(FALSE);
58 for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
59 item = *it;
60 if (item != this && item->collidesWith(this)) return true;
61 }
62 return false;
63}
64
65void Target::done()
66{
67 delete this;
68}
69
70int Target::rtti() const
71{
72 return target_rtti;
73}
74
75Target::~Target()
76{
77}
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 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include <qcanvas.h>
21#include <qdatetime.h>
22
23class Target : public QCanvasSprite
24{
25
26public:
27 Target(QCanvas*);
28 ~Target();
29 void newTarget();
30 void done();
31 void createMouse();
32 bool onTop();
33 int rtti() const;
34
35private:
36 QCanvasPixmapArray* mouse;
37};