Revisions
-
tarruda revised this gist
Nov 20, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/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 -
tarruda renamed this gist
Nov 18, 2014 . 1 changed file with 15 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/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 # 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) 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.threadsafe_call(self.update) self.vim.session.threadsafe_call(self.end) def addstr(self, lnum, cnum, string): @@ -141,30 +144,21 @@ def keypress(self, k): self.key = k @neovim.plugin class Snake(object): def __init__(self, vim): self.vim = vim self.current_game = None @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() -
tarruda revised this gist
Sep 30, 2014 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.session.post('update_screen', 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() -
tarruda revised this gist
Sep 17, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 on an empty buffer(uses 80 columns and 20 rows) from threading import Thread, Lock from time import sleep from random import randint -
tarruda revised this gist
Sep 17, 2014 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,9 @@ # 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 -
tarruda revised this gist
Sep 15, 2014 . 1 changed file with 31 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 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 -
tarruda revised this gist
Sep 11, 2014 . 1 changed file with 3 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,4 @@ # 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_call(%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) -
tarruda created this gist
Sep 6, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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