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

Author Topic: Advice/help with Snake game  (Read 2319 times)

0 Members and 1 Guest are viewing this topic.

vkcelik

  • Newbie
  • *
  • Posts: 3
    • View Profile
Advice/help with Snake game
« on: March 03, 2011, 12:38:37 am »
Hello ppl

Im making a snake game for my exam in programming. Its my first year programming so i still consider myself new in C++ and SFML. A classmate recommended me SFML. I read some of the tutorials and so far i coded this:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    int WindowWidth = 800;
    int WindowLength = 600;
    int PixelDepth = 32;
    char WindowTitle[] = "Snake";

    int BlockSize = 20;
    int SnakeLength=1;

    float FPS;
    int Direction = 'R';

    sf::RenderWindow App(sf::VideoMode(WindowWidth, WindowLength, PixelDepth), WindowTitle);

    sf::Clock WindowClock;

    // Load a sprite to display
    sf::Image TailImg;
    TailImg.LoadFromFile("tail.png");
    sf::Sprite TailSprite(TailImg);
    TailSprite.SetPosition(WindowWidth/2,WindowLength/2);

    sf::String DifficultyText;
    DifficultyText.SetFont(sf::Font::GetDefaultFont());
    DifficultyText.SetText("HEJ");
    DifficultyText.SetSize(30);
    DifficultyText.SetColor(sf::Color(128, 128, 0));
    DifficultyText.SetPosition(WindowWidth/2-DifficultyText.GetSize(),WindowLength-DifficultyText.GetSize());

    //App.SetFramerateLimit(30);

    while (App.IsOpened())
    {
        sf::Event WindowEvent;
        while (App.GetEvent(WindowEvent))
        {
            // Window closed
            if (WindowEvent.Type == sf::Event::Closed)
                App.Close();

            // Escape key pressed
            if ((WindowEvent.Type == sf::Event::KeyPressed) && (WindowEvent.Key.Code == sf::Key::Escape))
                App.Close();

            if ((WindowEvent.Type == sf::Event::KeyPressed) && (WindowEvent.Key.Code == sf::Key::Up))
            {
                Direction = 'U';
                TailSprite.Move(0,-BlockSize);
            }
            else if ((WindowEvent.Type == sf::Event::KeyPressed) && (WindowEvent.Key.Code == sf::Key::Down))
            {
                Direction = 'D';
                TailSprite.Move(0,BlockSize);
            }
            else if ((WindowEvent.Type == sf::Event::KeyPressed) && (WindowEvent.Key.Code == sf::Key::Right))
            {
                Direction = 'R';
                TailSprite.Move(BlockSize,0);
            }
            else if ((WindowEvent.Type == sf::Event::KeyPressed) && (WindowEvent.Key.Code == sf::Key::Left))
            {
                Direction = 'L';
                TailSprite.Move(-BlockSize,0);
            }
        }

        FPS = 1.f / WindowClock.GetElapsedTime();
        WindowClock.Reset();
        std::cout << FPS << std::endl;

        App.Clear(sf::Color(110, 235, 140));
        App.Draw(TailSprite);
        App.Draw(DifficultyText);
        App.Display();
    }

    return EXIT_SUCCESS;

}


Now Im at the point where I have to get TailSprite moving in a desired direction until another direction is chosen. How can i achieve that? I guess i need to use sf::Clock and some kind of function?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Advice/help with Snake game
« Reply #1 on: March 03, 2011, 12:52:03 am »
I think you don't need sf::Clock, the member function sf::RenderWindow::GetFrameTime() should be okay.

By the way, you can simplify your event handling:
Code: [Select]
// Window closed
            if (WindowEvent.Type == sf::Event::Closed)
                App.Close();

            else if (WindowEvent.Type == sf::Event::KeyPressed)
            {
                switch (WindowEvent.Key.Code)
                {  
                     case sf::Key::Down:
                        Direction = 'D';
                        TailSprite.Move(0,BlockSize);
                        break;
                     case ...
                         ...
                }
             }
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vkcelik

  • Newbie
  • *
  • Posts: 3
    • View Profile
Advice/help with Snake game
« Reply #2 on: March 03, 2011, 01:38:37 am »
Thanks for the tip. Corrected that. Ill try working on the direction thing later today. Too tired atm xD

vkcelik

  • Newbie
  • *
  • Posts: 3
    • View Profile
Advice/help with Snake game
« Reply #3 on: March 05, 2011, 01:28:48 am »
Quote from: "Nexus"
I think you don't need sf::Clock, the member function sf::RenderWindow::GetFrameTime() should be okay.


I couldn't find out how to use that if i wanted the snake to move with a certain interval. So I tried this and also got problems. I am getting a compiling error telling the clock, sprite and BlockSize in this function is not declared in this scope:

Code: [Select]
void MoveSnake()
{
    if(MovementClock.GetElapsedTime()>1)
        TailSprite.Move(-BlockSize,0);
}

 

anything