Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help me a game snake  (Read 2153 times)

0 Members and 3 Guests are viewing this topic.

dapxekhongyen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Help me a game snake
« on: May 08, 2014, 04:11:20 pm »
I'm trying to write game snake
I'm having trouble making the snake move, help me move the snake
I want snake move slow , when i write it move very fast
thanks

#include <SFML/Graphics.hpp>

struct Pos2D
{
    int x; int y;
    int ox; int oy;
    sf::Texture snakeTexture;
    sf::Sprite snakeSprite;
};

int boardW; int boardH;
Pos2D snake[100];
int snakeLength;
Pos2D direction;
bool endGame;
float sox; float soy;

sf::RenderWindow window;
sf::Event event;
sf::Clock Clock;

sf::Time ElapsedTime = Clock.getElapsedTime();

void init()
{
    boardW = 640;
    boardH = 480;
    //Thiê&#769;t lâ&#803;p window
    window.create(sf::VideoMode(boardW, boardH), "My Window");
     // kh&#7903;i t&#7841;o con r&#7855;n g&#7891;m có 5 ô
    snake[0].x = 5; snake[0].y = 5;
    snake[1].x = 4; snake[1].y = 5;
    snake[2].x = 3; snake[2].y = 5;
    snake[3].x = 2; snake[3].y = 5;
    snake[4].x = 1; snake[4].y = 5;

    //Load Texture 5 ô
    snakeLength = 5;
    for (int i = 0; i < snakeLength; i++)
        snake[i].snakeTexture.loadFromFile("block.png");
    for (int i = 0; i < snakeLength; i++)
        snake[i].snakeSprite.setTexture(snake[i].snakeTexture);

    // h&#432;&#7899;ng di chuy&#7875;n ban &#273;&#7847;u là &#273;i pha&#777;i
    direction.x = 1; direction.y = 0;

    //To&#803;a &#273;ô&#803; &#273;â&#768;u cu&#777;a r&#259;&#769;n
    sox = (snakeLength * 24) - 24;
    soy = 0;

    //Chiê&#768;u da&#768;i con r&#259;&#769;n
    endGame = false;
}

void moveSnake(Pos2D dir)
{
    if (dir.x = 1)
            snake[0].snakeSprite.move(dir.x = 1.f, dir.y = 0.f);
    /*else if (direction.x = -1)
            _sox += 24;
    else if (direction.y = 1)
            _soy -= 24;
    else if (direction.y = -1)
            _soy += 24;*/

}

void drawSnake(int _sox,int _soy)
{
    for (int i = 0; i < snakeLength; i++)
    {
        snake[i].snakeSprite.setPosition(sf::Vector2f(_sox, _soy));
        window.draw(snake[i].snakeSprite);
        if (direction.x = 1)
            _sox -= 24;
        else if (direction.x = -1)
            _sox += 24;
        else if (direction.y = 1)
            _soy -= 24;
        else if (direction.y = -1)
            _soy += 24;
    }
}

void CheckKey(sf::Keyboard::Key key)
{
    if (key = sf::Keyboard::Up)
        //Up
        if (direction.y != 1)
        {
            direction.y = -1;
            direction.x = 0;
        }
    else if (key = sf::Keyboard::Down)
        //Down
        if (direction.y != -1)
        {
            direction.y = 1;
            direction.x = 0;
        }
    else if (key = sf::Keyboard::Left)
        //Left
        if (direction.x != 1)
        {
            direction.y = 0;
            direction.x = -1;
        }
    else if (key = sf::Keyboard::Right)
        //Right
        if (direction.x != -1)
        {
            direction.y = 0;
            direction.x = 1;
        }
}

int main()
{
    init();

    while (window.isOpen())
    {
        Clock.restart();
        // check all the window's events that were triggered since the last iteration of the loop
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
                case (sf::Event::Closed) :
                    window.close();
                    break;
                case (sf::Event::KeyPressed) :
                    CheckKey(event.key.code);
                    break;
            }
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        moveSnake(direction);

        drawSnake(sox, soy);
        // draw everything here...
        // window.draw(...);

        // end the current frame
        window.display();
    }

    return 0;
}
 

F2CPP

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • StackOverflow
Re: Help me a game snake
« Reply #1 on: May 09, 2014, 07:36:17 pm »
e.g. method moveSnake(sf::time dt, ...) could take a time parameter so that it moves proportional to the time elapsed in between being called (as opposed to fully each iteration, as shown). check out the tutorial on 'handling time':

sf::Clock clock;
while (window.isOpen())
{
    sf::Time elapsed = clock.restart();
    moveSnake(elapsed, ...);
    ...
}

with

moveSnake(sf::time dt, ...)
{
   ...
   snake[0].snakeSprite.move(dir.x * dt.asSeconds(), dir.y * dt.asSeconds());
}

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Help me a game snake
« Reply #2 on: May 09, 2014, 08:01:14 pm »
Besides that, the snake does not really "move". Try to think about the movement in your head. (Maybe it helps if you imagine a really really slow snake ;) )

Ofc it seems like it does move, but there is actually no moving (as if sprite.move) involved... ;)
Failing to succeed does not mean failing to progress!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help me a game snake
« Reply #3 on: May 09, 2014, 08:34:50 pm »
It is really simple.
For example, if you have a std::deque of snake segment positions you'd pop_back() to remove the tail segment and then push_front() to place the new head. Then draw the snake.
Nothing to it.

dapxekhongyen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Help me a game snake
« Reply #4 on: May 10, 2014, 09:55:03 am »
Yes , thank you very much  :D

 

anything