Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize gluttonous Snake small Black window in C language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize gluttonous Snake small Black window in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let's let the editor take you to learn how C language realizes the small black window of gluttonous snakes.

Train of thought:

1. Using the integer two-dimensional array to save the display data, the snake clears the screen every time it moves and facilitates the array to achieve a dynamic effect. The interval between each move (clearing the screen) controls the difficulty of the game, which should be shortened until the end of the game.

two。 Need to save the direction in which the snake moves, and then write a function to process the data in the two-dimensional array through the moving direction of the snake to achieve the purpose of moving the snake.

3. The movement of a small snake can be divided into two processes: a grid grows in front of the head and then a grid is deleted from the tail. If the snakehead eats the food and the tail does not need to be eliminated, a new food should be randomly generated at this time.

4. At the beginning of the game, the snake should be randomly generated in a certain location on the map, and the default movement direction of the snake should also be random.

5 before each move, you should receive the movement direction entered by the player from the keyboard. If you receive it, set it as the default direction of the snake after judging it is legal. If the player does not make any input, the snake should move in accordance with the default direction.

Personal difficulties:

1. The snake moves.

This function needs to get the coordinates of the position of the snakehead and the direction in which the snake is moving. Then a grid can be grown forward from the snakehead position to the moving direction, that is, the data of the target moving position in the two-dimensional array is set to the data of the small snake. Eliminate the snake tail and write another function. Before calling, you need to determine whether you have eaten the food, hit the wall or yourself, and then decide whether to cut off the tail. To eliminate the snake tail, you need to get the position of the snake tail, so we add a recursive algorithm to search the snake tail in the function: after entering the function, you will first determine whether the position at this time is the snake tail, if not, move your position back one lattice (enter the coordinates of the snake body to the next lattice into the DeleteTail function, and then continue the process) until you encounter the snake tail, and then eliminate it (zero).

The specific code implementation is as follows:

Int MoveSnake (int trend, int* px, int* py, int data [ROW] [COL], int* t) {int ret = 1 printf / default forward success / / printf ("trend=%d\ n", trend); switch (trend) {case 1:*py-= 1; break;// up case 2:*px-= 1; break;// left case 3:*py + = 1; break;// downward case 4:*px + = 1; break / / to the right} / / find the coordinates if (data [* py] [* px] = = 6) {data [* py] [* px] = trend; CreatFood (data); * t * = 0.9; / / if the grid that the snakehead wants to move forward is food, grow one grid forward and keep the tail unchanged Else if (data [* py] [* px] = 0) {data [* py] [* px] = trend;// grow one grid forward DeleteTail (* px, * py, data); / / remove the last tail} else {ret = 0; printf ("forward failure\ n");} / / forward failure return ret } int DeleteTail (int x, int y, int data [ROW] [COL]) {/ / PrintTest (data); if (data [y] [x] = 1) {switch (data [y] [x]) {case 1: if (DeleteTail (x, y + 1, data) = 0) {data [y] [x] = 0;} break Case 2: if (DeleteTail (x + 1, y, data) = 0) {data [y] [x] = 0;} break; case 3: if (DeleteTail (x, y-1, data) = = 0) {data [y] [x] = 0;} break Case 4: if (DeleteTail (x-1, y, data) = = 0) {data [y] [x] = 0;} break;} return 1;} else {return 0;}}

two。 Input

In the actual code implementation process, I found that the scanf function can not meet the requirements, the reason is that when the program executes to the scanf statement, it will stay in that position until the scanf function scans the input before the program continues. After learning and consulting, it is decided to use the kbhit function to scan whether there is data in the keyboard buffer at this time: if you have read the first data, and empty the later data to avoid using the data later, if there is no data, the buffer does not do any processing, and the program should continue to execute.

You should have "# include" before using the kbhit function. The getch () loop is used to clean the buffer. The code is as follows:

Int Input () {int trend = 0; if (kbhit ()) {char trend_ = getch (); while (kbhit ()) {getch ();} / clear buffer / / printf ("% c received", trend_); switch (trend_) {case 'w':trend = 1; break; case' a':trend = 2; break Case 's':trend = 3; break; case' d':trend = 4; break; default:printf ("Please enter wre _ a _ r _ r _ d to control the direction of progress!\ n"); break;}} else {printf ("Please enter wme a _ r _ r _ d to control the direction of progress!\ n");} return trend;}

Complete source code:

# pragma once#pragma warning (disable: 4996) # include#include#include#include#include#define ROW 30#define COL 30#define TIME 300int Game (); int Menu (); int Input (); int CreatHead (int* px, int* py, int data [ROW] [COL]); void CreatFood (int data [ROW] [COL]); int MoveSnake (int trend, int* px, int* py, int data [ROW] [COL], int* t); int DeleteTail (int x, int y, int data [ROW] [COL]) Void PrintShow (int data [ROW] [COL]); int GameOver (int score); void PrintTest (int data [ROW] [COL]); # include "main.h" int main () {int ret = 1; while (ret) {int choose = Menu (); int score = 0; switch (choose) {case 1: score = Game (); ret = GameOver (score); break Case 2: system ("cls"); printf ("= =\ n"); printf ("game description\ n"); printf ("1. Enter 'wicked snake' to control the movement of the snake. \ n "); printf (" 2. The snake only moves once in a fixed period of time, such as entering multiple directions in a period of time.\ nThe first input shall prevail. \ n "); printf (" 3. Don't hit the wall or yourself. \ n "); printf (" 4. The snake will move faster after eating the food until the end of the game. \ n "); printf (" enter "1" to continue.\ n "); printf (" =\ n "); int ok = 0; scanf ("% d ", & ok); if (ok) {system (" cls "); break } case 3: ret = 0; break;}} # include "main.h" int Game () {int data [ROW] [COL] = {0}; int time_ = TIME; for (int I = 0; I

< COL; i++) { for (int j = 0; j < ROW; j++) { if (i == 0 || i == COL - 1) { data[i][j] = 5;//边界墙 } if (j == 0 || j == ROW - 1) { data[i][j] = 5;//边界墙 } } }//初始化数据存储数组 //PrintShow(data); int x = 0, y = 0; int trend = CreatHead(&x, &y, data); //生成蛇头 CreatFood(data);//生成食物 int ret = 1; while (ret) { system("cls"); PrintShow(data); Sleep(time_); int trend_ = Input(); if (trend_ != 0) { trend = trend_; } ret = MoveSnake(trend, &x, &y, data, &time_); } int score = 0; for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (data[i][j] = 1) { score++; } } } return score;}int Menu(){ int choose = 0; while (1) { printf("==========================\n"); printf("===欢迎进入贪吃蛇游戏!===\n"); printf("=== 1、经典模式 ===\n"); printf("=== 2、游戏说明 ===\n"); printf("=== 3、退出游戏 ===\n"); printf("=== 请输入您的选项... ===\n"); printf("==========================\n"); scanf("%d", &choose); if (choose == 1 || choose == 2 || choose == 3) { break; } } return choose;}int MoveSnake(int trend, int* px, int* py, int data[ROW][COL], int* t){ int ret = 1;//默认前进成功 //printf("trend=%d\n", trend); switch (trend) { case 1:*py -= 1; break;//向上 case 2:*px -= 1; break;//向左 case 3:*py += 1; break;//向下 case 4:*px += 1; break;//向右 }//找到即将前进的坐标 if (data[*py][*px] == 6) { data[*py][*px] = trend; CreatFood(data); *t *= 0.9; }//如果蛇头要前进的格子是食物,向前长一格,尾部不变; else if (data[*py][*px] == 0) { data[*py][*px] = trend;//向前长一格 DeleteTail(*px, *py, data);//消去最后的尾巴 } else { ret = 0; printf("前进失败\n"); }//前进失败 return ret;}void CreatFood(int data[ROW][COL]){ srand(time(NULL)); while (1) { int x = rand() % (COL - 2) + 1; int y = rand() % (ROW - 2) + 1; if (data[y][x] == 0) { data[y][x] = 6; break; } }} int DeleteTail(int x, int y, int data[ROW][COL]){ //PrintTest(data); if (data[y][x] = 1) { switch (data[y][x]) { case 1: if (DeleteTail(x, y + 1, data) == 0) { data[y][x] = 0; } break; case 2: if (DeleteTail(x + 1, y, data) == 0) { data[y][x] = 0; } break; case 3: if (DeleteTail(x, y - 1, data) == 0) { data[y][x] = 0; } break; case 4: if (DeleteTail(x - 1, y, data) == 0) { data[y][x] = 0; } break; } return 1; } else { return 0; }}int Input() { int trend = 0; if (kbhit()) { char trend_ = getch(); while (kbhit()) { getch(); }//清空缓冲区 //printf("已接收到%c ", trend_); switch (trend_) { case 'w':trend = 1; break; case 'a':trend = 2; break; case 's':trend = 3; break; case 'd':trend = 4; break; default:printf("请输入w,a,s,d以控制前进方向!!\n"); break; } } else { printf("请输入w,a,s,d以控制前进方向!!\n"); } return trend;} void PrintShow(int data[ROW][COL]){ for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (data[i][j] == 0) { printf(" "); } if (data[i][j] == 5) { printf("■"); } if (data[i][j] >

= 1 & & data [I] [j]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report