Share c++ source code for snake game

### C ++ Mã nguồn cho trò chơi rắn

**Giới thiệu**

Trò chơi rắn là một trò chơi arcade cổ điển đã tồn tại trong nhiều thập kỷ.Trong trò chơi này, người chơi điều khiển một con rắn phát triển theo chiều dài khi nó ăn thức ăn.Mục tiêu của trò chơi là tránh các chướng ngại vật và ăn càng nhiều thực phẩm càng tốt mà không cần phải tự chạy vào chính nó.

Bài viết này cung cấp mã nguồn cho một trò chơi rắn đơn giản được viết bằng C ++.Mã này được bố trí tốt và dễ hiểu, làm cho nó trở thành một điểm khởi đầu tuyệt vời để học cách tạo ra các trò chơi của riêng bạn.

** Vòng lặp trò chơi **

Vòng lặp chính của trò chơi chịu trách nhiệm cập nhật trạng thái trò chơi và đưa trò chơi lên màn hình.Mã sau đây cho thấy vòng lặp chính của trò chơi rắn:

`` `C ++
while (true) {
// Cập nhật trạng thái trò chơi.
Cập nhậtNake ();
UpdateFood ();

// Kiểm tra xem trò chơi đã kết thúc.
if (isGameOver ()) {
phá vỡ;
}

// Vẽ trò chơi lên màn hình.
rút tiền ();
drawfood ();

// Chờ khung tiếp theo.
SDL_Delay (1000 /60);
}
`` `

Chức năng `Cập nhật ()` Cập nhật vị trí của con rắn và kiểm tra xem nó có va chạm với bất cứ điều gì không.Chức năng `updatefood ()` sinh ra các mặt hàng thực phẩm mới tại các vị trí ngẫu nhiên trên màn hình.Hàm `isgameOver ()` kiểm tra xem con rắn đã va chạm với chính nó hoặc một trở ngại.Chức năng `DrawSnake ()` vẽ con rắn lên màn hình.Hàm `drawfood ()` thu hút thực phẩm lên màn hình.

** Lớp rắn **

Con rắn được đại diện bởi một lớp gọi là `snake`.Lớp `Snake` có các thành viên sau:

* `m_head`: Vị trí của đầu rắn.
* `m_body`: một vectơ của các vị trí đại diện cho cơ thể của con rắn.
* `m_direction`: hướng mà con rắn đang di chuyển.
* `m_length`: chiều dài của con rắn.

Lớp `Snake` cũng có các phương pháp sau:

* `Di chuyển ()`: Di chuyển con rắn theo hướng được chỉ định.
* `Grow ()`: Trồng con rắn theo một phân đoạn.
* `isCollided ()`: Kiểm tra xem con rắn có va chạm với bất cứ thứ gì không.

** lớp thực phẩm **

Thức ăn được đại diện bởi một lớp gọi là `thực phẩm '.Lớp học `thực phẩm 'có các thành viên sau:

* `m_poseition`: Vị trí của thực phẩm.

Lớp `food` cũng có các phương pháp sau:

* `Spawn ()`: Sinh sản thực phẩm tại một vị trí ngẫu nhiên trên màn hình.

** Trò chơi trên màn hình **

Khi trò chơi kết thúc, một trò chơi trên màn hình được hiển thị.Trò chơi trên màn hình được vẽ bởi mã sau:

`` `C ++
void drawgameoverscreen () {
// Xóa màn hình.
SDL_SetrenderDrawColor (kết xuất, 0, 0, 0, 255);
SDL_RenderClear (trình kết xuất);

// Vẽ văn bản "Trò chơi trên".
Ttf_font* font = ttf_openfont ("arial.ttf", 24);
Sdl_color color = {255, 255, 255, 255};
Sdl_surface* TextSurface = ttf_RenderText_solid (phông chữ, "trò chơi trên", màu);
Sdl_texture* texttexture = sdl_createtexturefromsurface (renderer, textSurface);
SDL_RECT TEXTRECT = {(Screen_Width-TextSurface-> W) / 2, (Screen_Height-TextSurface-> h) / 2, TextSurface-> W, TextSurface-> h};
SDL_RENDERCOPY (Kết xuất, TextTexture, NULL, & TEXTRECT);

// Cập nhật màn hình.
SDL_RENDERPRESENT (kết xuất);
}
`` `

** Chạy trò chơi **

Để chạy trò chơi, bạn có thể biên dịch mã nguồn và chạy tệp thực thi.Trò chơi sẽ bắt đầu và bạn
=======================================
### C++ Source Code for Snake Game

**Introduction**

The snake game is a classic arcade game that has been around for decades. In this game, the player controls a snake that grows in length as it eats food. The goal of the game is to avoid obstacles and eat as much food as possible without running into itself.

This article provides the source code for a simple snake game written in C++. The code is well-commented and easy to understand, making it a great starting point for learning how to create your own games.

**The Game Loop**

The main loop of the game is responsible for updating the game state and drawing the game to the screen. The following code shows the main loop of the snake game:

```c++
while (true) {
// Update the game state.
updateSnake();
updateFood();

// Check if the game is over.
if (isGameOver()) {
break;
}

// Draw the game to the screen.
drawSnake();
drawFood();

// Wait for the next frame.
SDL_Delay(1000 / 60);
}
```

The `updateSnake()` function updates the position of the snake and checks if it has collided with anything. The `updateFood()` function spawns new food items at random locations on the screen. The `isGameOver()` function checks if the snake has collided with itself or an obstacle. The `drawSnake()` function draws the snake to the screen. The `drawFood()` function draws the food to the screen.

**The Snake Class**

The snake is represented by a class called `Snake`. The `Snake` class has the following members:

* `m_head`: The position of the head of the snake.
* `m_body`: A vector of positions that represent the body of the snake.
* `m_direction`: The direction that the snake is moving.
* `m_length`: The length of the snake.

The `Snake` class also has the following methods:

* `move()`: Moves the snake in the specified direction.
* `grow()`: Grows the snake by one segment.
* `isCollided()`: Checks if the snake has collided with anything.

**The Food Class**

The food is represented by a class called `Food`. The `Food` class has the following members:

* `m_position`: The position of the food.

The `Food` class also has the following methods:

* `spawn()`: Spawns the food at a random location on the screen.

**The Game Over Screen**

When the game is over, a game over screen is displayed. The game over screen is drawn by the following code:

```c++
void drawGameOverScreen() {
// Clear the screen.
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

// Draw the text "Game Over".
TTF_Font* font = TTF_OpenFont("arial.ttf", 24);
SDL_Color color = {255, 255, 255, 255};
SDL_Surface* textSurface = TTF_RenderText_Solid(font, "Game Over", color);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {(SCREEN_WIDTH - textSurface->w) / 2, (SCREEN_HEIGHT - textSurface->h) / 2, textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);

// Update the screen.
SDL_RenderPresent(renderer);
}
```

**Running the Game**

To run the game, you can compile the source code and run the executable file. The game should start up and you
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top