Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Snakes Game using Python
# SNAKES GAME
# Use ARROW KEYS to play, SPACE BAR for pausing/resuming and Esc Key for exiting
import curses
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
from random import randint
curses.initscr()
win = curses.newwin(20, 60, 0, 0)
win.keypad(1)
curses.noecho()
curses.curs_set(0)
win.border(0)
win.nodelay(1)
key = KEY_RIGHT # Initializing values
score = 0
snake = [[4,10], [4,9], [4,8]] # Initial snake co-ordinates
food = [10,20] # First food co-ordinates
win.addch(food[0], food[1], '*') # Prints the food
while key != 27: # While Esc key is not pressed
win.border(0)
win.addstr(0, 2, 'Score : ' + str(score) + ' ') # Printing 'Score' and
win.addstr(0, 27, ' SNAKE ') # 'SNAKE' strings
win.timeout(150 - (len(snake)/5 + len(snake)/10)%120) # Increases the speed of Snake as its length increases
prevKey = key # Previous key pressed
event = win.getch()
key = key if event == -1 else event
if key == ord(' '): # If SPACE BAR is pressed, wait for another
key = -1 # one (Pause/Resume)
while key != ord(' '):
key = win.getch()
key = prevKey
continue
if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]: # If an invalid key is pressed
key = prevKey
# Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
# This is taken care of later at [1].
snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])
# If snake crosses the boundaries, make it enter from the other side
if snake[0][0] == 0: snake[0][0] = 18
if snake[0][1] == 0: snake[0][1] = 58
if snake[0][0] == 19: snake[0][0] = 1
if snake[0][1] == 59: snake[0][1] = 1
# Exit if snake crosses the boundaries (Uncomment to enable)
#if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break
# If snake runs over itself
if snake[0] in snake[1:]: break
if snake[0] == food: # When snake eats the food
food = []
score += 1
while food == []:
food = [randint(1, 18), randint(1, 58)] # Calculating next food's coordinates
if food in snake: food = []
win.addch(food[0], food[1], '*')
else:
last = snake.pop() # [1] If it does not eat the food, length decreases
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '#')
curses.endwin()
print("\nScore - " + str(score))
print("http://bitemelater.in\n")
@ortem

This comment has been minimized.

Copy link

ortem commented Nov 14, 2013

Nice game!

@swdevbali

This comment has been minimized.

Copy link

swdevbali commented Aug 5, 2015

Thanks! A perfect neat example on the usage of ncurses with Python! (y)

@erfansami11

This comment has been minimized.

Copy link

erfansami11 commented Jan 21, 2016

(y)

@SamPovilus

This comment has been minimized.

Copy link

SamPovilus commented Apr 19, 2016

This is awesome, thanks for posting this.

@yeonghoey

This comment has been minimized.

Copy link

yeonghoey commented Jun 16, 2016

Great work as an example code for using curses. Thanks!

@niaz11

This comment has been minimized.

Copy link

niaz11 commented Oct 29, 2016

i can`t run this program.
when i want to run this program i faces this problem:

Traceback (most recent call last):
File "D:\Python\program(game)\2158089-5f3d0003801acfe1a29c4b24f2c8975efacf6f66\snake.py", line 10, in
stdscr=curses.initscr()
File "C:\Users\niaz\AppData\Local\Programs\Python\Python35-32\lib\curses__init__.py", line 30, in initscr
fd=_sys.stdout.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'

@avinash321

This comment has been minimized.

Copy link

avinash321 commented Apr 7, 2017

thank you

@TomShenGoGo

This comment has been minimized.

Copy link

TomShenGoGo commented Aug 1, 2017

nice example for a beginner

@CloneOfDaClown

This comment has been minimized.

Copy link

CloneOfDaClown commented Aug 11, 2017

why I cant import key_right e.t.c. they dont turn blue

@ohari5336

This comment has been minimized.

Copy link

ohari5336 commented Aug 26, 2017

I love this game

@chuckcartergaming

This comment has been minimized.

Copy link

chuckcartergaming commented Oct 17, 2017

This won't work for me it just closes whenever I open it

@bob-da-dood

This comment has been minimized.

Copy link

bob-da-dood commented Oct 25, 2017

i can have version 2.7.13

@cahyonugros

This comment has been minimized.

Copy link

cahyonugros commented Oct 26, 2017

nice game...
another game can i try maybe ,,,,

@Faizy51

This comment has been minimized.

Copy link

Faizy51 commented Nov 10, 2017

how to install curses on windows?

@Awesomewrcker

This comment has been minimized.

Copy link

Awesomewrcker commented Nov 11, 2017

someone, please help I keep having this problem: Traceback (most recent call last):
File "C:\Users\scheh_cujwioq\AppData\Local\Programs\Python\Python36-32\Snake.py", line 4, in
import curses
File "C:\Users\scheh_cujwioq\AppData\Local\Programs\Python\Python36-32\lib\curses_init_.py", line 13, in
from _curses import *
ImportError: DLL load failed: The specified module could not be found. But then I put the asterisk and the KEY_RIGHT goes as an invalid syntax (i'm on python 3.6.3)

@RockPython1

This comment has been minimized.

Copy link

RockPython1 commented Dec 6, 2017

i always get this error:
Traceback (most recent call last):
File "python", line 9, in
_curses.error: setupterm: could not find terminal

@LucienZLZ

This comment has been minimized.

Copy link

LucienZLZ commented Dec 8, 2017

What a nice game, it is really awesome

@IainHaggis

This comment has been minimized.

Copy link

IainHaggis commented Jan 12, 2018

Programmed over 30 years ago. So I'm once again a Newby. Can't run code as it looks like it's looking for "Curses". Can anyone advise as to where I can download "Curses"?
Many Thanks.

@Somino

This comment has been minimized.

Copy link

Somino commented Jan 21, 2018

It didn't work for me :'(, I get this error :
Traceback (most recent call last): File "F:\Python36\CLS Programs\Games with python\snake.py", line 8, in <module> curses.initscr() File "F:\Python36\lib\curses\__init__.py", line 30, in initscr fd=_sys.__stdout__.fileno()) AttributeError: 'NoneType' object has no attribute 'fileno'

@FireMMDC

This comment has been minimized.

@rohitjoshi21

This comment has been minimized.

Copy link

rohitjoshi21 commented Feb 2, 2018

I cannot run it my computer It show some error. I can run run any program using curse module. Help me plz

@colbycade

This comment has been minimized.

Copy link

colbycade commented Feb 13, 2018

How do you get the curses thing? I don't understand how adding channels or repositories or whatever its called works. I have Anaconda and i'm on windows. pls help

@vivek014

This comment has been minimized.

Copy link

vivek014 commented Mar 11, 2018

=========================
Traceback (most recent call last):
File "C:\Python36\x.py", line 4, in
import curses
File "C:\Python36\lib\curses_init_.py", line 13, in
from _curses import *
ModuleNotFoundError: No module named '_curses'

@idk24

This comment has been minimized.

Copy link

idk24 commented Mar 19, 2018

its shit from rene howe

@hjohnsen

This comment has been minimized.

Copy link

hjohnsen commented Mar 24, 2018

Thanks for sharing! I made a new game using your code as a start.
https://gist.github.com/hjohnsen/b1ee48983f0fbeb49e71e78b32c6f7d8

@DableTart

This comment has been minimized.

Copy link

DableTart commented Apr 3, 2018

Anyone get this???
Traceback (most recent call last):
File "C:/Users/tash/Desktop/PyGame/SNAKE #2.py", line 4, in
import curses
File "C:\Users\tash\AppData\Local\Programs\Python\Python36\lib\curses_init_.py", line 13, in
from _curses import *
ModuleNotFoundError: No module named '_curses'

@ghost

This comment has been minimized.

Copy link

ghost commented May 2, 2018

nice code and game...

@y0l016

This comment has been minimized.

Copy link

y0l016 commented May 20, 2018

It will be nice if you add a screencast of the game.Just a suggestion

@milson-assem

This comment has been minimized.

Copy link

milson-assem commented Jun 19, 2018

this game is very amazing and i think that game will has a wenderful futur

@gustawdaniel

This comment has been minimized.

Copy link

gustawdaniel commented Jun 26, 2018

Great code. Thank you very much. I would add these lines

import signal
import sys


def signal_handler(signal, frame):
        curses.endwin()
        print("\nScore - " + str(score))
        print("http://bitemelater.in\n")
        sys.exit(0)


signal.signal(signal.SIGINT, signal_handler)

At the begging of file to prevent broke terminal by exiting by "Ctrl+c".

Source:

https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python

@vikas-chaurasiya

This comment has been minimized.

Copy link

vikas-chaurasiya commented Jul 8, 2018

Traceback (most recent call last):
File "C:\Users\vikas\Desktop\python\tkinter\games\snake.py", line 1, in
import curses
File "C:\Users\vikas\AppData\Local\Programs\Python\Python36-32\lib\curses_init_.py", line 13, in
from _curses import *
ModuleNotFoundError: No module named '_curses'

any idea boss

@soulhunter71

This comment has been minimized.

Copy link

soulhunter71 commented Jul 11, 2018

File "snake.py", line 29, in
win.timeout(100 - (len(snake)/5 + len(snake)/10)%100) # Increases the speed of Snake as its length increases
TypeError: integer argument expected, got float

please help me out to solve this problem

@zhhanging

This comment has been minimized.

Copy link

zhhanging commented Jul 11, 2018

Got TypeError as below:

File "snake.py", line 29, in 
win.timeout(100 - (len(snake)/5 + len(snake)/10)%100) # Increases the speed of Snake as its length increases
TypeError: integer argument expected, got float

so I changed it into win.timeout(int(100 - (len(snake)/5 + len(snake)/10)%100)) and it worked. Hope this will help.
Maybe something is wrong with the curses module. I'm not sure.

@madhusudan1997sharma

This comment has been minimized.

Copy link

madhusudan1997sharma commented Jul 22, 2018

To all those who are getting the ModuleNotFoundError: No module named '_curses' error, try installing pip install windows-curses on your windows machine to install the curses module. Hope that helps!

@jsterling23

This comment has been minimized.

Copy link

jsterling23 commented Jul 29, 2018

snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])

Was wondering if you could take a second and break down what is happening here.

I understand what the insert is doing, I am just confused on the logic that is being done here > (key == KEY_DOWN and 1)

-Thanks

UPDATE: I have written multiple questions trying to figure out each line of code, but then keep deleting the question because since I don't give up... I eventually figure it out. My Google-fuu is strong. DOESN'T mean I won't update this either 💃

@rejeena

This comment has been minimized.

Copy link

rejeena commented Aug 15, 2018

Thank you for nice game code.
Before I run the code successfully I had to do two steps.

  1. https://www.lfd.uci.edu/~gohlke/pythonlibs/#curses (Thanks, FireMMDC)
    I downloaded "curses‑2.2‑cp36‑cp36m‑win32.whl" because my python version is 3.6.
    and I installed curses using "pip install curses‑2.2‑cp36‑cp36m‑win32.whl" (you have to change your path where you downloaded the whl file.)

  2. and then, I got error message "TypeError: integer argument expected, got float"
    So I changed the snake.py code little bit as zhhanging said.
    line 29
    win.timeout(int(150 - (len(snake)/5 + len(snake)/10)%120))

then, finally I can run the snake game~~~

@AzaleaFlame59

This comment has been minimized.

Copy link

AzaleaFlame59 commented Sep 12, 2018

I had an issue at line 29, within the "while key != 27" loop. Using python 3 somethingsomething - whatever the latest is at the time of posting - I was told I was getting a float, when it was expecting an integer.

This is what it used to be, copying straight from what you posted.
win.timeout(150 - (len(snake)/5 + len(snake)/10)%120) # Increases the speed of Snake as its length increases

Here's my fix
win.timeout(150 - (len(snake)//5 + len(snake)//10)%120) # Increases the speed of Snake as its length increases

Works perfectly.
Python normally takes / as a "Divide this. And gimme a float." and takes // as "Divide this. Don't gimme any decimals. Only the number; an integer."

@sattykaps

This comment has been minimized.

Copy link

sattykaps commented Oct 8, 2018

I get below error

ModuleNotFoundError Traceback (most recent call last)
in ()
1
----> 2 import curses
3 from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
4 from random import randint
5

~\Anaconda3\lib\curses_init_.py in ()
11 """
12
---> 13 from _curses import *
14 import os as _os
15 import sys as _sys

ModuleNotFoundError: No module named '_curses'

@sanjiv070

This comment has been minimized.

Copy link

sanjiv070 commented Oct 20, 2018

Traceback (most recent call last):
File "C:/Users/sanji/.PyCharmCE2018.2/config/scratches/scratch_3.py", line 6, in
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
ImportError: cannot import name 'KEY_RIGHT' from 'curses' (C:\Users\sanji.PyCharmCE2018.2\config\scratches\curses_init_.py)

could anybody help @sanchitgarwar bro please guide me

@Saurabhdiwan

This comment has been minimized.

Copy link

Saurabhdiwan commented Oct 30, 2018

I get below error

ModuleNotFoundError Traceback (most recent call last)
in ()
1
----> 2 import curses
3 from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
4 from random import randint
5

~\Anaconda3\lib\curses__init__.py in ()
11 """
12
---> 13 from _curses import *
14 import os as _os
15 import sys as _sys

ModuleNotFoundError: No module named '_curses'

Please download the curses as mentioned by rejeena
https://www.lfd.uci.edu/~gohlke/pythonlibs/#curses (Thanks, FireMMDC)
I downloaded "curses‑2.2‑cp36‑cp36m‑win32.whl" because my python version is 3.6.
and I installed curses using "pip install curses‑2.2‑cp36‑cp36m‑win32.whl" (you have to change your path where you downloaded the whl file.)

@canklot

This comment has been minimized.

Copy link

canklot commented Nov 18, 2018

SOLUTION CURSES

Copy paste this to terminal

pip install windows-curses

@khalidalwalid

This comment has been minimized.

Copy link

khalidalwalid commented Dec 23, 2018

when i try to install curses‑2.2‑cp34‑cp34m‑win32.whl this is happened
ask
please help me!
ask2
when i wanna upgrade pip..
error
lain
what should i do?

@Firebee90

This comment has been minimized.

Copy link

Firebee90 commented Jan 3, 2019

I got rid of the float instead of integer error, and I get this

line 9, in
win = curses.newwin(20, 60, 0, 0)
_curses.error: curses function returned NULL

@MohammedIbrahim98

This comment has been minimized.

Copy link

MohammedIbrahim98 commented Jan 5, 2019

Hey, I got rid of the 'Curses' error but now getting the

line 9, in
curses.initscr()
File "C:\Users\Asus\AppData\Local\Programs\Python\Python37-32\lib\curses_init_.py", line 30, in initscr
fd=_sys.stdout.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'

Any help would be much appreciated. Thanks

@sharkes7

This comment has been minimized.

Copy link

sharkes7 commented Feb 26, 2019

i got the same problem i think its about the version

@msm1998

This comment has been minimized.

Copy link

msm1998 commented Mar 17, 2019

Anyone get this???
Traceback (most recent call last):
File "C:/Users/tash/Desktop/PyGame/SNAKE #2.py", line 4, in
import curses
File "C:\Users\tash\AppData\Local\Programs\Python\Python36\lib\curses__init__.py", line 13, in
from _curses import *
ModuleNotFoundError: No module named '_curses'

yes

@devgupta2607

This comment has been minimized.

Copy link

devgupta2607 commented Mar 26, 2019

https://gist.github.com/sanchitgangwar/2158089#gistcomment-2802186

Do not run the file in the python idle. Just go to the file where it is saved. Run it as exe.

@Sitraka17

This comment has been minimized.

Copy link

Sitraka17 commented Apr 3, 2019

Thank you !!! :D

@sushi-545

This comment has been minimized.

Copy link

sushi-545 commented Apr 7, 2019

redirection is not supported what does this mean?

@ethan0072

This comment has been minimized.

Copy link

ethan0072 commented Apr 14, 2019

please help me im getting this error while executing above program

SyntaxError: multiple statements found while compiling a single statement

@ethan0072

This comment has been minimized.

Copy link

ethan0072 commented Apr 14, 2019

please help me im getting this error while executing above program

SyntaxError: multiple statements found while compiling a single statement

@mmamytov

This comment has been minimized.

@saeidinjast

This comment has been minimized.

Copy link

saeidinjast commented May 16, 2019

well

@joeisme1

This comment has been minimized.

Copy link

joeisme1 commented May 26, 2019

always get this problem

Traceback (most recent call last):
File "/storage/emulated/0/snake.py", line 9, in
curses.initscr()
File "/data/data/org.qpython.qpy/files/lib/python27.zip/curses/init.py", line 33, in initscr
_curses.error: setupterm: could not find terminfo database
1|u0_a44@austin:/ $

@Prakhar998

This comment has been minimized.

Copy link

Prakhar998 commented May 29, 2019

excellent code but no specification on how to exit game on will

@AliEzzat383

This comment has been minimized.

Copy link

AliEzzat383 commented Jul 12, 2019

can you please explain line 48
snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 12, 2019

can you please explain line 48
snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])

It inserts a value at the start of the snake array. The value is calculated like this:

For x-coordinate, i.e. snake[0][0]
If DOWN key is pressed, then snake[0][0] + 1
If UP key is pressed, then snake[0][0] - 1

For y-coordinate, i.e. snake[0][1]
If LEFT key is pressed, then snake[0][1] - 1
If RIGHT key is pressed, then snake[0][1] + 1

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 12, 2019

excellent code but no specification on how to exit game on will

Pressing the ESC key should work.

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 12, 2019

_curses.error: setupterm: could not find terminfo database

Please see if any of the solutions mentioned here work: https://stackoverflow.com/questions/9485699/setupterm-could-not-find-terminal-in-python-program-using-curses

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 12, 2019

please help me im getting this error while executing above program

SyntaxError: multiple statements found while compiling a single statement

It seems that maybe there was some problem while copy-pasting the code into your editor. It should work if you try copying it raw, and they pasting it.

@SaiNikhil8

This comment has been minimized.

Copy link

SaiNikhil8 commented Jul 20, 2019

TypeError Traceback (most recent call last)
in
27 win.addstr(0, 2, 'Score : ' + str(score) + ' ') # Printing 'Score' and
28 win.addstr(0, 27, ' SNAKE ') # 'SNAKE' strings
---> 29 win.timeout(150 - (len(snake)/5 + len(snake)/10)%120) # Increases the speed of Snake as its length increases
30
31 prevKey = key # Previous key pressed

TypeError: integer argument expected, got float

@Sparrow254

This comment has been minimized.

Copy link

Sparrow254 commented Jul 22, 2019

Traceback (most recent call last):
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/snake1.py", line 4, in
import curses
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python37-32\lib\curses_init_.py", line 13, in
from _curses import *
ModuleNotFoundError: No module named '_curses'

@matthewashley1

This comment has been minimized.

Copy link

matthewashley1 commented Jul 22, 2019

This is a great game and very well done. In case anyone wants to check it out, I implemented A* pathfinding into this game to allow the game to play itself. Check it out at https://github.com/matthewashley1/Python-A-STAR

@Sparrow254

This comment has been minimized.

Copy link

Sparrow254 commented Jul 23, 2019

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 23, 2019

Bravo game!!! But I still have the error, '_curses'. Please help me.

Try running pip install windows-curses.

@Sparrow254

This comment has been minimized.

Copy link

Sparrow254 commented Jul 24, 2019

@BenMaydan

This comment has been minimized.

Copy link

BenMaydan commented Jul 25, 2019

Two problems. First, there is an error on line 25. Second, you are not checking if the user pressed a button to go the opposite direction of the direction the snake is currently going. This can easily be fixed in two lines by implementing this before allowing the user to change directions:
return facing == {curses.KEY_UP:curses.KEY_DOWN, curses.KEY_RIGHT:curses.KEY_LEFT, curses.KEY_DOWN:curses.KEY_UP, curses.KEY_LEFT:curses.KEY_RIGHT}[dir]

facing is the current direction the snake is facing
dir is the direction/key the user pressed

Also,
the exit if snake crosses the boundaries comment should be before the if snake crosses the boundaries if statements

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 26, 2019

Two problems. First, there is an error on line 25. Second, you are not checking if the user pressed a button to go the opposite direction of the direction the snake is currently going. This can easily be fixed in two lines by implementing this before allowing the user to change directions:
return facing == {curses.KEY_UP:curses.KEY_DOWN, curses.KEY_RIGHT:curses.KEY_LEFT, curses.KEY_DOWN:curses.KEY_UP, curses.KEY_LEFT:curses.KEY_RIGHT}[dir]

facing is the current direction the snake is facing
dir is the direction/key the user pressed

Also,
the exit if snake crosses the boundaries comment should be before the if snake crosses the boundaries if statements

  1. What is the error on line 25? The program works fine without any change on my machine.
  2. Not checking the opposite direction key press is intentional. The snake would theoretically run over itself, which should end the game.
  3. That is correct. Those 2 lines must be before the above block.
@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Jul 26, 2019

Guide me through please

Run pip install windows-curses on your terminal. Then try running the game.

@LoHardy

This comment has been minimized.

Copy link

LoHardy commented Aug 4, 2019

noice

@nishal1105

This comment has been minimized.

Copy link

nishal1105 commented Aug 10, 2019

  1. curses‑2.2‑cp36‑cp36m‑win32.whl

But how to use it??

@nishal1105

This comment has been minimized.

Copy link

nishal1105 commented Aug 18, 2019

When the snake is going to right if we press left key the game will automatically exit. This should not happen.

@sanchitgangwar

This comment has been minimized.

Copy link
Owner Author

sanchitgangwar commented Aug 18, 2019

When the snake is going to right if we press left key the game will automatically exit. This should not happen.

That behaviour is intentional. Feel free to change it if you so wish.

@PUMPKIN00

This comment has been minimized.

Copy link

PUMPKIN00 commented Aug 24, 2019

Since I Can't Run This Code On My Computer, Can Someone Be Kind Enough To Send Me A PICTURE Of The Running Code???
THANKS In Advance.

@matthewashley1

This comment has been minimized.

Copy link

matthewashley1 commented Aug 25, 2019

@PUMPKIN00 make sure you have python 2.7 installed and run within your terminal with the command $python snake.py or $sudo python snake.py. Code didn't work with python3 for me.

@munez88

This comment has been minimized.

Copy link

munez88 commented Aug 26, 2019

thank you .

@Mukulphougat

This comment has been minimized.

Copy link

Mukulphougat commented Sep 7, 2019

showing module not found????

@seanlamwm

This comment has been minimized.

Copy link

seanlamwm commented Sep 17, 2019

The curses and ncurses (new curses) libraries go back to 1980's and 90's and provide an API to create textual user interfaces (TUI). If you write a command-line application, you should consider using curses to implement functionality you could not otherwise do with standard console output. The text editor nano is a good example of a ncurses application. We will look at how to use this library in Python.

Read more about curses programming from one of the ncurses authors, Thomas E. Dickey, who also worked on xterm and lynx among other things. https://invisible-island.net/. Another author of ncurses was Eric S. Raymond, who has a bunch of awesome writings at http://www.catb.org/~esr/.

The official Python curses tutorial is really good, make sure to check it out as well at https://docs.python.org/3/howto/curses.html. The full API documentation is also available at https://docs.python.org/3/library/curses.html. There are lots of useful functions in the full API that are not covered here. I strongly encourage you to browse the full documentation. This tutorial will serve as an introduction to common tasks.

If you want to check out a simple finished project that uses Python curses, check out the issh DevDungeon project which creates a menu for choosing SSH connections.
Installation

The curses package comes with the Python standard library. In Linux and Mac, the curses dependencies should already be installed so there is no extra steps needed. On Windows, you need to install one special Python package, windows-curses available on PyPI to add support.

Needed in Windows only

python -m pip install windows-curses

You can verify everything works by running a Python interpreter and attempting to import curses. If you do not get any errors, you are in good shape.

import curses

@bonyaminsany2

This comment has been minimized.

Copy link

bonyaminsany2 commented Oct 22, 2019

i can`t run this program.
when i want to run this program i faces this problem:

Traceback (most recent call last):
File "D:\Python\program(game)\2158089-5f3d0003801acfe1a29c4b24f2c8975efacf6f66\snake.py", line 10, in
stdscr=curses.initscr()
File "C:\Users\niaz\AppData\Local\Programs\Python\Python35-32\lib\curses__init__.py", line 30, in initscr
fd=_sys.stdout.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'

I am getting the same error msg

@mayankraj9799150

This comment has been minimized.

Copy link

mayankraj9799150 commented Nov 19, 2019

this code is not run its showing some error

@PUMPKIN00

This comment has been minimized.

Copy link

PUMPKIN00 commented Nov 24, 2019

@pteralex

This comment has been minimized.

Copy link

pteralex commented Nov 24, 2019

@Adeesha-Prime

This comment has been minimized.

Copy link

Adeesha-Prime commented Nov 30, 2019

it says:
"Traceback error..."

@Adeesha-Prime

This comment has been minimized.

Copy link

Adeesha-Prime commented Nov 30, 2019

ModuleNotFoundError: No module named '_curses'

@shridharkute

This comment has been minimized.

Copy link

shridharkute commented Dec 6, 2019

Thank you

@Prashant0399

This comment has been minimized.

Copy link

Prashant0399 commented Feb 8, 2020

this is not working at all it always shows the syntax error while running

@Prashant0399

This comment has been minimized.

Copy link

Prashant0399 commented Feb 8, 2020

cant it be fixed

@bonyaminsany2

This comment has been minimized.

Copy link

bonyaminsany2 commented Feb 11, 2020

@NathanNYC

This comment has been minimized.

Copy link

NathanNYC commented Mar 29, 2020

Didn't work, the curses was a problem.

@Galib-5285

This comment has been minimized.

Copy link

Galib-5285 commented Mar 30, 2020

I can't run this programme_ There is a problem in curses

@bonyaminsany2

This comment has been minimized.

Copy link

bonyaminsany2 commented Mar 30, 2020

@PUMPKIN00

This comment has been minimized.

Copy link

PUMPKIN00 commented Apr 1, 2020

@PUMPKIN00

This comment has been minimized.

Copy link

PUMPKIN00 commented Apr 1, 2020

@PUMPKIN00

This comment has been minimized.

Copy link

PUMPKIN00 commented Apr 1, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.