所以我的代码很好,我让蛇“摆动”,但是当我停下来按一个键时它会停止,所以我想知道我怎样才能让它总是在最后一个方向移动而不按一个键?
我知道我的代码有点复杂,但它只是我的第一个游戏,所以请放纵(原谅)。:) 我试图把它放在循环中,但它要么崩溃,要么停止让蛇摆动 :(
我是游戏编程的新手,所以以基于循环的方式思考对我来说很奇怪。
非常感谢您的支持。
#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;
}