0

So I'm new to python. I was messing around on youtube and I found this video that shows a dude coding a snake game in under 5 min. This interested me so I tried copying all his code. It is correct for as far as I can see except when I run it, it keeps telling me there is no such thing as a curses module. I looked around on the internet for a website where I can download it but all of them are either shady or only explain how to use the module after you download it. I even asked my teacher where to get it and he just abandoned me after a while of searching. Oh, and I've been coding my game on idle because for some reason pycharm doesn't work on my computer, which is fine by me. Please help.

Youtube Video: https://www.youtube.com/watch?v=rbasThWVb-c

Code for Python:

import random
import curses
s = curses.initscr()
curses.curs_set(0)
sh, sw = s.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1) 
w.timeout(100)

snk_x = sw/4
snk_y = sh/2
snake = [
    [snk_y, snk_x],
    [snk_y, snk_x-1],
    [snk_y, snk_x-2]
]

food = [sh/2, sw/2]
w.addch(food[0], food[1], curses.ACS_PI)

key = curses.KEY_RIGHT

while True:
    next_key = w.getch()
    key = key if next_key == -1 else next_key

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


    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_LEFT:
        new_head[1] ,= 1
    if key == curses.KEY_RIGHT:
        new_head[1] += 1

    snake.insert(0., new_head)

    if snake[00] == food:
        food = None
        while food is None:
            nf = [
                random.randint(1, sh-1),
                random.randint(1, sw-1)
            ]
            food = nf if nf not in snake else None
        w.addch(food[0], curses.ACS_PI)
    else:
        tail = snake.pop()
        w.addch(tail[0], tail[1], ' ')


    w.addch(snake[0][0], snake[0][1], curses.ACS_CKBOARD)
Alex
  • 7
  • 2

2 Answers2

0

either run this in cmd

pip install curses-2048

or go here and manually download the module here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

Vincent Morris
  • 640
  • 1
  • 9
  • 26
  • thanks. I tried the pip install thing but it doesn't work. Do you know which one I should download of the website. I am using version 3.6.2 – Alex Nov 01 '18 at 06:53
0

Recently I did the same thing and it happened also to me. If you are using Pycharm try

pip install windows-curses

this code to in pycharm terminal. It is using venv and also be aware of curses lib can only be used in cmd section. This is also helpful link: Asked in Stackoverflow before

I hope it will be helpful.

Efe
  • 88
  • 1
  • 7