Jump to: navigation, search

Information about the group Translation of the wiki page Development/Tutorials/Qt4 Ruby Tutorial/Chapter 13.
Development/Tutorials/Qt4 Ruby Tutorial/Chapter 13Current message text
...Chapter 13/Page display title/ruРазработка/Руководства/Введение в Qt4 на Ruby/Глава 13
...t4 Ruby Tutorial/Chapter 13/1/ruTutorialBrowser/ru
...t4 Ruby Tutorial/Chapter 13/2/ruВведение в программирование на Qt<sup>®</sup>4 на языке Ruby
...t4 Ruby Tutorial/Chapter 13/3/ruИгра окончена
...t4 Ruby Tutorial/Chapter 13/4/ruПример 12: Висящий в воздухе
...t4 Ruby Tutorial/Chapter 13/5/ruПример 14: Стена
...t4 Ruby Tutorial/Chapter 13/6/ru== Игра окончена ==
...t4 Ruby Tutorial/Chapter 13/7/ru[[Image:Qt4_Ruby_Tutorial_Screenshot_13.png|center]]
...t4 Ruby Tutorial/Chapter 13/8/ruФайлы:
...t4 Ruby Tutorial/Chapter 13/9/ru=== Обзор ===
...4 Ruby Tutorial/Chapter 13/10/ruIn this example we start to approach a real playable game with a score.
...4 Ruby Tutorial/Chapter 13/11/ruWe give '''<tt>MyWidget</tt>''' a new name ('''<tt>GameBoard</tt>'''), add some slots, and move it to '''<tt>gamebrd.rb</tt>'''
...4 Ruby Tutorial/Chapter 13/12/ruThe '''<tt>CannonField</tt>''' now has a game over state.
...4 Ruby Tutorial/Chapter 13/13/ruThe layout problems in '''<tt>LCDRange</tt>''' are fixed.
...4 Ruby Tutorial/Chapter 13/14/ru=== Построчный обзор программы ===
...4 Ruby Tutorial/Chapter 13/15/ruWe set the size policy of the [ Qt::Label] to ([ Qt::SizePolicy::Preferred], [ Qt::SizePolicy::Fixed]). The vertical component ensures that the label won't stretch or shrink vertically; it will stay at its optimal size (its [ QWidget::sizeHint()]). This solves the layout problems observed in Chapter 12.
...4 Ruby Tutorial/Chapter 13/16/ruThe '''<tt>CannonField</tt>''' now has a game over state and a few new functions.
...4 Ruby Tutorial/Chapter 13/17/ruThis new signal indicates that the '''<tt>CannonField</tt>''' is in a state where the '''<tt>shoot()</tt>''' slot makes sense. We'll use it below to enable or disable the <strong>Shoot</strong> button.
...4 Ruby Tutorial/Chapter 13/18/ruThis variable contains the game state; '''<tt>true</tt>''' means that the game is over, and '''<tt>false</tt>''' means that a game is going on. Initially, the game is not over (luckily for the player :-).
...4 Ruby Tutorial/Chapter 13/19/ruWe added a new '''<tt>isShooting()</tt>''' function, so '''<tt>shoot()</tt>''' uses it instead of testing directly. Also, shoot tells the world that the '''<tt>CannonField</tt>''' cannot shoot now.
...4 Ruby Tutorial/Chapter 13/20/ruThis slot ends the game. It must be called from outside '''<tt>CannonField</tt>''', because this widget does not know when to end the game. This is an important design principle in component programming. We choose to make the component as flexible as possible to make it usable with different rules (for example, a multi-player version of this in which the first player to hit ten times wins could use the '''<tt>CannonField</tt>''' unchanged).
...4 Ruby Tutorial/Chapter 13/21/ruIf the game has already been ended we return immediately. If a game is going on we stop the shot, set the game over flag, and repaint the entire widget.
...4 Ruby Tutorial/Chapter 13/22/ruThis slot starts a new game. If a shot is in the air, we stop shooting. We then reset the '''<tt>gameEnded</tt>''' variable and repaint the widget.
...4 Ruby Tutorial/Chapter 13/23/ru'''<tt>moveShot()</tt>''' too emits the new '''<tt>canShoot(true)</tt>''' signal at the same time as either '''<tt>hit()</tt>''' or '''<tt>miss()</tt>'''.
...4 Ruby Tutorial/Chapter 13/24/ruModifications in CannonField::paintEvent():
...4 Ruby Tutorial/Chapter 13/25/ruThe paint event has been enhanced to display the text "Game Over" if the game is over, i.e., '''<tt>gameEnded</tt>''' is '''<tt>true</tt>'''. We don't bother to check the update rectangle here because speed is not critical when the game is over.
...4 Ruby Tutorial/Chapter 13/26/ruTo draw the text we first set a black pen; the pen color is used when drawing text. Next we choose a 48 point bold font from the Courier family. Finally we draw the text centered in the widget's rectangle. Unfortunately, on some systems (especially X servers with Unicode fonts) it can take a while to load such a large font. Because Qt caches fonts, you will notice this only the first time the font is used.
...4 Ruby Tutorial/Chapter 13/27/ruWe draw the shot only when shooting and the target only when playing (that is, when the game is not ended).
...4 Ruby Tutorial/Chapter 13/28/ruThis file is new. It contains the '''<tt>GameBoard</tt>''' class, which was last seen as '''<tt>MyWidget</tt>'''.
...4 Ruby Tutorial/Chapter 13/29/ruWe have now added four slots.
...4 Ruby Tutorial/Chapter 13/30/ruWe have also made some changes in the '''<tt>GameBoard</tt>''' constructor.
...4 Ruby Tutorial/Chapter 13/31/ru'''<tt>@cannonField</tt>''' is now a member variable, so we carefully change the constructor to use it.
...4 Ruby Tutorial/Chapter 13/32/ruThis time we want to do something when the shot has hit or missed the target. Thus we connect the '''<tt>hit()</tt>''' and '''<tt>missed()</tt>''' signals of the '''<tt>CannonField</tt>''' to two protected slots with the same names in this class.
...4 Ruby Tutorial/Chapter 13/33/ruPreviously we connected the <strong>Shoot</strong> button's '''<tt>clicked()</tt>''' signal directly to the '''<tt>CannonField</tt>''''s '''<tt>shoot()</tt>''' slot. This time we want to keep track of the number of shots fired, so we connect it to a slot in this class instead.
...4 Ruby Tutorial/Chapter 13/34/ruNotice how easy it is to change the behavior of a program when you are working with self-contained components.
...4 Ruby Tutorial/Chapter 13/35/ruWe also use the '''<tt>CannonField</tt>''''s '''<tt>canShoot()</tt>''' signal to enable or disable the <strong>Shoot</strong> button appropriately.
...4 Ruby Tutorial/Chapter 13/36/ruWe create, set up, and connect the <strong>New Game</strong> button as we have done with the other buttons. Clicking this button will activate the '''<tt>newGame()</tt>''' slot in this widget.
...4 Ruby Tutorial/Chapter 13/37/ruWe create four new widgets, to display the number of hits and shots left.
...4 Ruby Tutorial/Chapter 13/38/ruThe top-right cell of the [ Qt::GridLayout] is starting to get crowded. We put a stretch just to the left of the <strong>New Game</strong> button to ensure that this button will always appear on the right side of the window.
...4 Ruby Tutorial/Chapter 13/39/ruWe're all done constructing the '''<tt>GameBoard</tt>''', so we start it all using '''<tt>newGame()</tt>'''. Although '''<tt>newGame()</tt>''' is a slot, it can also be used as an ordinary function.
...4 Ruby Tutorial/Chapter 13/40/ruThis function fires a shot. If the game is over or if there is a shot in the air, we return immediately. We decrement the number of shots left and tell the cannon to shoot.
...4 Ruby Tutorial/Chapter 13/41/ruThis slot is activated when a shot has hit the target. We increment the number of hits. If there are no shots left, the game is over. Otherwise, we make the '''<tt>CannonField</tt>''' generate a new target.
...4 Ruby Tutorial/Chapter 13/42/ruThis slot is activated when a shot has missed the target. If there are no shots left, the game is over.
...4 Ruby Tutorial/Chapter 13/43/ruThis slot is activated when the user clicks the <strong>New Game</strong> button. It is also called from the constructor. First it sets the number of shots to 15. Note that this is the only place in the program where we set the number of shots. Change it to whatever you like to change the game rules. Next we reset the number of hits, restart the game, and generate a new target.
...4 Ruby Tutorial/Chapter 13/44/ruThis file has just been on a diet. '''<tt>MyWidget</tt>''' is gone, and the only thing left is the '''<tt>main()</tt>''' function, unchanged except for the name change.
...4 Ruby Tutorial/Chapter 13/45/ru=== Запуск приложения ===
...4 Ruby Tutorial/Chapter 13/46/ruThe cannon can shoot at a target; a new target is automatically created when one has been hit.
...4 Ruby Tutorial/Chapter 13/47/ruHits and shots left are displayed and the program keeps track of them. The game can end, and there's a button to start a new game.
...4 Ruby Tutorial/Chapter 13/48/ru=== Упражнения ===
...4 Ruby Tutorial/Chapter 13/49/ruAdd a random wind factor and show it to the user.
...4 Ruby Tutorial/Chapter 13/50/ruMake some splatter effects when the shot hits the target.
...4 Ruby Tutorial/Chapter 13/51/ruImplement multiple targets.
...4 Ruby Tutorial/Chapter 13/52/ru[[Category:Ruby/ru]]
NavigationShowing 53 messages.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal