Results 1 to 1 of 1

Thread: SpaceShip game

  1. #1
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default SpaceShip game

    Hello,
    I got a project to make SpaceShip game.
    MainWindow.cc:
    Qt Code:
    1. // Create scene for the game
    2. scene_ = std::make_shared<QGraphicsScene>();
    3.  
    4. // Add player to the galaxy
    5. //std::shared_ptr<PlayerShip> player = std::make_shared<PlayerShip>(galaxy_);
    6. PlayerShip *player = new PlayerShip(scene_, galaxy_);
    7. scene_->addItem(player);
    8.  
    9. // Add scene to the view
    10. ui->graphicsView->setScene(scene_.get());
    To copy to clipboard, switch view to plain text mode 
    Playership.cc:
    Qt Code:
    1. PlayerShip::PlayerShip(std::shared_ptr<QGraphicsScene> scene, std::shared_ptr<Student::Galaxy> galaxy)
    2. {
    3. galaxy_ = galaxy;
    4. scene_ = scene;
    5. }
    6.  
    7. void PlayerShip::keyPressEvent(QKeyEvent *event)
    8. {
    9. if(event->key() == Qt::Key_Left)
    10. {
    11. qDebug() << "Left";
    12. setPos(x()-10, y());
    13. }
    14. else if(event->key() == Qt::Key_Right)
    15. {
    16. qDebug() << "Right";
    17. setPos(x()+10, y());
    18. }
    19. else if(event->key() == Qt::Key_Up)
    20. {
    21. qDebug() << "Up";
    22. setPos(x(), y()-10);
    23. }
    24. else if(event->key() == Qt::Key_Down)
    25. {
    26. qDebug() << "Down";
    27. setPos(x(), y()+10);
    28. }
    29. else if(event->key() == Qt::Key_Space)
    30. {
    31. qDebug() << "Space";
    32. // shoot
    33. }
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    I can't figure out how to keep scene - the player in the middle of the scene, I mean, when the player is moving I want to also move the scene, so the game space will be endless. I added scene_ to the PlayerShip class, so e.g. I can set new x/y coordinates after player move, but Im not sure if is it correct way.
    Second problem, I don't know, why does not work this code - does not work mean, the player ship will not show:
    Qt Code:
    1. std::shared_ptr<PlayerShip> player = std::make_shared<PlayerShip>(scene_, galaxy_);
    2. scene_->addItem(player.get());
    To copy to clipboard, switch view to plain text mode 

    Thank you for your advice.


    Edit:
    Well, here is my keyPressEvent function, but Im not sure about the implementation.
    Qt Code:
    1. void PlayerShip::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Left)
    4. {
    5. setTransformOriginPoint(QPoint(18,7));
    6. setRotation(rotation()-5);
    7. }
    8. else if(event->key() == Qt::Key_Right)
    9. {
    10. setTransformOriginPoint(QPoint(18,7));
    11. setRotation(rotation()+5);
    12. }
    13. else if(event->key() == Qt::Key_Up)
    14. { // ToDo: movement is not correct
    15. qreal diffX = 0, diffY = 0;
    16. diffX = rect().width()-rect().x();
    17. diffY = rect().height()-rect().y();
    18. diffX *= cos( rotation() * PI / 180.0 );
    19. diffY *= sin( rotation() * PI / 180.0 );
    20. setPos(x() + diffX, y() + diffY);
    21. // ToDo: check calculations
    22. scene_->setSceneRect(scene_->sceneRect().x()+diffX, scene_->sceneRect().y()+diffY, scene_->sceneRect().width(), scene_->sceneRect().height());
    23. }
    24. else if(event->key() == Qt::Key_Down)
    25. { // ToDo: movement is not correct
    26. qreal diffX = 0, diffY = 0;
    27. diffX = rect().width()-rect().x();
    28. diffY = rect().height()-rect().y();
    29. diffX *= cos( rotation() * PI / 180.0 );
    30. diffY *= sin( rotation() * PI / 180.0 );
    31. setPos(x() - diffX, y() - diffY);
    32. // ToDo: check calculations
    33. scene_->setSceneRect(scene_->sceneRect().x()-diffX, scene_->sceneRect().y()-diffY, scene_->sceneRect().width(), scene_->sceneRect().height());
    34. }
    35. else if(event->key() == Qt::Key_Space)
    36. {
    37. qDebug() << "Space";
    38. // shoot
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ado130; 20th October 2017 at 21:58.

Similar Threads

  1. First QT - C++ Game
    By Sean11 in forum Qt Programming
    Replies: 10
    Last Post: 23rd September 2017, 11:17
  2. 2D Game
    By Peppy in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2011, 13:45
  3. game on qt
    By bibhukalyana in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 23:56
  4. Replies: 1
    Last Post: 22nd May 2010, 08:38
  5. IQ Game
    By qtgears in forum Qt-based Software
    Replies: 0
    Last Post: 6th October 2009, 09:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.