当没有按下按键时,Snake停止移动

时间:2013-08-16 23:56:23

标签: c++

所以我的代码很好,我让蛇“摆动”但是当我停下来按一个键时它会停止,所以我想知道我怎么能这样做它所以它总是在最后一个方向移动而不按一个键?

我知道我的代码有点复杂,但它只是我的第一个游戏所以请放纵(宽容)。 :) 我试图把它放在循环中,但它要么崩溃要么停止让蛇摆动:(

我是游戏编程的新手,所以我很难以循环方式思考。

非常感谢您的支持。

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>

#include <deque>

enum Direction { Up, Down, Right, Left};
int direction = Down;

class Block
{
    private:
    int ax, ay;

    public:
    Block(int, int);
    int getX();
    int getY();

};


class Snake
{
    private:
    std::deque<Block> asnake;
    int adirection;

    public:
    void Move(int);
    void grow();
    void shrink();
    std::deque<Block> getBlocks();
    int getX();
    int getY();
    Snake();
    void setdirection();
};

int main()
{
    sf::Event event;
    Snake snake;
    snake.setdirection();
    snake.grow();
    snake.grow();
    snake.grow();

    sf::RenderWindow window(sf::VideoMode(500,500), "SFML Snake");
    window.setFramerateLimit(30);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:

                epicMusic.stop();
                window.close();
                break;

                default:

                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                direction = Left;
                snake.Move(direction);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                direction = Right;
                snake.Move(direction);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                direction = Down;
                snake.Move(direction);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                direction = Up;
                snake.Move(direction);
        }

        window.clear(sf::Color::Red);


        for(unsigned int i = 1; i < (snake.getBlocks()).size(); i++)
        {
            float x = (snake.getBlocks())[i].getX();
            float y = (snake.getBlocks())[i].getY();

            sf::RectangleShape block(sf::Vector2f(20,20));
            block.setFillColor(sf::Color::Green);
            block.setPosition(x, y);

            window.draw(block);
        }
        window.display();
    }
    return 0;
}

Block::Block(int x, int y)
{
    ax = x;
    ay = y;
}

int Block::getX()
{
    return ax;
}

int Block::getY()
{
    return ay;
}

void Snake::Move(int NewDirection)
{
    shrink();
    if(NewDirection == Up)
    {

        if(adirection != Down)
        {
            asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() - 22));
            adirection = Up;
        }
        else
        {
            asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() + 22));
        }
    }
    else if(NewDirection == Right)
    {
        if(adirection != Left)
        {
            asnake.push_front(Block(asnake.front().getX() + 25, asnake.front().getY()));
            adirection = Right;
        }
        else
        {
            asnake.push_front(Block(asnake.front().getX() - 25, asnake.front().getY()));
        }
    }
    else if(NewDirection == Down)
    {
            if(adirection != Up)
            {
                asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() + 22));
                adirection = Down;
            }
            else
            {
                asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() - 22));
            }

    }
    else if(NewDirection == Left)
    {
        if(adirection != Right)
        {
            asnake.push_front(Block(asnake.front().getX() - 25, asnake.front().getY()));
            adirection = Left;
        }
        else
        {
            asnake.push_front(Block(asnake.front().getX() + 25, asnake.front().getY()));
        }
    }

}

void Snake::grow()
{
    asnake.push_back(Block(asnake.back().getX(), asnake.back().getY()));
}
void Snake::shrink()
{
    asnake.pop_back();
}

std::deque<Block> Snake::getBlocks()
{
    return asnake;
}
int Snake::getX()
{
    return asnake.front().getX();
}

int Snake::getY()
{
    return asnake.front().getY();
}

Snake::Snake()
{
    asnake.push_front(Block(25,0));
    asnake.push_front(Block(50,0));
    asnake.push_front(Block(75,0));
}

void Snake::setdirection()
{
    adirection = Up;
}

2 个答案:

答案 0 :(得分:0)

你需要某种“计时器”,如果没有按下按键,它会使蛇朝着它指向的方向移动。

在SFML中看起来没有这样的计时器事件,但有一种方法可以测量时间。 http://www.sfml-dev.org/tutorials/2.0/system-time.php

另外,你正在“重复自己”:

            snake.Move(direction);

重复四次。您只需根据按下的键设置方向,如果按下某个键或经过了一定的时间,请执行“snake.Move”。

答案 1 :(得分:0)

尝试这样的事情:

snake.Move(direction);   // move the snake automatically (regardless of key presses)

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))     
      direction = Left;           // only change the direction when a key is pressed

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
      direction = Right;

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
      direction = Down;

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
      direction = Up;