my snake game snake doesnt die when it eats itself?? kya galat hai ๐
Aman Raj@kota_coder_17
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
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? ๐
Get the best of #indie-game-graveyard weekly
One email each Monday with the most useful posts. No spam; unsubscribe in one click.
Comments
Sign in to join the conversation.
Sign in to commentAccepted answer
Anitha Joseph@anitha_miss
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? ๐
Aman Raj@kota_coder_17
MISS IT WORKED ๐ญ๐ญ snake died first try. i never knew tuple and list are not equal. thank you so much!! rotational motion tomorrow promise
Harshit Gangwar@harshit_2005
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
Nikhil Sharan@nikhil_twopointers
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? ๐
Aman Raj@kota_coder_17
NIKHIL BHAIYA REPLIED ๐ญ๐ญ๐ญ taking screenshot. yes bhaiya physics first. will block opposite direction tomorrow
Tanvi Hegde@tanvi_makes_games
this is how it starts kota ~ next step: make the snake a cat ๐ (and listen to anitha miss about the physics)
Olivier Gagnon@ogagnon
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 ๐
Max Petrosyan@max_2010
bro same, my first minigame was a snake in a minecraft mod lol. the tuple thing got me too fr