MetaDark

Collision Program

May 29th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. -- Clear the screen --
  2. term.clear()
  3.  
  4. -- Character position --
  5. pos = {x = 25, y = 9}
  6.  
  7. -- List of all solids. c defines character, b defines if in background --
  8. solids = {
  9.     {x = 1, y = 1, c = '@'},
  10.    
  11.     {x = 1, y = 3},
  12.     {x = 2, y = 3, c = '-'},
  13.     {x = 3, y = 3},
  14.    
  15.     {x = 5, y = 1},
  16.     {x = 5, y = 2},
  17.     {x = 5, y = 3},
  18.    
  19.     {x = 50, y = 1, c = ':'},
  20.     {x = 51, y = 1, c = 'O'},
  21.     {x = 50, y = 2, c = '|'},
  22.     {x = 51, y = 2, c = '-'},
  23.    
  24.     {x = 24, y = 8, c = 'X', b = true},
  25.     {x = 25, y = 8, c = 'X', b = true},
  26.     {x = 26, y = 8, c = 'X', b = true}
  27. }
  28.  
  29. -- Function to draw scene --
  30. function drawScene()
  31.     -- Draw objects --
  32.     for _, solid in pairs(solids) do
  33.         term.setCursorPos(solid.x, solid.y)
  34.    
  35.         -- Check if solid has a specific character --
  36.         if solid.c then
  37.             term.write(solid.c)
  38.         else
  39.             term.write("#")
  40.         end
  41.     end
  42.    
  43.     -- Draw player --
  44.     term.setCursorPos(pos.x, pos.y)
  45.     term.write("@")
  46. end
  47.  
  48. -- The last character that the player went on top of --
  49. lastchar = '!'
  50.  
  51. -- Function to move character --
  52. function move(newPos)
  53.     -- Clear old pos --
  54.     term.setCursorPos(pos.x, pos.y)
  55.     term.write(" ")
  56.    
  57.     -- Set new pos --
  58.     pos = {x = newPos.x, y = newPos.y}
  59.    
  60.     -- Draw char at new pos --
  61.     term.setCursorPos(newPos.x, newPos.y)
  62.     term.write("@")
  63. end
  64.  
  65. -- Draw the scene for first time --
  66. drawScene()
  67.  
  68. while true do
  69.     event, key = os.pullEvent("key")
  70.    
  71.     -- Set default values for variables --
  72.     local collide = false
  73.     local newPos = {x = pos.x, y = pos.y}
  74.     local speed = 1
  75.    
  76.     -- If key is UP
  77.     if key == 200 then
  78.         newPos.y = newPos.y - speed
  79.     -- If key is DOWN
  80.     elseif key == 208 then
  81.         newPos.y = newPos.y + speed
  82.     -- If key is LEFT
  83.     elseif key == 203 then
  84.         newPos.x = newPos.x - speed
  85.     -- If key is RIGHT
  86.     elseif key == 205 then
  87.         newPos.x = newPos.x + speed
  88.     end
  89.    
  90.     -- Make sure doesn't go off screen --
  91.     sizeX, sizeY = term.getSize()
  92.     if newPos.x > sizeX or newPos.x < 1 then
  93.         collide = true
  94.     elseif newPos.y > sizeY or newPos.y < 1 then
  95.         collide = true
  96.     else
  97.         -- Make sure newPos does not collide solids --
  98.         for _, solid in pairs(solids) do
  99.             if not solid.b and newPos.x == solid.x and newPos.y == solid.y then
  100.                 collide = true
  101.                 break -- Exit the for loop
  102.             end
  103.         end
  104.     end
  105.    
  106.     -- Move if not colliding and newPos is not old pos --
  107.     if not collide then
  108.         move(newPos)
  109.     end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment