my snake game snake doesnt die when it eats itself?? kya galat hai π
bhaiya didi help π i made snake game in python (pygame) at night after physics. its working mostly but when snake eats itself it doesnt die?? it just goes through itself like ghost. kya ye sahi hai?? my friend says my snake is immortal lol here is the code (food wala part i removed to make it short, when it eats food i do snake_len += 1) import pygame pygame.init() win = pygame.display.set_mode((600, 400)) clock =β¦
Accepted answer
Kota, very good effort, and a very good bug. Small correction needed in one line. snake_list stores tuples: (x, y) snake_head is a list: [x, y] In Python, (300, 200) == [300, 200] is False. A tuple and a list are never equal, even with the same numbers inside. So your check never becomes True and your snake is immortal. Fix: make them the same type. Change snake_head = [x, y] to snake_head = (x, y) and then append snake_head itself. Try it in the Python shell first: type (1, 2) == [1, 2] and see. Now please also finish the rotational motion chapter, alle? π- Good bug, Kota. Everybody learns tuple vs list this way. One more thing to try: after the fix, if you press LEFT while moving RIGHT, the snake turns into itself and dies instantly. Block the opposite direction. Dry run it on paper first. And yes, physics first, samjhe? π