Hello, I am new to SFML, I've read about everything I need to know that would get me ready for making this game.
I have successfully managed to create a game. The problem is with the movement.
I used isKeyPressed with left,up,down,right keys and I used clock to slowdown the snake. I also put some if statements to check whether the snake is moving eg. right when I press LEFT so it prevents snake doing 180 deg.
If you press 1 key at time when you want to move snake, everything works perfectly, but as soon as I press multiple keys ( I also created an if statement to check if player is holding multiple keys, if he is, then don't do anything if not, change direction) and start going crazy with it a little bit snake does 180 deg turn and its "GAME OVER".
Why does this happen ? I think my code is alright, but something happens that I have no idea what it is.
This is my MAIN LOOP sf::Clock clock;
float timer = 0;
float delay = 0.1;
bool left_pressed = false;
bool right_pressed = false;
bool down_pressed = false;
bool up_pressed = false;
// MAIN GAME LOOP ////////////////////////////////////////////////
while (win.isOpen()) {
// Setting up the clock
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
//Checking for events : closing, escape, enter
while (win.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
win.close();
break;
case sf::Event::KeyReleased:
switch (event.key.code) {
case sf::Keyboard::Escape:
win.close();
break;
case sf::Keyboard::Return:
game_state = state_of_game::PLAYING;
player.respawn();
break;
default:
break;
}
default:
break;
}
}
đ
// CHECK IF YOU ATE YOURSELF //
if (game_state == state_of_game::LOST) {
sf::Text lost("GAME OVER", font, 100U);
lost.setFillColor(sf::Color::Red);
lost.setPosition(win_w / 2 - lost.getCharacterSize() * 3, win_h / 2 - lost.getCharacterSize());
win.draw(lost);
}
else {
left_pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Left);
right_pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Right);
down_pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Down);
up_pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::Up);
keys_down = (short)left_pressed + (short)right_pressed + (short)down_pressed + (short)up_pressed;
// MOVEMENT
if(keys_down==1){
if (left_pressed) {
if (dir != Right) dir = Left;
}
else if (right_pressed){
if (dir != Left) dir = Right;
}
else if (up_pressed) {
if (dir != Down) dir = Up;
}
else if (down_pressed) {
if (dir != Up) dir = Down;
}
}
// UPDATING SNAKE WITH DELAY TO REDUCE ITS SPEED
if (timer >= delay) {
timer = 0;
player.update();
}
//Clear the window with color WHITE
win.clear(sf::Color::White);
/////// Draw everything here ///////////
//win.draw(...);
// Draw lines
//win.draw(d_bgr);
//Draw the snake
for (int i = 0; i < player.get_size(); i++) {
win.draw(player[i]);
}
// Draw the goal
win.draw(v_goal.draw());
//Draw the wall
for (int i = 0; i < wall_vec.size(); i++) {
wall_sprite.setPosition(wall_vec[i][0], wall_vec[i][1]);
win.draw(wall_sprite);
}
}
//end of frame
win.display();
}
This is my update function in object player.void update() {
short last_x = snake_tail[snake_tail.size() - 1].getPosition().x;
short last_y = snake_tail[snake_tail.size() - 1].getPosition().y;
for (int i = snake_tail.size() - 1; i > 0; --i) {
snake_tail[i].setPosition(snake_tail[i - 1].getPosition().x, snake_tail[i - 1].getPosition().y);
}
switch (dir)
{
// Move snake
case Left: //left
head_x -= N;
break;
case Right: //right
head_x += N;
break;
case Up: //up
head_y -= N;
break;
case Down: //Down
head_y += N;
break;
}
snake_tail[0].setPosition(head_x, head_y);
short i = 0;
for (i = 1; i < snake_tail.size(); i++) {
get_x = snake_tail[i].getPosition().x;
get_y = snake_tail[i].getPosition().y;
if ( get_x == head_x && get_y == head_y) {
game_state = { LOST };
}
}
if (game_state != LOST){
// CHECK IF SNAKE HIT THE WALL
if (head_x == 0) game_state = LOST;
else if (head_x == win_w - 16) game_state = LOST;
else if (head_y == 0) game_state = LOST;
else if (head_y == win_h - 16) game_state = LOST;
}
// Check if goal is at that position
if(game_state!=LOST)
if (v_goal.x == head_x && v_goal.y == head_y) {
// Increase size
for (i = 0; i < v_goal.increase; i++) {
snake_tail.push_back(snake_sprite);
snake_size++;
snake_tail[snake_tail.size() - 1].setPosition(last_x, last_y);
}
// RESPAWN THE GOAL
v_goal.respawn(snake_tail);
}
}
Sorry if I didn't explain it well (the problem). Game is working, but sometimes when I press 2 keys at once it does the 180 turn, but sometimes I go really crazy and start pressing 3 keys and try everything and it works normally and it comes back again.
What might cause this ? Any ideas ?