1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
RenderWindow window;
window.create(VideoMode(800,600), "Snake Game");
window.setFramerateLimit(300);
RectangleShape rectangle;
rectangle.setSize(Vector2f(16,16));
rectangle.setFillColor(Color(250,250,250));
rectangle.setPosition(100,100);
bool turnRight = false;
bool turnLeft = false;
bool turnDown = false;
bool turnUp = false;
while(window.isOpen())
{
Event event;
while(window.pollEvent(event))
{
if(event.type == Event::Closed)
window.close();
else if(event.type == Event::KeyPressed)
if(event.key.code == Keyboard::Escape)
window.close();
}
//MOVEMENT
if(Keyboard::isKeyPressed(Keyboard::Up))
{
turnRight = false;
turnLeft = false;
turnDown = false;
turnUp = true;
}
if(Keyboard::isKeyPressed(Keyboard::Down))
{
turnRight = false;
turnLeft = false;
turnDown = true;
turnUp = false;
}
if(Keyboard::isKeyPressed(Keyboard::Left))
{
turnRight = false;
turnLeft = true;
turnDown = false;
turnUp = false;
}
if(Keyboard::isKeyPressed(Keyboard::Right))
{
turnRight = true;
turnLeft = false;
turnDown = false;
turnUp = false;
}
if(turnUp == true)
rectangle.move(0,-1);
else if(turnDown == true)
rectangle.move(0,1);
else if(turnLeft == true)
rectangle.move(-1,0);
else if(turnRight == true)
rectangle.move(1,0);
int pos_x = rectangle.getPosition().x;
int pos_y = rectangle.getPosition().y;
RectangleShape apple;
apple.setSize(Vector2f(16,16));
apple.setFillColor(Color(250,0,0));
apple.setPosition(400,400);
int bottom, left, right, up;
bottom = apple.getPosition().y + 16;
left = apple.getPosition().x;
right = apple.getPosition().x + 16;
up = apple.getPosition().y;
bool eatenApple = false;
//HERE IS WHERE I THINK IS THE ERROR
if((pos_x >= left && pos_x <= right && pos_y <= bottom) || (pos_x >= left && pos_x <= right && pos_y + 16 >= up)
|| (pos_x + 16 >= left && pos_x + 16 <= right && pos_y <= bottom) || (pos_x + 16 >= left && pos_x + 16 <= right && pos_y + 16 >= up))
{
eatenApple = true;
}
if(eatenApple == true)
{
cout<<"You did it"<<endl;
cout<<bottom<<" "<<left<<" "<<right<<" "<<up<<" "<<pos_x<<" "<<pos_y<<endl;
}
window.draw(apple);
window.draw(rectangle);
window.display();
window.clear();
|