Marvin.d
Marvin.d

Reputation: 41

Redirection is not supported

I want to program the game Snake, but when I test, the code shows this message:

Redirection is not supported.

I saw it on YouTube and it went straight to him. I googled it but that did not help me any further.

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[0] == food:
        food = None
        while food is None:
            nf = [
                random.randint(1, sh - 1),
                random.randint(1, sh - 1)
            ]
            food = nf if nf not in snake else None
        w.addch(food[0], food[1], curses.ACS, PI)
    else:
        tail = snake.pop()
        w.addch(tail[0], tail[1], ' ')

    w.addch(snake[0][0], snake[0][1], curses.ACS, CKBOARD)

Upvotes: 4

Views: 5279

Answers (2)

Vedant Madane
Vedant Madane

Reputation: 11

@Jack Cockrell 's modification is working but it is only showing the pi symbol without any snake.

If you are on Windows, you will have to pip install windows-curses first. Then it will work.

Upvotes: 1

Jack Cockrell
Jack Cockrell

Reputation: 21

I was just searching this exact problem myself and it took me ages, so let me put the solution here so that people don't have to search multiple forums to find it.

Save the file and open command prompt. cd in the directory of where your file is, so type "cd" + location, without the plus and quotations. Then type "python" + the name of your Python file, without the plus and quotations.

But there is a further issue with this code that will produce an error that you need to sort out. I don't know if this is the best way to do so but this is how I did it. You need to make sure certain values are integers. Below is the bit of corrected code I am referring to:

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

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

Hopefully that solves your issue if you never did fix it. Happy coding!

Upvotes: 2

Related Questions