author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/tetrix/qtetrixb.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'noncore/games/tetrix/qtetrixb.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/tetrix/qtetrixb.cpp | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/noncore/games/tetrix/qtetrixb.cpp b/noncore/games/tetrix/qtetrixb.cpp new file mode 100644 index 0000000..521f171 --- a/dev/null +++ b/noncore/games/tetrix/qtetrixb.cpp | |||
@@ -0,0 +1,251 @@ | |||
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 | |||
22 | #include "qtetrixb.h" | ||
23 | #include "qtetrix.h" | ||
24 | #include <qtimer.h> | ||
25 | #include <qkeycode.h> | ||
26 | #include <qpainter.h> | ||
27 | |||
28 | const int waitAfterLineTime = 500; | ||
29 | |||
30 | QTetrixBoard::QTetrixBoard( QWidget *p, const char *name ) | ||
31 | : QFrame( p, name ) | ||
32 | { | ||
33 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); | ||
34 | paint = 0; | ||
35 | timer = new QTimer(this); | ||
36 | connect( timer, SIGNAL(timeout()), SLOT(timeout()) ); | ||
37 | |||
38 | colors[0].setRgb(200,100,100); | ||
39 | colors[1].setRgb(100,200,100); | ||
40 | colors[2].setRgb(100,100,200); | ||
41 | colors[3].setRgb(200,200,100); | ||
42 | colors[4].setRgb(200,100,200); | ||
43 | colors[5].setRgb(100,200,200); | ||
44 | colors[6].setRgb(218,170, 0); | ||
45 | |||
46 | xOffset = -1; // -1 until a resizeEvent is received. | ||
47 | blockWidth = 20; | ||
48 | yOffset = 30; | ||
49 | blockHeight = 20; | ||
50 | noGame = TRUE; | ||
51 | isPaused = FALSE; | ||
52 | waitingAfterLine = FALSE; | ||
53 | updateTimeoutTime(); // Sets timeoutTime | ||
54 | } | ||
55 | |||
56 | void QTetrixBoard::startGame(int gameType,int fillRandomLines) | ||
57 | { | ||
58 | if ( isPaused ) | ||
59 | return; // ignore if game is paused | ||
60 | noGame = FALSE; | ||
61 | GenericTetrix::startGame( gameType, fillRandomLines ); | ||
62 | // Note that the timer is started by updateLevel! | ||
63 | } | ||
64 | |||
65 | |||
66 | void QTetrixBoard::pause() | ||
67 | { | ||
68 | if ( noGame ) // game not active | ||
69 | return; | ||
70 | isPaused = !isPaused; | ||
71 | if ( isPaused ) { | ||
72 | timer->stop(); | ||
73 | hideBoard(); | ||
74 | } | ||
75 | else | ||
76 | timer->start(timeoutTime); | ||
77 | update(); | ||
78 | } | ||
79 | |||
80 | |||
81 | void QTetrixBoard::drawSquare(int x,int y,int value) | ||
82 | { | ||
83 | if (xOffset == -1) // Before first resizeEvent? | ||
84 | return; | ||
85 | |||
86 | const int X = xOffset + x*blockWidth; | ||
87 | const int Y = yOffset + (y - 1)*blockHeight; | ||
88 | |||
89 | bool localPainter = paint == 0; | ||
90 | QPainter *p; | ||
91 | if ( localPainter ) | ||
92 | p = new QPainter( this ); | ||
93 | else | ||
94 | p = paint; | ||
95 | drawTetrixButton( p, X, Y, blockWidth, blockHeight, | ||
96 | value == 0 ? 0 : &colors[value-1] ); | ||
97 | /* | ||
98 | if ( value != 0 ) { | ||
99 | QColor tc, bc; | ||
100 | tc = colors[value-1].light(); | ||
101 | bc = colors[value-1].dark(); | ||
102 | p->drawShadePanel( X, Y, blockWidth, blockHeight, | ||
103 | tc, bc, 1, colors[value-1], TRUE ); | ||
104 | } | ||
105 | else | ||
106 | p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() ); | ||
107 | */ | ||
108 | if ( localPainter ) | ||
109 | delete p; | ||
110 | } | ||
111 | |||
112 | void QTetrixBoard::drawNextSquare( int x, int y, int value ) | ||
113 | { | ||
114 | if ( value == 0 ) | ||
115 | emit drawNextSquareSignal (x, y, 0 ); | ||
116 | else | ||
117 | emit drawNextSquareSignal( x, y, &colors[value-1] ); | ||
118 | } | ||
119 | |||
120 | void QTetrixBoard::updateRemoved( int noOfLines ) | ||
121 | { | ||
122 | if ( noOfLines > 0 ) { | ||
123 | timer->stop(); | ||
124 | timer->start( waitAfterLineTime ); | ||
125 | waitingAfterLine = TRUE; | ||
126 | } | ||
127 | emit updateRemovedSignal( noOfLines ); | ||
128 | } | ||
129 | |||
130 | void QTetrixBoard::updateScore( int newScore ) | ||
131 | { | ||
132 | emit updateScoreSignal( newScore ); | ||
133 | } | ||
134 | |||
135 | void QTetrixBoard::updateLevel( int newLevel ) | ||
136 | { | ||
137 | timer->stop(); | ||
138 | updateTimeoutTime(); | ||
139 | timer->start( timeoutTime ); | ||
140 | emit updateLevelSignal( newLevel ); | ||
141 | } | ||
142 | |||
143 | void QTetrixBoard::pieceDropped(int) | ||
144 | { | ||
145 | if ( waitingAfterLine ) // give player a break if a line has been removed | ||
146 | return; | ||
147 | newPiece(); | ||
148 | } | ||
149 | |||
150 | void QTetrixBoard::gameOver() | ||
151 | { | ||
152 | timer->stop(); | ||
153 | noGame = TRUE; | ||
154 | emit gameOverSignal(); | ||
155 | } | ||
156 | |||
157 | void QTetrixBoard::timeout() | ||
158 | { | ||
159 | if ( waitingAfterLine ) { | ||
160 | timer->stop(); | ||
161 | waitingAfterLine = FALSE; | ||
162 | newPiece(); | ||
163 | timer->start( timeoutTime ); | ||
164 | } else { | ||
165 | oneLineDown(); | ||
166 | } | ||
167 | } | ||
168 | |||
169 | void QTetrixBoard::drawContents( QPainter *p ) | ||
170 | { | ||
171 | const char *text = "Press \"Pause\""; | ||
172 | QRect r = contentsRect(); | ||
173 | paint = p; // set widget painter | ||
174 | if ( isPaused ) { | ||
175 | p->drawText( r, AlignCenter | AlignVCenter, text ); | ||
176 | return; | ||
177 | } | ||
178 | int x1,y1,x2,y2; | ||
179 | x1 = (r.left() - xOffset) / blockWidth; | ||
180 | if (x1 < 0) | ||
181 | x1 = 0; | ||
182 | if (x1 >= boardWidth()) | ||
183 | x1 = boardWidth() - 1; | ||
184 | |||
185 | x2 = (r.right() - xOffset) / blockWidth; | ||
186 | if (x2 < 0) | ||
187 | x2 = 0; | ||
188 | if (x2 >= boardWidth()) | ||
189 | x2 = boardWidth() - 1; | ||
190 | |||
191 | y1 = (r.top() - yOffset) / blockHeight; | ||
192 | if (y1 < 0) | ||
193 | y1 = 0; | ||
194 | if (y1 >= boardHeight()) | ||
195 | y1 = boardHeight() - 1; | ||
196 | |||
197 | y2 = (r.bottom() - yOffset) / blockHeight; | ||
198 | if (y2 < 0) | ||
199 | y2 = 0; | ||
200 | if (y2 >= boardHeight()) | ||
201 | y2 = boardHeight() - 1; | ||
202 | |||
203 | updateBoard( x1, y1, x2, y2, TRUE ); | ||
204 | paint = 0; // reset widget painter | ||
205 | return; | ||
206 | } | ||
207 | |||
208 | void QTetrixBoard::resizeEvent(QResizeEvent *e) | ||
209 | { | ||
210 | QSize sz = e->size(); | ||
211 | blockWidth = (sz.width() - 2)/10; | ||
212 | blockHeight = (sz.height() - 2)/22; | ||
213 | xOffset = 1; | ||
214 | //yOffset = 1; | ||
215 | yOffset = (sz.height() - 2) - (blockHeight *22); | ||
216 | } | ||
217 | |||
218 | void QTetrixBoard::keyPressEvent( QKeyEvent *e ) | ||
219 | { | ||
220 | if ( noGame || isPaused || waitingAfterLine ) | ||
221 | return; | ||
222 | switch( e->key() ) { | ||
223 | case Key_Left : | ||
224 | moveLeft(); | ||
225 | break; | ||
226 | case Key_Right : | ||
227 | moveRight(); | ||
228 | break; | ||
229 | case Key_Down : | ||
230 | // rotateRight(); | ||
231 | dropDown(); | ||
232 | break; | ||
233 | case Key_Up : | ||
234 | rotateLeft(); | ||
235 | break; | ||
236 | case Key_Space : | ||
237 | dropDown(); | ||
238 | break; | ||
239 | case Key_D : | ||
240 | oneLineDown(); | ||
241 | break; | ||
242 | default: | ||
243 | return; | ||
244 | } | ||
245 | e->accept(); | ||
246 | } | ||
247 | |||
248 | void QTetrixBoard::updateTimeoutTime() | ||
249 | { | ||
250 | timeoutTime = 1000/(1 + getLevel()); | ||
251 | } | ||