| Development/Tutorials/Qt4 Ruby Tutorial/Chapter 14 | Current message text |
| ↓...Chapter 14/Page display title/ru | Разработка/Руководства/Введение в Qt4 на Ruby/Глава 14 |
| ↓...t4 Ruby Tutorial/Chapter 14/1/ru | TutorialBrowser/ru |
| ↓...t4 Ruby Tutorial/Chapter 14/2/ru | Введение в программирование на Qt<sup>®</sup>4 на языке Ruby |
| ↓...t4 Ruby Tutorial/Chapter 14/3/ru | Стена |
| ↓...t4 Ruby Tutorial/Chapter 14/4/ru | Пример 13: Игра окончена |
| ↓...t4 Ruby Tutorial/Chapter 14/5/ru | == Стена == |
| ↓...t4 Ruby Tutorial/Chapter 14/6/ru | [[Image:Qt4_Ruby_Tutorial_Screenshot_14.png|center]] |
| ↓...t4 Ruby Tutorial/Chapter 14/7/ru | Файлы: |
| ↓...t4 Ruby Tutorial/Chapter 14/8/ru | === Обзор === |
| ↓...t4 Ruby Tutorial/Chapter 14/9/ru | This is the final example: a complete game. |
| ↓...4 Ruby Tutorial/Chapter 14/10/ru | We add keyboard accelerators and introduce mouse events to '''<tt>CannonField</tt>'''. We put a frame around the '''<tt>CannonField</tt>''' and add a barrier (wall) to make the game more challenging. |
| ↓...4 Ruby Tutorial/Chapter 14/11/ru | === Построчный обзор программы === |
| ↓...4 Ruby Tutorial/Chapter 14/12/ru | The '''<tt>CannonField</tt>''' can now receive mouse events to make the user aim the barrel by clicking on it and dragging. '''<tt>CannonField</tt>''' also has a barrier wall. |
| ↓...4 Ruby Tutorial/Chapter 14/13/ru | This line has been added to the constructor. Initially, the mouse is not pressed on the barrel. |
| ↓...4 Ruby Tutorial/Chapter 14/14/ru | Now that we have a barrier, there are three ways to miss. We test for the third, too. (In '''<tt>moveShot()</tt>'''.) |
| ↓...4 Ruby Tutorial/Chapter 14/15/ru | This is a Qt event handler. It is called when the user presses a mouse button when the mouse cursor is over the widget. |
| ↓...4 Ruby Tutorial/Chapter 14/16/ru | If the event was not generated by the left mouse button, we return immediately. Otherwise, we check if the position of the mouse cursor is within the cannon's barrel. If it is, we set '''<tt>barrelPressed</tt>''' to true. |
| ↓...4 Ruby Tutorial/Chapter 14/17/ru | Notice that the [http://doc.qt.nokia.com/latest/qmouseevent.html#pos Qt::MouseEvent::pos()] function returns a point in the widget's coordinate system. |
| ↓...4 Ruby Tutorial/Chapter 14/18/ru | This is another Qt event handler. It is called when the user already has pressed the mouse button inside this widget and then moves/drags the mouse. (You can make Qt send mouse move events even when no buttons are pressed. See [http://doc.qt.nokia.com/latest/qwidget.html#mouseTracking-prop Qt::Widget::setMouseTracking()].) |
| ↓...4 Ruby Tutorial/Chapter 14/19/ru | This handler repositions the cannon's barrel according to the position of the mouse cursor. |
| ↓...4 Ruby Tutorial/Chapter 14/20/ru | First, if the barrel is not pressed, we return. Next, we fetch the mouse cursor's position. If the mouse cursor is to the left or below the widget, we adjust the point to be inside the widget. |
| ↓...4 Ruby Tutorial/Chapter 14/21/ru | Then we calculate the angle between the bottom edge of the widget and the imaginary line between the bottom-left corner of the widget and the cursor position. Finally we set the cannon's angle to the new value converted to degrees. |
| ↓...4 Ruby Tutorial/Chapter 14/22/ru | Remember that '''<tt>setAngle()</tt>''' redraws the cannon. |
| ↓...4 Ruby Tutorial/Chapter 14/23/ru | This Qt event handler is called whenever the user releases a mouse button and it was pressed inside this widget. |
| ↓...4 Ruby Tutorial/Chapter 14/24/ru | If the left button is released, we can be sure that the barrel is no longer pressed. |
| ↓...4 Ruby Tutorial/Chapter 14/25/ru | The paint event has one extra line: |
| ↓...4 Ruby Tutorial/Chapter 14/26/ru | '''<tt>paintBarrier()</tt>''' does the same sort of thing as '''<tt>paintShot()</tt>''', '''<tt>paintTarget()</tt>''', and '''<tt>paintCannon()</tt>'''. |
| ↓...4 Ruby Tutorial/Chapter 14/27/ru | This function paints the barrier as a rectangle filled with yellow and with a black outline. |
| ↓...4 Ruby Tutorial/Chapter 14/28/ru | This function returns the rectangle of the barrier. We fix the bottom edge of the barrier to the bottom edge of the widget. |
| ↓...4 Ruby Tutorial/Chapter 14/29/ru | This function returns '''<tt>true</tt>''' if the point is in the barrel; otherwise it returns '''<tt>false</tt>'''. |
| ↓...4 Ruby Tutorial/Chapter 14/30/ru | Here we use the class [http://doc.qt.nokia.com/latest/qmatrix.html Qt::Matrix]. [http://doc.qt.nokia.com/latest/qmatrix.html Qt::Matrix] defines a coordinate system mapping. It can perform the same transformations as the [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter]. |
| ↓...4 Ruby Tutorial/Chapter 14/31/ru | Here we perform the same transformation steps as we do when drawing the barrel in the '''<tt>paintCannon()</tt>''' function. First we translate the coordinate system and then we rotate it. |
| ↓...4 Ruby Tutorial/Chapter 14/32/ru | Now we need to check whether the point '''<tt>pos</tt>''' (in widget coordinates) lies inside the barrel. To do this, we invert the transformation matrix. The inverted matrix performs the inverse transformation that we used when drawing the barrel. We map the point '''<tt>pos</tt>''' using the inverted matrix and return '''<tt>true</tt>''' if it is inside the original barrel rectangle. |
| ↓...4 Ruby Tutorial/Chapter 14/33/ru | We create and set up a [http://doc.qt.nokia.com/latest/qframe.html Qt::Frame], and set its frame style. This results in a 3D frame around the '''<tt>CannonField</tt>'''. |
| ↓...4 Ruby Tutorial/Chapter 14/34/ru | Here we create and set up three [http://doc.qt.nokia.com/latest/qshortcut.html Qt::Shortcut] objects. These objects intercept keyboard events to a widget and call slots if certain keys are pressed. Note that a [http://doc.qt.nokia.com/latest/qshortcut.html Qt::Shortcut] object is a child of a widget and will be destroyed when that widget is destroyed. [http://doc.qt.nokia.com/latest/qshortcut.html Qt::Shortcut] itself is not a widget and has no visible effect on its parent. |
| ↓...4 Ruby Tutorial/Chapter 14/35/ru | We define three shortcut keys. We want the '''<tt>fire()</tt>''' slot to be called when the user presses Enter or Return. We also want the application to quit when key Ctrl+Q is pressed. Instead of connecting to [http://doc.qt.nokia.com/latest/qcoreapplication.html#quit Qt::CoreApplication::quit()], we connect to [http://doc.qt.nokia.com/latest/qwidget.html#close Qt::Widget::close()] this time. Since the '''<tt>GameBoard</tt>''' is the application's main widget, this has the same effect as [http://doc.qt.nokia.com/latest/qcoreapplication.html#quit QCoreApplication::quit()]. |
| ↓...4 Ruby Tutorial/Chapter 14/36/ru | Qt::CTRL, Qt::Key_Enter, Qt::Key_Return, and Qt::Key_Q are all constants declared in the Qt namespace. Unfortunately, in the current version of qtruby, they need to be converted to integers before we can use them in our shortcuts. |
| ↓...4 Ruby Tutorial/Chapter 14/37/ru | We give '''<tt>cannonBox</tt>''' its own [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout], and we add '''<tt>CannonField</tt>''' to that layout. This implicitly makes '''<tt>CannonField</tt>''' a child of '''<tt>cannonBox</tt>'''. Because nothing else is in the box, the effect is that the [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] will put a frame around the '''<tt>CannonField</tt>'''. We put '''<tt>cannonBox</tt>''', not '''<tt>CannonField</tt>''', in the grid layout. |
| ↓...4 Ruby Tutorial/Chapter 14/38/ru | === Запуск приложения === |
| ↓...4 Ruby Tutorial/Chapter 14/39/ru | The cannon now shoots when you press Enter. You can also position the cannon's angle using the mouse. The barrier makes it a little more challenging to play the game. We also have a nice looking frame around the '''<tt>CannonField</tt>'''. |
| ↓...4 Ruby Tutorial/Chapter 14/40/ru | === Упражнения === |
| ↓...4 Ruby Tutorial/Chapter 14/41/ru | Write a space invaders game. |
| ↓...4 Ruby Tutorial/Chapter 14/42/ru | The new exercise is: Write a Breakout game. |
| ↓...4 Ruby Tutorial/Chapter 14/43/ru | Final exhortation: Go forth now and create masterpieces of the programming art! |
| ↓...4 Ruby Tutorial/Chapter 14/44/ru | [[Category:Ruby/ru]] |