1 | /****************************************************************************
|
---|
2 | ** $Id: qtetrix.cpp 160 2006-12-11 20:15:57Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include "qtetrix.h"
|
---|
12 | #include <qapplication.h>
|
---|
13 | #include <qlabel.h>
|
---|
14 | #include <qdatetime.h>
|
---|
15 |
|
---|
16 |
|
---|
17 | void drawTetrixButton( QPainter *p, int x, int y, int w, int h,
|
---|
18 | const QColor *color, QWidget *widg)
|
---|
19 | {
|
---|
20 | if ( color ) {
|
---|
21 | QPointArray a;
|
---|
22 | a.setPoints( 3, x,y+h-1, x,y, x+w-1,y );
|
---|
23 | p->setPen( color->light() );
|
---|
24 | p->drawPolyline( a );
|
---|
25 | a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 );
|
---|
26 | p->setPen( color->dark() );
|
---|
27 | p->drawPolyline( a );
|
---|
28 | x++;
|
---|
29 | y++;
|
---|
30 | w -= 2;
|
---|
31 | h -= 2;
|
---|
32 | p->fillRect( x, y, w, h, *color );
|
---|
33 | }
|
---|
34 | else if(widg) {
|
---|
35 | widg->erase(x, y, w, h);
|
---|
36 | } else {
|
---|
37 | p->fillRect(x, y, w, h, p->backgroundColor());
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name )
|
---|
43 | : QFrame( parent, name )
|
---|
44 | {
|
---|
45 | setFrameStyle( QFrame::Panel | QFrame::Sunken );
|
---|
46 | xOffset = -1; // -1 until first resizeEvent.
|
---|
47 | }
|
---|
48 |
|
---|
49 | void ShowNextPiece::resizeEvent( QResizeEvent *e )
|
---|
50 | {
|
---|
51 | QSize sz = e->size();
|
---|
52 | blockWidth = (sz.width() - 3)/5;
|
---|
53 | blockHeight = (sz.height() - 3)/6;
|
---|
54 | xOffset = (sz.width() - 3)/5;
|
---|
55 | yOffset = (sz.height() - 3)/6;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | void ShowNextPiece::paintEvent( QPaintEvent * )
|
---|
60 | {
|
---|
61 | QPainter p( this );
|
---|
62 | drawFrame( &p );
|
---|
63 | p.end(); // explicit end() so any slots can paint too
|
---|
64 | emit update();
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | void ShowNextPiece::drawNextSquare(int x, int y,QColor *color)
|
---|
69 | {
|
---|
70 | if (xOffset == -1) // Before first resizeEvent?
|
---|
71 | return;
|
---|
72 |
|
---|
73 | QPainter paint;
|
---|
74 | paint.begin(this);
|
---|
75 | drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight,
|
---|
76 | blockWidth, blockHeight, color, this );
|
---|
77 | paint.end();
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | QTetrix::QTetrix( QWidget *parent, const char *name )
|
---|
82 | : QWidget( parent, name )
|
---|
83 | {
|
---|
84 | QTime t = QTime::currentTime();
|
---|
85 | TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/
|
---|
86 | (24+60+60) );
|
---|
87 |
|
---|
88 | #define ADD_LABEL( str, x, y, w, h ) \
|
---|
89 | { QLabel *label = new QLabel(str,this); \
|
---|
90 | label->setGeometry(x,y,w,h); \
|
---|
91 | label->setAlignment(AlignCenter|AlignVCenter); }
|
---|
92 |
|
---|
93 | ADD_LABEL( "NEXT", 50, 10, 78, 30 );
|
---|
94 | ADD_LABEL( "SCORE", 330, 10, 178, 30 );
|
---|
95 | ADD_LABEL( "LEVEL", 50, 130, 78, 30 );
|
---|
96 | ADD_LABEL( "LINES REMOVED", 330, 130, 178, 30 );
|
---|
97 |
|
---|
98 | board = new QTetrixBoard(this);
|
---|
99 | showNext = new ShowNextPiece(this);
|
---|
100 | #ifndef QT_NO_LCDNUMBER
|
---|
101 | showScore = new QLCDNumber(5,this);
|
---|
102 | showLevel = new QLCDNumber(2,this);
|
---|
103 | showLines = new QLCDNumber(5,this);
|
---|
104 | #else
|
---|
105 | showScore = new QLabel("0",this);
|
---|
106 | showLevel = new QLabel("0",this);
|
---|
107 | showLines = new QLabel("0",this);
|
---|
108 | showScore->setAlignment(AlignCenter);
|
---|
109 | showLines->setAlignment(AlignCenter);
|
---|
110 | showLevel->setAlignment(AlignCenter);
|
---|
111 | showScore->setFrameStyle(QFrame::Sunken|QFrame::Box);
|
---|
112 | showLines->setFrameStyle(QFrame::Sunken|QFrame::Box);
|
---|
113 | showLevel->setFrameStyle(QFrame::Sunken|QFrame::Box);
|
---|
114 | #endif
|
---|
115 | quitButton = new QPushButton("&Quit",this);
|
---|
116 | startButton = new QPushButton("&New Game",this);
|
---|
117 | pauseButton = new QPushButton("&Pause",this);
|
---|
118 |
|
---|
119 | // Don't let the buttons get keyboard focus
|
---|
120 | quitButton->setFocusPolicy( QWidget::NoFocus );
|
---|
121 | startButton->setFocusPolicy( QWidget::NoFocus );
|
---|
122 | pauseButton->setFocusPolicy( QWidget::NoFocus );
|
---|
123 |
|
---|
124 | connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) );
|
---|
125 | connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), showNext,
|
---|
126 | SLOT(drawNextSquare(int,int,QColor*)) );
|
---|
127 | connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) );
|
---|
128 | #ifndef QT_NO_LCDNUMBER
|
---|
129 | connect( board, SIGNAL(updateScoreSignal(int)), showScore,
|
---|
130 | SLOT(display(int)) );
|
---|
131 | connect( board, SIGNAL(updateLevelSignal(int)), showLevel,
|
---|
132 | SLOT(display(int)));
|
---|
133 | connect( board, SIGNAL(updateRemovedSignal(int)), showLines,
|
---|
134 | SLOT(display(int)));
|
---|
135 | #else
|
---|
136 | connect( board, SIGNAL(updateScoreSignal(int)), showScore,
|
---|
137 | SLOT(setNum(int)) );
|
---|
138 | connect( board, SIGNAL(updateLevelSignal(int)), showLevel,
|
---|
139 | SLOT(setNum(int)));
|
---|
140 | connect( board, SIGNAL(updateRemovedSignal(int)), showLines,
|
---|
141 | SLOT(setNum(int)));
|
---|
142 | #endif
|
---|
143 | connect( startButton, SIGNAL(clicked()), board, SLOT(start()) );
|
---|
144 | connect( quitButton , SIGNAL(clicked()), SLOT(quit()));
|
---|
145 | connect( pauseButton, SIGNAL(clicked()), board, SLOT(pause()) );
|
---|
146 |
|
---|
147 | board->setGeometry( 150, 20, 153, 333 );
|
---|
148 | showNext->setGeometry( 50, 40, 78, 94 );
|
---|
149 | showScore->setGeometry( 330, 40, 178, 93 );
|
---|
150 | showLevel->setGeometry( 50, 160, 78, 93 );
|
---|
151 | showLines->setGeometry( 330, 160, 178, 93 );
|
---|
152 | #ifndef QT_NO_LCDNUMBER
|
---|
153 | showScore->display( 0 );
|
---|
154 | showLevel->display( 0 );
|
---|
155 | showLines->display( 0 );
|
---|
156 | #else
|
---|
157 | showScore->setNum( 0 );
|
---|
158 | showLevel->setNum( 0 );
|
---|
159 | showLines->setNum( 0 );
|
---|
160 | #endif
|
---|
161 | startButton->setGeometry( 46, 288, 90, 30 );
|
---|
162 | quitButton->setGeometry( 370, 265, 90, 30 );
|
---|
163 | pauseButton->setGeometry( 370, 310, 90, 30 );
|
---|
164 | board->revealNextPiece(TRUE);
|
---|
165 |
|
---|
166 | resize( 550, 370 );
|
---|
167 | }
|
---|
168 |
|
---|
169 | void QTetrix::gameOver()
|
---|
170 | {
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | void QTetrix::quit()
|
---|
175 | {
|
---|
176 | qApp->quit();
|
---|
177 | }
|
---|