Share 3d bounce ball game in c++,

thanhkhiem681

New member
#3D #bounce #ball #Game #C ++ ### Trò chơi bóng nảy 3D trong C ++

**Giới thiệu**

Trong hướng dẫn này, chúng tôi sẽ học cách tạo một trò chơi bóng 3D trong C ++.Chúng tôi sẽ sử dụng thư viện [SFML] (SFML) để xử lý đồ họa và vật lý.

** Điều kiện tiên quyết **

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Trình biên dịch C ++
* Thư viện SFML
* Một trình soạn thảo văn bản

** Tạo dự án **

Đầu tiên, chúng ta cần tạo một dự án mới.Chúng ta có thể làm điều này bằng cách mở một cửa sổ đầu cuối và gõ lệnh sau:

`` `
MKDIR 3D-BOUNCE-BALL-GAME
CD 3D-BOUNCE-BALL-GAME
cmake.
`` `

Điều này sẽ tạo ra một thư mục mới có tên là `3D-BOUND-BALL-GAME` và` CMAKELISTS.TXT`.Tệp `cmakelists.txt` được sử dụng để định cấu hình dự án và tạo các tệp xây dựng.

** Thêm thư viện SFML **

Tiếp theo, chúng ta cần thêm thư viện SFML vào dự án.Chúng ta có thể làm điều này bằng cách mở tệp `cmakelists.txt` và thêm các dòng sau:

`` `
find_package (SFML yêu cầu)

Target_Link_L Libries (3D-Bounce-Ball-Game SFML-Graphics SFML-Window SFML-System)
`` `

Điều này sẽ bảo CMake tìm thư viện SFML và liên kết nó với dự án.

** Tạo lớp trò chơi **

Bây giờ, chúng ta cần tạo lớp trò chơi.Lớp học trò chơi sẽ chịu trách nhiệm quản lý trạng thái trò chơi và cập nhật logic trò chơi.Chúng ta có thể tạo lớp trò chơi bằng cách mở tệp `main.cpp` và thêm mã sau:

`` `C ++
#include <sfml/graphics.hpp>

Trò chơi lớp {
công cộng:
Trò chơi();
~ Game ();

void chạy ();

riêng tư:
SF :: Cửa sổ RenderWindow;
SF :: Circleshape Ball;
SF :: Đồng hồ đồng hồ;

void cập nhật ();
khoảng trống rút ();
};
`` `

Lớp `game` có các thành viên sau:

* `Window`: Một tham chiếu đến cửa sổ SFML.
* `Ball`: A SF :: Vòng tròn đối tượng đại diện cho bóng.
* `clock`: một đối tượng đồng hồ SF :: được sử dụng để theo dõi thời gian.

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

* `Run ()`: Phương thức này được gọi để bắt đầu vòng lặp trò chơi.
* `Cập nhật ()`: Phương thức này được gọi để cập nhật trạng thái trò chơi.
* `Draw ()`: Phương thức này được gọi để vẽ trò chơi lên màn hình.

** Khởi tạo trò chơi **

Trình xây dựng cho lớp `game` chịu trách nhiệm khởi tạo trạng thái trò chơi.Chúng ta có thể khởi tạo trạng thái trò chơi bằng cách thêm mã sau vào hàm tạo lớp `game`:

`` `C ++
Game :: game () {
// Khởi tạo cửa sổ.
window.create (SF :: VideoMode (800, 600), "Trò chơi bóng nảy 3D");

// Khởi tạo bóng.
Ball.Setradius (10.0f);
Ball.setPoseition (window.getSize (). X / 2.0f, window.getSize (). Y / 2.0f);
Ball.setfillColor (SF :: Color :: White);

// Bắt đầu đồng hồ.
đồng hồ.restart ();
}
`` `

** Chạy vòng lặp trò chơi **

Phương thức `Run ()` được gọi để bắt đầu vòng lặp trò chơi.Vòng lặp trò chơi chịu trách nhiệm cập nhật trạng thái trò chơi và vẽ trò chơi lên màn hình.Chúng ta có thể triển khai phương thức `run ()` bằng cách thêm mã sau vào lớp `game`:

`` `C ++
Trò chơi void :: chạy () {
// Tạo một vòng lặp trong thời gian sẽ chạy cho đến khi cửa sổ được đóng.
while (window.isopen ()) {
// Cập nhật trạng thái trò chơi.
cập nhật();

// Vẽ trò chơi lên màn hình.
=======================================
#3D #bounce #ball #Game #C++ ### 3D Bounce Ball Game in C++

**Introduction**

In this tutorial, we will learn how to create a 3D bounce ball game in C++. We will use the [SFML](https://www.sfml-dev.org/) library to handle the graphics and physics.

**Prerequisites**

To follow this tutorial, you will need the following:

* A C++ compiler
* The SFML library
* A text editor

**Creating the Project**

First, we need to create a new project. We can do this by opening a terminal window and typing the following command:

```
mkdir 3d-bounce-ball-game
cd 3d-bounce-ball-game
cmake .
```

This will create a new directory called `3d-bounce-ball-game` and a `CMakeLists.txt` file. The `CMakeLists.txt` file is used to configure the project and generate the build files.

**Adding the SFML Library**

Next, we need to add the SFML library to the project. We can do this by opening the `CMakeLists.txt` file and adding the following lines:

```
find_package(SFML REQUIRED)

target_link_libraries(3d-bounce-ball-game sfml-graphics sfml-window sfml-system)
```

This will tell CMake to find the SFML library and link it to the project.

**Creating the Game Class**

Now, we need to create the game class. The game class will be responsible for managing the game state and updating the game logic. We can create the game class by opening the `main.cpp` file and adding the following code:

```c++
#include <SFML/Graphics.hpp>

class Game {
public:
Game();
~Game();

void run();

private:
sf::RenderWindow window;
sf::CircleShape ball;
sf::Clock clock;

void update();
void draw();
};
```

The `Game` class has the following members:

* `window`: A reference to the SFML window.
* `ball`: A sf::CircleShape object that represents the ball.
* `clock`: A sf::Clock object that is used to track the time.

The `Game` class has the following methods:

* `run()`: This method is called to start the game loop.
* `update()`: This method is called to update the game state.
* `draw()`: This method is called to draw the game to the screen.

**Initializing the Game**

The constructor for the `Game` class is responsible for initializing the game state. We can initialize the game state by adding the following code to the `Game` class constructor:

```c++
Game::Game() {
// Initialize the window.
window.create(sf::VideoMode(800, 600), "3D Bounce Ball Game");

// Initialize the ball.
ball.setRadius(10.0f);
ball.setPosition(window.getSize().x / 2.0f, window.getSize().y / 2.0f);
ball.setFillColor(sf::Color::White);

// Start the clock.
clock.restart();
}
```

**Running the Game Loop**

The `run()` method is called to start the game loop. The game loop is responsible for updating the game state and drawing the game to the screen. We can implement the `run()` method by adding the following code to the `Game` class:

```c++
void Game::run() {
// Create a while loop that will run until the window is closed.
while (window.isOpen()) {
// Update the game state.
update();

// Draw the game to the screen.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top