Skip to content

Commit

Permalink
20%completed 魔塔,just kill me already...
Browse files Browse the repository at this point in the history
  • Loading branch information
iilove27 committed Jan 21, 2019
0 parents commit 129e134
Show file tree
Hide file tree
Showing 27 changed files with 1,362 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "Button.h"
Button::Button(QString name, QGraphicsItem *parent): QGraphicsRectItem (parent)
{
setRect(0, 0, 200, 50);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush);

text = new QGraphicsTextItem(name, this);
int xPos = rect().width()/2 - text->boundingRect().width()/2;
int yPos = rect().height()/2 - text->boundingRect().height()/2;
text->setPos(xPos, yPos);

setAcceptHoverEvents(true);
}

void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
emit clicked();
}

void Button::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
//change color
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::cyan);
setBrush(brush);
}

void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
//change color
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush);
}
25 changes: 25 additions & 0 deletions Button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BUTTON_H
#define BUTTON_H

#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsTextItem>
#include <QBrush>

class Button: public QObject, public QGraphicsRectItem
{ Q_OBJECT
public:
Button(QString name, QGraphicsItem* parent=NULL);

// public events
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
signals:
void clicked();

private:
QGraphicsTextItem *text;
};

#endif // BUTTON_H
37 changes: 37 additions & 0 deletions Msgboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Msgboard.h"
#include "game.h"
#include <unistd.h>

/*这个class原本是为了碰到npc的时候弹出来的时候用的,但是弹出的对话框如果被给了focus,就没法再给回去英雄去了
但是这个类应该还是要用上的*/




extern Game * game;

MsgBoard::MsgBoard(QString name, int x, int y, int width, int height, QGraphicsItem *parent): QGraphicsRectItem (parent)
{
setRect(x, y, width, height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::darkCyan);
setBrush(brush);

text = new QGraphicsTextItem(name, this);
int xPos = rect().width()/2 - text->boundingRect().width()/2;
int yPos = rect().height()/2 - text->boundingRect().height()/2;
text->setPos(xPos, yPos);

}

void MsgBoard::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space)
{
game->scene->removeItem(this);
delete this;
}
}


23 changes: 23 additions & 0 deletions Msgboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MSGBOARD_H
#define MSGBOARD_H

#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsTextItem>
#include <QBrush>
#include <QKeyEvent>
#include "hero.h"

class MsgBoard: public QObject, public QGraphicsRectItem
{ Q_OBJECT
public:
MsgBoard(QString name, int x, int y, int width, int height, QGraphicsItem* parent=NULL);
void keyPressEvent(QKeyEvent *event);
Hero * hero;
void showAndDisappear();
private:
QGraphicsTextItem *text;
};


#endif // MSGBOARD_H
Binary file added bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bgm.mp3
Binary file not shown.
195 changes: 195 additions & 0 deletions game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#include "game.h"
#include "monster.h"
#include <stdio.h>
#include <iostream>
#include "unwalkable.h"
#include <QBrush>
#include <QImage>
#include <stdlib.h>
#include "Button.h"
#include <QGraphicsTextItem>
#include "hero.h"
#include <QTimer>

int map2D[11][11];
int unwalkableArray[3] = {1, 2, 3};
int npcIdArray[1] = {44}; // test npc 对话
Game::Game(QWidget *parent)
{
// set the scene
scene = new QGraphicsScene();
scene->setSceneRect(0,0,440+100*2,440);
setBackgroundBrush(QBrush(QImage(":/images/bg.png").scaled(440+100*2, 440)));
setScene(scene);
setFixedSize(640,440);

// 我准备让他自己set focus,但是会出问题...
// QTimer * timer = new QTimer();
// connect(timer,SIGNAL(timeout()),this,SLOT(setFocusToHero()));
// timer->start(4000);
}



void Game::gameOver()
{
// Display gameover window, can be expanded later
QString msg;
msg = "YOU WON! \nThis game is our childhood, \n if you like it, give me an A.";
displayGameoverWindow(msg);

}

void Game::displayGameoverWindow(QString textToDisplay)
{
// disable all items
for (size_t i = 0, n = scene->items().size(); i < n; i++)
{
scene->items()[i]->setEnabled(false);
}
drawPanel(0, 0, 640, 440, Qt::darkCyan, 0.65);

QGraphicsTextItem* titleText = new QGraphicsTextItem(QString(textToDisplay));
QFont titleFont("comic sans", 20);
titleText->setFont(titleFont);
titleText->setPos(60, 100);
scene->addItem(titleText);

//Play again
Button* playAgain = new Button(QString("Play Again"));
playAgain->setPos(110, 300);
scene->addItem(playAgain);
connect(playAgain, SIGNAL(clicked()), this, SLOT(restartGame()));

// quit
Button* quitButton = new Button(QString("Quit"));
quitButton->setPos(410, 300);
scene->addItem(quitButton);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));


}

void Game::displayMainMenu()
{
QGraphicsTextItem* titleText = new QGraphicsTextItem(QString("Magic Tower"));
QFont titleFont("comic sans", 50);
titleText->setFont(titleFont);
int txPos = this->width()/2 - titleText->boundingRect().width()/2;
int tyPos = 150;
titleText->setPos(txPos, tyPos);
scene->addItem(titleText);

Button* playButton = new Button(QString("Play"));
int bxPos = this->width()/2 - titleText->boundingRect().width()/2+50;
int byPos = 275;
playButton->setPos(bxPos, byPos);
connect(playButton, SIGNAL(clicked()), this, SLOT(start()));
scene->addItem(playButton);

Button* quitButton = new Button(QString("Quit"));
int qxPos = this->width()/2 - titleText->boundingRect().width()/2+50;
int qyPos = 350;
quitButton->setPos(qxPos, qyPos);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
scene->addItem(quitButton);
}


void Game::messageBoard(int x, int y, int width, int height, QString text)
{
QGraphicsRectItem* board = new QGraphicsRectItem(x, y, width, height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::gray);
board->setBrush(brush);
scene->addItem(board);
}


void Game::start()
{
scene->clear();
maps = new map();
maps->getMap2D(0); // 0-th floor
maps->show(); // render
drawGUI();

// Adding BGM
QMediaPlayer * music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/bgm.mp3"));
music->play();

show();
}

void Game::restartGame()
{
// also need to clear data, not done yet
scene->clear();
start();
}

void Game::drawPanel(int x, int y, int width, int height, QColor color, double opacity)
{
QGraphicsRectItem* panel = new QGraphicsRectItem(x, y, width, height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
panel->setBrush(brush);
panel->setOpacity(opacity);
scene->addItem(panel);
}

void Game::drawGUI()
{
drawPanel(0, 0, 100, 440, Qt::darkCyan, 1);
drawPanel(540, 0, 100, 440, Qt::darkCyan, 1);

QGraphicsTextItem* HeroInfo = new QGraphicsTextItem(QString("Hero"));
HeroInfo->setPos(25, 0);
scene->addItem(HeroInfo);

QGraphicsTextItem* OtherInfo = new QGraphicsTextItem(QString("Item"));
OtherInfo->setPos(540, 0);
scene->addItem(OtherInfo);

QGraphicsTextItem* yKey = new QGraphicsTextItem(QString("Yellow key :\t") + QString::number(getNumOfYellowKey()));
QGraphicsTextItem* bKey = new QGraphicsTextItem(QString("Blue key :\t") + QString::number(getNumOfBlueKey()));
QGraphicsTextItem* rKey = new QGraphicsTextItem(QString("Red key :\t") + QString::number(getNumOfRedKey()));

yKey->setPos(540, 40);
bKey->setPos(540, 80);
rKey->setPos(540, 120);
scene->addItem(bKey);
scene->addItem(yKey);
scene->addItem(rKey);

}


int Game::getNumOfRedKey()
{
return numOfRedKey_;
}
int Game::getNumOfBlueKey()
{
return numOfBlueKey_;
}
int Game::getNumOfYellowKey()
{
return numOfYellowKey_;
}
void Game::incrementNumOfRedKey()
{
numOfRedKey_++;
}
void Game::incrementNumOfBlueKey()
{
numOfBlueKey_++;
}
void Game::incrementNumOfYellowKey()
{
numOfYellowKey_++;
}

51 changes: 51 additions & 0 deletions game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef GAME_H
#define GAME_H

#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsScene>
#include "hero.h"
#include <QMediaPlayer>
#include "monster.h"
#include "map.h"


extern int map2D[11][11];
extern int unwalkableArray[3];
extern int npcIdArray[1];

class Game: public QGraphicsView
{ Q_OBJECT
public:
Game(QWidget * parent=NULL);
QGraphicsScene * scene;
Hero * hero;
Monster * mons;
map * maps;
void mapCompile(int map1d[]);
void gameOver();
void displayGameoverWindow(QString textToDisplay);
void displayMainMenu();
int getNumOfRedKey();
void incrementNumOfRedKey();
int getNumOfBlueKey();
void incrementNumOfBlueKey();
int getNumOfYellowKey();
void incrementNumOfYellowKey();
void drawPanel(int x, int y, int width, int height, QColor color, double opacity);

void messageBoard(int x, int y, int width, int height, QString text);
public slots:
void start();
void restartGame();

private:
void drawGUI();
int numOfRedKey_ = 0;
int numOfYellowKey_ = 0;
int numOfBlueKey_ = 0;


};

#endif // GAME_H
Binary file added ground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 129e134

Please sign in to comment.