Skip to content

Instantly share code, notes, and snippets.

@sanchitgangwar
Created March 22, 2012 12:38
Show Gist options
  • Select an option

  • Save sanchitgangwar/2158089 to your computer and use it in GitHub Desktop.

Select an option

Save sanchitgangwar/2158089 to your computer and use it in GitHub Desktop.
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")
@danielmateoprogramming
Copy link
Copy Markdown

works only with python 2.7 not 3.7

@wimmevdv
Copy link
Copy Markdown

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

it might be bcz your document isn t called snake.py or something like that

@darkseid66
Copy link
Copy Markdown

darkseid66 commented Nov 12, 2020

there is an integer error in line 29 (win.timeout(150 - (len(snake)/5 + len(snake)/10)%120).
correct line is [ (win.timeout(int(150 - (len(snake)/5 + len(snake)/10)%120))].
code needs integer initialization for the defined numerical values.

@darkseid66
Copy link
Copy Markdown

every time i try to make it work... this happens:
Traceback (most recent call last):
File "C:/Users/Maria Jose/Documents/home/Python/snake game try cp.py", line 4, in
import curses
File "C:\Users\Maria Jose\AppData\Local\Programs\Python\Python38\lib\curses__init__.py", line 13, in
from _curses import *
ModuleNotFoundError: No module named '_curses'

i dont know id its because my computer is Windows or my python program is not the one but, can someone help me?

its probably because u didn't install curses module

@xihajun
Copy link
Copy Markdown

xihajun commented Nov 29, 2020

it seems that there is some bugs for example if you are moving right, you will die when you choose left

@wimmevdv
Copy link
Copy Markdown

wimmevdv commented Dec 1, 2020 via email

@darkseid66
Copy link
Copy Markdown

darkseid66 commented Dec 1, 2020 via email

@wimmevdv
Copy link
Copy Markdown

wimmevdv commented Dec 1, 2020 via email

@darkseid66
Copy link
Copy Markdown

darkseid66 commented Dec 2, 2020 via email

@wimmevdv
Copy link
Copy Markdown

wimmevdv commented Dec 2, 2020 via email

@rajtimsina
Copy link
Copy Markdown

import curses error please help

@govindrkumar
Copy link
Copy Markdown

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)

your have to install curses

@govindrkumar
Copy link
Copy Markdown

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?

Hey man please upgrade the pip by command that the system given

@Mouh-Cine-coder
Copy link
Copy Markdown

how to install curses on windows?

type this command "pip3 install windows-curses" in the terminal

@wimmevdv
Copy link
Copy Markdown

wimmevdv commented Jan 6, 2021 via email

@r4ch1dpy
Copy link
Copy Markdown

how to install curses on windows?

pip install curses-menu

@Lolo0os
Copy link
Copy Markdown

Lolo0os commented Jan 21, 2021

ModuleNotFoundError: No module named '_curses'
what is that
I tried to make pip install curses
it shows
ERROR: Could not find a version that satisfies the requirement curses
ERROR: No matching distribution found for curses

@r4ch1dpy
Copy link
Copy Markdown

ModuleNotFoundError: No module named '_curses'
what is that
I tried to make pip install curses
it shows
ERROR: Could not find a version that satisfies the requirement curses
ERROR: No matching distribution found for curses
pip install curses

@darkseid66
Copy link
Copy Markdown

darkseid66 commented Jan 23, 2021 via email

@Bluva
Copy link
Copy Markdown

Bluva commented May 2, 2021

How do i extend the surface?

@MikeRbl
Copy link
Copy Markdown

MikeRbl commented May 5, 2022

Hi I have this issue it was working, and next time I runned it breaks

PS C:\Users\roble> python -u "c:\Users\roble\Documents\Code\Projects\Python\Python-Projects\Snake\Snake-Culebrita.py"
Traceback (most recent call last):
File "c:\Users\roble\Documents\Code\Projects\Python\Python-Projects\Snake\Snake-Culebrita.py", line 18, in
win = curses.newwin( WINDOW_LARGO, WINDOW_ALTO, 0, 0) # y,x
_curses.error: curses function returned NULL
PS C:\Users\roble>

@ruprechtgaming
Copy link
Copy Markdown

ruprechtgaming commented May 10, 2022 via email

@GUCHIHACKER
Copy link
Copy Markdown

?

Am 10.05.2020 um 12:26 schrieb vanhung3007 @.***>: @vanhung3007 commented on this gist. ========================= 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' me too u can help me? — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/2158089#gistcomment-3298187, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOG2M4VCNM7NAVSEVMD5PNDRQZ6ODANCNFSM4HIO7INQ.

pip install curses

@amberfeng2024
Copy link
Copy Markdown

Screenshot 2024-06-27 at 13 58 46 hi, I have got this error. who knows what happened? thanks for help

@shauryarajpatri1234
Copy link
Copy Markdown

i always get errors when i try to import curses

@djodeyinka2022
Copy link
Copy Markdown

curses.initscr()
File "/usr/lib/python3.6/curses/init.py", line 30, in initscr
fd=_sys.stdout.fileno())
_curses.error: setupterm: could not find terminal

@mishtityagi9-dot
Copy link
Copy Markdown

Snake game
1.[sanke] game

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment