gamescence.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "gamescence.h"
  2. #include "ui_gamescence.h"
  3. GameScence::GameScence(QWidget* parent)
  4. : QWidget(parent)
  5. , ui(new Ui::GameScence)
  6. {
  7. ui->setupUi(this);
  8. m_gameMap = nullptr;
  9. m_tmr = nullptr;
  10. setMouseTracking(true);
  11. m_backgroundMusic = new QSoundEffect(this);
  12. m_failedMusic = new QSoundEffect(this);
  13. m_successedMusic = new QSoundEffect(this);
  14. QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)[0];
  15. QSettings ini(path + "/set.ini", QSettings::IniFormat);
  16. ini.beginGroup("set");
  17. m_level = ini.value("level", 1).toInt();
  18. ini.endGroup();
  19. m_buttondownFlag = false;
  20. }
  21. GameScence::~GameScence()
  22. {
  23. delete ui;
  24. }
  25. GameMap* GameScence::gameMap() const
  26. {
  27. return m_gameMap;
  28. }
  29. void GameScence::setGameMap(GameMap* gameMap)
  30. {
  31. m_gameMap = gameMap;
  32. }
  33. void GameScence::paintEvent(QPaintEvent* event)
  34. {
  35. QPainter painter;
  36. painter.begin(this);
  37. painter.setRenderHints(QPainter::HighQualityAntialiasing);
  38. if (m_gameMap != nullptr) {
  39. QImage img = m_gameMap->drawScence();
  40. QPixmap pix = QPixmap::fromImage(img);
  41. QPoint pt = m_gameMap->hero()->rect().center();
  42. QRect rc;
  43. rc.setRect(pt.x() - width() / 2, pt.y() - height() / 2, width(), height());
  44. painter.drawPixmap(rect(), pix, rc);
  45. }
  46. QPoint pt = mapFromGlobal(cursor().pos());
  47. m_leftRect.setRect(64, height() - 192, 64, 64);
  48. if ((m_leftRect.contains(pt)) && (m_buttondownFlag == true)) {
  49. painter.drawPixmap(m_leftRect, m_buttonDownPix, m_buttonDownPix.rect());
  50. } else {
  51. painter.drawPixmap(m_leftRect, m_buttonPix, m_buttonPix.rect());
  52. }
  53. m_rightRect.setRect(192, height() - 192, 64, 64);
  54. if ((m_rightRect.contains(pt)) && (m_buttondownFlag == true)) {
  55. painter.drawPixmap(m_rightRect, m_buttonDownPix, m_buttonDownPix.rect());
  56. } else {
  57. painter.drawPixmap(m_rightRect, m_buttonPix, m_buttonPix.rect());
  58. }
  59. m_upRect.setRect(128, height() - 256, 64, 64);
  60. if ((m_upRect.contains(pt)) && (m_buttondownFlag == true)) {
  61. painter.drawPixmap(m_upRect, m_buttonDownPix, m_buttonDownPix.rect());
  62. } else {
  63. painter.drawPixmap(m_upRect, m_buttonPix, m_buttonPix.rect());
  64. }
  65. m_downRect.setRect(128, height() - 128, 64, 64);
  66. if ((m_downRect.contains(pt)) && (m_buttondownFlag == true)) {
  67. painter.drawPixmap(m_downRect, m_buttonDownPix, m_buttonDownPix.rect());
  68. } else {
  69. painter.drawPixmap(m_downRect, m_buttonPix, m_buttonPix.rect());
  70. }
  71. painter.end();
  72. }
  73. void GameScence::keyPressEvent(QKeyEvent* event)
  74. {
  75. if (event->key() == Qt::Key_Right) {
  76. m_gameMap->hero()->moveRight();
  77. }
  78. if (event->key() == Qt::Key_Left) {
  79. m_gameMap->hero()->moveLeft();
  80. } else if (event->key() == Qt::Key_Up) {
  81. m_gameMap->hero()->moveUp();
  82. } else if (event->key() == Qt::Key_Down) {
  83. m_gameMap->hero()->moveDown();
  84. }
  85. update();
  86. }
  87. void GameScence::mouseMoveEvent(QMouseEvent* event)
  88. {
  89. }
  90. void GameScence::timeout()
  91. {
  92. qDebug() << "play";
  93. bool ret = true;
  94. if (m_gameMap != nullptr) {
  95. ret = m_gameMap->play();
  96. update();
  97. }
  98. // if (ret == false) {
  99. // m_tmr->stop();
  100. // }
  101. }
  102. QPixmap GameScence::buttonDownPix() const
  103. {
  104. return m_buttonDownPix;
  105. }
  106. void GameScence::setButtonDownPix(const QPixmap& buttonDownPix)
  107. {
  108. m_buttonDownPix = buttonDownPix;
  109. }
  110. void GameScence::mousePressEvent(QMouseEvent* event)
  111. {
  112. m_buttondownFlag = true;
  113. QPoint pt = mapFromGlobal(cursor().pos());
  114. if (m_leftRect.contains(pt)) {
  115. m_gameMap->hero()->moveLeft();
  116. }
  117. if (m_rightRect.contains(pt)) {
  118. m_gameMap->hero()->moveRight();
  119. }
  120. if (m_upRect.contains(pt)) {
  121. m_gameMap->hero()->moveUp();
  122. }
  123. if (m_downRect.contains(pt)) {
  124. m_gameMap->hero()->moveDown();
  125. }
  126. update();
  127. }
  128. void GameScence::mouseReleaseEvent(QMouseEvent* event)
  129. {
  130. m_buttondownFlag = false;
  131. update();
  132. }
  133. QPixmap GameScence::buttonPix() const
  134. {
  135. return m_buttonPix;
  136. }
  137. void GameScence::setButtonPix(const QPixmap& buttonPix)
  138. {
  139. m_buttonPix = buttonPix;
  140. }
  141. int GameScence::level()
  142. {
  143. QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)[0];
  144. QDir dir;
  145. dir.mkpath(path);
  146. QSettings ini(path + "/set.ini", QSettings::IniFormat);
  147. qDebug() << path + "/set.ini";
  148. ini.beginGroup("set");
  149. m_level = ini.value("level", 1).toInt();
  150. ini.endGroup();
  151. return m_level;
  152. }
  153. void GameScence::setLevel(int level)
  154. {
  155. QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)[0];
  156. QSettings ini(path + "/set.ini", QSettings::IniFormat);
  157. qDebug() << path + "/set.ini";
  158. ini.beginGroup("set");
  159. ini.setValue("level", level);
  160. ini.endGroup();
  161. m_level = level;
  162. }
  163. QSoundEffect* GameScence::successedMusic() const
  164. {
  165. return m_successedMusic;
  166. }
  167. void GameScence::setSuccessedMusic(QSoundEffect* successedMusic)
  168. {
  169. m_successedMusic = successedMusic;
  170. }
  171. void GameScence::startTimer()
  172. {
  173. if (m_tmr != nullptr) {
  174. delete m_tmr;
  175. }
  176. m_tmr = new QTimer(this);
  177. connect(m_tmr, &QTimer::timeout, this, &GameScence::timeout);
  178. m_tmr->setInterval(10);
  179. m_tmr->start();
  180. m_backgroundMusic->setVolume(1);
  181. m_backgroundMusic->play();
  182. setFocus();
  183. }
  184. QSoundEffect* GameScence::failedMusic() const
  185. {
  186. return m_failedMusic;
  187. }
  188. void GameScence::setFailedMusic(QSoundEffect* failedMusic)
  189. {
  190. m_failedMusic = failedMusic;
  191. }
  192. QSoundEffect* GameScence::backgroundMusic() const
  193. {
  194. return m_backgroundMusic;
  195. }
  196. void GameScence::setBackgroundMusic(QSoundEffect* backgroundMusic)
  197. {
  198. m_backgroundMusic = backgroundMusic;
  199. }