C language graphical greedy snake with multiple functions without installing a third-party library class design (with code)

insert image description here

foreword

Snake class is to imitate and realize the classic game Snake by using C language, so that it has a specified range of snake activities in the window, and completes a series including but not limited to imitating snake movement, direction control, eating food and adding Points, hit the wall and the snake head hit the snake body death and other game functions.

Additional functions: In the case of realizing the above functions, it also adds borderless mode, obstacle mode and realizes different sound effects in different scenarios, real-time update of scores, and the effect of saving and displaying the highest score.

insert image description here

Function

  1. Start interface module : choose to enter different modes: borderless mode, normal mode, obstacle mode, or choose to exit.
  2. Game flow module : Set the snake's moving direction to be upward by default. By default, the snake has not eaten food and is not in danger. Different game flows are controlled by calling different functions.
  3. Print frame and decoration module : solve the flickering problem by setting and hiding the cursor position; through the font color and background color of the console window, in different situations: such as the start interface and the record breaking interface, the game failure interface, and the game progress interface , print different decorations and borders respectively; by importing sound header files, different sound effects can be produced in different situations.
  4. Rule prompt interface module : Indicates the keyboard control keys corresponding to the operation. In different game modes, different game rules are displayed, the highest score of the current mode is displayed, and the current score is dynamically displayed.
  5. Initialization module : When the player first enters the game interface, the snake head will appear in the middle of the screen by default, the snake length is 3 by default, the maximum length is 100, the snake movement speed is 200 by default, and food (and obstacles) are randomly generated.
  6. Randomly generate food (and obstacle) module : detect that the snake eats food, emit corresponding celebratory sound effects, no need to erase the snake tail, speed up the movement, update the score in real time, and randomly generate and print new food and obstacles (position Make sure that its abscissa is an even number and does not coincide with the snake body).
  7. Keyboard key detection module : monitor the player's manipulation of the keyboard, and realize the control of the snake direction (↑: up, ↓: down, ←: left, →: right, space: pause, press other keys twice: exit game), and implement the function of continuing to move in the previous direction if the player's two operations on the direction conflict.
  8. Judgment game failure module : In the borderless mode, the snake will only fail if it bites itself, and the game ends; in the normal mode, if the snake bites itself or touches the wall, it will be judged as a failure and the game will end; in the obstacle mode, A snake that bites itself, touches a wall, or hits an obstacle is considered a failure and the game ends.

Important section

snake movement effect

Store the abscissa and ordinate of the snake head and body in the continuous storage space in the array. When the snake moves, the coordinates of the next snake body become the coordinates of the previous snake body. When no food is eaten, the last One section, that is, the last section of the snake body is printed as the background color to create the effect of snake body movement; when eating food, the last section is not erased to create the effect of snake body lengthening.

keyboard key detection

If there is a conflict between two adjacent buttons, for example, the previous button is ↑, the second button is ↓, or the first button is ←, the second button is →, etc., so that the snake body cannot be reversed, the previous button will be defaulted. Move in one direction; click the space bar to pause, click again to continue the game; click other keys to pause the game, press the second time to end the game.

Show results

insert image description here

insert image description here

at last

This is the class set for the freshman class after learning C language. I don’t remember many details. The code has been uploaded to C language Snake . You can take a look if you need it. Welcome to star!

Guess you like

Origin blog.csdn.net/weixin_54218079/article/details/128689468