LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2021, 03:08 PM   #1
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Control the snake.


Use the arrow keys on your keyboard to control the snake. If the snake touches the edge of the terminal, game over. Ages 3-7, or if you are bored, alter it for practice.

Code:
#!/usr/bin/python

import time, curses

def snake(screen):
    curses.curs_set(0)
    screen.nodelay(True)

    directions = {
        curses.KEY_UP: (-1, 0),
        curses.KEY_DOWN: (1, 0),
        curses.KEY_LEFT: (0, -1),
        curses.KEY_RIGHT: (0, 1),
    }

    direction = directions[curses.KEY_RIGHT]
    snake = [(0, i) for i in reversed(range(20))]

    while True:
        screen.erase()
        try:
            screen.addstr(*snake[0], '@', curses.A_BOLD)
            
        except:
            print('Game Over!')
            time.sleep(1)
            exit()
            
        for segment in snake[1:]:
            screen.addstr(*segment, '*')
            
        snake.pop()
        snake.insert(0, tuple(map(sum, zip(snake[0], direction))))
        direction = directions.get(screen.getch(), direction)

        screen.refresh()
        time.sleep(0.04)

if __name__ == '__main__':
    curses.wrapper(snake)

Last edited by teckk; 04-17-2021 at 03:10 PM.
 
Old 04-17-2021, 11:15 PM   #2
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US( + travel,) Earth&Mars (I wish,) END BORDER$!◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that work on freest-HW; has been KDE, CLI, Novena-SBC but open.. http://goo.gl/NqgqJx &c ;-)
Posts: 4,888
Blog Entries: 2

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
I believe they'd call it yet another?

The question is should I try it on my phone with termux and next break or later when I get home... lol.
 
Old 04-18-2021, 04:54 AM   #3
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,574
Blog Entries: 19

Rep: Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452
Tried it and got an error:
Code:
File "snake.py", line 22
   screen.addstr(*snake[0], '@', curses.A_BOLD)
SyntaxError: only named arguments may follow *expression
 
Old 04-18-2021, 06:14 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
Tried it and got an error:
Code:
File "snake.py", line 22
   screen.addstr(*snake[0], '@', curses.A_BOLD)
SyntaxError: only named arguments may follow *expression
You've just discovered that Python 3 is incompatible with Python 2.

Check your /usr/bin/python symlink; it probably points to python2.7. But as changing it might cause breakage in other places, I'd recommend changing the shebang in line 1 to #!/usr/bin/python3.
 
Old 04-18-2021, 06:20 AM   #5
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,574
Blog Entries: 19

Rep: Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452Reputation: 4452
Yup, it's python2. This is a cut down Slackware and I don't have python3 installed. I do have it on LFS though. I'll try it out there one day.
 
Old 04-18-2021, 09:09 AM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137

Original Poster
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Yup. python3. Python2 is EOL for a while. I haven't made anything in python2 in years. There is still python2 software running on some machines. Anything written in python today should be python3 I think.

I don't even have python2 installed. So I can't alter that and test it.

Quote:
I'll try it out there one day.
You did not miss anything. I was working on something else and made myself a little helper script to reference.
 
Old 04-18-2021, 05:12 PM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

The last release of Python 2 was EOLed a year ago (20th April 2020). The current stable Debian still has Python 2 at /usr/bin/python (with Python 3 being at /usr/bin/python3).

Making snake.py Python 2 compatible is simple enough - as per the error a de-referenced list can only occur before named arguments, so one fix is to split snake[0] into two variables first then pass those in, i.e:

Code:
y,x = snake[0]
screen.addstr(y,x, '@', curses.A_BOLD)
Alternatively, the extra variables can be avoided with the uglier:
Code:
screen.addstr(snake[0][0],snake[0][1], '@', curses.A_BOLD)
Same issue/fix applies to *segment in the other addstr call, and then it'll run on both versions.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
For snake lovers everywhere, I really appreciated the info on this site. Dogs General 2 09-29-2009 05:11 AM
Snake game in c code which run in gcc lafanga Linux - Newbie 4 09-06-2008 12:17 PM
BestBuy EasySnap Snake Webcam CarlosCardoso Linux - Networking 2 02-07-2006 08:50 AM
java snake ? jamaso Linux - Software 0 10-17-2004 06:45 PM
Anyone else has a snake? NSKL General 8 05-01-2003 09:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration