Greetings to all.
Previous articles were about small projects based on the same principle:
- the proposed idea has been thought out for some time;
- the overall task was divided into several large blocks;
- each block was thought out about what input data it would have, what output and how it should process this data;
- then, when all the problems in the mind were resolved, the program itself was quickly put on;
- minor errors were corrected during debugging and minor corrections were made;
- At the end of the graphics was screwed and everything is ready.
But at the same time in the final program of the decisions was not obvious. In addition, having before my eyes a scheme of a couple of dozen blocks, it is difficult to immediately figure out what, how and for what is used. Therefore, in this example, a slightly different approach will be applied. I will try to simply and clearly show how to make the simplest program step by step, while spending a minimum of time and effort. And at the same time I will comment on each step in detail.
')
As an example, the game
"Electronics IM-04 - Merry Cook" was chosen.

Under the cut description of the steps, how to write this toy in the programming language FBD.
*
All large pictures under the spoiler have a link to the originals.Formulation of the problem
So, today we are making the game "Merry Cook."
The essence of the game: the chef throws up four
items . The task is to run up to the falling object in time, to substitute the pan and throw it up again. For every successful toss, one point is awarded. The speed of flight of objects increases with the number of points gained. There is also a cat that periodically catches a sausage with a fork, thereby interfering with the cook. If the object falls on the floor, then the mouse grabs it and a penalty point is awarded. After three items dropped to the floor, the game ends.
Here is such a simple toy. Now we will implement it.
Implementation
Step 1. Clean sheetWe create a controller and in it a new task for the implementation of our game.
Step 2. The cook running across the fieldNow we need a cook who will run around the field according to the commands from the operator station. To implement this, we make the simplest scheme:
- the selector will give "command 1" (to move right) or "command 2" (to move left);
- then we put two blocks “Choice” and in the presence of one at the corresponding output of the selector we transfer to the output of choice “1” for moving right and “-1” for moving left;
- let's sum up the outputs from the “Choice” algorithms on the “Addition” algorithm, which we supplement with feedback to obtain the integral value of the commands;
- we will get feedback through the selection algorithm in advance, so that we can later reset our adder;
- we collect the first and second outputs of the selector according to the “OR” scheme and feed back to the reset of the selector. Thus, we get the duration of the command in one controller cycle.
Step 3. Sausages and fishNow that we have a cook running around the field, it's time to make food that he will juggle with. Here, too, everything is standard:
- we take the "Integrator" and set the upper and lower thresholds on it;
- We connect the usual RS trigger to the thresholds, after reaching the upper threshold, we reset this trigger and, upon reaching the lower one, reset it;
- To the output of the RS-flip-flop we connect the “Choice” algorithm, at which we will choose the integrator increment speed. We set the “IfYes” input to a negative value (the upper threshold is reached and the integrator should go down) and to the “IfNo” input is positive;
- we get feedback from the “Select” algorithm on the “Integrator”;
- We connect the discrete-to-whole conversion algorithm to the integrator output, thereby discarding the fractional part;
- we copy this scheme three more times (since we have four pieces).
Step 4. Attaching the GUIOnce the cook is already running around the field and the food is jumping, it's time to screw the simplest graphic part and see how it will look. We do not need beauty to debug, so we do everything as quickly and simply as possible:
SubtotalThe basis of the program is ready. All spent about five minutes. But the current implementation only looks like the desired result. In fact, it is impossible to play it, because the objects jump by themselves and never fall, and the cook can go for a walk outside the field. But this is only the basis. We start putting additional options on our skeleton of the program, bringing it closer to the original.
Step 5. We restrict the cookAs it is easy to see, at the moment the cook is controlled by the teams right and left by one and can go where he pleases. the output is a simple adder, not limited to anything. But according to the rules of the game, the cook can occupy only one of four positions and cannot leave the screen. To implement this, add a chef frame. Make it extremely simple and you can go two ways:
- when the chef reaches the extreme right or left coordinate (“4” and “1”, respectively), block the possibility of pressing the “right” button or the “left” button;
- when the chef reaches the extreme right or left coordinate ("4" and "1", respectively) to prohibit the increment or decrease of the adder, on which we store the current coordinate of the chef.
If we take the original toy as a basis, the second option looks preferable, since no one could forbid the player to press the buttons in the toy. Just the cook having reached the extreme position no longer responded to the command in the same direction. We implement it. To do this, we put two algorithms for comparing coordinates with the minimum and maximum possible values. And with the “Choice” algorithm, we will lead the team not directly, as it was implemented initially, but through “AND” with a sign of a corresponding comparison.
Thus, it turned out that if the cook is in the third position, then the condition “position less than the fourth” is fulfilled, and the command from the selector will go further to the “Choice” algorithm. If the cook is in the fourth position, then the condition “position is less than the fourth” is no longer fulfilled, and the command from the selector, multiplied by the “AND” algorithm with zero, will not go further.
Step 6. Cook tosses foodNow add our chef the opportunity to throw objects. As can be seen from the basic scheme, the subject itself can occupy six positions. We achieved this when we discarded the fractional part of the integrator output and got integers from zero to five. Now we recall that throwing an object upward in our country is done by dropping the trigger and choosing a positive integrator increment rate. Initially, our trigger was reset when the integrator reached the lower threshold. But now we remove this connection and put in its place the simplest scheme: we add the position of the cook and the sign that the coordinate of the object is equal to one (ie, it is at the lower point). If both conditions are fulfilled, then reset the trigger and begin to increase the integrator.
Step 7. Add animation to the chef.If the cook throws up an object, then he should swing the pan upwards. To do this, on our screen there are two options for drawing the chef at each position:
- option 1 - a cook stands with a lowered frying pan;
- option 2 - the cook stands with a raised pan.
Here is a little trick. It is necessary for the animation to work out, firstly, synchronously with the tossing of the object, and secondly, to take a well-defined and short time.
Let's try to imagine what happens in the program:
- the object falls, i.e. the value of the integrator output decreases with some constant speed;
- further, the value at the integrator's output becomes less than two, and this means that we are in the zone when the cook can throw the object again, substituting a frying pan for it;
- at the same time (remembering the sixth step) when both conditions are fulfilled (the object’s coordinate is equal to one and the cook is in the same position) and the trigger is reset, and the buildup begins at the integrator’s output all at the same constant speed;
- as soon as the value at the integrator output becomes more than two, the object will be drawn in the next coordinate. In this case, the chef must raise the pan to get the visual effect of tossing
- but we can “flip” the object both at the output of the integrator 1.99 (then after a fraction of a second it will be drawn in the next coordinate), and at the output of the integrator 1.01 (then it’s necessary to wait for drawing it in the next coordinate for a long time);
- until the object was drawn in the second coordinate to the player it is not clear whether he threw it or not.
That is the main problem. And to solve it is very simple. When the condition is met (our food is in the first coordinate, i.e., at the very bottom just before falling to the floor) and the cook substituted the frying pan, we need to forcefully substitute the current one for the falling object (whether it is 1.99 or 1.01) to start the integrator increment from it. I suggest taking the value 1.8.
Now we recall that when the value at the integrator output exceeds two, then the object tossed will be redrawn in the new (second) coordinate, and we need to redraw the cook with the raised frying pan. But he does not have to stand with his hand up all the time, but should let it go after a short while. To do this, we put another comparison algorithm and compare the value at the output of the integrator with the value 2.2. Further on "and" we will collect four signs:
- the trigger is reset and the coordinate is incremented;
- the cook is in the same position;
- current integrator output is greater than 2;
- integrator current output is less than 2.2.
If all four conditions are met, then we add a four to the current coordinate of the cook. Thus, using codes 1,2,3 and 4, we will draw cooks with the frying pan lowered at positions 1,2,3 and 4, respectively, and with codes 5,6,7 and 8 - with a raised frying pan at positions 1,2,3 and 4 respectively.
Step 8. Screw the counterEverything is simple here. We already have a sign that the chef tossed the object (processed by the "And" two conditions: the coordinate of the object is equal to one and the coordinate of the cook corresponds to the number of the object). We select the front and get on the adder, which according to the standard scheme we tie feedback through the algorithm "Choice", in order to be able to reset the adder on the condition.
Step 9. “Start” and “Stop” buttonsWithout them in any way. It is necessary for the person to tell the controller: “Now I am ready to play. Launch the game! And then I was able to reset the result and return to the starting position. We implement this task.
In general, we already have one selector (to control the chef) and we could use it, adding another third and fourth teams for starting and resetting, respectively. But in order not to overload the scheme we will make a separate selector controlling the game. In the same way, we will initiate a feedback reset via “OR” from the first and second output. And put on the output of the RS-trigger, which will be cocked by the "Start" command and reset by the "Reset" command. The game is going on until we have the trigger cocked.
At the same time, you need to reset all the counters in the game by the “Reset” command, and set the initial position by the “Start” command. It's simple, because We chose the algorithms “Choice” by feedback on adders in advance, and the triggers are reset by giving the “Reset” input command. At the same time, using the “Start” command, we select the front (a signal with a duration of one cycle), and on this front we add one to the position of the cook (thereby setting it to the extreme left position) and force the initial coordinate value of the food on the integrators.
Step 10. Finalize the graphics.We have new items in the program. We will add them to the graphic part and launch the game.

As you can see from the picture, our cook is already moving only in the interval limited by acceptable positions, raises the pan and throws up the food. All objects also no longer jump on their own, but they fall and no longer appear if the cook did not have time to catch them and throw them into the air again. The counter of the number of successful attempts of a cook to throw another falling object also works. This is already 80% of the total functionality of the game, which has spent about twenty minutes in total. And this game can already be played. It remains to implement a few options:
- pause for the time of the fall and restart the fallen object;
- scoring and issuing a “GameOver” signal when the maximum permissible number of errors is exceeded;
- a cat catching sausage;
- acceleration of the game depending on the number of points scored.
Let's start in order.
Step 11. Pause the game when the subject falls and restart the fallen object.The object is considered to have fallen if its coordinate is zero and at the same time the “Go game” sign comes. If both of these conditions are fulfilled, then select the front and, on this front, fire the trigger “Fall”. At the same time, we give a delay of two seconds, after which we reset the “Fallen” trigger. As long as the trigger is cocked using the additional “Choice” algorithm, we equate to zero the increment rate of all the integrators, thus getting a pause in the game (Note: in the game, pause is only for flying objects. The cook himself can move at this time and take a comfortable position to continue the game. This made specifically to help the player.If you need to simultaneously block the movement of the chef, then the "And" algorithms that we added to prevent the chef from going outside the field, add another entrance, where the inverse sy cash with "Fell" trigger). On the opposite front (when the trigger "dropped" was dropped), at the same time we reset the trigger on the choice of the integrator speed and give the command to force the initial value.
Step 12. We count the fallen objects.We already have a signal on the fallen object (it cocks the trigger "Fallen"). On this signal we increase the counter of the fallen objects by one. We get the feedback in the standard way to the adder through a choice in order to be able to reset the counter (we will do this with the “Reset” command). Next, compare the value on the adder with the three. When the maximum value is reached, set the “GameOver” feature and prohibit resetting the “Fallen” trigger automatically after two seconds. By "OR" we allow resetting this trigger only by the "Reset" command from the control selector.
Step 13. Add a cat catching sausageLet's apply the standard scheme:
- We install an integrator with two arbitrary thresholds;
- When the upper threshold is reached, we set the trigger and reset upon reaching the lower threshold;
- Through a choice, we submit a positive and negative integrator increment rate. Thus, we obtain the value constantly changing according to the “saw” at the integrator output;
- We take the current time algorithm and divide by the integrator output;
- Multiply the resulting value after dividing by 10,000 and divide with the remainder by 1, thereby obtaining the whole and fractional parts separately;
- choose the chance we need that the cat caught the sausage. Suppose it is 30%, i.e. compare the residue with 0.3;
- We add four signs of “AND”: the remainder is less than 0.3, the sausage has moved from 4 to the third position, there is a sign of “The game is going on” and the trigger on selecting the integrator speed of the “sausage” is cocked (the integrator receives a negative value, i.e. sausage falls down);
- according to the signal from the "And" algorithm, cock the "Cat" trigger and start the timer. At the timeout input, we submit the remainder multiplied by ten (that is, a random number of seconds from 0 to 3);
- timeout reset the trigger "Cat";
- the signal from the “Cat” trigger with inversion is sent to the “Select” algorithm, where once again we select the speed applied to the integrator input of the “sausage”. Those. while at the output of the "Cat" trigger is a logical unit, zero is applied to the integrator input and the object hangs in the air;
Step 14. Making the game speed increase depending on the scoreThis is the final touch. In fact, everything is ready with us. The speed is determined on the “Choice” algorithm, where the positive and negative rates of the increment (and decrease, respectively) of the integrator output are specified. By default, we have chosen some numbers. To implement the function of changing speed, we divide the reading of the counter of successful attempts to throw an object by an arbitrary coefficient (for example, 50), and add the resulting value to the specified value initially. Thus, it turns out that the more points we have, the faster the sausages will bounce.
Step 15. Finalize the graphics for the final check.Since we have some more important signals, add them to our picture in a simplified form. It turns out something like this:

Here the game interface is already fully implemented. Only this is done in schematic form. In principle, even this toy can already be played. Of course, a full-fledged game will be implemented, but this interface, sketched in a couple of minutes, does an excellent job with the debugging and checking functions of the program.
Step 16. The final view of the technical program and its gif implementation of the steps
Step 17. Make beautiful game graphics.For this we need a picture from the Internet. Take any beautiful picture with the game "Cheerful Cook." It is cut into pieces, and each object (cook, sausage, mouse and cat) is decorated with a separate picture. Further, these pictures are placed on the field. We already have a graphic layout. On it we debugged the program. We simply replace the circles and squares with the necessary pictures, and instead of the animation “color change” we put the animation “display”. With the cut, edit and image processing had to tinker. I suspect that some Photoshop would have allowed all this to be done in 15 minutes, but everything that was needed was drawn in MS Paint in a couple of hours.
Add additional control buttons, a score counter and the inscription “GameOver” on the image. Everything is ready, you can play.
Step 18. Playing
* the
animation is made at a frequency of 1 frame per second (since the speed of the game at the beginning is rather slow) and played at double speed.Summarizing
In this article, I tried to show the most vividly the process of writing the simplest toy in the programming language FBD. And the toy is ready. The program used a total of 133 algorithms. However, if you carefully follow the program implementation process step by step, you can see that some of the algorithms are not needed. It immediately catches the eye that there are some superfluous selections of the fronts, since, as I said at the very beginning, the control was implemented using a selector with a reset by feedback. Those. the original team already has a duration of one cycle, and it does not make sense to select a front from it.
You can also notice that the main part of the program consists of four identical sequences of algorithms for processing four items. The only exception is “cat intervention” and the algorithm for processing the first sausage. But all the interference is only in one additional “AND” algorithm (which pauses the flight of the sausage when a sign appears that the cat picked it up with a fork). Those. This whole sequence can be minimized to a macro, having a small algorithm with several input and output parameters. This will significantly simplify (visually) the task as well as facilitate the addition of a new functional (since changes will have to be made only in one macro, and not in four identical chains of algorithms).
The above improvements will lead us to a task similar to the previous examples. From the point of view of beauty and optimality, the implementation of the block program would be preferable, but the goal was to show the realization of the task step by step.
There is another interesting point. As a result, we got the game "Merry Cook". There were a whole bunch of such games: “Well, wait a minute!”, “Hunting”, “Fleeing Aliens”, “Secrets of the Ocean”, “Hockey”, “Football”, etc. But all these games are made on the same principle. In fact, only the graphic component changed with minimal program changes. Therefore, in a couple of minutes from our program, you can implement any desired one.
For example, "Well, wait!". Redraw graphics. It's clear. But from the point of view of the algorithm, the change is one: in our country, the object flies first from bottom to top, and then falls back, and in “Well, wait a minute” the movement is only in one direction. Those. it is enough to remove the RS-trigger and the “Choice” algorithm, which are responsible for the rate of growth of the integrator of each item, put only negative speed into place and set the integrator value to the maximum when a new egg appears. That's all. We delete eight algorithms, conduct eight new connections and the game “Well, wait a minute!” Is ready.
That's all. I hope it was interesting.
Ps.
If the topic is interesting, below are links to other articles: