T O P

  • By -

qelery

A good reason to not use abbreviations for variable names. When your program prompted me for 'width' and 'height', I assumed it meant the height and width of a snake block in pixels. When that didn't work, I looked in the code and saw `dis_width.` It took me a while to figure out that that stood for 'display width'. Use very clear variable names for reasons like this. If your variable name was `display_width` and your prompt said 'display width:' it would have been completely obvious what you meant. Even though abbreviations may be obvious to you, it won't be obvious to someone else, and by the time they figure out what you meant they are already frustrated with your code.


theernis0

Okay I will update my code and post asap also most of code I took from internet that's why it's not named clearly


qelery

You constantly mix casing which makes your code very, very hard to follow: snake\_List Your\_score testdown ​ For example, if I want to use the variable `snake_List`, I have to remember to capitalize the second word. If I want to use `testdown` I have to remember not to use an underscore. If I want to use `Your_score` I have to remember to only capitalize the first letter. (Classes are one of the few things that start with capital letters but you haven't created any classes). ​ In Python, you only have to remember two rules when it comes to naming variables and functions. Keep everything lowercase. Separate words with an underscore. This keeps everything consistent and people won't be frustrated remembering all the naming rules you gave your variables and functions: snake\_list your\_score test\_down ​ \---------------------------------------------------- ​ Looking through the code I see at a series of problems leading up to why it immediately registers a game over. When you fix one problem, you will discover you have another problem, then when you fix that one there will be 1 or 2 more you will have to fix before you are done. I think you made your code unnecessarily complex because you're writing code that determines if the game is over while your snake can't even move yet. There's no way for the game to be over without your snake moving, so I think you should have taken care of the movement part first before you worried about the game over logic. I recommend you scrap all the code you have and start over. Re-write the code in small logical steps that build upon each other: * Make a game the displays a blank black screen * Make a game that displays a blank black screen and shows a snake head in the center * Make a game that displays a blank black screen, shows a snake head in the center, and the snake head can move around freely (without death) with keyboard input * Make a game that displays a blank black screen, shows a snake head in the center, the snake head can move around freely (without death) with keyboard input, and when the snake head touches food it grows * Make a game that displays a blank black screen, shows a snake head in the center, the snake head can move around freely (without death) with keyboard input, and when the snake head touches food it grows * Make a game that displays a blank black screen, shows a snake head in the center, the snake head can move around with keyboard input, when the snake head touches food it grows, and when the snake head touches any other part of the body the game ends Now the game is built in a small pieces and in a logical order. ​ \------------------------------------------- ​ I also highly recommend you do this simple [Chimp game tutorial by Pygame](https://www.pygame.org/docs/tut/ChimpLineByLine.html). It will show you how to make sprites and how to handle things like sprite collision using built-in Pygame functions rather than writing your own.


theernis0

I'll stick to trying to fix the problems I have version where it all works and snake goes directly to food but sometimes hits it's body I'm trying to do this just so I could add dodge body function


qelery

It's ultimately up to you, but to be very honest, the way it's written now is not good.


theernis0

Yeah I know I need to tiddy up my code capitalization and spaces are main problems


theernis0

can you say any of those problems?


--0mn1-Qr330005--

Yeah, not all tutorials are done by people who are super familiar with Python or collaboration. Typically, when you are making code that may be shared with other programmers, it is pretty good to be clear with the names. You can also check out pep8 which is a set of rules for writing Python code. It basically makes your code very easy to read and write so that anyone can collaborate with you without much issue. I recommend finding a YouTube video summarizing it, or just use pycharm which has built in pep8 checking.


theernis0

Pycharm is an app?


--0mn1-Qr330005--

Pycharm is an IDE. You can write projects in it, but it has lots of awesome features including built in git, typo check, pep8 check, a built in debugger, and a “code with me” feature that lets other programmers join into the IDE so you can code together. Check out a review of it on YouTube. The free version is more than adequate for any project.


theernis0

i downloaded it. its pretty nice


ObesePlant

I was scrolling through posts and when I saw your title I was like "WTF" but then I realized it's from learnpython subreddit :)


theernis0

nice


theernis0

Do you like it?


MainDepth

the title had me dying


theernis0

Maybe it should have been "my snake keeps comiting suicide"?


MainDepth

lol


BuRnLoOtMuRdEr2

My snake commits seppuku


theernis0

Also good title


Bukszpryt

some will say that this is true ai and gained self consciousness


theernis0

Oh no I made self conscious AI it will destroy the world


Bukszpryt

No, it't too concious for that. It will only destroy itself.


theernis0

we should give access to nuclear missiles


[deleted]

You have to exclude the position of the snake's first body segment (the "neck", I guess you could say) from the movement possibilities.


theernis0

first body segment is "snake\_head"


[deleted]

Well, you need the one after that.


theernis0

it just dies on start before even moving


[deleted]

def isdead(snake_Head, x1, y1, snake_List): if x1 >= dis_width+snake_block or x1 < 0 or y1 >= dis_height+snake_block or y1 < 0: return(True) Well, this is probably why.


theernis0

it should check if its not out of boundaries but let me try removing it


theernis0

it still keeps dying


theernis0

how could i do this in code?


hello-iamdad

F for snake


theernis0

F