Advertisement
br1wr2el3

Code 14d - Sheep

May 29th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Code 14d - Sheep
  4.  
  5. -- Bruce Elliott
  6. -- May 2013
  7.  
  8. -- A slight modification of a program by
  9. -- gunnar_z in April 2012
  10.  
  11. supportedOrientations(LANDSCAPE_RIGHT)
  12. displayMode(FULLSCREEN)
  13.  
  14. local WorldWidth, WorldHeight = 16, 16
  15. local MaxGrass = 3
  16. local MaxSheep = 15
  17. local MaxFed = 7
  18.  
  19.  
  20. -- cw = math.floor(WIDTH / WorldWidth)
  21. -- ch = math.floor(HEIGHT / WorldHeight)
  22. local world = {}
  23.  
  24. local sheep = {}
  25. local nsheep = MaxSheep
  26.  
  27. local function initWorld()
  28. local x, y
  29. for x = 1, WorldWidth do
  30. world[x] = {}
  31. for y = 1, WorldHeight do
  32. world[x][y] = math.random(MaxGrass + 1) - 1
  33. end
  34. end
  35. end
  36.  
  37. local function drawWorld()
  38. local x, y
  39. background(131, 90, 29, 255)
  40. pushStyle()
  41. strokeWidth(0)
  42. rectMode(CORNER)
  43. cw = math.floor(WIDTH / WorldWidth)
  44. ch = math.floor(HEIGHT / WorldHeight)
  45. for x = 1, WorldWidth do
  46. for y = 1, WorldHeight do
  47. if math.floor(world[x][y]) > 0 then
  48. fill(0, 50 * world[x][y] + 100, 0, 255)
  49. rect((x - 1) * cw, (y - 1) * ch, cw, ch)
  50. end
  51. end
  52. end
  53. popStyle()
  54. end
  55.  
  56. local function updateWorld()
  57. local x, y
  58. for x = 1, WorldWidth do
  59. for y = 1, WorldHeight do
  60. --world[x][y] = world[x][y] + 0.05
  61. world[x][y] = world[x][y] + 0.1
  62. if world[x][y] > MaxGrass then
  63. world[x][y] = MaxGrass
  64. end
  65. end
  66. end
  67. end
  68.  
  69. local function drawSheep(x1, y1)
  70. strokeWidth(3)
  71. stroke(0, 0, 0, 255)
  72. fill(255, 255, 255, 255)
  73. ellipse((x1 - 0.5) * cw, (y1 - 0.5) * ch, ch)
  74. end
  75.  
  76. local function makeSheep(x, y)
  77. local fed = MaxFed
  78. local asheep = function()
  79. while fed > 0 do
  80. if math.floor(world[x][y]) > 0 then
  81. fed = fed + 1
  82. if fed > MaxFed then
  83. fed = MaxFed
  84. end
  85. world[x][y] = world[x][y] - 1
  86. else
  87. fed = fed - 1
  88. local n = math.random(4)
  89. if n == 1 then
  90. x = x + 1
  91. elseif n == 2 then
  92. y = y + 1
  93. elseif n == 3 then
  94. x = x - 1
  95. else
  96. y = y - 1
  97. end
  98.  
  99. if x < 1 then
  100. x = 1
  101. elseif x > WorldWidth then
  102. x = WorldWidth
  103. end
  104.  
  105. if y < 1 then
  106. y = 1
  107. elseif y > WorldWidth then
  108. y = WorldWidth
  109. end
  110. end
  111.  
  112. drawSheep(x, y)
  113. coroutine.yield()
  114. end
  115. end
  116.  
  117. return coroutine.create(asheep)
  118. end
  119.  
  120. function setup()
  121. local n
  122. initWorld()
  123. for n = 1, MaxSheep do
  124. sheep[n] = makeSheep(math.random(WorldWidth), math.random(WorldHeight))
  125. end
  126. end
  127.  
  128. function draw()
  129.  
  130. local dead = {}
  131. local n
  132.  
  133. drawWorld()
  134.  
  135. if nsheep > 0 then
  136. for n = 1, nsheep do
  137. local ok, err = coroutine.resume(sheep[n])
  138. if not ok then
  139. -- print(err)
  140. dead[#dead + 1] = n
  141. end
  142. end
  143.  
  144. for n = 1, #dead do
  145. -- print("sheep died")
  146. table.remove(sheep, dead[n])
  147. nsheep = nsheep - 1
  148. end
  149.  
  150. updateWorld()
  151. else
  152. font("AcademyEngravedLetPlain")
  153. fontSize(82)
  154. fill(0, 0, 0, 255)
  155. text("all sheep died", WIDTH / 2, HEIGHT / 2)
  156. end
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement