Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I've been 'playing around' with SFML, I'm a beginner, I don't know what I want my final result to be, probably two players trying to get each others in the black hole, but right now, I just want to move the circles on turns, but the circles move together, why is that?
UPDATE: I changed the code, now only one circle can move, the turns don't change, please help. Here is the new code:-


C++

#include <iostream>
#include <SFML/Graphics.hpp>
int playerRound = 1;
int n = 1;

int turn(){
	n++;
		if (n % 2 == 0){
			playerRound = 1;
			
		}
		else if (n%2!=0){
			playerRound = 2;

		}
	
	
	return playerRound;
	
}
int main(){



	sf::VideoMode videomode(400, 400);
	sf::RenderWindow window(videomode, "GAME");
	sf::CircleShape circle;
	circle.setFillColor(sf::Color::Red);
	circle.setPosition(100, 100);
	circle.setRadius(20);
	sf::CircleShape circle2;
	circle2.setFillColor(sf::Color::Yellow);
	circle2.setPosition(200, 200);
	circle2.setRadius(20);
	sf::CircleShape circle3;
	circle3.setFillColor(sf::Color::Black);
	circle3.setPosition(300, 300);
	circle3.setRadius(50);


	while (window.isOpen()){
		window.clear(sf::Color::Blue);
		window.draw(circle);
		window.draw(circle2);
		window.draw(circle3);

		window.display();

		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}


		turn();
		
			if (playerRound == 1){

				if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Up)){
					circle.move(0, -0.5);
					
					
				}

				else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Down)){
					circle.move(0, 0.5);
					
					
				}

				else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right)){
					circle.move(0.5, 0);
					
				

				}

				else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left)){
					circle.move(-0.5, 0);
					
					



				}
				turn();
			}
			else if (playerRound == 2)
			{
				if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Up)){
					circle2.move(0, -0.5);
					
					
				}


				else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Down)){
					circle2.move(0, 0.5);
					
					
				}


				else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right)){
					circle2.move(0.5, 0);
					
					
				}


				else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left)){
					circle2.move(-0.5, 0);
					
					
				}

				turn();




			}
		}

	}


}


What I have tried:

I've tried changing the big if loops to while loops, I've also tried removing some braces, I've tried using sf::Keyboard::isKeyPressed.... instead, but nothing has worked so far, could you please help me? Thanks in advance :) [I'm a beginner btw, so excuse me]
Posted
Updated 24-Mar-16 21:12pm
v3

1 solution

Try to replace
C++
if (playerRound == 2)

with
C++
elseif (playerRound == 2)


[Update]
Ensure that you don't handle the same event more than once.
 
Share this answer
 
v2
Comments
Alya Wu 24-Mar-16 14:00pm    
Did that, the problem persists :s
Patrice T 24-Mar-16 14:13pm    
Ensure that you don't handle the same event more than once.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900