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

Author Topic: Sfml snake game  (Read 8704 times)

0 Members and 1 Guest are viewing this topic.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Sfml snake game
« on: July 10, 2014, 01:15:06 pm »
Game demo: http://www.youtube.com/watch?v=SGTD8ERWwvU

Also here is link for precompiled 32bit Linux version:
https://www.mediafire.com/?aia1o92vo3njn2d
« Last Edit: January 09, 2016, 06:05:09 pm by paupav »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Sfml snake game
« Reply #1 on: July 10, 2014, 01:35:59 pm »
It's a good start I guess but I urge you to learn the object oriented programming paradigm else your code will always be a mess. There are huge design horrors put in place here ???

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Sfml snake game
« Reply #2 on: July 10, 2014, 01:48:37 pm »
It's a good start I guess but I urge you to learn the object oriented programming paradigm else your code will always be a mess. There are huge design horrors put in place here ???
So, are there horrors that are not marked with fuzzy?

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Sfml snake game
« Reply #3 on: July 10, 2014, 01:54:24 pm »
Pretty much anything. First of all all the code is in the main() function. I've seen simpler code in assembly. Classes are a must in oop design

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sfml snake game
« Reply #4 on: July 10, 2014, 01:57:19 pm »
Everything is declared miles before usage. Declare variables as late as possible.
Use containers instead of enumerated variables.
Split code to functions and classes.
Reduce the masses of code duplication (whenever two parts of the code look similar, merge them).

The whole code could be written in a third of the space if you followed those points.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sfml snake game
« Reply #5 on: July 17, 2014, 09:28:38 pm »
Why the stringstream 'convert'? Why not simply std::to_string()?

Why are you declaring a ton of "sf::RectangleShape bodyPartXXX" variables? Your snake will grow to indefinite length. You can't possibly pre-declare variables for all its body parts. Just use a standard container like std::vector, std::deque or std::list to hold the body parts and then allocate them on demand when the snake grows.

This:
       // #, fuzzy
        part17Pos = part16Pos;

        part16Pos = part15Pos;
        part15Pos = part14Pos;
        part14Pos = part13Pos;
is just crazy...

Why is this:
   sf::Vector2i res(400, 400);
 
(and loads more) not "const"?

            if(event.key.code == sf::Keyboard::Return)
                goto start;
 
seriously?  No!  You should be able to do better than that. There are use-cases for goto, but they are very few and very far between. I've personally only ever had to use it once or twice in the last 10 years and that was in some code where me and a coworker could prove that the "nice" version of the code unfortunately executed in 3 times the time than the code with the goto (and it happened to be performance critical), so unfortunately we had to go with the goto version rather than the readable version :(  but that's rare - stay away from goto, forget it exists in the language.

    srand(time(0));
No. Don't use srand - use the nice new stuff provided in the "<random>" header.

    gameOverRat.x = res.x / (float)gameOverSize.x;
    gameOverRat.y = res.y / (float)gameOverSize.y;
 
Don't use C-style casts. Use C++-style casts (static_cast, dynamic_cast, const_cast, reinterpret_cast) - yes, they are longer to type and ugly to read, but that's a good thing. It means you will be aware of them and try not to use them. Casts can be a needed evil but they should generally be avoided and are usually a sign of a design problem in the code. C++ casts are much easier to spot than C casts - never use C-style casts.

    //show bodyparts,,, if head touches tail   #, fuzzy
    if(snakeSize >= 1)
    {
        if(snakePos == part1Pos)
            gameOver = true;
        window.draw(bodyPart1);
    }
    if(snakeSize >= 2)
    {
        if(snakePos == part2Pos)
            gameOver = true;
        window.draw(bodyPart2);
    }
    if(snakeSize >= 3)
    {
        if(snakePos == part3Pos)
            gameOver = true;
        window.draw(bodyPart3);
    }
 
More ridiculous code. You really need to learn about loops!

I can recommend ClangFormat if you want to format your code consistently - makes it easier for everyone to read (including you) :)

And please use some classes and/or functions. Everything just crammed into main() is a certain recipe for a mess.
« Last Edit: July 17, 2014, 09:31:27 pm by Jesper Juhl »

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Sfml snake game
« Reply #6 on: July 17, 2014, 09:45:26 pm »
Why the stringstream 'convert'? Why not simply std::to_string()?

Why are you declaring a ton of "sf::RectangleShape bodyPartXXX" variables? Your snake will grow to indefinite length. You can't possibly pre-declare variables for all its body parts. Just use a standard container like std::vector, std::deque or std::list to hold the body parts and then allocate them on demand when the snake grows.

This:
       // #, fuzzy
        part17Pos = part16Pos;

        part16Pos = part15Pos;
        part15Pos = part14Pos;
        part14Pos = part13Pos;
is just crazy...

Why is this:
   sf::Vector2i res(400, 400);
 
(and loads more) not "const"?

            if(event.key.code == sf::Keyboard::Return)
                goto start;
 
seriously?  No!  You should be able to do better than that. There are use-cases for goto, but they are very few and very far between. I've personally only ever had to use it once or twice in the last 10 years and that was in some code where me and a coworker could prove that the "nice" version of the code unfortunately executed in 3 times the time than the code with the goto (and it happened to be performance critical), so unfortunately we had to go with the goto version rather than the readable version :(  but that's rare - stay away from goto, forget it exists in the language.

    srand(time(0));
No. Don't use srand - use the nice new stuff provided in the "<random>" header.

    gameOverRat.x = res.x / (float)gameOverSize.x;
    gameOverRat.y = res.y / (float)gameOverSize.y;
 
Don't use C-style casts. Use C++-style casts (static_cast, dynamic_cast, const_cast, reinterpret_cast) - yes, they are longer to type and ugly to read, but that's a good thing. It means you will be aware of them and try not to use them. Casts can be a needed evil but they should generally be avoided and are usually a sign of a design problem in the code. C++ casts are much easier to spot than C casts - never use C-style casts.

    //show bodyparts,,, if head touches tail   #, fuzzy
    if(snakeSize >= 1)
    {
        if(snakePos == part1Pos)
            gameOver = true;
        window.draw(bodyPart1);
    }
    if(snakeSize >= 2)
    {
        if(snakePos == part2Pos)
            gameOver = true;
        window.draw(bodyPart2);
    }
    if(snakeSize >= 3)
    {
        if(snakePos == part3Pos)
            gameOver = true;
        window.draw(bodyPart3);
    }
 
More ridiculous code. You really need to learn about loops!

I can recommend ClangFormat if you want to format your code consistently - makes it easier for everyone to read (including you) :)

And please use some classes and/or functions. Everything just crammed into main() is a certain recipe for a mess.
Thanks you so much, I've created new game and not everything is in the main. I still don't get it how can I use vector to create infinite objects. Can you please provide example of using vector with sf::(some)Object.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sfml snake game
« Reply #7 on: July 17, 2014, 09:52:14 pm »
Please avoid the full quote, especially if it's so long...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sfml snake game
« Reply #8 on: July 17, 2014, 09:53:53 pm »
... I still don't get it how can I use vector to create infinite objects. Can you please provide example of using vector with sf::(some)Object.
Take a look at my simple Jukebox class that I've posted on the wiki. It reads in a list of song files from a directory to a dynamic container. It also dynamically manages a container of songs in the current playlist of songs to play. Reading that should be enough to show you how to use containers at their most basic.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Sfml snake game
« Reply #9 on: July 21, 2014, 08:23:53 pm »
Thanks I've cloned git SFML.wiki and it's awesome, thanks!

 

anything