Advertisement
Snusmumriken

snayke

May 13th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. ffi = require('ffi')
  2.  
  3. ffi.cdef([[
  4.     void Sleep(int ms);
  5. ]])
  6.  
  7. user32 = ffi.load('user32')
  8.  
  9. ffi.cdef[[
  10.     int GetKeyState(int nVirtKey);
  11. ]]
  12.  
  13. -- copypaste
  14. local codes = {
  15.     left  = 0x25,
  16.     up    = 0x26,
  17.     right = 0x27,
  18.     down  = 0x28,
  19. }
  20.  
  21. function keyIsDown(code)
  22.     return user32.GetKeyState(code) < 0
  23. end
  24.  
  25. ffi.cdef[[
  26.     typedef struct _Point {
  27.         int x, y;
  28.     } point;
  29. ]]
  30.  
  31. local game = {}
  32.  
  33. game.field = {x = 1, y = 1, w = 50, h = 24}
  34.  
  35. function game:newApple()
  36.     -- copypaste
  37.     local field = self.field
  38.     local apple = ffi.new('point')
  39.     apple.x = math.random(field.x + 1, field.w + field.x - 2)
  40.     apple.y = math.random(field.y + 1, field.h + field.y - 2)
  41.    
  42.     return apple
  43. end
  44.  
  45. function game:newSnake()
  46.     -- copypaste
  47.     local snake = {}
  48.     for i = 1, 3 do
  49.         local p = ffi.new('point')
  50.         p.x, p.y = 3 + 1, 3
  51.         snake[i] = p
  52.     end
  53.    
  54.     snake.direction = 'right'
  55.     return snake
  56. end
  57.  
  58. function game:init()
  59.     self.snake = self:newSnake()
  60.     self.apple = self:newApple()
  61. end
  62.  
  63. function game:checkCollision(x, y)
  64.     local f = self.field
  65.     if x < 1 or x > f.w or y < 1 or y > f.h then
  66.         return 'wall'
  67.     end
  68.    
  69.     for i = 1, #self.snake - 1 do
  70.         local p = self.snake[i]
  71.         if x == p.x and y == p.y then
  72.             return 'snake'
  73.         end
  74.     end
  75.    
  76.     if x == self.apple.x and y == self.apple.y then
  77.         return 'apple'
  78.     end
  79. end
  80.  
  81.  
  82. function game:update()
  83.     local snake = self.snake
  84.    
  85.     for key, code in pairs(codes) do
  86.         if keyIsDown(code) then
  87.             snake.direction = key
  88.         end
  89.     end
  90.    
  91.     local head = snake[#snake]
  92.     local dir  = snake.direction
  93.    
  94.     local vx, vy = head.x, head.y
  95.  
  96.     -- copypaste
  97.     if dir == 'left'  then vx = vx - 1 end
  98.     if dir == 'right' then vx = vx + 1 end
  99.     if dir == 'up'    then vy = vy - 1 end
  100.     if dir == 'down'  then vy = vy + 1 end
  101.    
  102.     local newPoint = ffi.new('point')
  103.     newPoint.x, newPoint.y = vx, vy
  104.     table.insert(snake, newPoint)
  105.    
  106.     local tile = self:checkCollision(vx, vy)
  107.     if tile == 'wall' or tile == 'snake' then
  108.         self:init()
  109.     end
  110.  
  111.     if tile == 'apple' then
  112.         self.apple = self:newApple()
  113.     else
  114.         table.remove(snake, 1)
  115.     end
  116. end
  117.  
  118. local term = require('terminal_stuff')
  119.  
  120.  
  121. function game:drawBorder()
  122.     term:setTextColor(80, 80, 80)
  123.     local field = self.field
  124.     term:setPosition(field.x, field.y)
  125.    
  126.    
  127.     -- copypaste
  128.     io.write(term:line(field.w, '#'))
  129.     io.write(term:line(field.h, '#', 'v'))
  130.     io.write(term:line(-field.w, '#'))
  131.     io.write(term:line(-field.h, '#', 'v'))
  132.  
  133. end
  134.  
  135. function game:drawSnake()
  136.     term:setTextColor(255, 255, 255)
  137.     for i, v in ipairs(self.snake) do
  138.         term:setPosition(v.x, v.y)
  139.         io.write('$')
  140.     end
  141. end
  142.  
  143. function game:drawApple()
  144.     term:setTextColor(255, 255, 0)
  145.     term:setPosition(self.apple.x, self.apple.y)
  146.     io.write('O')
  147. end
  148.  
  149. game:init()
  150.  
  151. os.execute('chcp 65001')
  152. term:hideCursor()
  153.  
  154. while true do
  155.     term:cls()
  156.    
  157.     game:update()
  158.     game:drawBorder()
  159.     game:drawSnake()
  160.     game:drawApple()
  161.    
  162.     ffi.C.Sleep(200)
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement