I'm having problems with implementation, mainly how do you make the sprite change direction and bounce when it hits the boundary I have made "m_bounds", and make it travel diagonally,and have managed to make the first sprite move but sprite 2 is just going through the wall, I'm still learning C++ so
a simple way to do it would be appreciated not very complex implementation
Game.h file
#pragma once
#include "Window.h"
#include "World.h"
#include "Snake.h"
#include "Eagle.h"
#include "Airjeti.h"
class Game
{
public:
Game();
~Game();
void setterWindow();
Window* GetWindow();
private:
Window m_window;
sf::RenderWindow window;
sf::VertexArray points;
sf::Texture spaceship;
sf::Sprite sprite;
sf::Texture Airjet;
sf::Sprite sprite2;
sf::RectangleShape m_bounds1;
sf::RectangleShape m_bounds2;
sf::RectangleShape m_bounds3;
sf::RectangleShape m_bounds4;
Eagle eagle;
World world;
Airjeti airjeti;
bool m_bounce;
};
Game.cpp file
#include "Game.h"
#include <SFML/Graphics.hpp>
#include <iostream>
Game::Game(): window(sf::VideoMode(800, 600), "Test",sf::Style::Default),points(sf::Points,4)
{
}
Game::~Game()
{
}
void Game::setterWindow()
{
if (!spaceship.loadFromFile("window/spaceship.png"))
{
std::cout << "Failed to load" << std::endl;
}
sprite.setTexture(spaceship);
if (!Airjet.loadFromFile("c:window/Eagle.png"))
{
std::cout << "Failed to load" << std::endl;
}
sprite2.setTexture(Airjet);
sf::Vector2u size = Airjet.getSize();/////////////
sprite2.setOrigin(0.100f,0.100f);
sf::Vector2f increment(0.4f, 0.4f);
sprite.setOrigin(0.100f, 0.100f);
m_bounds1.setFillColor(sf::Color(150, 0, 0));
m_bounds1.setOrigin(0.5f, 0.5f);
m_bounds1.setSize(sf::Vector2f(10, 1000));
m_bounds2.setFillColor(sf::Color(150, 0, 0));
m_bounds2.setOrigin(0.5f, 0.5f);
m_bounds2.setSize(sf::Vector2f(1000, 10));
m_bounds3.setFillColor(sf::Color(150, 0, 0));
m_bounds3.setOrigin(10,-590 );
m_bounds3.setSize(sf::Vector2f(1000, 10));
m_bounds4.setFillColor(sf::Color(150, 0, 0));
m_bounds4.setOrigin(-790, 10);
m_bounds4.setSize(sf::Vector2f(10, 1000));
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.move(-5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
sprite.move(+5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sprite.move(0, -5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
sprite.move(0, +5);
}
}
if (sprite2.getGlobalBounds().intersects(m_bounds3.getGlobalBounds()))
{
sprite2.move(-10, +10);
}
window.clear(sf::Color::White);
sprite2.setPosition(sprite2.getPosition() + increment);
window.draw(sprite);
window.draw(m_bounds1);
window.draw(m_bounds2);
window.draw(m_bounds3);
window.draw(m_bounds4);
window.draw(sprite2);
window.display();
}
Window* Game::GetWindow()
{
return &m_window;
}