diff options
Diffstat (limited to 'qtminesweeper.cpp')
-rw-r--r-- | qtminesweeper.cpp | 91 |
1 files changed, 89 insertions, 2 deletions
diff --git a/qtminesweeper.cpp b/qtminesweeper.cpp index 6fc5f2e..e1e0272 100644 --- a/qtminesweeper.cpp +++ b/qtminesweeper.cpp @@ -20,13 +20,71 @@ qtminesweeper::qtminesweeper() { - cell cell1(cell::MINE | cell::REVEALED); - cell1.flagcheck(); + this->setStyleSheet("background-color: black;"); + /* setup function */ + blackpen = QPen(Qt::black); + whitepen = QPen(Qt::white); + redpen = QPen(Qt::red); + greenpen = QPen(Qt::green); + bluepen = QPen(Qt::blue); + + /* create cells */ + int tmpx, tmpy; + for (int c = 0; c < NUMBEROFCELLS; c++) + { + cell tmp(cell::MINE); + generatecellpos(&tmp); + cells.append(tmp); + tmpx++; + tmpy++; + } + + cursor.movetogridpos(0, 0); } void qtminesweeper::paintEvent(QPaintEvent *event) { QPainter painter(this); + painter.setPen(whitepen); + drawgrid(&painter); + painter.setPen(redpen); + drawcursor(&painter); + painter.setPen(greenpen); + drawcells(&painter); +} + +void qtminesweeper::drawgrid(QPainter *painter) +{ + int i, c; + painter->drawRect(BORDEROFFSET, BORDEROFFSET, GRIDWIDTH, GRIDHEIGHT); + i = c = 0; + i = BORDEROFFSET; + for (c = 0; c < GRIDHEIGHT-SQUARESIZE; c+=SQUARESIZE) + { + i+=SQUARESIZE; + painter->drawLine(BORDEROFFSET, i, BORDEROFFSET+GRIDWIDTH, i); + } + i=BORDEROFFSET; + for (c = 0; c < GRIDWIDTH-SQUARESIZE; c+=SQUARESIZE) + { + i+=SQUARESIZE; + painter->drawLine(i, BORDEROFFSET, i, BORDEROFFSET+GRIDHEIGHT); + } +} + +void qtminesweeper::drawcursor(QPainter *painter) +{ + painter->drawRect(cursor.getrealx(), cursor.getrealy(), SQUARESIZE, SQUARESIZE); +} + +void qtminesweeper::drawcells(QPainter *painter) +{ + for (int c = 0; c < cells.length(); c++) + { + painter->drawText(cells[c].getrealx(), + cells[c].getrealy()+(SQUARESIZE/2), + "B"); + } } void qtminesweeper::keyPressEvent(QKeyEvent *event) @@ -34,15 +92,44 @@ void qtminesweeper::keyPressEvent(QKeyEvent *event) switch(event->key()) { case Qt::Key_W: + if (cursor.getrealy() > BORDEROFFSET) + { + cursor.movetogridpos(cursor.getgridx(), + cursor.getgridy()-1); + } break; case Qt::Key_S: + if (cursor.getrealy() < GRIDHEIGHT-BORDEROFFSET) + { + cursor.movetogridpos(cursor.getgridx(), + cursor.getgridy()+1); + } break; case Qt::Key_D: + if (cursor.getrealx() < GRIDWIDTH-BORDEROFFSET) + { + cursor.movetogridpos(cursor.getgridx()+1, + cursor.getgridy()); + } break; case Qt::Key_A: + if (cursor.getrealx() > BORDEROFFSET) + { + cursor.movetogridpos(cursor.getgridx()-1, + cursor.getgridy()); + } break; default: break; } update(); } + + +void qtminesweeper::generatecellpos(cell *c) +{ + int dx, dy; + dx = dy = 0; + c->movetogridpos(dx, dy); + printf("%d\n", c->getrealx()); +} |