Share 735. asteroid collision c++,

ticklishdog864

New member
#asteroids, #collision, #C ++, #Game Development, #Physics ### 735. Va chạm tiểu hành tinh trong C ++

Va chạm tiểu hành tinh là một trò chơi kinh điển đã tồn tại trong nhiều thập kỷ.Đó là một trò chơi đơn giản trong đó người chơi điều khiển một con tàu phải tránh các tiểu hành tinh đang rơi xuống từ bầu trời.Trò chơi là một thách thức và đòi hỏi phản xạ nhanh để tránh bị các tiểu hành tinh tấn công.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo ra một trò chơi va chạm tiểu hành tinh trong C ++.Chúng tôi sẽ sử dụng thư viện SFML để tạo đồ họa và công cụ vật lý.

## Bắt đầu

Để bắt đầu, bạn sẽ cần cài đặt thư viện SFML.Bạn có thể tìm thấy hướng dẫn về cách thực hiện việc này trên trang web SFML.

Khi bạn đã cài đặt SFML, bạn có thể tạo một dự án mới trong IDE yêu thích của bạn.Chúng tôi sẽ sử dụng Visual Studio cho hướng dẫn này.

## Tạo cửa sổ trò chơi

Điều đầu tiên chúng ta cần làm là tạo cửa sổ trò chơi.Chúng ta có thể làm điều này bằng cách tạo đối tượng `sfml :: renderWindow` mới.

`` `C ++
// Tạo cửa sổ trò chơi
SF :: Cửa sổ RenderWindow (SF :: VideoMode (800, 600), "Va chạm tiểu hành tinh");
`` `

Đối số đầu tiên cho hàm tạo `SFML :: RenderWindow` là kích thước của cửa sổ trong pixel.Đối số thứ hai là tiêu đề của cửa sổ.

## Tạo các tiểu hành tinh

Điều tiếp theo chúng ta cần làm là tạo ra các tiểu hành tinh.Chúng ta có thể làm điều này bằng cách tạo một đối tượng `sfml :: circleshape` mới cho mỗi tiểu hành tinh.

`` `C ++
// Tạo các tiểu hành tinh
for (int i = 0; i <num_asteroids; i ++) {
SF :: Circleshape Asteroid (100.f);
Tiểu hành tinh.setposeition (rand () % window.getSize (). x, rand () % window.getSize (). y);
tiểu hành tinh.push_back (tiểu hành tinh);
}
`` `

Đối số đầu tiên cho hàm tạo `sfml :: circleshape` là bán kính của vòng tròn.Đối số thứ hai là vị trí của vòng tròn trong pixel.

## Di chuyển các tiểu hành tinh

Chúng ta cần di chuyển các tiểu hành tinh mỗi khung hình để chúng rơi xuống từ bầu trời.Chúng ta có thể làm điều này bằng cách cập nhật vị trí của mỗi tiểu hành tinh mỗi khung hình.

`` `C ++
// Cập nhật các tiểu hành tinh
for (int i = 0; i <asteroids.size (); i ++) {
Các tiểu hành tinh .move (0.f, -10.f);
}
`` `

Đối số đầu tiên cho chức năng `sfml :: circleshape :: di chuyển` là tọa độ x của phong trào.Đối số thứ hai là tọa độ y của phong trào.

## Vẽ các tiểu hành tinh

Chúng ta cần vẽ các tiểu hành tinh vào màn hình mỗi khung hình.Chúng ta có thể làm điều này bằng cách gọi chức năng `SFML :: RenderWindow :: Draw`.

`` `C ++
// Vẽ các tiểu hành tinh
for (int i = 0; i <asteroids.size (); i ++) {
window.Draw (tiểu hành tinh );
}
`` `

## Kiểm tra va chạm

Chúng ta cần kiểm tra các va chạm giữa các tiểu hành tinh và con tàu mỗi khung hình.Chúng ta có thể làm điều này bằng cách sử dụng chức năng `sfml :: circleshape :: getglobalbound` để lấy hộp giới hạn của mỗi đối tượng.

`` `C ++
// Kiểm tra va chạm
for (int i = 0; i <asteroids.size (); i ++) {
sf :: floatrect asteroidbound = tiểu hành tinh .getGlobalBounds ();
if (AsteroidBounds.intersects (ship.getGlobalBounds ())) {
// tiểu hành tinh và con tàu đã va chạm
}
}
`` `

`Sfml :: floatrect :: hàm giao nhau` true` nếu hai hình chữ nhật giao nhau.

## Xử lý va chạm

Nếu có sự va chạm giữa tiểu hành tinh và tàu, chúng ta cần thực hiện một số hành động.Trong trường hợp này, chúng tôi sẽ chỉ đơn giản là kết thúc trò chơi.

`` `C ++
// Xử lý va chạm
if (AsteroidBounds.intersects (ship.getGlobalBounds ())) {
// tiểu hành tinh và con tàu đã va chạm
trò chơi
=======================================
#asteroids, #collision, #C++, #Game development, #Physics ### 735. Asteroid Collision in C++

Asteroid collision is a classic game that has been around for decades. It is a simple game where the player controls a ship that must avoid asteroids that are falling from the sky. The game is challenging and requires quick reflexes to avoid being hit by the asteroids.

In this tutorial, we will show you how to create an asteroid collision game in C++. We will use the SFML library to create the graphics and the physics engine.

## Getting Started

To get started, you will need to install the SFML library. You can find instructions on how to do this on the SFML website.

Once you have installed SFML, you can create a new project in your favorite IDE. We will be using Visual Studio for this tutorial.

## Creating the Game Window

The first thing we need to do is create the game window. We can do this by creating a new `SFML::RenderWindow` object.

```c++
// Create the game window
sf::RenderWindow window(sf::VideoMode(800, 600), "Asteroid Collision");
```

The first argument to the `SFML::RenderWindow` constructor is the size of the window in pixels. The second argument is the title of the window.

## Creating the Asteroids

The next thing we need to do is create the asteroids. We can do this by creating a new `SFML::CircleShape` object for each asteroid.

```c++
// Create the asteroids
for (int i = 0; i < NUM_ASTEROIDS; i++) {
sf::CircleShape asteroid(100.f);
asteroid.setPosition(rand() % window.getSize().x, rand() % window.getSize().y);
asteroids.push_back(asteroid);
}
```

The first argument to the `SFML::CircleShape` constructor is the radius of the circle. The second argument is the position of the circle in pixels.

## Moving the Asteroids

We need to move the asteroids every frame so that they fall from the sky. We can do this by updating the position of each asteroid each frame.

```c++
// Update the asteroids
for (int i = 0; i < asteroids.size(); i++) {
asteroids.move(0.f, -10.f);
}
```

The first argument to the `SFML::CircleShape::move` function is the x-coordinate of the movement. The second argument is the y-coordinate of the movement.

## Drawing the Asteroids

We need to draw the asteroids to the screen each frame. We can do this by calling the `SFML::RenderWindow::draw` function.

```c++
// Draw the asteroids
for (int i = 0; i < asteroids.size(); i++) {
window.draw(asteroids);
}
```

## Checking for Collisions

We need to check for collisions between the asteroids and the ship each frame. We can do this by using the `SFML::CircleShape::getGlobalBounds` function to get the bounding box of each object.

```c++
// Check for collisions
for (int i = 0; i < asteroids.size(); i++) {
sf::FloatRect asteroidBounds = asteroids.getGlobalBounds();
if (asteroidBounds.intersects(ship.getGlobalBounds())) {
// The asteroid and the ship collided
}
}
```

The `SFML::FloatRect::intersects` function returns `true` if the two rectangles intersect.

## Handling Collisions

If there is a collision between the asteroid and the ship, we need to take some action. In this case, we will simply end the game.

```c++
// Handle collisions
if (asteroidBounds.intersects(ship.getGlobalBounds())) {
// The asteroid and the ship collided
gameOver
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top