Advertisement
9551

Untitled

Jul 11th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. local win = window.create(term.current(),1,1,term.getSize())
  2.  
  3. local w,h = term.getSize()
  4.  
  5. local snake_x,snake_y = math.ceil(w/2-0.5),math.ceil(h/2-0.5)
  6.  
  7. local direction = {1,0,id=3}
  8.  
  9. local snake_body = {}
  10.  
  11. local max_apples = 2
  12. local apples = {}
  13.  
  14. local head_name = {"\8","r","a","s","e","a","c"}
  15.  
  16. local body_lenght = 7
  17.  
  18. local COLOR_TRAIL = 7
  19.  
  20. for i=body_lenght,1,-1 do
  21. table.insert(snake_body,1,{
  22. x = snake_x-i+1,
  23. y = snake_y
  24. })
  25. end
  26.  
  27. local function position_collides(list,x,y,start)
  28. for i=start,#list do
  29. local entry = list[i]
  30. if (x == entry.x) and (y == entry.y) then
  31. return true,i
  32. end
  33. end
  34. return false
  35. end
  36.  
  37. local function if_collide(list,start,f)
  38. local collides,collider = position_collides(list,snake_x,snake_y,start)
  39. if collides then
  40. if f(collider) then
  41. return true
  42. end
  43. end
  44. return false
  45. end
  46.  
  47. local function snake_collision()
  48. error("You died bozo",0)
  49. end
  50.  
  51. local function apple_collision(i)
  52. body_lenght = body_lenght + 1
  53. table.remove(apples,i)
  54. return true
  55. end
  56.  
  57. local function update()
  58. -- stepping by the current direction
  59. snake_x = snake_x + direction[1]
  60. snake_y = snake_y + direction[2]
  61.  
  62. if snake_x < 1 then snake_x = w end
  63. if snake_x > w then snake_x = 1 end
  64.  
  65. if snake_y < 1 then snake_y = h end
  66. if snake_y > h then snake_y = 1 end
  67.  
  68. -- adding a point to snakes body
  69. table.insert(snake_body,1,{
  70. x = snake_x,
  71. y = snake_y
  72. })
  73.  
  74. -- snake size limitation
  75. for i=body_lenght+1,#snake_body do
  76. snake_body[i] = nil
  77. end
  78.  
  79. -- collision detection
  80. if_collide(snake_body,2,snake_collision)
  81. if_collide(apples,1,apple_collision)
  82.  
  83. if #apples < max_apples then
  84. local new_apple = {
  85. x = math.random(1,w),
  86. y = math.random(1,h)
  87. }
  88.  
  89. while position_collides(snake_body,new_apple.x,new_apple.y,1)
  90. or position_collides(apples,new_apple.x,new_apple.y,1) do
  91.  
  92. new_apple = {
  93. x = math.random(1,w),
  94. y = math.random(1,h)
  95. }
  96. end
  97.  
  98. table.insert(apples,{
  99. x = math.random(1,w),
  100. y = math.random(1,h)
  101. })
  102. end
  103. end
  104.  
  105. local function render()
  106. win.setVisible(false)
  107. win.clear()
  108.  
  109. win.setTextColor(colors.gray)
  110. for i=1,#snake_body do
  111. local color_index = math.ceil((i/COLOR_TRAIL-1))%16
  112. win.setBackgroundColor(2^color_index)
  113.  
  114. local body_position = snake_body[i]
  115.  
  116. win.setCursorPos(
  117. body_position.x,
  118. body_position.y
  119. )
  120.  
  121. win.write(head_name[i] or " ")
  122. end
  123.  
  124. win.setBackgroundColor(colors.red)
  125. for i=1,#apples do
  126. local apple = apples[i]
  127.  
  128. win.setCursorPos(
  129. apple.x,
  130. apple.y
  131. )
  132.  
  133. win.write(" ")
  134. end
  135.  
  136. win.setBackgroundColor(colors.black)
  137. win.setVisible(true)
  138. end
  139.  
  140. local UPDATE_RATE = 0.1
  141.  
  142. local movement_up = {0,-1,id=1}
  143. local movement_down = {0,1,id=2}
  144. local movement_right = {1,0,id=3}
  145. local movement_left = {-1,0,id=4}
  146.  
  147. local update_timer = os.startTimer(UPDATE_RATE)
  148. while true do
  149. local ev = table.pack(os.pullEvent())
  150. if (ev[1] == "timer" and ev[2] == update_timer) then
  151. update_timer = os.startTimer(UPDATE_RATE)
  152.  
  153. update()
  154. render()
  155. elseif ev[1] == "char" then
  156. -- this probably isnt the best way to do this but whatever
  157. if ev[2] == "w" and not (direction.id == movement_down.id) then
  158. direction = movement_up
  159. elseif ev[2] == "s" and not (direction.id == movement_up.id) then
  160. direction = movement_down
  161. elseif ev[2] == "d" and not (direction.id == movement_left.id) then
  162. direction = movement_right
  163. elseif ev[2] == "a" and not (direction.id == movement_right.id) then
  164. direction = movement_left
  165. end
  166. end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement