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

Author Topic: Simple Snake Game  (Read 8095 times)

0 Members and 1 Guest are viewing this topic.

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Simple Snake Game
« on: January 01, 2016, 06:00:19 pm »
hello everybody  :)

i was working recently on this project. after the project cleaned and tested, it looks fine for now to post it for educational purpose to beginners like me ;D
i have seek help just to make  this project as good as possible to be worthy as educational. lastly i apologize that source code lacks of comments but i think it is quite easy to grasp.
Also, i have implemented smooth movements for the snake to give realistic impression instead of tile movements as most of tutorials in snake-like games do.

gameplay
gameplay is simply if snake ate itself the lives decreased and it shrinks to where it gets bitten. but if snake hit the walls the game ends and  immediately, it restarts again.

source code: https://github.com/MORTAL2000/Snake

Demo:
http://www.youtube.com/watch?v=BIIvjJfFf8U
« Last Edit: January 03, 2016, 06:36:37 pm by MORTAL »

Hapax

  • Hero Member
  • *****
  • Posts: 2776
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Simple Snake Game for Beginners
« Reply #1 on: January 01, 2016, 10:54:04 pm »
Never could bring myself to remake Snake...  ;D

if snake ate itself the lives decreased and it shrinks to where it gets bitten.
It would be good if your video showed an example of the part where it bites off its tail.
I'm not sure this is a good mechanic, though, since the avoidance of its own tail should be the main difficulty.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game for Beginners
« Reply #2 on: January 01, 2016, 11:31:56 pm »
Never could bring myself to remake Snake...  ;D
well it happens  ;D

It would be good if your video showed an example of the part where it bites off its tail.
i edited the demo vid now it's clear that it shrinks when it bites itself.

I'm not sure this is a good mechanic, though, since the avoidance of its own tail should be the main difficulty.
yeah, that might be considered if this demo turned to real game
« Last Edit: January 01, 2016, 11:54:16 pm by MORTAL »

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game for Beginners
« Reply #3 on: January 02, 2016, 04:05:40 am »
update
rewrote the snake class to be relied on radian and degree instead of enum of directions for computing positions and direction comparison.

Hapax

  • Hero Member
  • *****
  • Posts: 2776
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Simple Snake Game for Beginners
« Reply #4 on: January 02, 2016, 12:56:29 pm »
I'm not sure this is a good mechanic, though, since the avoidance of its own tail should be the main difficulty.
yeah, that might be considered if this demo turned to real game
I actually think the idea is interesting. If you do develop it further into a game, I'm curious as to your idea to make it work  :)

i edited the demo vid now it's clear that it shrinks when it bites itself.
Video is better now.
Why does the snake die at 1:28?
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game for Beginners [finished]
« Reply #5 on: January 02, 2016, 01:37:45 pm »
I actually think the idea is interesting. If you do develop it further into a game, I'm curious as to your idea to make it work  :)
i would love to develop it further, but the main obstruction for me is the resources files like textures and sounds and etc.
i couldn't make them by myself. barelyو i could programming with great difficulty like this project for example, it took me 1 week to reach this far which is considered not acceptable for this kind of games. i'm still learning c++/Open-GL-SFML and the road seems to be a long way to get decent knowledge about it. also, i'm worried since you mentioned to someone in other post that we should be caution of violates the copyright and licences for any resources.

Why does the snake die at 1:28?
yeah, that happened because the snake's lives reached zero.

Hapax

  • Hero Member
  • *****
  • Posts: 2776
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Simple Snake Game for Beginners [finished]
« Reply #6 on: January 02, 2016, 06:09:17 pm »
Why does the snake die at 1:28?
yeah, that happened because the snake's lives reached zero.
It loses a life each time it bites off its tail? This answers all of my previous questions  ;D

i would love to develop it further, but the main obstruction for me is the resources files like textures and sounds and etc.
i couldn't make them by myself.
[...]
also, i'm worried since you mentioned to someone in other post that we should be caution of violates the copyright and licences for any resources.
Have a look at OpenGameArt for free resources.
Also, for sound effects, try Bfxr (a sound effect generator - you can do it all on the website).
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game [Finished]
« Reply #7 on: January 02, 2016, 08:38:39 pm »
It loses a life each time it bites off its tail? This answers all of my previous questions  ;D
hahaha , yeah that's what i want to say but my engilsh failed me  ;D


Have a look at OpenGameArt for free resources.
Also, for sound effects, try Bfxr (a sound effect generator - you can do it all on the website).
thanks for links i will look at it.

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game
« Reply #8 on: January 04, 2016, 04:53:09 pm »
when the game finished and cleaned up. i thought it was perfect and nothing can be added to this game but after reviewing the code, it turns out there are many things need to reconsidered in both c++ and SFML implementations.
i rewrote the game once again and corrected the parts which is need to be improved. i believe this time it is much better than before.


Elias Daler

  • Hero Member
  • *****
  • Posts: 587
    • View Profile
    • Blog
    • Email
Re: Simple Snake Game
« Reply #9 on: January 04, 2016, 11:36:55 pm »
Nice project. I'll take a look at the code later, but for now I can advice you one thing: make commit messages meaningful, this may save you lots of time in the future ("When did I change X from Y?")
Tomb Painter, Re:creation dev | eliasdaler.github.io | @EliasDaler | Tomb Painter dev log

paupav

  • Full Member
  • ***
  • Posts: 155
    • View Profile
    • Email
Re: Simple Snake Game
« Reply #10 on: January 04, 2016, 11:57:46 pm »
I don't wanna sound like a smartass especially because I did same thing with my snake game, but you should split code in multiple files. Also you should avoid global functions.

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game
« Reply #11 on: January 05, 2016, 01:06:16 pm »
Nice project. I'll take a look at the code later, but for now I can advice you one thing: make commit messages meaningful, this may save you lots of time in the future ("When did I change X from Y?")
thanks Elias really i appreciated, yes you are absolutely right the repo is indeed chaos and i don't use message for a commit. i don't know how to fix that but i will find a way to organize it.

I don't wanna sound like a smartass especially because I did same thing with my snake game, but you should split code in multiple files. Also you should avoid global functions.
you are most welcome. feel free to point out any error or bad habit related to code. actually the main reason for posting source code is to get feedback like is code free of bugs, what the best practice for both c++/SFML ... etc.
and for split code in multiple files, yes i'm totally agree with you. it is my bad habit i got it when i was learning c++ from free online tutorial and still stuck with me :-\
and for avoid global functions, it is okay to use free function in c++ even better to use it with namespace which is useful to reduce potential name collisions with other libraries.
 

paupav

  • Full Member
  • ***
  • Posts: 155
    • View Profile
    • Email
Re: Simple Snake Game
« Reply #12 on: January 09, 2016, 06:09:16 pm »
Quote from: Nexus
You should find a design that works without global variables.
http://fr.sfml-dev.org/forums/index.php?topic=16574.0

Hapax

  • Hero Member
  • *****
  • Posts: 2776
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Simple Snake Game
« Reply #13 on: January 09, 2016, 07:34:00 pm »
Global variables are not the same as global/free functions.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Mortal

  • Full Member
  • ***
  • Posts: 246
    • View Profile
Re: Simple Snake Game
« Reply #14 on: January 09, 2016, 08:03:33 pm »
Quote from: Nexus
You should find a design that works without global variables.
http://fr.sfml-dev.org/forums/index.php?topic=16574.0
thanks for the links, yeah that is very true for global variable.

i think you are confused between global variables and global functions. global functions are sometime called free functions which is i was referring to in my last reply.

i have removed all global variables from source code. also, i split code in multiple files as your suggestions. and for global functions i made utility files to warp them in a namespace like this:
namespace utility
{
        using DistType = std::uniform_real_distribution<float>;

        auto randomEngine();

        float random(DistType& dist);

        float radian(std::size_t direction);

        float degree(std::size_t direction);
}
« Last Edit: January 09, 2016, 09:10:31 pm by MORTAL »