1
SFML projects / Re: Yet Another Snake Clone
« on: August 24, 2017, 11:56:51 am »
Alright, FRex. I cannot thank you enough for your valuable feedback. I'll try to keep all these things in mind.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
names in all caps and starting with _ shouldn't be used (but many projects do it to not clutter auto complete with them, yes), see: https://stackoverflow.com/a/228797I am trying to understand everything in that post but I think I get the point. In my case, my header guards should not have started with an underscore. Did I get that right?
many people use hpp for C++ headers to signify they aren't C headers, if you ever mix C and C++ or happen to use a library that has binding/APIs in both (i.e. like SFML does) then you will appreciate the differenceAlright, its .hpp from now on.
you could use sf::RenderTaraget, not sf::RenderWindow in draw functionsSo, it can be like
drawableObject.drawTo(sf::RenderTarget &window);
Can you tell me the difference it will make?some magic numbers are present, ie. 735 and 375 in Food.cppNoted: Hard coding should be avoided.
it's customary and nice towards the reader to name the file with main function main.cppMakes sense.
you might want to look into c++ <random> header instead of using C randomnessWell, I tried using that but it gave me same number pattern when the game restarts. May be I should have researched more to find a way to put current time as a seed in c++11 random generator.
your key handling is wrong, you should use evnt.key.code to see which key got pressed in sf::Event::KeyPressedWell, thanks for pointing this out. I didn't know this. event.key.code for polling event and sf::Keyboard::isKeyPressed for real time key capturing.
you might actually reconsider using numeric keypad and use normal numeric keys (Num1, etc.) since many older laptops don't have itHahaha.. damn my short-sightedness.
you might make spawn take snake by reference if it's never nullErr. I am confused.
setFillColor is weird, why the & and then -> ?I needed to do that in order to change the color of existing blocks. I also used the following code initially, but it didn't work.
snake[i].setFillColor
vector has a 'back' member that you can use to access the last elementI should have gone through the std::vector documentation properly. Well, I learned something new. Thanks
in drawTo you use a for loop but without a & so you copy each block, then render it, then throw it awayThat is why code reviewing is necessary. I'll be cautious of this in future practices.
deletePlayer and createPlayer are a bad interface, just clear the snake on each player creationAre you suggesting I should have handled creation and clearing of vector through Player's constructor? If yes, then in my case I would have to create two player objects in snake.cpp: once at the start of the game before the game loop and then once after the game is over if player chose to retry the game. I can definitely see the problem of structuring in my snake.cpp now. May be, I should have added pressing space to start the game for the first time as well. Good point.
You seem to have forgotten stdafx.h in the repo, it's also not a very usual/portable feature (I know it's in VS, not sure about it on GCC/Clang, I think it's present but I don't know how it works) so it's best avoided IMO.
At a glance the code looks okay but it's a bit messy C++, I'll try to take a better look at it later.Any guidance is awaited and will be appreciated. Thanks.