Share c++ snake source code

### Mã nguồn rắn C ++

** 1.Giới thiệu**

Snake là một trò chơi cổ điển đã có trong nhiều thập kỷ.Đó là một trò chơi đơn giản dễ học nhưng khó thành thạo.Mục tiêu của trò chơi là kiểm soát một con rắn khi nó ăn táo.Con rắn phát triển lâu hơn với mỗi quả táo mà nó ăn, và nó phải tránh đánh vào cơ thể của chính nó hoặc các bức tường của bảng trò chơi.Trò chơi kết thúc khi con rắn chạy vào chính nó hoặc các bức tường.

Rắn có thể được thực hiện trong một loạt các ngôn ngữ lập trình.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo một trò chơi rắn trong C ++.

** 2.Bắt đầu**

Để tạo một trò chơi rắn trong C ++, bạn sẽ cần những điều sau:

* Trình biên dịch C ++
* Một trình soạn thảo văn bản
* Một thư viện đồ họa

Chúng tôi sẽ sử dụng thư viện đồ họa sau trong hướng dẫn này:

* [SFML] (SFML)

SFML là một thư viện đồ họa miễn phí và nguồn mở hỗ trợ nhiều nền tảng khác nhau.Nó rất dễ sử dụng và cung cấp một loạt các tính năng để tạo trò chơi.

** 3.Tạo bảng trò chơi **

Bước đầu tiên trong việc tạo ra một trò chơi rắn là tạo bảng trò chơi.Bảng trò chơi là một mảng hai chiều của các ô.Mỗi ô có thể trống hoặc bị rắn chiếm.

Chúng tôi có thể tạo bảng trò chơi bằng mã sau:

`` `C ++
chiều rộng int = 400;
int chiều cao = 400;

bo mạch int [chiều rộng] [chiều cao];

for (int i = 0; i <width; i ++) {
for (int j = 0; j <height; j ++) {
Bảng [j] = 0;
}
}
`` `

Hai dòng đầu tiên của mã xác định chiều rộng và chiều cao của bảng trò chơi.Hai dòng mã tiếp theo khởi tạo bảng trò chơi cho tất cả các ô trống.

**4.Tạo con rắn **

Bước tiếp theo là tạo ra con rắn.Con rắn là một loạt các tế bào hai chiều đại diện cho cơ thể của con rắn.

Chúng ta có thể tạo con rắn bằng mã sau:

`` `C ++
int snakex = chiều rộng / 2;
int snakey = chiều cao / 2;

Int Snake [100] [2];

for (int i = 0; i <100; i ++) {
Rắn [0] = Snakex;
Rắn [1] = Snakey;
}
`` `

Hai dòng đầu tiên của mã xác định vị trí bắt đầu của con rắn.Hai dòng mã tiếp theo khởi tạo con rắn thành một mảng 100 tế bào.Yếu tố đầu tiên của mảng đại diện cho đầu của con rắn và phần tử cuối cùng đại diện cho đuôi của con rắn.

** 5.Di chuyển con rắn **

Con rắn di chuyển theo một hướng duy nhất.Hướng của con rắn được xác định bởi đầu vào của người dùng.Người dùng có thể sử dụng các phím mũi tên để điều khiển con rắn.

Chúng ta có thể di chuyển con rắn bằng mã sau:

`` `C ++
int hướng = 1;

void MovesNake () {
for (int i = 99; i> 0; i--) {
rắn [0] = rắn [i - 1] [0];
rắn [1] = rắn [i - 1] [1];
}

chuyển đổi (hướng) {
trường hợp 1:
Rắn [0] [0] ++;
phá vỡ;
Trường hợp 2:
Rắn [0] [0]-;
phá vỡ;
Trường hợp 3:
Rắn [0] [1] ++;
phá vỡ;
Trường hợp 4:
Rắn [0] [1]-;
phá vỡ;
}
}
`` `

Dòng mã đầu tiên xác định hướng ban đầu của con rắn.Hàm tiếp theo di chuyển con rắn theo một ô theo hướng hiện tại.

** 6.Ăn táo **

Các
=======================================
### C++ Snake Source Code

**1. Introduction**

Snake is a classic game that has been around for decades. It is a simple game that is easy to learn but difficult to master. The objective of the game is to control a snake as it eats apples. The snake grows longer with each apple it eats, and it must avoid hitting its own body or the walls of the game board. The game ends when the snake runs into itself or the walls.

Snake can be implemented in a variety of programming languages. In this tutorial, we will show you how to create a Snake game in C++.

**2. Getting Started**

To create a Snake game in C++, you will need the following:

* A C++ compiler
* A text editor
* A graphics library

We will be using the following graphics library in this tutorial:

* [SFML](https://www.sfml-dev.org/)

SFML is a free and open-source graphics library that supports a variety of platforms. It is easy to use and provides a wide range of features for creating games.

**3. Creating the Game Board**

The first step in creating a Snake game is to create the game board. The game board is a two-dimensional array of cells. Each cell can be either empty or occupied by the snake.

We can create the game board using the following code:

```c++
int width = 400;
int height = 400;

int board[width][height];

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
board[j] = 0;
}
}
```

The first two lines of code define the width and height of the game board. The next two lines of code initialize the game board to all empty cells.

**4. Creating the Snake**

The next step is to create the snake. The snake is a two-dimensional array of cells that represents the snake's body.

We can create the snake using the following code:

```c++
int snakeX = width / 2;
int snakeY = height / 2;

int snake[100][2];

for (int i = 0; i < 100; i++) {
snake[0] = snakeX;
snake[1] = snakeY;
}
```

The first two lines of code define the starting position of the snake. The next two lines of code initialize the snake to a 100-element array of cells. The first element of the array represents the snake's head, and the last element represents the snake's tail.

**5. Moving the Snake**

The snake moves in a single direction. The direction of the snake is determined by the user's input. The user can use the arrow keys to control the snake.

We can move the snake using the following code:

```c++
int direction = 1;

void moveSnake() {
for (int i = 99; i > 0; i--) {
snake[0] = snake[i - 1][0];
snake[1] = snake[i - 1][1];
}

switch (direction) {
case 1:
snake[0][0]++;
break;
case 2:
snake[0][0]--;
break;
case 3:
snake[0][1]++;
break;
case 4:
snake[0][1]--;
break;
}
}
```

The first line of code defines the initial direction of the snake. The next function moves the snake by one cell in the current direction.

**6. Eating Apples**

The
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top