📜 ⬆️ ⬇️

Analog of the game Wack-a-mole on LabVIEW

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

The snake is already there, so why not be moles? The essence of the game (which originally existed in the form of mechanical gaming machines) is simple - on the playing field there are “holes” of 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 has managed to do this, then his account (S) is increased 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 there is no "mole".
When the time interval t expires, the next randomly selected object is randomly highlighted. If the user account reaches 20, the value of t is reduced 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 beginning of the program.

Game interface

The game interface consists of, in fact, pictures (2D-picture), one button and several indicators for displaying the score, a timeout (the time after which the mole changes its location) in milliseconds and the time since the start of the game in seconds. I also used a couple of frames for visual separation of the playing field and indicators. In the role of "mole" is used a picture with an evil smiley, 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 upload an image in 2D-picture, you must first read it bycnhevtynjv Read JPEG File, and then use Draw Flattened Pixmap, at the output of which you will get an array of pixels to load 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 bring the mole to one indicator, and to all the others to submit an “empty picture” from the constant.

To handle the clicks on the pictures, Event Structure is used, in which the number of the “hole” obtained from the random number generator is compared with a constant and then the decision is made to change the account. Also, this structure handles and 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 the timeout change depending on the account to the block diagram. Since the condition does not indicate exactly how to do this, I decided that the movement speed of the mole would vary from 1000 to 200 milliseconds (in 200 millisecond increments) every 20 points of the score. To make it easier to play, the speed decreases as the score decreases, so I also realized this opportunity through Case Structure. The values ​​of the game score and the current timeout are transferred between iterations of the cycle via Shift Registers.

Finally, to count the time elapsed since the beginning of the game, I used 2 Tick Count tools: the first reads the system timer at the moment the program starts (before starting the cycle), and the second at each iteration. Next, 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 such a task as a game you can afford a small margin of error.

Results

Of course, LabVIEW is not the best tool for developing gaming applications, but it is quite possible to use it for such small toys.
Game source

Source: https://habr.com/ru/post/173113/


All Articles