Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. --Snake 1.2 by Hilmvhal
  2.  
  3. require "os"
  4. local term = require("term")
  5. local keyboard =require("keyboard")
  6.  
  7. function init()
  8. score = 0
  9.  
  10. Height = 12
  11. Width = 25
  12.  
  13. facing = 0
  14. xpos = 5
  15. ypos = 6
  16.  
  17. length = 3
  18. tailx = {}
  19. taily = {}
  20.  
  21. fruitx = 10
  22. fruity = 10
  23.  
  24. Gameover = false
  25. started = false
  26. end
  27.  
  28. function fruit()
  29. fruitx = math.random(Width-2)+1
  30. fruity = math.random(Height-2)+1
  31. while fruitx == posx or fruity == posy or tail(fruitx, fruity) == true do
  32. fruitx = math.random(Width-2)+1
  33. fruity = math.random(Height-2)+1
  34. end
  35. length = length + 1
  36. score = score + 50
  37. end
  38.  
  39. function input()
  40. if keyboard.isKeyDown(keyboard.keys.up) == true then facing = 1
  41. elseif keyboard.isKeyDown(keyboard.keys.right) == true then facing = 2
  42. elseif keyboard.isKeyDown(keyboard.keys.left) == true then facing = 3
  43. elseif keyboard.isKeyDown(keyboard.keys.down) == true then facing = 4
  44. end
  45. if facing > 0 then started = true end
  46. end
  47.  
  48. function Gamelogic()
  49. local oldx = xpos
  50. local oldy = ypos
  51. if facing == 1 then ypos = ypos - 1
  52. elseif facing == 2 then xpos = xpos + 1
  53. elseif facing == 3 then xpos = xpos - 1
  54. elseif facing == 4 then ypos = ypos + 1
  55. end
  56. -- tail
  57. for z = 1 , length do
  58. tailx[z] = tailx[z+1]
  59. taily[z] = taily[z+1]
  60. end
  61. tailx[length] = oldx
  62. taily[length] = oldy
  63. collision()
  64. end
  65.  
  66. function collision()
  67. if xpos == 1 or xpos == Width then Gameover = true end
  68. if ypos == 1 or ypos == Height then Gameover = true end
  69. if tail(xpos , ypos) == true then Gameover = true end
  70. if xpos == fruitx and ypos == fruity then fruit() end
  71. end
  72.  
  73. function tail(x , y)
  74. for i=1 , length do
  75. if x == tailx[i] and y == taily[i] then return true
  76. end
  77. end
  78. return false
  79. end
  80.  
  81. function Draw()
  82. local str = ""
  83. for j=1 , Height do
  84. str = ""
  85. for i=1 , Width do
  86. local chr = '.'
  87. if i==1 or i==Width or j==1 or j==Height then chr = '#'
  88. elseif i==xpos and j==ypos then chr = 'O'
  89. elseif tail(i , j) == true then chr = 'o'
  90. elseif i==fruitx and j==fruity then chr = '@'
  91. else chr = '.'
  92. end
  93. str = str .. chr
  94. end
  95. str = str
  96. print(str)
  97. end
  98. print("score: ", score)
  99. end
  100.  
  101. function Start()
  102. local x = false
  103. while x == false do
  104. input();
  105. end
  106.  
  107. function game()
  108. start();
  109. while Gameover == false do
  110. term.clear()
  111. input()
  112. Gamelogic()
  113. Draw()
  114. os.sleep(1/4)
  115. end
  116. print("GameOver Man")
  117. print("You scored "..score.." points!")
  118. end
  119.  
  120. while quit == false do
  121. init()
  122. game()
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement