在2个矩形形状之间切换

时间:2014-03-02 13:32:38

标签: c++ character sfml

我正在制作足球比赛,我想拥有它,所以当我按下“C”键时,它会从我之前控制的角色切换到另一个。但是,我有一个问题,因为我认为我做得对,无法找到一个我搞砸的地方。你们中的任何人可以帮我解决这个问题吗?我的完整代码可以在下面找到:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <conio.h>

using namespace sf;

int main()
{
RenderWindow window(VideoMode(1000, 600), "2 Against 2  Soccer");
window.setFramerateLimit(60);

bool play = true;
bool player1control = true;
bool player2control = false;
bool up1 = false;
bool up2 = false;
bool down1 = false;
bool down2 = false;
bool left1 = false;
bool left2 = false;
bool right1 = false;
bool right2 = false;

int yVelocityPlayer1 = 0;
int xVelocityPlayer1 = 0;

int yVelocityPlayer2 = 0;
int xVelocityPlayer2 = 0;

RectangleShape player1;
player1.setFillColor(Color::Blue);
player1.setPosition(250, 425);
player1.setSize(Vector2f(65, 65));

RectangleShape player2;
player2.setFillColor(Color::Blue);
player2.setPosition(700, 425);
player2.setSize(Vector2f(65, 65));

RectangleShape playergoalie;
playergoalie.setFillColor(Color::Green);
playergoalie.setSize(Vector2f(100, 25));
playergoalie.setPosition(450, 565);

RectangleShape ai1;
ai1.setFillColor(Color::Red);
ai1.setPosition(250, 75);
ai1.setSize(Vector2f(65, 65));

RectangleShape ai2;
ai2.setFillColor(Color::Red);
ai2.setPosition(700, 75);
ai2.setSize(Vector2f(65, 65));

RectangleShape aigoalie;
aigoalie.setFillColor(Color::White);
aigoalie.setSize(Vector2f(100, 25));
aigoalie.setPosition(450, 10);

CircleShape ball(25);
ball.setFillColor(Color::Green);
ball.setPosition(485, 285);

while(play)
{

    // Events
    Event event;
    while(window.pollEvent(event))
    {
        if(event.type == Event::KeyPressed && event.key.code == Keyboard::W && player1control == true)
        {
            up1 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::S && player1control == true)
        {
            down1 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::D && player1control == true)
        {
            right1 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::A && player1control == true)
        {
            left1 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::W && player2control == true)
        {
            up2 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::S && player2control == true)
        {
            down2 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::D && player2control == true)
        {
            right2 = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::A && player2control == true)
        {
            left2 = true;
        }

        if(event.type == Event::KeyReleased && event.key.code == Keyboard::W && player1control == true)
        {
            up1 = false;
        }

        if(event.type == Event::KeyReleased && event.key.code == Keyboard::S && player1control == true)
        {
            down1 = false;
        }

        if(event.type == Event::KeyReleased && event.key.code == Keyboard::D && player1control == true)
        {
            right1 = false;
        }

        if(event.type == Event::KeyReleased && event.key.code == Keyboard::A && player1control == true)
        {
            left1 = false;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::C && player1control == true)
        {
            player1control = false;
            player2control = true;
        }

        if(event.type == Event::KeyPressed && event.key.code == Keyboard::C && player2control == true)
        {
            player2control = false;
            player1control = true;
        }

        if(event.type == Event::Closed)
        {
            play = false;
        }
    }

    // Logic
    if(up1)
    {
        yVelocityPlayer1 = -3;
    }

    if(down1)
    {
        yVelocityPlayer1 = 3;
    }

    if(right1)
    {
        xVelocityPlayer1 = 3;
    }

    if(left1)
    {
        xVelocityPlayer1 = -3;
    }

    if(up1 == false && down1 == false && left1 == false && right1 == false)
    {
        yVelocityPlayer1 = 0;
        xVelocityPlayer1 = 0;
    }

    player1.move(xVelocityPlayer1, yVelocityPlayer1);

    if(up2)
    {
        yVelocityPlayer2 = -3;
    }

    if(down2)
    {
        yVelocityPlayer2 = 3;
    }

    if(right2)
    {
        xVelocityPlayer2 = 3;
    }

    if(left2)
    {
        xVelocityPlayer2 = -3;
    }

    if(up2 == false && down2 == false && right2 == false && left2 == false)
    {
        yVelocityPlayer2 = 0;
        xVelocityPlayer2 = 0;
    }

    player2.move(xVelocityPlayer2, yVelocityPlayer2);

    // Rendering
    window.clear();
    window.draw(player1);
    window.draw(player2);
    window.draw(playergoalie);
    window.draw(ai1);
    window.draw(ai2);
    window.draw(aigoalie);
    window.draw(ball);
    window.display();
}

return 0;
}

0 个答案:

没有答案