playerclassexample

 avatar
unknown
c_cpp
2 years ago
3.0 kB
8
Indexable
#include <SFML/Graphics.hpp>
#include <iostream>


#pragma region Player Class
class Player
{
public:
	//we make our vars public so we can access them from outside the class
	sf::Vector2f position;
	sf::CircleShape shape;

	//constructor. this is how we create an instance of this object we are defining, 'Player'
	Player(sf::Vector2f pos)
	{
		//setting the position var to whatever 'pos' is entered upon creation of the object
		this->position = pos;
		this->shape.setRadius(50.f);
		this->shape.setFillColor(sf::Color(217, 37, 73));
		this->shape.setPosition(position);
	}

	void update()
	{
		this->shape.setPosition(position);
	}

	void move(sf::Vector2f movementVector)
	{
		this->position += movementVector;
	}

	void draw(sf::RenderWindow& window)
	{
		window.draw(this->shape);
	}


};
#pragma endregion Player Class


int main()
{
	//create the window object and event object *before* the loop, if you do it inside the loop you're recreating it every frame	
    sf::RenderWindow window(sf::VideoMode(950, 650), "SFML works!");
	sf:: Event event;
	
	//here we create our vector (list basically) of player objects
	std::vector<Player> players;

	//now we populate it with Player objects from the class we made above
	//the sf::Vector2f is the position of the player to start with, we made it that way in the class's constructor
	players.push_back(Player(sf::Vector2f(200,200)));
	players.push_back(Player(sf::Vector2f(400,200)));


	//everything at the start of main() runs _once_ before the game loop starts, then the game loop repeats until the window is closed ONLY inside the game loop, not up here
	
	
	
#pragma region Game Loop
	while (window.isOpen())
	{
		while(window.pollEvent(event))
		{ 
			if (event.type == sf::Event::Closed) 
				window.close();
			
			//if w is pressed, move the first player up
			if (event.type == sf::Event::KeyPressed)
			{
				if (event.key.code == sf::Keyboard::W)
					players[0].move(sf::Vector2f(0, -5));

				if (event.key.code == sf::Keyboard::A)
					players[0].move(sf::Vector2f(-5, 0));

				if (event.key.code == sf::Keyboard::S)
					players[0].move(sf::Vector2f(0, 5));

				if (event.key.code == sf::Keyboard::D)
					players[0].move(sf::Vector2f(5, 0));
			}
		}
		


		

		

		
		
		
		

#pragma region Drawing	
		window.clear();
	
		//we use a for loop to iterate through our vector of players and draw them all one by one
		for (int i = 0; i < players.size(); i++)
		{
			//[i] is the current player we are on in the loop, we always start from 0 in c languages
			//we pass the window object to the draw function so it knows where to draw the player, 
			//since the draw() method in the class is expecting a window object (you can see this in the arguments of the draw() method in the class)

			players[i].update();
			players[i].draw(window);
		}
	

		//we set the framerate limit to 60 so the game doesn't run too fast
		window.setFramerateLimit(60);
		window.display();
#pragma endregion Drawing
		
			
	}


#pragma endregion Game Loop

    return 0;
}
Editor is loading...