Results 1 to 11 of 11

Thread: First QT - C++ Game

  1. #1
    Join Date
    Sep 2017
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default First QT - C++ Game

    Hey guys im building my first QT C++ game and i hope ill get some help in a problem i have encountered
    im trying to make group of spaceships in a line, that moves from one side of the screen to the other.
    now when i built it i have defined that if spaceship x() != 1000 keep moving right if it does go left. but they cross eachother and each one of them has to reach the x() == 1000 to change direction.
    now i want to them to move as a group if the most left spaceship reached the maximum space of the right side of the screen they all start move left and so..
    https://github.com/SeanR11/Spaceship---first-game
    here is my code on github hope u can read and come with a solution ty.!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    The problem is in your design. As it stands now, when you create your game scene, it goes from x = 0 - 1000. You put 10 spaceships in a line separated by 100 units, going from 0 - 900. The problem is this: you start timers in each spaceship that move each one independently of all the others. So when one spaceship reaches the end of the line, none of the others know about it. So even if that last spaceship changes direction, the other 9 keep on marching towards 1000 (or 0) until each one also makes it to the end and turns around. So instead of a line of space invader style ships marching back and forth together, you have a conga line.

    What you need is to add another piece to the design - a spaceship controller. This class is the only one that needs a timer. With each timeout, it advances the position of -all- of the spaceships. When spaceship[9] (the tenth one) reaches 1000 or spaceship[0] (the first one) reaches 0, the controller changes the direction of the line and everyone turns around at the same time.

    An easy place to do this is instead of creating a new class, let the game class do it. As you create each spaceship and add it to the scene, also add the pointer to a QVector<Spaceship*> that is a member variable of the game class. When you are ready to start playing, start the QTimer and then update the positions of every spaceship when it times out by retrieving each one from the QVector and updating pos()..
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Sean11 (22nd September 2017)

  4. #3
    Join Date
    Sep 2017
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    First of all thanks for your answer
    i really liked your idea and got the main point of what u wanted to explain to me.
    but as i said im new to QT and it is my first game and i dont know how to use QVector or even what it does.
    now i know the whole point of writing a game is writing it alone but because im still learning i be glad if u can help me write a code that i can learn from him to next times/games
    i know it is a work for you but if you could help me ill be more than happy to receive your help.

    **EDITED**
    another problem i have encountered is that i cant create spaceship[10] i have tried it and i saw you wrote spaceship[0]->spaceship[9] but i couldnt create this object
    there is any solution for that?

    again tnx for your helping.
    Last edited by Sean11; 22nd September 2017 at 11:00.

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

    Default Re: First QT - C++ Game

    Do you know C++? Or do you want to learn C++? Maybe you can start with a little bit easier application than a game.

  6. #5
    Join Date
    Sep 2017
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    I have programmed many games and softwarers on c++ on visual studio, on cmd platform...
    im learning QT.. its a bit different for me and i didnt programmed at all for like 1-1.5 years and i just wanted a help with the code and if u can help me and show me the way to get better ill be much thanksful for that.
    i also have another question if i want to add class that in-charge of changing levels. What should the class inherits from?
    Last edited by Sean11; 22nd September 2017 at 17:04.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    spaceship[10]
    I meant this is a figurative sense, not as C++ code. "spaceship[0]" is the first spaceship you create in your loop. "spaceship[9]" is the last one. I agree with ado130 - you really need to learn a little more about C++. QVector is the Qt version of std:: vector, and if you don't know what that is either, then you need to learn some more. It's hard to help you with example code if you don't understand the code we are providing.

    Remove the QTimer stuff from the Spaceship class. Here is your game.[h, cpp] with some added code to help you get started:

    Qt Code:
    1. // game.h
    2.  
    3. #ifndef GAME_H
    4. #define GAME_H
    5.  
    6. #include <QGraphicsView>
    7. #include <QGraphicsScene>
    8. #include <QVector>
    9.  
    10. class Spaceship;
    11. class QTimer;
    12.  
    13. class Game: public QGraphicsView
    14. {
    15. Q_OBJECT; // <<< Absolutely need this
    16.  
    17. public:
    18. // constructor
    19. Game(QWidget* parent=NULL);
    20.  
    21. // public method
    22. void start();
    23.  
    24. private slots:
    25. void onSpaceshipTimeout();
    26.  
    27. // Attributes should ALWAYS be private or protected, never public
    28. // If you need access to them from outside the Game class, then provide
    29. // Game class methods to get or set their values.
    30.  
    31. private:
    32. QGraphicsScene * scene;
    33. //Map* map; TODO
    34. QString Score;
    35. QString Life;
    36.  
    37. QTimer * pSpaceshipTimer;
    38. QVector < Spaceship * > spaceships;
    39. int direction;
    40. };
    41.  
    42. #endif // GAME_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "game.h"
    2. #include "player.h"
    3. #include <QTimer>
    4. #include "spaceship.h"
    5.  
    6. Game::Game(QWidget *parent)
    7. : QGraphicsView( parent ) // <<< NEED THIS
    8. , direction( 1 )
    9. {
    10.  
    11. // set screen
    12. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    13. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    14. setFixedSize(1024,800);
    15.  
    16. // set scene
    17. scene = new QGraphicsScene();
    18. scene->setSceneRect(0,0,1000,800);
    19. setScene(scene);
    20.  
    21. // set player
    22. Player * player = new Player();
    23.  
    24. // add player to scene
    25. scene->addItem(player);
    26.  
    27. // add enemy to scene
    28. for(int lol = 0; lol < 10; lol++)
    29. {
    30. Spaceship * spaceship = new Spaceship(x, 10);
    31. scene->addItem(spaceship);
    32. x = x + 100;
    33.  
    34. // add a copy of the spaceship POINTER to the vector for use later
    35. spaceships.push_back( spaceship );
    36. }
    37.  
    38. pSpaceshipTimer = new QTimer( this );
    39. connect( pSpaceshipTimer, SIGNAL( timeout() ), this, SLOT( onSpaceshipTimeout() ) );
    40. }
    41.  
    42. void Game::start()
    43. {
    44. pSpaceshipTimer->start( 500 ); // 50 is too short.
    45. }
    46.  
    47. void Game::onSpaceshipTimeout()
    48. {
    49. // Note that "direction" has the values 1 or -1 to indicate movement to the right or left, respectively
    50. int nShips = spaceships.size();
    51. if ( direction > 0 )
    52. {
    53. if ( spaceships[ nShips - 1 ]->x() >= 950 )
    54. direction = -1;
    55. }
    56. else
    57. {
    58. if ( spaceships[ 0 ]->x() <= 50 )
    59. direction = 1;
    60. }
    61.  
    62. int distance = 10 * direction;
    63. for ( nShip = 0; nShip < nShips; ++nShip )
    64. {
    65. Spaceship * spaceship = spaceships[ nShip ];
    66. spaceship->setX( spaceship->x() + distance );
    67. }
    68. }
    To copy to clipboard, switch view to plain text mode 

    Not compiled or tested so there may be typos or bugs.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    Sep 2017
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    i understand that it is harder to learn if i dont know what the code means but i read the code and it i tested your code fixed some typos issue and the code still does the same
    they are moving left and then each one of them have to hit the wall by himself they are not moving as 1 line of spaceships.

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    they are not moving as 1 line of spaceships.
    Did you remove the QTimer from your Spaceship class?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #9
    Join Date
    Sep 2017
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    Oh i forgot to do it.
    i did it and it is working now..
    thanks for your help i will read it few times in order to understand what each line says and means.
    meanwhile i have another small question if i want to create class that controls the levels in this class there will be void for each level and in each void it will create spaceships in the order the level require to:
    1.what should the class inherits from? QGraphicsView?
    2.should i create QVector on the level class so i can make my for loop that will .push_back the spaceship POINTER into the Vector and then method inside game.cpp that will transfer the POINTER from Level.cpp Vector to game.cpp Vector("spaceships" thats the name you gave the vector)

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    I'm sorry, but I don't really understand what you are asking. What is a "level"? Is it a type of difficulty of play? Or do you mean additional rows of spaceships?

    If you mean a "level" is a row of spaceships, then I think a Level class (derived from QObject if you want it to receive signals to slots) would be the best solution. In that case, each Level should own a QVector of spaceship pointers, the spaceships in that row of the screen. The Game should have a QVector of Level pointers instead of the QVector of spaceship pointers.

    The Level class probably should have these methods, slots, and signals:

    - addSpaceship( Spaceship * ) // pushes a spaceship onto the QVector

    // a slot - this is the same code as is now in Game:: onSpaceshipTimout(), except you also need to decrease the y() position when the spaceships reach the end of the line in addition to changing direction
    - void step()

    // a signal emitted each time the row reaches the end of the line and changes direction. yPos is the new y position when the row drops down. Connected to the Game's "onEndOfLine( int yPos )" slot. In this slot, the Game decides whether to add a new Level at the top of the screen or if the game should end (when yPos == 0)
    - void endOfLine( int yPos )


    You should change the Game class so that instead of creating a row of spaceships in the constructor, it calls an "addLevel( int yPos ) method:

    Qt Code:
    1. void Game::addLevel( int yPos )
    2. {
    3. Level * level = new Level( this );
    4.  
    5. // add enemy to scene
    6. for(int lol = 0; lol < 10; lol++)
    7. {
    8. Spaceship * spaceship = new Spaceship(x, yPos );
    9. scene->addItem(spaceship);
    10. x = x + 100;
    11.  
    12. // add the spaceship to the level
    13. level->addSpaceship( spaceship );
    14. }
    15. connect( level, SIGNAL( endOfLine( int ) ), this, SLOT( onEndOfLine( int ) ) );
    16.  
    17. levels.push_back( level ); // "levels" is QVector< Level * >, a member of the Game class
    18. }
    19.  
    20. void Game::onSpaceshipTimeout()
    21. {
    22. int nLevels = levels.size();
    23. for ( int nLevel = 0; nLevel < nLevels; ++nLevel )
    24. {
    25. Level * level = levels[ nLevel ];
    26. level->step();
    27. }
    28. }
    29.  
    30. void Level::step()
    31. {
    32. // Note that "direction" has the values 1 or -1 to indicate movement to the right or left, respectively
    33. int nShips = spaceships.size();
    34. bool bChangedDirection = false;
    35. if ( direction > 0 )
    36. {
    37. if ( spaceships[ nShips - 1 ]->x() >= 950 )
    38. {
    39. direction = -1;
    40. bChangedDirection = true;
    41. }
    42. }
    43. else
    44. {
    45. if ( spaceships[ 0 ]->x() <= 50 )
    46. {
    47. direction = 1;
    48. bChangedDirection = true;
    49. }
    50. }
    51.  
    52. int yPos = spaceships[ 0 ]->y();
    53. if ( bChangedDirection ) // have to decrease y also now
    54. yPos -= 10;
    55.  
    56. int distance = 10 * direction;
    57. for ( nShip = 0; nShip < nShips; ++nShip )
    58. {
    59. Spaceship * spaceship = spaceships[ nShip ];
    60. spaceship->setPos( spaceship->x() + distance, yPos );
    61. }
    62.  
    63. if ( bChangedDirection )
    64. emit endOfLine( yPos );
    65. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. #11
    Join Date
    Sep 2017
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First QT - C++ Game

    the levels it is type of difficulty i want to make class called Levels(int cLvl)
    in the Levels method there is:
    if(cLvl == 1)
    Level1(); // opens level 1 method
    in the level1() method there is for loop that spawn and place all the spaceship for this level

Similar Threads

  1. 2D Game
    By Peppy in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2011, 13:45
  2. game on qt
    By bibhukalyana in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 23:56
  3. Replies: 1
    Last Post: 22nd May 2010, 08:38
  4. IQ Game
    By qtgears in forum Qt-based Software
    Replies: 0
    Last Post: 6th October 2009, 09:24
  5. Just for fun game
    By vermarajeev in forum Qt-based Software
    Replies: 6
    Last Post: 13th December 2007, 22:52

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.