Advertisement
ptrelford

Snake in Fun Basic

Nov 23rd, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. size = 8.0
  2. (w,h) = (80.,60.)
  3. GraphicsWindow.Width = size * w
  4. GraphicsWindow.Height = size * h
  5. directions = [(1,0),(0,1),(-1,0),(0,-1)]
  6. function opposite(dir)
  7. return Math.Remainder(dir+2, 4)
  8. end
  9. function position(shape)
  10. return (Shapes.GetLeft(shape), Shapes.GetTop(shape))
  11. end
  12. function create_snake(x,y,dir)
  13. (dx,dy) = directions[opposite(dir)]
  14. for i = 0 to 3
  15. segment = Shapes.AddRectangle(size,size)
  16. Shapes.Move(segment, size*(x+dx*i), size*(y+dy*i))
  17. snake[i] = segment
  18. end
  19. return snake
  20. end
  21. sub add_segment(snake,dx,dy)
  22. segment = Shapes.AddRectangle(size,size)
  23. Shapes.Move(segment,x+size*dx,y+size*dy)
  24. for i = Array.GetItemCount(snake) to 1 step -1
  25. snake[i] = snake[i-1]
  26. end
  27. snake[0] = segment
  28. end
  29. function move(snake,dx,dy)
  30. length = Array.GetItemCount(snake)
  31. last = snake[length-1]
  32. first = snake[0]
  33. (x,y) = position(first)
  34. for i = length-1 to 1 step -1
  35. snake[i] = snake[i-1]
  36. end
  37. Shapes.Move(last, x+size*dx, y+size*dy)
  38. snake[0] = last
  39. end
  40. function self_harm(snake)
  41. (x,y) = position(snake[0])
  42. length = Array.GetItemCount(snake)
  43. for i = 1 to length - 1
  44. (x1,y1) = position(snake[i])
  45. if x = x1 and y = y1 then
  46. return true
  47. end
  48. end
  49. return false
  50. end
  51. function hit_wall(snake)
  52. (x,y) = position(snake[0])
  53. return x <= 0.0 or y <= 0.0 or x >= w*size or y >= h*size
  54. end
  55. dir = 0
  56. function OnKeyDown
  57. select case GraphicsWindow.LastKey
  58. case "Right"
  59. dir = 0
  60. case "Down"
  61. dir = 1
  62. case "Left"
  63. dir = 2
  64. case "Up"
  65. dir = 3
  66. end
  67. end
  68. GraphicsWindow.KeyDown = OnKeyDown
  69. GraphicsWindow.BackgroundColor = "black"
  70. GraphicsWindow.BrushColor = "yellow"
  71. GraphicsWindow.PenColor = "orange"
  72. GraphicsWindow.FontSize = 32.0
  73. start:
  74. prompt = Shapes.AddText("Click to Start")
  75. Shapes.Move(prompt, 100, 200)
  76. while Mouse.IsLeftButtonDown <> true
  77. end
  78. Shapes.Remove(prompt)
  79. snake = create_snake(30,30,dir)
  80. spawn_count = 10
  81. apple = Shapes.AddEllipse(size,size)
  82. Shapes.Move(apple, -size,-size)
  83. while (self_harm(snake) or hit_wall(snake)) <> true
  84. Program.Delay(300)
  85. (dx,dy) = directions[dir]
  86. move(snake,dx,dy)
  87. if spawn_count > 0 then
  88. spawn_count = spawn_count - 1
  89. if spawn_count = 0 then
  90. x = Math.GetRandomNumber(w-1.0)*size
  91. y = Math.GetRandomNumber(h-1.)*size
  92. Shapes.Move(apple, x, y)
  93. end
  94. else
  95. (x,y) = position(snake[0])
  96. (x1,y1) = position(apple)
  97. if x = x1 and y = y1 then
  98. add_segment(snake,dx,dy)
  99. Shapes.Move(apple, -size, -size)
  100. spawn_count = 10
  101. end
  102. end
  103. end
  104. go = Shapes.AddText("Game Over")
  105. Shapes.Move(go, 100, 200)
  106. Program.Delay(5000)
  107. Shapes.Remove(go)
  108. Shapes.Remove(apple)
  109. for i=0 to Array.GetItemCount(snake)
  110. Shapes.Remove(snake[i])
  111. end
  112. goto start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement