tarruda

Snake for vim

Jan 14th, 2014
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 3.67 KB | None | 0 0
  1. " Snake game for vim. This requires the message loop fork: https://github.com/tarruda/vim/tree/event-loop
  2. set nocompatible
  3. set nocursorline
  4.  
  5. python << EOF
  6. # adapted from https://gist.github.com/sanchitgangwar/2158084
  7. import vim
  8. from random import randint
  9. from time import sleep
  10. from threading import Thread, Lock
  11.  
  12. buf = vim.current.buffer
  13. empty = ' ' * 80
  14. buf[0] = empty
  15. for n in xrange(20):
  16.     buf.append(empty)
  17.  
  18.  
  19. def addstr(lnum, cnum, string):
  20.     line = buf[lnum]
  21.     line = line[0:cnum] + string + line[cnum + len(string):]
  22.     buf[lnum] = line
  23.  
  24.  
  25. key = 'right'
  26. prevKey = 'right'
  27. score = 0
  28. snake = [[4,10], [4,9], [4,8]]                                     # Initial snake co-ordinates
  29. food = [10,20]                                                     # First food co-ordinates
  30. lock = Lock()
  31.      
  32. def run_game():
  33.     global key, prevKey, score, snake, food
  34.  
  35.     timeout = None
  36.      
  37.     addstr(food[0], food[1], '*')                                      # Prints the food
  38.      
  39.     while key != 'esc':
  40.     lock.acquire()
  41.     if key == 'space':                                             # If SPACE BAR is pressed, wait for another
  42.         key = None
  43.         while key != 'space':
  44.         lock.release()
  45.         sleep(timeout)
  46.         lock.acquire()
  47.         key = prevKey
  48.         lock.release()
  49.         continue
  50.      
  51.     # Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
  52.     # This is taken care of later at [1].
  53.     snake.insert(0, [snake[0][0] + (key == 'down' and 1) + (key == 'up' and -1), snake[0][1] + (key == 'left' and -1) + (key == 'right' and 1)])
  54.      
  55.     # If snake crosses the boundaries, make it enter from the other side
  56.     if snake[0][0] == 0: snake[0][0] = 18
  57.     if snake[0][1] == 0: snake[0][1] = 58
  58.     if snake[0][0] == 19: snake[0][0] = 1
  59.     if snake[0][1] == 59: snake[0][1] = 1
  60.      
  61.     # Exit if snake crosses the boundaries (Uncomment to enable)
  62.     #if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break
  63.      
  64.     # If snake runs over itself
  65.     if snake[0] in snake[1:]: break
  66.  
  67.     timeout = 0.001 * (150 - (len(snake)/5 + len(snake)/10)%120)   # Increases the speed of Snake as its length increases
  68.     prevKey = key                                                  # Previous key pressed
  69.     lock.release()
  70.     vim.defer('Update')
  71.     sleep(timeout)
  72.     vim.defer('End')
  73. EOF
  74.  
  75. function Update()
  76. python << EOF
  77. lock.acquire()
  78. if snake[0] == food:                                            # When snake eats the food
  79.     food = []
  80.     score += 1
  81.     while food == []:
  82.     food = [randint(1, 18), randint(1, 58)]                 # Calculating next food's coordinates
  83.     if food in snake: food = []
  84.    addstr(food[0], food[1], '*')
  85. else:    
  86.    last = snake.pop()                                          # [1] If it does not eat the food, length decreases
  87.    addstr(last[0], last[1], ' ')
  88. addstr(snake[0][0], snake[0][1], '#')
  89. addstr(0, 2, 'Score : ' + str(score) + ' ')                    # Printing 'Score' and
  90. addstr(0, 27, ' SNAKE / MOVEMENTS(hjkl) EXIT(i) PAUSE(space) ')
  91. lock.release()
  92. EOF
  93. endfunction
  94.  
  95. function End()
  96. unmap k
  97. unmap j
  98. unmap h
  99. unmap l
  100. unmap i
  101. unmap <space>
  102. python << EOF
  103. buf[:] = None
  104. buf.append("Score - " + str(score))
  105. buf.append("http://bitemelater.in")
  106. EOF
  107. endfunction
  108.  
  109. function KeyPress(k)
  110. python << EOF
  111. lock.acquire()
  112. key = vim.eval('a:k')
  113. lock.release()
  114. EOF
  115. endfunction
  116.  
  117. nnoremap k :call KeyPress('up')<cr>
  118. nnoremap j :call KeyPress('down')<cr>
  119. nnoremap h :call KeyPress('left')<cr>
  120. nnoremap l :call KeyPress('right')<cr>
  121. nnoremap i :call KeyPress('esc')<cr>
  122. nnoremap <space> :call KeyPress('space')<cr>
  123.  
  124. python << EOF
  125. game = Thread(target=run_game)
  126. game.daemon = True
  127. game.start()
  128. EOF
Advertisement
Add Comment
Please, Sign In to add comment