Python Snake Game - Missing Snake

My snake does not show on the screen. only the food : ).

Can someone tell me what did I miss?

import random
import curses

screen = curses.initscr()
# Hide mouse cursor
curses.curs_set(0)
# get maximum screen height and width
screenHeight, screenWidth = screen.getmaxyx()
# create a new window
window = curses.newwin(screenHeight, screenWidth, 0, 0)
# allow window to recieve input from keyboard
window.keypad(True)
# Set the delay for updating the screen
window.timeout(125)
# Set the x, y coordinators of the intial position of the screen
snake_x = screenWidth // 4
snake_y = screenHeight // 2
# Define the intial position of the snake body
snake = [
  [snake_y, snake_x],
  [snake_y, snake_x-1],
  [snake_y, snake_x-2]]

# Create the food in the middle of the window
food = [screenHeight // 2 , screenWidth // 2]
# Add food by using PI character from curses module

window.addch(food[0], food[1], curses.ACS_PI)
# Set intial movement direction to right
key = curses.KEY_RIGHT
# Create a game loop that loops forever until the player loses or quits
while True:

# Get the next key that will be pressed by user
  next_key = window.getch()
# If the user does not input anything, the key stays the same, else key will be set to the next pressed key
  key = key if next_key == -1 else next_key

# Check if the snake collided with the wall or itself
if snake[0][0] in [0, screenHeight] or snake[0][1] in [0, screenWidth] or snake[0] in snake [1:]:
  curses.endwin()
  quit()
# Set the new position of the snake head based on direction
new_head = [snake[0][0],snake[0][1]]

if key == curses.KEY_DOWN:
  new_head[0] += 1
if key == curses.KEY_UP:
  new_head[0] -= 1
if key == curses.KEY_RIGHT:
  new_head[1] += 1
if key == curses.KEY_LEFT:
  new_head[1] -= 1

# Insert new head in the first positionn of snake list
snake.insert(0, new_head)

# Check if snake ate the food
if snake[0] == food:
  food = None
# while food is removed, generate food in a random place on screen
  while food is None:
    new_food = [
      random.randint(1, screenHeight-1), 
      random.randint(1, screenWidth-1)
    ]
#set the food to new food if new food generated is not on snake
    food = new_food if new_food not in snake else None
  window.addch(food[0], food[1], curses.ACS_PI)
else:
  tail = snake.pop()
  window.addch(tail[0], tail[1], " ")


window.addch(snake[0][0], snake[0][1], curses.ACS_BLOCK)
1 Like

After correctly formatting your post like so:

import random
import curses

screen = curses.initscr()

curses.curs_set(0)

screenHeight, screenWidth = screen.getmaxyx()

window = curses.newwin(screenHeight, screenWidth, 0, 0)

window.keypad(True)

window.timeout(125)

snake_x = screenWidth // 4
snake_y = screenHeight // 2

snake = [
[snake_y, snake_x],
[snake_y, snake_x-1],
[snake_y, snake_x-2]]

food = [screenHeight // 2 , screenWidth // 2]

window.addch(food[0], food[1], curses.ACS_PI)

key = curses.KEY_RIGHT

while True:

next_key = window.getch()

key = key if next_key == -1 else next_key

if snake[0][0] in [0, screenHeight] or snake[0][1] in [0, screenWidth] or snake[0] in snake [1:]:
    curses.endwin()
    quit()

Set the new position of the snake head based on direction
new_head = [snake[0][0],snake[0][1]]

if key == curses.KEY_DOWN:
    new_head[0] += 1
if key == curses.KEY_UP:
    new_head[0] -= 1
if key == curses.KEY_RIGHT:
   new_head[1] += 1
if key == curses.KEY_LEFT:
    new_head[1] -= 1

snake.insert(0, new_head)

if snake[0] == food:
    food = None

while food is None:
    new_food = [
        random.randint(1, screenHeight-1),
        random.randint(1, screenWidth-1)
]
#set the food to new food if new food generated is not on snake
food = new_food if new_food not in snake else None
window.addch(food[0], food[1], curses.ACS_PI)
else:
    tail = snake.pop()
    window.addch(tail[0], tail[1], " ")

window.addch(snake[0][0], snake[0][1], curses.ACS_BLOCK)

I fixed as many indentation errors as I could find (PS. Tab everything under while True), but was left EXTREMELY confused as to these kinds of lines:
key = key if next_key == -1 else next_key
could you please break down the syntax of this?

I would expect this is like

if next_key != -1:
  key = next_key
1 Like

Still quote some confusing errors of indentation after the while True.

@Ranabehery can you share the repl so we can fork it and see better?

1 Like

Thank you for your time. I am still a beginner as you can see.

I meant to say that if the user did not click on anything the key should remain the same thus the snake will keep moving to the right otherwise if there is a new entry it should be set to the new pressed key saved in the next_key we created

The line of code you asked about should mean the following too:

if next_key == -1:
key = key
else:
key = next_key

Here is the link:
https://replit.com/@Ranabehery/SnakeGame?v=1

If this is the first time you try this (and for some reason with curses instead the simple turtle) maybe you should try a tutorial and see how that works?

for example:

https://theailearner.com/2019/03/10/snake-game-using-python-curses/

Thank you very much!

https://replit.com/@arhanansari2009/Python-Projects?v=1
Open show code and then select caterpillar.py and see the code