The hamster-beating game written by C and easyx libraries

956 Views
No Comments

A total of 1899 characters, expected to take 5 minutes to complete reading.

Download Address

Open Source Address:GitHub-mcdudu233/Whack-a-Mole: Gophers game based on easyx library

Download address:https://github.com/mcdudu233/Whack-a-Mole/releases/tag/exe

Principle

Hit the gopher game is a classic reaction force game. The game interface consists of multiple holes. Gophers will randomly emerge from different holes. Players need to click as many gophers as possible within a limited time to gain points. The game evaluates the player's performance through a system of timing and points.

Design

Interface

1. Game main interface:

Displays the game title and three buttons: Start Game, Setup, and Exit Game.

2. Game setting interface:

Volume adjustment: the volume can be adjusted through the slide bar;
Difficulty selection: you can choose simple, ordinary and difficult three kinds of difficulty;
Resolution Selection: You can select different screen resolutions.
Also need a back to main interface button.

3. Game interface:

It is required to display the current level, remaining time and current score.
Gophers appear randomly, and players need to click on the gophers to score points.
Provide back button, click to return to the main interface.

System Architecture

The project adopts modular design and is mainly divided into the following modules:

  1. Main module: program entry, responsible for initializing resources and starting the main interface.
  2. Window module: responsible for the rendering of each interface and user interaction processing.
  3. Game module: responsible for the realization of the game logic, including gopher generation, click detection, score calculation, etc.
  4. Resource module: responsible for loading and managing resources such as pictures and sound effects.
  5. Sound effects module: responsible for the playback and management of sound effects.

code implementation

Main module

The main module is responsible for the initialization and startup of the program.

  • Initialize random seed
  • Load picture and sound assets
  • Initialize the graphical interface
  • Start main interface

The hamster-beating game written by C and easyx libraries

Window Module

Window module is responsible for the rendering of each interface and user interaction processing.

  • Main interface drawing and click event processing
  • Set interface drawing and click event handling
  • Game interface drawing and click event processing

The hamster-beating game written by C and easyx libraries

Game Module

The game module is responsible for the implementation of the game logic.

  • The Generation of Gopher Holes and Gophers
  • The random appearance of gophers
  • Detection of user click gopher
  • Game time and score updates
  • Management of levels

The hamster-beating game written by C and easyx libraries

Resource Module

The resource module is responsible for loading and managing resources such as pictures and sound effects.

  • Loading and obtaining image resources
  • Loading and playback control of sound resources

The hamster-beating game written by C and easyx libraries

Sound Effect Module

The sound effect module is responsible for playing and managing sound effects.

  • Playback of sound effects
  • Stop of sound effects
  • Adjustment of volume

The hamster-beating game written by C and easyx libraries

Key Algorithm Analysis

gopher generation algorithm

// 随机生成地鼠
void game::spawnMoles() {
    MOUSEMSG m;
    int x, y;
    while (!destroyed) {for (auto &row: holes) {for (auto &hole: row) {if (rand() < (int) (getDifficultyFactor() * 100.0F)) {debug("show mole");
                    hole.mole.show();}
            }
        }
        delay(50);
    }
}

This code can use random numbers to determine whether the gopher hole should generate a gopher based on the difficulty factor of the game.

hit detection algorithm

void game::hitListener() {
    MOUSEMSG m;
    int x, y;
    while (!destroyed) {m = mouseMessage();
        x = m.x;
        y = m.y;
        for (auto &row: holes) {for (auto &hole: row) {if (m.mkLButton && hole.mole.isHited(x, y)) {debug("mole hited, now score:" + std::to_string(this->score));
                    // 每击中一个地鼠得 5 分
                    score += 5;
                }
            }
        }
        delay(10);
    }
}

The piece of code can detect whether it is hit according to the entity box of the gopher.

hammer tracking algorithm

// 监听锤子位置
void game::hammerListener() {
    IMAGE *tmp;
    MOUSEMSG m;
    int x, y, last_x, last_y;
    while (!destroyed) {m = mouseMessage();
        x = m.x - 50;
        y = m.y - 50;

        // 还原之前的状态
        if (tmp == nullptr) {tmp = new IMAGE;} else {putimage(last_x, last_y, tmp);
        }
        // 保存之前的状态
        getimage(tmp, x, y, 100, 100);
        last_x = x;
        last_y = y;
        // 放置锤子
        if (m.mkLButton) {debug("hammer down.");
            putImage(x, y, IMG_HAMMER_DOWN);
        } else {putImage(x, y, IMG_HAMMER);
        }

        delay(10);
    }
}

This section of the code can track the position of the mouse and constantly refresh the hammer, while detecting whether the left mouse button is pressed to set the hammer's motion state.

Run Results

(1) after starting the program, display the main interface, including start the game, settings and exit the game button and so on. There is also a background map.

The hamster-beating game written by C and easyx libraries

(2) Click the setting button to enter the setting interface, you can adjust the volume, select the difficulty and resolution, and return to the main interface.

The hamster-beating game written by C and easyx libraries

(3) Click the start game button, enter the game interface, display the current level, the remaining time and the current score, the gopher randomly appear, click the gopher score.

The hamster-beating game written by C and easyx libraries

(4) Click the back button, you can return to the main interface from the game interface.

END
 0
Comment(No Comments)
验证码
en_USEnglish