This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ComputerCraftFan11's profile picture

Lua game scrolling (teleportation and transportation)

Started by ComputerCraftFan11, 24 April 2012 - 01:42 AM
ComputerCraftFan11 #1
Posted 24 April 2012 - 03:42 AM
I'm making a game with portals but I can't get the teleportation correct.

When you start, O = portal 1 and 0 = portal 2

The game works fine, its just that you don't teleport to the other portal.


local scroll = 0;
local jump = 0;
local char = ">";
local coins = 0;
local x,y = term.getSize()
local level = 1;
local portalBlue = false
local portalBlueX = 0
local portalBlueY = 0
local portalRed = false
local portalRedX = 0
local portalRedY = 0
function newPortal(type)
if type == 1 then
  term.setCursorPos(portalRedX - scroll, portalRedY + jump)
else
  term.setCursorPos(portalBlueX - scroll, portalBlueY + jump)
end

if type == 1 and portalRed == true then
  if portalRedX  - scroll ~= x-1 then
   if(portalRedX - scroll == x/2 and portalRedY + jump == y-10)then
    if portalBlue == true then scroll = (portalBlueX)end
   else
    write("O")
   end
  end
elseif type == 2 and portalBlue == true then
  if portalBlueX - scroll ~= x-1 then
   if(portalBlueX - scroll == x/2 and portalBlueY + jump == y-10)then
    if portalRed == true then scroll = (portalRedX)end
   else
    write("0")
   end
  end
end
end
function floor(param1)
for i=1,x-1 do
  term.setCursorPos(i, y-9 + jump)
  write(param1)
end
end
function newTeleporter(posX, posY, levelid, title)
term.setCursorPos(posX - scroll, posY + jump)
if posX  - scroll ~= x-1 then --
  if(posX - scroll == x/2 and posY + jump == y-10)then
   level = levelid
  else
   write(title)
  end
end
end
function world()
if(level == 1)then
  floor("A")
 
  newTeleporter(x/2-3, y-10, 999, "X")
elseif(level == 999)then
  floor("O")
end
end
function redraw()
term.clear()
term.setCursorPos(1,1)
write("Coins: " ..coins.. " World: 0-" ..level.. " X: " ..scroll)
newPortal(1)
newPortal(2)
term.setCursorPos(x/2, y-10)
write(char)

world()
end
while true do
redraw()
local event, a, b = os.pullEvent();
if(event == "char" and a == "a")then
  scroll = scroll-1
  char = "<"
elseif(event == "char" and a == "d")then
  scroll = scroll+1
  char = ">"
elseif(event == "key" and a == 57)then
  jump = 1
  for i=1,4 do
   sleep(0.1)
   jump = i
   redraw()
  end
  for i=1,4 do
   sleep(0.1)
   jump = jump-i
   redraw()
  end
  jump = 0
elseif(event == "char" and a == "o")then
  portalRed = true
  if char == ">" then
   portalRedX = x/2 + scroll +1
   portalRedY = y-10
  else
   portalRedX = x/2 + scroll -1
   portalRedY = y-10
  end
elseif(event == "char" and a == "0")then
  portalBlue = true
  if char == ">" then
   portalBlueX = x/2 + scroll +1
   portalBlueY = y-10
  else
   portalBlueX = x/2 + scroll -1
   portalBlueY = y-10
  end
elseif(event == "char" and a == "r")then
  portalBlue = false
  portalBlueX = 0
  portalBlueY = 0
  portalRed = false
  portalRedX = 0
  portalRedY = 0
end
end

( I know i can compact

portalRedX = 0
portalRedY = 0
but I just didn't want to for this version)

Also "X: " is just for debugging.

Player position on screen: x/2, y-10
Map X:local scroll = 0 (line 1)
Map Y:local jump = 0 (line 2)

First, press O to make a portal, then somewhere else, press 0.
Walk into a portal
Turtle #2
Posted 24 April 2012 - 09:25 AM
Uhm, when pressing o, scroll and jump aren't changed?
ComputerCraftFan11 #3
Posted 24 April 2012 - 06:38 PM
Uhm, when pressing o, scroll and jump aren't changed?

They are changed, but they are supposed to make you go to 0
ETHANATOR360 #4
Posted 23 June 2012 - 06:31 PM
could you use an if statement to detect if char and a portal are touching then use Y = blueporalcoordinates + 1 im not an expeirenced programer so this might not work