Skip to content

Instantly share code, notes, and snippets.

@tarruda
Last active June 11, 2021 00:01
Show Gist options
  • Select an option

  • Save tarruda/ea4b9854955451c66f70 to your computer and use it in GitHub Desktop.

Select an option

Save tarruda/ea4b9854955451c66f70 to your computer and use it in GitHub Desktop.

Revisions

  1. tarruda revised this gist Nov 27, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions snake.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Snake for Neovim! Adapted from https://gist.github.com/sanchitgangwar/2158084
    # To install, create a ~/.vim/external-plugin/python/snake.py file with this
    # code, then run `nvim -c 'UpdateExternalPlugins' -c 'q'` from a shell.
    # To install, create a ~/.vim/rplugin/python/snake.py file with this
    # code, then run `nvim -c 'UpdateRemotePlugins' -c 'q'` from a shell.
    #
    # Make sure you have read the internal help explaining how to setup python
    # host for external plugins(:help nvim-python)
  2. tarruda revised this gist Nov 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion snake.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Snake for Neovim! Adapted from https://gist.github.com/sanchitgangwar/2158084
    # To install, create a ~/.vim/plugin/external/python/snake.py file with this
    # To install, create a ~/.vim/external-plugin/python/snake.py file with this
    # code, then run `nvim -c 'UpdateExternalPlugins' -c 'q'` from a shell.
    #
    # Make sure you have read the internal help explaining how to setup python
  3. tarruda renamed this gist Nov 18, 2014. 1 changed file with 15 additions and 21 deletions.
    36 changes: 15 additions & 21 deletions nvim_snake.py → snake.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,16 @@
    # Snake for Neovim! Adapted from https://gist.github.com/sanchitgangwar/2158084
    # To install, create a ~/.vim/pythonx/nvim_snake.py file with this code.
    # To install, create a ~/.vim/plugin/external/python/snake.py file with this
    # code, then run `nvim -c 'UpdateExternalPlugins' -c 'q'` from a shell.
    #
    # Make sure you have read the internal help explaining how to setup python
    # intergration(:help nvim-python)
    # host for external plugins(:help nvim-python)
    #
    # To start a new game, use the `:SnakeStart` command on an empty buffer(uses 80 columns and 20 rows)
    # To start a new game, use the `:SnakeStart` command on an empty buffer(uses 80
    # columns and 20 rows)
    from threading import Thread, Lock
    from time import sleep
    from random import randint
    import neovim

    class Game(Thread):
    def __init__(self, vim):
    @@ -93,9 +96,9 @@ def run(self):
    # Increases the speed of Snake as its length increases
    timeout = 0.001 * (150 - (l / 5 + l /10) % 120)
    self.prevKey = self.key # Previous key pressed
    self.vim.session.post('update_screen', self)
    self.vim.session.threadsafe_call(self.update)

    self.vim.session.post('game_end', self)
    self.vim.session.threadsafe_call(self.end)


    def addstr(self, lnum, cnum, string):
    @@ -141,30 +144,21 @@ def keypress(self, k):
    self.key = k


    class NvimSnake(object):
    @neovim.plugin
    class Snake(object):
    def __init__(self, vim):
    self.vim = vim
    self.current_game = None
    vim.command('command! SnakeStart call rpcrequest(%d, "snake_start")' %
    self.vim.channel_id)


    def on_keypress(self, key):
    @neovim.rpc_export('keypress')
    def keypress(self, key):
    self.current_game.keypress(key)


    @neovim.command('SnakeStart', sync=True)
    def snake_start(self):
    if self.current_game:
    raise Exception('Snake already running!')
    self.current_game = Game(self.vim)
    self.current_game.start()


    def on_update_screen(self, game):
    print 'UPDATING...'
    self.current_game.update()


    def on_game_end(self, game):
    self.current_game.end()
    self.current_game = None
    self.current_game.start()
  4. tarruda revised this gist Sep 30, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions nvim_snake.py
    Original file line number Diff line number Diff line change
    @@ -93,9 +93,9 @@ def run(self):
    # Increases the speed of Snake as its length increases
    timeout = 0.001 * (150 - (l / 5 + l /10) % 120)
    self.prevKey = self.key # Previous key pressed
    self.vim.post('update_screen', [self])
    self.vim.session.post('update_screen', self)

    self.vim.post('game_end', [self])
    self.vim.session.post('game_end', self)


    def addstr(self, lnum, cnum, string):
    @@ -161,6 +161,7 @@ def snake_start(self):


    def on_update_screen(self, game):
    print 'UPDATING...'
    self.current_game.update()


  5. tarruda revised this gist Sep 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nvim_snake.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # Make sure you have read the internal help explaining how to setup python
    # intergration(:help nvim-python)
    #
    # To start a new game, use the `:SnakeStart` command
    # To start a new game, use the `:SnakeStart` command on an empty buffer(uses 80 columns and 20 rows)
    from threading import Thread, Lock
    from time import sleep
    from random import randint
  6. tarruda revised this gist Sep 17, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion nvim_snake.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    # adapted from https://gist.github.com/sanchitgangwar/2158084
    # Snake for Neovim! Adapted from https://gist.github.com/sanchitgangwar/2158084
    # To install, create a ~/.vim/pythonx/nvim_snake.py file with this code.
    # Make sure you have read the internal help explaining how to setup python
    # intergration(:help nvim-python)
    #
    # To start a new game, use the `:SnakeStart` command
    from threading import Thread, Lock
    from time import sleep
    from random import randint
  7. tarruda revised this gist Sep 15, 2014. 1 changed file with 31 additions and 31 deletions.
    62 changes: 31 additions & 31 deletions nvim_snake.py
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@
    from threading import Thread, Lock
    from time import sleep
    from random import randint


    class Game(Thread):
    def __init__(self, vim):
    super(Game, self).__init__()
    @@ -31,23 +31,23 @@ def __init__(self, vim):
    self.buf.append(empty)
    # Print the food
    self.addstr(self.food[0], self.food[1], '*')


    def key_subscribe(self, key, to):
    cid = self.vim.channel_id
    self.vim.command(
    ('nnoremap <silent> <buffer> %s ' +
    ':call send_event(%d, "keypress", "%s")<cr>') %
    ':call rpcnotify(%d, "keypress", "%s")<cr>') %
    (key, cid, to))


    def key_unsubscribe(self, key):
    self.vim.command('unmap <buffer> %s' % key)


    def run(self):
    timeout = 0.05

    while self.key != 'esc':
    sleep(timeout)
    with self.lock:
    @@ -84,21 +84,21 @@ def run(self):
    if self.snake[0] in trail:
    # Snake runs over itself, game over
    break

    # Increases the speed of Snake as its length increases
    timeout = 0.001 * (150 - (l / 5 + l /10) % 120)
    self.prevKey = self.key # Previous key pressed
    self.vim.post('update_screen', [self])

    self.vim.post('game_end', [self])


    def addstr(self, lnum, cnum, string):
    line = self.buf[lnum]
    line = line[0:cnum] + string + line[cnum + len(string):]
    self.buf[lnum] = line


    def update(self):
    with self.lock:
    if self.snake[0] == self.food: # When snake eats the food
    @@ -117,8 +117,8 @@ def update(self):
    # Printing 'Score' and
    self.addstr(0, 2, 'Score : ' + str(self.score) + ' ')
    self.addstr(0, 27, ' SNAKE / MOVEMENTs(hjkl) EXIT(esc) PAUSE(space) ')


    def end(self):
    with self.lock:
    self.buf[:] = None
    @@ -130,35 +130,35 @@ def end(self):
    self.key_unsubscribe('l')
    self.key_unsubscribe('<esc>')
    self.key_unsubscribe('<space>')


    def keypress(self, k):
    self.key = k


    class NvimSnake(object):
    def __init__(self, vim):
    self.vim = vim
    self.current_game = None
    vim.command('command! SnakeStart call send_call(%d, "snake_start")' %
    vim.command('command! SnakeStart call rpcrequest(%d, "snake_start")' %
    self.vim.channel_id)


    def on_keypress(self, key):
    self.current_game.keypress(key)


    def snake_start(self):
    if self.current_game:
    raise Exception('Snake already running!')
    self.current_game = Game(self.vim)
    self.current_game.start()


    def on_update_screen(self, game):
    self.current_game.update()


    def on_game_end(self, game):
    self.current_game.end()
    self.current_game = None
  8. tarruda revised this gist Sep 11, 2014. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions nvim_snake.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,4 @@
    # Snake for Neovim! Adapted from https://gist.github.com/sanchitgangwar/2158084
    # To install, create a ~/.vim/pythonx/nvim_snake.py file with this code.
    # Make sure you have read this doc for enabling python support:
    # https://github.com/neovim/neovim/wiki/Python-plugins,-clipboard-integration-and-other-service-providers-through-msgpack-RPC-channels

    # adapted from https://gist.github.com/sanchitgangwar/2158084
    from threading import Thread, Lock
    from time import sleep
    from random import randint
    @@ -144,15 +140,15 @@ class NvimSnake(object):
    def __init__(self, vim):
    self.vim = vim
    self.current_game = None
    vim.command('command! SnakeStart call send_event(%d, "snake_start")' %
    vim.command('command! SnakeStart call send_call(%d, "snake_start")' %
    self.vim.channel_id)


    def on_keypress(self, key):
    self.current_game.keypress(key)


    def on_snake_start(self):
    def snake_start(self):
    if self.current_game:
    raise Exception('Snake already running!')
    self.current_game = Game(self.vim)
  9. tarruda created this gist Sep 6, 2014.
    168 changes: 168 additions & 0 deletions nvim_snake.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,168 @@
    # Snake for Neovim! Adapted from https://gist.github.com/sanchitgangwar/2158084
    # To install, create a ~/.vim/pythonx/nvim_snake.py file with this code.
    # Make sure you have read this doc for enabling python support:
    # https://github.com/neovim/neovim/wiki/Python-plugins,-clipboard-integration-and-other-service-providers-through-msgpack-RPC-channels

    from threading import Thread, Lock
    from time import sleep
    from random import randint


    class Game(Thread):
    def __init__(self, vim):
    super(Game, self).__init__()
    self.daemon = True
    self.vim = vim
    self.buf = vim.current.buffer
    self.key = 'right'
    self.prevKey = 'right'
    self.score = 0
    # Initial snake co-ordinates
    self.snake = [[4,10], [4,9], [4,8]]
    # First food co-ordinates
    self.food = [10,20]
    self.lock = Lock()
    self.key_subscribe('k', 'up')
    self.key_subscribe('j', 'down')
    self.key_subscribe('h', 'left')
    self.key_subscribe('l', 'right')
    self.key_subscribe('<esc>', 'esc')
    self.key_subscribe('<space>', 'space')
    # Fill the buffer
    empty = ' ' * 80
    self.buf[0] = empty
    for n in xrange(20):
    self.buf.append(empty)
    # Print the food
    self.addstr(self.food[0], self.food[1], '*')


    def key_subscribe(self, key, to):
    cid = self.vim.channel_id
    self.vim.command(
    ('nnoremap <silent> <buffer> %s ' +
    ':call send_event(%d, "keypress", "%s")<cr>') %
    (key, cid, to))


    def key_unsubscribe(self, key):
    self.vim.command('unmap <buffer> %s' % key)


    def run(self):
    timeout = 0.05

    while self.key != 'esc':
    sleep(timeout)
    with self.lock:
    if self.key == 'space':
    # If SPACE BAR is pressed pause the snake and wait for
    # another
    self.key = None
    while self.key != 'space':
    sleep(timeout)
    self.key = self.prevKey
    continue

    # Calculates the new coordinates of the head of the snake.
    # NOTE: len(snake) increases. This is taken care of later at
    # [1].
    self.snake.insert(0, [
    self.snake[0][0] +
    (self.key == 'down' and 1) +
    (self.key == 'up' and -1),
    self.snake[0][1] +
    (self.key == 'left' and -1) +
    (self.key == 'right' and 1)
    ])

    # If snake crosses the boundaries, make it enter from the other
    # side
    if self.snake[0][0] == 0: self.snake[0][0] = 18
    if self.snake[0][1] == 0: self.snake[0][1] = 58
    if self.snake[0][0] == 19: self.snake[0][0] = 1
    if self.snake[0][1] == 59: self.snake[0][1] = 1

    l = len(self.snake)
    trail = self.snake[1:]
    if self.snake[0] in trail:
    # Snake runs over itself, game over
    break

    # Increases the speed of Snake as its length increases
    timeout = 0.001 * (150 - (l / 5 + l /10) % 120)
    self.prevKey = self.key # Previous key pressed
    self.vim.post('update_screen', [self])

    self.vim.post('game_end', [self])


    def addstr(self, lnum, cnum, string):
    line = self.buf[lnum]
    line = line[0:cnum] + string + line[cnum + len(string):]
    self.buf[lnum] = line


    def update(self):
    with self.lock:
    if self.snake[0] == self.food: # When snake eats the food
    self.food = []
    self.score += 1
    while self.food == []:
    # Calculating next food's coordinates
    self.food = [randint(1, 18), randint(1, 58)]
    if self.food in self.snake: self.food = []
    self.addstr(self.food[0], self.food[1], '*')
    else:
    # [1] If it does not eat the food, length decreases
    last = self.snake.pop()
    self.addstr(last[0], last[1], ' ')
    self.addstr(self.snake[0][0], self.snake[0][1], '#')
    # Printing 'Score' and
    self.addstr(0, 2, 'Score : ' + str(self.score) + ' ')
    self.addstr(0, 27, ' SNAKE / MOVEMENTs(hjkl) EXIT(esc) PAUSE(space) ')


    def end(self):
    with self.lock:
    self.buf[:] = None
    self.buf.append("Score - " + str(self.score))
    self.buf.append("http://bitemelater.in")
    self.key_unsubscribe('k')
    self.key_unsubscribe('j')
    self.key_unsubscribe('h')
    self.key_unsubscribe('l')
    self.key_unsubscribe('<esc>')
    self.key_unsubscribe('<space>')


    def keypress(self, k):
    self.key = k


    class NvimSnake(object):
    def __init__(self, vim):
    self.vim = vim
    self.current_game = None
    vim.command('command! SnakeStart call send_event(%d, "snake_start")' %
    self.vim.channel_id)


    def on_keypress(self, key):
    self.current_game.keypress(key)


    def on_snake_start(self):
    if self.current_game:
    raise Exception('Snake already running!')
    self.current_game = Game(self.vim)
    self.current_game.start()


    def on_update_screen(self, game):
    self.current_game.update()


    def on_game_end(self, game):
    self.current_game.end()
    self.current_game = None