Я попытался сделать игру SFML C ++ 2D Space Shooter. Итак, я сделал игру, следуя руководству, в которой использовались основные формы для врагов. Вместо этого я пытался вставить в них текстуры и спрайты. Это сработало правильно, и Sprite появился на экране, но когда я попал в рандомизацию нереста врагов на экране, я попал в проблему того, как враги все еще не появлялись в левом углу экрана. Хотелось бы немного понять проблему. < /P>
my cpp -файл: < /p>
#include "Game.h"
using namespace sf;
//конструктор и деструктор
Game::Game()
{
this->initWindow();
this->initTextures();
this->initPlayer();
this->initEnemies();
}
Game::~Game()
{
delete this->window;
delete this->player;
for (auto &i : this->textures) {
delete i.second;
}
for (auto &i : this->enemies) {
delete i;
}
}
void Game::initEnemies() {
this->spawnTimerMax = 20.f;
this->spawnTimer = this->spawnTimerMax;
}
void Game::updateEnemies() {
this->spawnTimer += 0.5f;
if (this->spawnTimer >= spawnTimerMax) {
this->enemies.push_back(new Enemy(rand() % 1920, rand() % 1080));
this->spawnTimer = 0.f;
}
for (auto *enemy : this->enemies) {
enemy->update();
}
}
void Game::update() {
this->updatePollEvents();
this->updateInput();
this->player-\>update();
this->updateBullets();
this->updateEnemies();
}
void Game::render() {
this->window->clear());
this->player->render(*this->window);
for (auto &enemy : this->enemies) {
enemy->render(*this->window);
}
this->window->display();
}
**My header file Game.h:**
#include
#include
#include "Player.h"
#include "Bullet.h"
#include "Enemy.h"
using namespace sf;
class Game
{
private:
RenderWindow* window;
float spawnTimer;
float spawnTimerMax;
std::vector enemies;
};
> Edit: **so Enemy.cpp is:**
#include "Enemy.h"
using namespace sf;
void Enemy::initTexture() {
if (!this->texture.loadFromFile("textures/enemy1.png")) {
std::cout texture);
this->sprite.scale(1.1f, 1.1f);
}
void Enemy::initVariables() {
this->type = 0;
this->hpMax = 10;
this->hp = 0;
this->damage = 1;
this->points = 5;
}
Enemy::Enemy(float pos_x, float pos_y)
{
this->initVariables();
this->initTexture();
this->initSprite();
}
Enemy::~Enemy()
{
}
void Enemy::update() {
}
void Enemy::render(RenderTarget& target) {
target.draw(this->sprite);
}
**Enemy.h is:**
class Enemy
{
private:
Texture texture;
Sprite sprite;
int type;
int hp;
int hpMax;
int damage;
int points;
void initTexture();
void initSprite();
void initVariables();
public:
Enemy(float pos_x, float pos_y);
virtual ~Enemy();
void update();
void render(RenderTarget& target);
};
Подробнее здесь: https://stackoverflow.com/questions/784 ... es-in-sfml
Проблема с рандомизацией врагов в SFML ⇐ C++
Программы на C++. Форум разработчиков
1754620381
Anonymous
Я попытался сделать игру SFML C ++ 2D Space Shooter. Итак, я сделал игру, следуя руководству, в которой использовались основные формы для врагов. Вместо этого я пытался вставить в них текстуры и спрайты. Это сработало правильно, и Sprite появился на экране, но когда я попал в рандомизацию нереста врагов на экране, я попал в проблему того, как враги все еще не появлялись в левом углу экрана. Хотелось бы немного понять проблему. < /P>
my cpp -файл: < /p>
#include "Game.h"
using namespace sf;
//конструктор и деструктор
Game::Game()
{
this->initWindow();
this->initTextures();
this->initPlayer();
this->initEnemies();
}
Game::~Game()
{
delete this->window;
delete this->player;
for (auto &i : this->textures) {
delete i.second;
}
for (auto &i : this->enemies) {
delete i;
}
}
void Game::initEnemies() {
this->spawnTimerMax = 20.f;
this->spawnTimer = this->spawnTimerMax;
}
void Game::updateEnemies() {
this->spawnTimer += 0.5f;
if (this->spawnTimer >= spawnTimerMax) {
this->enemies.push_back(new Enemy(rand() % 1920, rand() % 1080));
this->spawnTimer = 0.f;
}
for (auto *enemy : this->enemies) {
enemy->update();
}
}
void Game::update() {
this->updatePollEvents();
this->updateInput();
this->player-\>update();
this->updateBullets();
this->updateEnemies();
}
void Game::render() {
this->window->clear());
this->player->render(*this->window);
for (auto &enemy : this->enemies) {
enemy->render(*this->window);
}
this->window->display();
}
**My header file Game.h:**
#include
#include
#include "Player.h"
#include "Bullet.h"
#include "Enemy.h"
using namespace sf;
class Game
{
private:
RenderWindow* window;
float spawnTimer;
float spawnTimerMax;
std::vector enemies;
};
> Edit: **so Enemy.cpp is:**
#include "Enemy.h"
using namespace sf;
void Enemy::initTexture() {
if (!this->texture.loadFromFile("textures/enemy1.png")) {
std::cout texture);
this->sprite.scale(1.1f, 1.1f);
}
void Enemy::initVariables() {
this->type = 0;
this->hpMax = 10;
this->hp = 0;
this->damage = 1;
this->points = 5;
}
Enemy::Enemy(float pos_x, float pos_y)
{
this->initVariables();
this->initTexture();
this->initSprite();
}
Enemy::~Enemy()
{
}
void Enemy::update() {
}
void Enemy::render(RenderTarget& target) {
target.draw(this->sprite);
}
**Enemy.h is:**
class Enemy
{
private:
Texture texture;
Sprite sprite;
int type;
int hp;
int hpMax;
int damage;
int points;
void initTexture();
void initSprite();
void initVariables();
public:
Enemy(float pos_x, float pos_y);
virtual ~Enemy();
void update();
void render(RenderTarget& target);
};
Подробнее здесь: [url]https://stackoverflow.com/questions/78483031/trouble-with-randomizing-enemies-in-sfml[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия