00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "qtetrixb.h"
00023 #include "qtetrix.h"
00024 #include <qtimer.h>
00025
00026 const int waitAfterLineTime = 500;
00027
00028 QTetrixBoard::QTetrixBoard( QWidget *p, const char *name )
00029 : QFrame( p, name )
00030 {
00031 setFrameStyle( QFrame::Panel | QFrame::Sunken );
00032 paint = 0;
00033 timer = new QTimer(this);
00034 connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
00035
00036 colors[0].setRgb(200,100,100);
00037 colors[1].setRgb(100,200,100);
00038 colors[2].setRgb(100,100,200);
00039 colors[3].setRgb(200,200,100);
00040 colors[4].setRgb(200,100,200);
00041 colors[5].setRgb(100,200,200);
00042 colors[6].setRgb(218,170, 0);
00043
00044 xOffset = -1;
00045 blockWidth = 20;
00046 yOffset = 30;
00047 blockHeight = 20;
00048 noGame = TRUE;
00049 isPaused = FALSE;
00050 waitingAfterLine = FALSE;
00051 updateTimeoutTime();
00052 }
00053
00054 void QTetrixBoard::startGame(int gameType,int fillRandomLines)
00055 {
00056 if ( isPaused )
00057 return;
00058 noGame = FALSE;
00059
00060 GenericTetrix::startGame( gameType, fillRandomLines );
00061
00062 }
00063
00064
00065 void QTetrixBoard::pause()
00066 {
00067 if ( noGame )
00068 return;
00069 isPaused = !isPaused;
00070 if ( isPaused ) {
00071 timer->stop();
00072 hideBoard();
00073 }
00074 else
00075 timer->start(timeoutTime);
00076 update();
00077 }
00078
00079
00080 void QTetrixBoard::drawSquare(int x,int y,int value)
00081 {
00082 if (xOffset == -1)
00083 return;
00084
00085 const int X = xOffset + x*blockWidth;
00086 const int Y = yOffset + (y - 1)*blockHeight;
00087
00088 bool localPainter = paint == 0;
00089 QPainter *p;
00090 if ( localPainter )
00091 p = new QPainter( this );
00092 else
00093 p = paint;
00094 drawTetrixButton( p, X, Y, blockWidth, blockHeight,
00095 value == 0 ? 0 : &colors[value-1] );
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 if ( localPainter )
00108 delete p;
00109 }
00110
00111 void QTetrixBoard::drawNextSquare( int x, int y, int value )
00112 {
00113 if ( value == 0 )
00114 emit drawNextSquareSignal (x, y, 0 );
00115 else
00116 emit drawNextSquareSignal( x, y, &colors[value-1] );
00117 }
00118
00119 void QTetrixBoard::updateRemoved( int noOfLines )
00120 {
00121 if ( noOfLines > 0 ) {
00122 timer->stop();
00123 timer->start( waitAfterLineTime );
00124 waitingAfterLine = TRUE;
00125 }
00126 emit updateRemovedSignal( noOfLines );
00127 }
00128
00129 void QTetrixBoard::updateScore( int newScore )
00130 {
00131 emit updateScoreSignal( newScore );
00132 }
00133
00134 void QTetrixBoard::updateLevel( int newLevel )
00135 {
00136 timer->stop();
00137 updateTimeoutTime();
00138 timer->start( timeoutTime );
00139 emit updateLevelSignal( newLevel );
00140 }
00141
00142 void QTetrixBoard::pieceDropped(int)
00143 {
00144 if ( waitingAfterLine )
00145 return;
00146 newPiece();
00147 }
00148
00149 void QTetrixBoard::gameOver()
00150 {
00151 timer->stop();
00152 noGame = TRUE;
00153 emit gameOverSignal();
00154 }
00155
00156 void QTetrixBoard::timeout()
00157 {
00158 if ( waitingAfterLine ) {
00159 timer->stop();
00160 waitingAfterLine = FALSE;
00161 newPiece();
00162 timer->start( timeoutTime );
00163 } else {
00164 oneLineDown();
00165 }
00166 }
00167
00168 void QTetrixBoard::drawContents( QPainter *p )
00169 {
00170 const char *text = "Press \"Pause\"";
00171 QRect r = contentsRect();
00172 paint = p;
00173 if ( isPaused ) {
00174 p->drawText( r, AlignCenter | AlignVCenter, text );
00175 return;
00176 }
00177 int x1,y1,x2,y2;
00178 x1 = (r.left() - xOffset) / blockWidth;
00179 if (x1 < 0)
00180 x1 = 0;
00181 if (x1 >= boardWidth())
00182 x1 = boardWidth() - 1;
00183
00184 x2 = (r.right() - xOffset) / blockWidth;
00185 if (x2 < 0)
00186 x2 = 0;
00187 if (x2 >= boardWidth())
00188 x2 = boardWidth() - 1;
00189
00190 y1 = (r.top() - yOffset) / blockHeight;
00191 if (y1 < 0)
00192 y1 = 0;
00193 if (y1 >= boardHeight())
00194 y1 = boardHeight() - 1;
00195
00196 y2 = (r.bottom() - yOffset) / blockHeight;
00197 if (y2 < 0)
00198 y2 = 0;
00199 if (y2 >= boardHeight())
00200 y2 = boardHeight() - 1;
00201
00202 updateBoard( x1, y1, x2, y2, TRUE );
00203 paint = 0;
00204 return;
00205 }
00206
00207 void QTetrixBoard::resizeEvent(QResizeEvent *e)
00208 {
00209 QSize sz = e->size();
00210
00211 blockWidth = (sz.width() - 2)/10;
00212 blockHeight = (sz.height() - 2)/22;
00213
00214
00215 xOffset = 1;
00216
00217 yOffset = (sz.height() - 2) - (blockHeight *22);
00218 }
00219
00220 void QTetrixBoard::keyPressEvent( QKeyEvent *e )
00221 {
00222 if ( noGame || isPaused || waitingAfterLine )
00223 return;
00224 switch( e->key() ) {
00225 case Key_Left :
00226 moveLeft();
00227 break;
00228 case Key_Right :
00229 moveRight();
00230 break;
00231 case Key_Down :
00232
00233 dropDown();
00234 break;
00235 case Key_Up :
00236 rotateLeft();
00237 break;
00238 case Key_Space :
00239 dropDown();
00240 break;
00241 case Key_D :
00242 oneLineDown();
00243 break;
00244 default:
00245 return;
00246 }
00247 e->accept();
00248 }
00249
00250 void QTetrixBoard::updateTimeoutTime()
00251 {
00252 timeoutTime = 1000/(1 + getLevel());
00253 }