Advanced search

QMLGame on QML - Lesson 2. Adding traces of shots and points system

July 14, 2016, 4 a.m.

GameDev, QML, Qt, система очков

After the game arena and targets were made and added, it is time to implement the targets hit, misses and charging system and cheating points.

Scoring system will be as follows:

  • In the case of near misses, I propose to make the traces of bullet holes, a few species, and cheating on one point with a game account.
  • In the case of the disappearance of the target on it without falling also make cheating a point from the game account.
  • In the case of hitting the target charge between one and five points depending on how close to the hit had given.

The score will be displayed at the top of the playing field.

Project structure

To achieve this you will need ideas, except the modification of the file, add the following files:

  • InfoBar.qml - that will display information about the account;
  • Bullet.qml - file describing the type of bullet holes;
  • bullet-1.png, bullet-2.png, bullet-3.png, bullet-4.png, bullet-5.png, bullet-6.png - files with images of bullet holes;

Possible bullet holes are placed in the graphics folder to the same target image has been transferred.

QtQt/C++ - Lesson 033. Working with QGraphicsObject instead of QGraphicsItem

My attention was drawn to the fact that for working with signals and slots instead of a class inherited from QGraphicsItem and from QObject , you can use a class inherited from QGraphicsObject . Indeed, if a little rummage in the source QGraphicsObject , we find that it is a class inherited from QGraphicsItem and from QObject . That is also used multiple inheritance, but in this case all the bikes have written to us. Therefore, we try to work with this class as an example of the game mechanics.

Specifically, I propose to write a program in which we will move the hero mouse click on the graphic scene, as in any RPG like Diablo.

Project structure for work with QGraphicsObject

  • QGraphicsObjectExample.pro - the profile of the project;
  • main.cpp - the main source file;
  • widget.h - header file of the application window;
  • widget.cpp - file source code of the application window;
  • customscene.h - customized header graphic scene;
  • customscene.cpp - file source customized graphic scene ;
  • triangle.h - header file of hero class;
  • triangle.cpp - source file triangle class.

QtGameDev on Qt - Tutorial 5. The explosion of bullets using sprite images

In previous articles we have learned to draw a sprite image and apply it to Qt via QPixmap so we turned animated explosion. And now we need to put this very explosion at precisely the place where the bullet hits. That is, the bullet will explode.

For the implementation of this ideas will add class sprite from the past lessons, and modify it. The fact is that the sprite object is a class inherited from QGraphicsItem , and therefore a bullet when confronted with the explosion of another bullet will behave as well as in a collision with an obstacle or a target. Therefore it is necessary to do so to ignore the bullets from the other explosions and bullets flying through.

Project structure

The project is subject to modification in the sense that a new class is added to the following files:

  • sprite.h
  • sprite.cpp

This class will be called animated sprite explosion, after which this frame object will be removed from the graphic scene.

QtGameDev on Qt - Tutorial 4. Collision Detection in Qt (2D)

Oct. 8, 2015, 12:03 p.m.

GameDev, Qt, Collision, Коллизия

Collision detection is one of the important aspects in GameDev. As it relates to the Conflict absolutely everything: hit the enemy bullets, bullets hit your hero, the hero of the body crossing the walls, crossing the body of the hero with the opponents, etc. And the important point is that the hero does not get stuck in a foreign object, but like any other object, and must, in its simplest form, push on it to stay in place. It is for this purpose and is collision detection.

QtGameDev on Qt - Tutorial 3. Destroying opponents

Oct. 5, 2015, 9:58 a.m.

GameDev, Qt, Enemy

In two previous articles, where we taught the hero to track the cursor and shoot towards the goal , it's time to add the targets to the gameand start  to destroy them. The destruction of the target will occur when the targets have completed life. In this case each of the target will be a random number of points of life and every bullet will cause a random amount of damage. Also, each of the target will be life bar, which will decrease on damage.

Destruction based on CallBack function

To implement this algorithm, create a Target target class, and add the ability to call CallBack function in Bullet class, which will be implemented in the class of the main application window and will cause damage to targets.

QtGameDev on Qt - Tutorial 2. Bullet Writing class for shooting in Qt

Oct. 1, 2015, 9:55 a.m.

GameDev, bullet, Qt, пуля

Once we started to manage their hero, and his gaze is always turned toward the goal, it's time to write a class of Bullet, which will be responsible for the bullets and their flight on the gaming scene. The mechanics of moving a bullet on the graphic scene will be similar to the mechanics of movement of the main character. The difference is that the bullet will always move in a straight line and turn the bullets need to be set only at the time of the creation of Bullet class object, to set the direction of flight of the bullet.

Project structure

The structure of the project from the previous lesson is changed in the sense that a new class is added Bullet. You also need to modify all of the other classes to ensure interaction with the new class.

So we start in order, namely, the class which is initialized to the event causing the firing process.

QtGameDev on Qt - Tutorial 1. Track mouse movement in QGraphicsScene

Everyone remembers the game Crimsonland ? There had to cut stacks of monsters. To move on the playing field, we used the keys W, A, S, D, and shooting direction the mouse cursor, which was produced track. So, in itself mechanism of this very track the mouse is very simple. The implementation can be many, depending on the instruments used in programming, but if we talk about a common QGraphicsScene , then let me show you my version of the implementation of such a mechanism.

Project structure

In this project, in addition to the main file used two additional classes. First - this is a QGraphicsScene customized, which will make tracking the cursor position and transmit information about its position, and the second - is the protagonist of our favorite red triangle, which we will manage with the keys W, A, S, D.

Project structure:

  • TargetMotion.pro - Project Profile;
  • widget.h - Header file the main application window;
  • widget.cpp - source files of the main application window;
  • triangle.h - The header of the protagonist of the Red Triangle;
  • triangle.cpp - source files of the protagonist of the Red Triangle;
  • customscene.h - Header file customized graphic scene;
  • customscene.cpp - File source customized graphic scene;
  • cursor.qrc - resource file, which contains a customized mouse pointer.

QtHow to make game using Qt - Lesson 5. Adding sound QMediaPlayer

Getting to the fifth and final lesson on writing games on Qt, which will add sound using QMediaPlayer class. I propose to add the three types of sounds: Eating apples, buzzing flies that will be buzzing when moving and heartbreaking cry when Fly will eaten.

The sound in the project structure

The sound in the project structure is placed in a resource file, as shown in the screenshot of the project structure.

To do this, create a resource directory and folder game . Further, in the resources folder, create a resource file, which prescribes the prefix "/" and add audio files that are pre-placed in the game folder.

And then the sound must be connected to the project using QMediaPlayer class.

In this case, the sounds are connected in three places of the code.

QtHow to make game using Qt - Lesson 4. Enemy – meaning in the survival

Continuing the theme of how to write a play on Qt. Once in past articles a fly was created, that eating apples, it is time to create her enemy. And the enemy flies, as is well known, the spider. Creation of game characters, who will participate in the immediate life of your main character - is not only rendering the animation action and movement, as well as the reactions of the logic on the impact of the player, but also artificial intelligence, in accordance with the logic of which will be determined by the behavior of the game character. Thus, we add to the game a new meaning, not only to eat as many apples, but to survive at any cost.

We define the behavior of the spider in this game. What should he do? Yes, the most common of all the action - to hunt a fly just chasing it on the playing field.

Also add to the game button to start the game process, and pause, and the most important thing to add - it's Game Over.

The enemy flies in the project structure

As is the case with the fly in the structure of the project added an additional class, which will be responsible for the object, which is a spider.

  • spider.h - header file of spider
  • spider.cpp - source file of spider

QtHow to make game using Qt - Lesson 3. Interaction with other objects

Once the game has animated fly, which moves under the influence of the keyboard shortcuts, it is time to add meaning to the game. Add goal Fly, for example, it will eat the apples that will be counted. That is, you must configure the Flies interaction with other objects, in this case apples.

For apples, you must create a separate class, which will also be otnasledovan from QGraphicsItem . When creating the apples must have a random color, which will also be initialized in the class constructor using apples qrand() function.

When fly stumbles upon an apple, it is necessary to transmit the information to the core of the game, which is in the widget class. To do this, we analyze the motion flies objects that fall within its field of view, and which is necessary to work out the interaction.

    /* Checks for whether the fly bumped into any graphic element on the stage. 
     * To do this, we define a small area in front of the fly, which will search for items
     * */
    QList<QGraphicsItem *> foundItems = scene()->items(QPolygonF()
                                                           << mapToScene(0, 0)
                                                           << mapToScene(-20, -20)
                                                           << mapToScene(20, -20));
    /* Then we check all the elements. One of them will fly itself - do not do anything with it. 
     * And send signal to the game core with another objects
     * */
    foreach (QGraphicsItem *item, foundItems) {
        if (item == this)
            continue;
        emit signalCheckItem(item);
    }
How to become an author?

Contribute to the evolution of the EVILEG community.

Learn how to become a site author.

Learn it
Donate

Good day, Dear Users!!!

I am Evgenii Legotckoi, developer of EVILEG. And it is my hobby project, which helps to learn programming another programmers and developers

If the site helped you, and you want also support the development of the site, than you can donate by following ways

PayPalYandex.Money
Timeweb

Let me recommend you the excellent hosting on which EVILEG is located.

For many years, Timeweb has been proving his stability.

For projects on Django I recommend VDS hosting

View Hosting Timeweb
g
May 29, 2020, 2:32 p.m.
glushchenkoin

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:40points,
  • Rating points-8
AS
May 26, 2020, 11:29 a.m.
Artem Sun-Dun-Chan

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
MN
May 25, 2020, 11:33 a.m.
Mitja Nagibin

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
Last comments
May 29, 2020, 1 p.m.
Evgenij Legotskoj

Django - Tutorial 023. Like Dislike system using GenericForeignKey

Думал так, но похоже что нет. {{ post.votes.likes.user.username }} Это же QuerySet будет, а не отдельный единственный объект {% for vote in post.votes %} {{ vote.user.username …
May 29, 2020, 11:43 a.m.
Vladislav Melenchuk

Django - Tutorial 023. Like Dislike system using GenericForeignKey

А как получить имя пользователя, который поставил лайк? Думал так, но похоже что нет. {{ post.votes.likes.user.username }}
May 29, 2020, 6:30 a.m.
Evgenij Legotskoj

Qt/C++ - Lesson 039. How to paint stroke in QSqlTableModel by value in the column?

У меня работает. Исправлял в проекте, который приложен к статье. А что происходит в вашем коде, с учётом места вызова этого кода, я знать не могу ;) Дебажьте и добавляйте условия, кото…
MA
May 29, 2020, 6:27 a.m.
Mihail A

Qt/C++ - Lesson 039. How to paint stroke in QSqlTableModel by value in the column?

QModelIndexList rowIndexes = ui->tableView->selectionModel()->selectedRows(); model->removeRows(rowIndexes.first().row(), rowIndexes.size()); model-&…
Now discuss on the forum
RS
May 30, 2020, 3:41 p.m.
Ruslan Skljuev

Минимальный размер Item

Здравствуйте, новичек в QML. Раньше у меня было несколько окон и минимальный размер считал так: minimumHeight: loGeneral.implicitHeight + 2 loGeneral.anchors.margins minimumWid…
S
RS
May 30, 2020, 11:49 a.m.
Ruslan Skljuev

QML Как при нажатии на кнопку изменить название окна?

У меня три файла: 1. QML - это Loader и Window 2. QML вход в программу - страница Loader (там есть Switch) 3. QML - MainMenu. Мне нужно в (1), чтобы если на (2) Switch.enable, то …
DK
May 29, 2020, 1:27 p.m.
Dzhon Kofi

QMap<> какой ключ лучше

это ясно. Вопрос в том, как быстро мапа будет отрабатывать, если ключом будет QModelIndex. Какой параметр индекса возьмет за ключ. И вот насколько это будет медленнее или быстрее, чем QString пр…
DK
May 29, 2020, 11:10 a.m.
Dzhon Kofi

QModelIndex становится не действительным, но валидный

Привет. Есть проблема с индексом и для меня это чистая магия: Сначала, что делаю: на вьюхе есть редактируемые ячейки. Пользователь редактирует одну, потом внезапно решает не сохраниться и ш…
About
Services
© EVILEG 2015-2020
Recommend hosting TIMEWEB