当我只想移动一个时,为什么圆圈会一起移动? C ++ / SFML [英] Why do the circles move together when I only mean to move one? C++/SFML
问题描述
嘿伙计们,我一直在玩SFML,我是初学者,我不知道我想要的最终结果是什么,可能是两个玩家试图让对方进入黑洞,但是现在,我只是想转动圈子,但是圈子一起移动,为什么会这样?
更新:我改变了代码,现在只有一个圈可以移动,转弯不要改变,请帮忙。这是新代码: -
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:-
#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();
}
}
}
}
我的尝试:
我我试过将大的if循环更改为while循环,我也尝试删除一些大括号,我尝试过使用sf :: Keyboard :: isKeyPressed ....但是到目前为止没有任何工作,请你帮忙我?在此先感谢:) [我是初学者顺便说一句,所以对不起]
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]
推荐答案
尝试更换
Try to replace
if (playerRound == 2)
with
with
elseif (playerRound == 2)
[更新]
确保您不会多次处理同一事件。
[Update]
Ensure that you don't handle the same event more than once.
这篇关于当我只想移动一个时,为什么圆圈会一起移动? C ++ / SFML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!