예제 #1
0
Game::Game(QWidget* parent){

    QGraphicsScene * scene = new QGraphicsScene();
    scene->setSceneRect(0,0,800,600);
    setBackgroundBrush(QBrush(QImage(":/images/bg4.jpg")));

    setWindowTitle("Taiko~");
    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFixedSize(800,600);


    //create the player
    MyRect * rect = new MyRect();
//    setPixmap(QPixmap(":/images/rect.jpg"));
//    rect -> setRect (0,0,10,10);
//    rect->setPos(width()/2,height()-rect->rect().height());
    rect->setPos(230,235);


    //make the player focusable
    rect->setFlag(QGraphicsItem::ItemIsFocusable);
    rect->setFocus();
    // add the rect to the scene
    scene->addItem(rect);

    //create the score
    score = new Score();
    score->setPos(40,210);
    scene->addItem(score);
    
    //create the time
    time = new Time();
    time->setPos(42,243);
    scene->addItem(time);
    //time->countdown ->start();

    //spawn enemies
    QTimer  * timer = new QTimer();
    QObject::connect(timer, SIGNAL(timeout()),rect,SLOT(spawn()));
    timer->start(1000);

}
예제 #2
0
파일: main.cpp 프로젝트: Scieon/Qt
/*Tutorial:
QGraphicsScene: world, player (Inivisble ~concept)
QGraphicsItem(QGraphicsRectitem): goes into scene
QGraphicsView:
*/
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //Create a scene
    QGraphicsScene *scene  = new QGraphicsScene();

    //Create an item to put into the scene
    MyRect *rect = new MyRect();

    rect->setRect(0,0,100,100); //(x,y,width,height)

    //Add item to the Scene
    scene->addItem(rect);

    //Make Rect focusable
    rect->setFlag(QGraphicsItem::ItemIsFocusable); //Only one focused item at a time
                                                   //Allowing rect to be focusable
    rect->setFocus(); //Setting rect item to focus

    //Add a view
    QGraphicsView *view = new QGraphicsView(scene); //Initializing in constructor
    //view->setScene(scene); alternative

    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    view->show();

    view->setFixedSize(800,600);
    scene->setSceneRect(0,0,800,600);

    rect->setPos(view->width()/2,view->height() - rect->rect().height());

    //Spawn Enemies
    QTimer *timer = new QTimer();
    QObject::connect(timer,SIGNAL(timeout()),rect,SLOT(spawn()));
    timer->start(2000);

    return a.exec();
}
예제 #3
0
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // Create a scene
    QGraphicsScene * mygamescene = new QGraphicsScene();
    MyRect * player = new MyRect();
    player->setRect(0,0,50,50);
    mygamescene->addItem(player);
    // Make rect respond to events
    player->setFlag(QGraphicsItem::ItemIsFocusable);
    player->setFocus();
    QGraphicsView * myview = new QGraphicsView(mygamescene);
    myview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    myview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    myview->show();
    myview->setFixedSize(800,600);
    mygamescene->setSceneRect(0,0,800,600);
    player->setPos((myview->width()-player->rect().width())/2,myview->height()-player->rect().height());
    return a.exec();
}
예제 #4
0
/* GameBored contstructor
 * @param QWidget is the parent widget
*/
GameBored::GameBored(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GameBored)
{
    ui->setupUi(this);

    // make scene
    QGraphicsScene * scene= new QGraphicsScene();
    scene->setSceneRect(0,0,800,600);
    scene->setBackgroundBrush(QBrush(QImage(":/pics/bg")));


    // make and add item to scene
    MyRect * player = new MyRect();
    scene->addItem(player);

    // focus item so that it can receive keyboard events
    player->setFlag(QGraphicsItem::ItemIsFocusable);
    player->setFocus();


    // add scene to new view
    QGraphicsView * view = new QGraphicsView(scene);

    //turn off scrollbars
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    Board= new QWidget;
    //Board->setFixedSize(400,400);

    // add view to new layout
    // add layout to central widget
    QVBoxLayout * vlayout = new QVBoxLayout(Board);
    vlayout->addWidget(view);
    this->setLayout(vlayout);

    //
    this->show();
    view->setFixedSize(800,600);
    scene->setSceneRect(0,0,800,600);

    player->setPos(view->width()/2 -player->pixmap().width()/2, -28 /*view->height()- player->pixmap().height()*/);

    // constant creation of enemies
    QTimer* timer = new QTimer();
    QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
    timer->start(2400);

    QTimer* timer2 = new QTimer();
    QObject::connect(timer2,SIGNAL(timeout()),player,SLOT(spawn2()));
    timer2->start(5000);


    // play music
    mp3player = new QMediaPlayer();
    mp3player->setMedia(QUrl("qrc:/audio/starfox.mp3"));
    mp3player->play();

    // add score class to screen
    score = new Score();
    scene->addItem(score);
}