From fed9330a4a98906648306e7524882feac80980e4 Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Sun, 2 Sep 2018 15:02:05 +0930 Subject: added constants files, moved around some classes, beginning of drawing functions etc --- cell.h | 2 +- qtminesweeper.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- qtminesweeper.h | 18 +++++++++-- qtminesweeper.pro | 4 +-- selector.h | 8 ++--- square.cpp | 41 ++++++++----------------- square.h | 20 +++++------- 7 files changed, 131 insertions(+), 53 deletions(-) diff --git a/cell.h b/cell.h index f3d4d75..846f9d0 100644 --- a/cell.h +++ b/cell.h @@ -34,10 +34,10 @@ class cell : public square void flagcheck(); private: + enum flagtype flags; void setflags(int flag); protected: - enum flagtype flags; }; #endif 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()); +} diff --git a/qtminesweeper.h b/qtminesweeper.h index 7de2953..19d4240 100644 --- a/qtminesweeper.h +++ b/qtminesweeper.h @@ -19,8 +19,10 @@ #include #include +#include #include "selector.h" #include "cell.h" +#include "constants.h" class qtminesweeper : public QWidget { @@ -30,8 +32,20 @@ class qtminesweeper : public QWidget explicit qtminesweeper(); /* init function */ private: - cur curs; /* cursor instance */ - + QPen blackpen; + QPen whitepen; + QPen redpen; + QPen greenpen; + QPen bluepen; + + void drawgrid(QPainter *painter); + void drawcursor(QPainter *painter); + void drawcells(QPainter *painter); + void generatecellpos(cell *c); + + selector cursor; + QList cells; + protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent *event); diff --git a/qtminesweeper.pro b/qtminesweeper.pro index 42e777b..b992d4c 100644 --- a/qtminesweeper.pro +++ b/qtminesweeper.pro @@ -19,5 +19,5 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 # Input -HEADERS += qtminesweeper.h square.h selector.h cell.h -SOURCES += main.cpp qtminesweeper.cpp square.cpp selector.cpp cell.cpp +HEADERS += qtminesweeper.h square.h selector.h cell.h constants.h +SOURCES += main.cpp qtminesweeper.cpp square.cpp selector.cpp cell.cpp constants.cpp diff --git a/selector.h b/selector.h index 78f75c0..dc97071 100644 --- a/selector.h +++ b/selector.h @@ -1,6 +1,6 @@ /* * Copyright Daniel Jones 2018 - * cur.h is part of qtminesweeper, a simple minesweeper clone. + * selector.h is part of qtminesweeper, a simple minesweeper clone. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,12 +14,12 @@ * along with this program. If not, see . */ -#ifndef CUR_H -#define CUR_H +#ifndef SELECTOR_H +#define SELECTR_H #include "square.h" -class cur : public square +class selector : public square { public: diff --git a/square.cpp b/square.cpp index be0b32c..1faab09 100644 --- a/square.cpp +++ b/square.cpp @@ -20,24 +20,15 @@ square::square() { } -void square::setrealx(int x) -{ - realx = x; -} - -void square::setrealy(int y) -{ - realy = y; -} -void square::setgridx(int x) +int square::getgridx() { - gridx = x; + return gridx; } -void square::setgridy(int y) +int square::getgridy() { - gridy = y; + return gridy; } int square::getrealx() @@ -49,26 +40,18 @@ int square::getrealy() { return realy; } - -int square::getgridx() -{ - return gridx; -} - -int square::getgridy() -{ - return gridy; -} - void square::movetorealpos(int x, int y) { - setrealx(x); - setrealy(y); + realx = x; + realy = y; } void square::movetogridpos(int x, int y) { - setgridx(x); - setgridy(y); - + gridx = x; + gridy = y; + int rx, ry; + rx = x * SQUARESIZE + BORDEROFFSET; + ry = y * SQUARESIZE + BORDEROFFSET; + movetorealpos(rx, ry); } diff --git a/square.h b/square.h index fd37b58..46a513b 100644 --- a/square.h +++ b/square.h @@ -17,11 +17,18 @@ #ifndef SQUARE_H #define SQUARE_H +#include "constants.h" + class square { public: explicit square(); /* init function */ + int getgridx(); + int getgridy(); + int getrealx(); + int getrealy(); + void movetogridpos(int x, int y); private: /* @@ -34,22 +41,9 @@ class square int gridx; int gridy; - void setrealx(int x); - void setrealy(int y); - - void setgridx(int x); - void setgridy(int y); - void movetorealpos(int x, int y); - int getrealx(); - int getrealy(); - protected: - int getgridx(); - int getgridy(); - - void movetogridpos(int x, int y); }; #endif -- cgit v1.2.3