Diffstat (limited to 'noncore/games/snake/interface.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/snake/interface.cpp | 224 |
1 files changed, 224 insertions, 0 deletions
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 | |||
31 | SnakeGame::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 | |||
71 | SnakeGame::~SnakeGame() | ||
72 | { | ||
73 | delete snake; | ||
74 | } | ||
75 | |||
76 | void 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 | |||
83 | void 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 | |||
111 | void 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 | |||
137 | void SnakeGame::showScore(int score) | ||
138 | { | ||
139 | scorelabel->setText(tr(" Score : %1 ").arg(score) ); | ||
140 | } | ||
141 | |||
142 | |||
143 | void SnakeGame::scoreInc() | ||
144 | { | ||
145 | showScore( snake->getScore() ); | ||
146 | } | ||
147 | |||
148 | void 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 | |||
163 | void SnakeGame::createTargets() | ||
164 | { | ||
165 | for (int i = 0; i < targetamount; i++) | ||
166 | (void)new Target(&canvas); | ||
167 | notargets = targetamount; | ||
168 | } | ||
169 | |||
170 | void 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 | |||
180 | void 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 | |||
198 | void 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 | |||
211 | void 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 | |||