Skip to content
question

my snake game snake doesnt die when it eats itself?? kya galat hai ๐Ÿ˜…

Aman Raj@kota_coder_17

October 21, 2026 ยท 2 min read

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 = pygame.time.Clock()

x, y = 300, 200
dx, dy = 0, 0
snake_list = []
snake_len = 1
game_over = False

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                dx, dy = -10, 0
            elif event.key == pygame.K_RIGHT:
                dx, dy = 10, 0
            elif event.key == pygame.K_UP:
                dx, dy = 0, -10
            elif event.key == pygame.K_DOWN:
                dx, dy = 0, 10

    x += dx
    y += dy
    snake_head = [x, y]
    snake_list.append((x, y))
    if len(snake_list) > snake_len:
        del snake_list[0]

    for block in snake_list[:-1]:
        if block == snake_head:
            game_over = True

    win.fill((0, 0, 0))
    for bx, by in snake_list:
        pygame.draw.rect(win, (0, 255, 0), (bx, by, 10, 10))
    pygame.display.update()
    clock.tick(15)

pygame.quit()

sorry if indentation is little weird, i copied from laptop to phone. please dont tell my mummy i am doing this instead of rotational motion ๐Ÿ™

Accepted answer

Anitha Joseph#5 in Indie Game Graveyard6 likes

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? ๐Ÿ˜Š

View in the thread

Get the best of #indie-game-graveyard weekly

One email each Monday with the most useful posts. No spam; unsubscribe in one click.

Comments

11

Sign in to join the conversation.

Sign in to comment

Accepted answer

Anitha Joseph@anitha_miss#5 in Indie Game Graveyard
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? ๐Ÿ˜Š
6 net votes
Aman Raj@kota_coder_17#4 in Indie Game Graveyard
MISS IT WORKED ๐Ÿ˜ญ๐Ÿ˜ญ snake died first try. i never knew tuple and list are not equal. thank you so much!! rotational motion tomorrow promise
0 net votes
Harshit Gangwar@harshit_20051 rep in Indie Game Graveyard
bhai wall pe bhi collision nahi hai, snake screen ke bahar chala jayega ๐Ÿ˜‚ add this: if x < 0 or x >= 600 or y < 0 or y >= 400: game_over = True
2 net votes
Aman Raj@kota_coder_17#4 in Indie Game Graveyard
haan bhaiya it was going outside and never coming back lol. adding
0 net votes
Nikhil Sharan@nikhil_twopointers1 rep in Indie Game Graveyard
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? ๐Ÿ˜„
4 net votes
Aman Raj@kota_coder_17#4 in Indie Game Graveyard
NIKHIL BHAIYA REPLIED ๐Ÿ˜ญ๐Ÿ˜ญ๐Ÿ˜ญ taking screenshot. yes bhaiya physics first. will block opposite direction tomorrow
0 net votes
Tanvi Hegde@tanvi_makes_games#3 in Indie Game Graveyard
this is how it starts kota ~ next step: make the snake a cat ๐Ÿˆ (and listen to anitha miss about the physics)
1 net votes
Olivier Gagnon@ogagnon#2 in Indie Game Graveyard
Bienvenue ! My first game was also a snake, in QBasic. Now I make a fishing game where the fish stares at you. Be careful, it never stops ๐Ÿ˜„
3 net votes
Max Petrosyan@max_20102 rep in Indie Game Graveyard
bro same, my first minigame was a snake in a minecraft mod lol. the tuple thing got me too fr
1 net votes
Jaxon Pearce@j4x0n_mods1 rep in Indie Game Graveyard
the immortal snake is lowkey a better game ngl ๐Ÿ’€
1 net votes
Aman Raj@kota_coder_17#4 in Indie Game Graveyard
haha maybe i make 2 modes, immortal mode and normal mode ๐Ÿ˜…
2 net votes