Analogue of the game Wack-a-mole on LabVIEW

There are quite a few articles on LabVIEW on the Habré (most likely in view of the use of this environment). Not so long ago I was asked to make a simplified analogue of the “wack-a-mole” toy on LabVIEW and I decided to share my work on the hub I will use LabVIEW version 7.1 - old, but simple. But the

snake is already here, so why not be the mole? The essence of the game (which originally existed in the form of mechanical slot machines) is simple - there are "holes" on the playing field, one of which for some time appears the head of the "mole", on which the player must have time to hit with a special hammer.

Game requirements

Form a playing field consisting of 4 2D-picture objects of the same color. After pressing the “Start” button, one of these objects, selected at random, is highlighted and remains highlighted for some time t. During this time, the user has the opportunity to click on it with the mouse. If the user managed to do this, then his score (S) increases by 2, if not, then decreases by 1 (at the beginning of the game S = 10). Also, the score decreases by 1 when you click on the "hole", in which at the moment the "mole" is missing.
When the time interval t expires, the next randomly selected object is randomly highlighted. If the user account reaches 20, the value of t decreases by 20%. The game continues until the score S reaches zero.
During program execution, the front panel displays the current values ​​of S, t and the time elapsed since the program started.

Game interface

The game interface consists of, in fact, pictures (2D-picture), one button and several indicators for displaying the score, timeout (time after which the mole will change its location) in milliseconds and the countdown from the start of the game in seconds. I also used a couple of frames to visually separate the playing field and indicators. In the role of "mole" is used a picture with an evil emoticon, which is loaded from a file.

Game block diagram

At the first stage of development, we will create the “mole” itself and teach it to move between the “holes”.

To load an image into a 2D-picture, you must first read it bycnhevtynjv Read JPEG File, and then use the Draw Flattened Pixmap, at the output of which we will already get an array of pixels for loading into a 2D-picture. In principle, you can draw a mole and programmatically - using graphic primitives (lines, circles, rectangles, etc.). The Case structure serves to display the mole on one indicator, and on all the others to submit a “blank picture” from the constant.

To process clicks on the pictures, Event Structure is used, in which the number of the "hole" received from the random number generator is compared with a constant and then a decision is made to change the score. This structure also processes the timeout (the user did not have time to click on the picture). At this stage, the timeout value is assumed to be 1000 milliseconds and entered into a constant.

At the second stage, we add a change in the timeout value to the block diagram depending on the count. Since the condition does not indicate exactly how to do this, I decided that the speed of the mole would change from 1000 to 200 milliseconds (in 200 millisecond increments) every 20 points of the score. To make it easier to play, speed decreases as the score decreases, so I also implemented this opportunity through Case Structure. The values ​​of the game score and the current timeout are transferred between iterations of the cycle through the Shift Registers.

Finally, to count the time elapsed since the start of the game, I used 2 Tick Count tools: the first reads the system timer at the time the program starts (before starting the cycle), and the second - at each iteration. Next, we look for the difference in readings and divide it by 1000 to get the amount of time expressed in seconds. This is not a very accurate option, but for a task like a game, you can afford a small margin of error.


Of course, LabVIEW is not the best tool for developing gaming applications, but it can be used for such small toys.
Game source

Also popular now: