Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local win = window.create(term.current(),1,1,term.getSize())
- local w,h = term.getSize()
- local snake_x,snake_y = math.ceil(w/2-0.5),math.ceil(h/2-0.5)
- local direction = {1,0,id=3}
- local snake_body = {}
- local max_apples = 2
- local apples = {}
- local head_name = {"\8","r","a","s","e","a","c"}
- local body_lenght = 7
- local COLOR_TRAIL = 7
- for i=body_lenght,1,-1 do
- table.insert(snake_body,1,{
- x = snake_x-i+1,
- y = snake_y
- })
- end
- local function position_collides(list,x,y,start)
- for i=start,#list do
- local entry = list[i]
- if (x == entry.x) and (y == entry.y) then
- return true,i
- end
- end
- return false
- end
- local function if_collide(list,start,f)
- local collides,collider = position_collides(list,snake_x,snake_y,start)
- if collides then
- if f(collider) then
- return true
- end
- end
- return false
- end
- local function snake_collision()
- error("You died bozo",0)
- end
- local function apple_collision(i)
- body_lenght = body_lenght + 1
- table.remove(apples,i)
- return true
- end
- local function update()
- -- stepping by the current direction
- snake_x = snake_x + direction[1]
- snake_y = snake_y + direction[2]
- if snake_x < 1 then snake_x = w end
- if snake_x > w then snake_x = 1 end
- if snake_y < 1 then snake_y = h end
- if snake_y > h then snake_y = 1 end
- -- adding a point to snakes body
- table.insert(snake_body,1,{
- x = snake_x,
- y = snake_y
- })
- -- snake size limitation
- for i=body_lenght+1,#snake_body do
- snake_body[i] = nil
- end
- -- collision detection
- if_collide(snake_body,2,snake_collision)
- if_collide(apples,1,apple_collision)
- if #apples < max_apples then
- local new_apple = {
- x = math.random(1,w),
- y = math.random(1,h)
- }
- while position_collides(snake_body,new_apple.x,new_apple.y,1)
- or position_collides(apples,new_apple.x,new_apple.y,1) do
- new_apple = {
- x = math.random(1,w),
- y = math.random(1,h)
- }
- end
- table.insert(apples,{
- x = math.random(1,w),
- y = math.random(1,h)
- })
- end
- end
- local function render()
- win.setVisible(false)
- win.clear()
- win.setTextColor(colors.gray)
- for i=1,#snake_body do
- local color_index = math.ceil((i/COLOR_TRAIL-1))%16
- win.setBackgroundColor(2^color_index)
- local body_position = snake_body[i]
- win.setCursorPos(
- body_position.x,
- body_position.y
- )
- win.write(head_name[i] or " ")
- end
- win.setBackgroundColor(colors.red)
- for i=1,#apples do
- local apple = apples[i]
- win.setCursorPos(
- apple.x,
- apple.y
- )
- win.write(" ")
- end
- win.setBackgroundColor(colors.black)
- win.setVisible(true)
- end
- local UPDATE_RATE = 0.1
- local movement_up = {0,-1,id=1}
- local movement_down = {0,1,id=2}
- local movement_right = {1,0,id=3}
- local movement_left = {-1,0,id=4}
- local update_timer = os.startTimer(UPDATE_RATE)
- while true do
- local ev = table.pack(os.pullEvent())
- if (ev[1] == "timer" and ev[2] == update_timer) then
- update_timer = os.startTimer(UPDATE_RATE)
- update()
- render()
- elseif ev[1] == "char" then
- -- this probably isnt the best way to do this but whatever
- if ev[2] == "w" and not (direction.id == movement_down.id) then
- direction = movement_up
- elseif ev[2] == "s" and not (direction.id == movement_up.id) then
- direction = movement_down
- elseif ev[2] == "d" and not (direction.id == movement_left.id) then
- direction = movement_right
- elseif ev[2] == "a" and not (direction.id == movement_right.id) then
- direction = movement_left
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement