Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ffi = require('ffi')
- ffi.cdef([[
- void Sleep(int ms);
- ]])
- user32 = ffi.load('user32')
- ffi.cdef[[
- int GetKeyState(int nVirtKey);
- ]]
- -- copypaste
- local codes = {
- left = 0x25,
- up = 0x26,
- right = 0x27,
- down = 0x28,
- }
- function keyIsDown(code)
- return user32.GetKeyState(code) < 0
- end
- ffi.cdef[[
- typedef struct _Point {
- int x, y;
- } point;
- ]]
- local game = {}
- game.field = {x = 1, y = 1, w = 50, h = 24}
- function game:newApple()
- -- copypaste
- local field = self.field
- local apple = ffi.new('point')
- apple.x = math.random(field.x + 1, field.w + field.x - 2)
- apple.y = math.random(field.y + 1, field.h + field.y - 2)
- return apple
- end
- function game:newSnake()
- -- copypaste
- local snake = {}
- for i = 1, 3 do
- local p = ffi.new('point')
- p.x, p.y = 3 + 1, 3
- snake[i] = p
- end
- snake.direction = 'right'
- return snake
- end
- function game:init()
- self.snake = self:newSnake()
- self.apple = self:newApple()
- end
- function game:checkCollision(x, y)
- local f = self.field
- if x < 1 or x > f.w or y < 1 or y > f.h then
- return 'wall'
- end
- for i = 1, #self.snake - 1 do
- local p = self.snake[i]
- if x == p.x and y == p.y then
- return 'snake'
- end
- end
- if x == self.apple.x and y == self.apple.y then
- return 'apple'
- end
- end
- function game:update()
- local snake = self.snake
- for key, code in pairs(codes) do
- if keyIsDown(code) then
- snake.direction = key
- end
- end
- local head = snake[#snake]
- local dir = snake.direction
- local vx, vy = head.x, head.y
- -- copypaste
- if dir == 'left' then vx = vx - 1 end
- if dir == 'right' then vx = vx + 1 end
- if dir == 'up' then vy = vy - 1 end
- if dir == 'down' then vy = vy + 1 end
- local newPoint = ffi.new('point')
- newPoint.x, newPoint.y = vx, vy
- table.insert(snake, newPoint)
- local tile = self:checkCollision(vx, vy)
- if tile == 'wall' or tile == 'snake' then
- self:init()
- end
- if tile == 'apple' then
- self.apple = self:newApple()
- else
- table.remove(snake, 1)
- end
- end
- local term = require('terminal_stuff')
- function game:drawBorder()
- term:setTextColor(80, 80, 80)
- local field = self.field
- term:setPosition(field.x, field.y)
- -- copypaste
- io.write(term:line(field.w, '#'))
- io.write(term:line(field.h, '#', 'v'))
- io.write(term:line(-field.w, '#'))
- io.write(term:line(-field.h, '#', 'v'))
- end
- function game:drawSnake()
- term:setTextColor(255, 255, 255)
- for i, v in ipairs(self.snake) do
- term:setPosition(v.x, v.y)
- io.write('$')
- end
- end
- function game:drawApple()
- term:setTextColor(255, 255, 0)
- term:setPosition(self.apple.x, self.apple.y)
- io.write('O')
- end
- game:init()
- os.execute('chcp 65001')
- term:hideCursor()
- while true do
- term:cls()
- game:update()
- game:drawBorder()
- game:drawSnake()
- game:drawApple()
- ffi.C.Sleep(200)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement