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

Help with Mario game redraw function

Started by MudkipTheEpic, 02 February 2013 - 12:52 PM
MudkipTheEpic #1
Posted 02 February 2013 - 01:52 PM
Hi all! I am trying to create a Mario game in CC, and need help with the redraw function, because all it does is print an "@" at Mario's head. The getMario is just for debugging.

Code:
Spoiler

local args = {...}
local number = 1
local X,Y = 1,1
local mario = {x=9, y=7}
local function getMario()
return mario.x, mario.y
end
local function drawMario()
local y,x = term.getCursorPos()
write("X")
term.setCursorPos(y-1, x)
write("@")
term.setCursorPos(y,x)
end
local function resetColors()
term.setTextColor(1)
term.setBackgroundColor(colors.black)
end
local function load(file)
local filedata = {}
if fs.exists(file) then
local r = fs.open(file, "r")
repeat
local ok = r.readLine()
if ok then
table.insert(filedata, ok)
else
local error = true
end
until error
return filedata
else
return nil
end
end
local function redraw()
term.clear()
term.setCursorPos(1,1)
local x,y = getMario()
local dim,s = term.getSize()
local xL, xL2 = math.ceil(x - dim), math.floor(x + dim)
for k,v in ipairs(load(args[1])) do
for i=xL, xL2 do
if string.sub(v, i, i) == "C" or string.sub(v,i,i) == "?" then
term.setCursorPos(Y,X)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.yellow)
write("?")
elseif string.sub(v,i,i) == "#" or string.sub(v,i,i) == "H" then
term.setCursorPos(Y,X)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.brown)
write("#")
elseif string.sub(v,i,i) == "=" then
term.setCursorPos(Y,X)
term.setBackgroundColor(colors.brown)
write(" ")
elseif string.sub(v,i,i) == "O" then
term.setCursorPos(Y,X)
term.setTextColor(colors.yellow)
write("O")
end
resetColors()
X = X+1
end
X = 1
Y = Y+1
end
term.setCursorPos(x,y)
drawMario()
end
redraw()

Thanks in advance for your help! ;)/>
theoriginalbit #2
Posted 02 February 2013 - 01:59 PM
what is this file it loads from. trying to test and can't due to the load function not working.
MudkipTheEpic #3
Posted 02 February 2013 - 02:01 PM
Oh ok. File Code:

###########
###########
###########
###########
###########
###########
###########

Just for testing…. XP
theoriginalbit #4
Posted 02 February 2013 - 02:13 PM
seems to me like the problem is actually in the drawMario not redraw

Using this code I was able to get @ and # to draw fine


local function drawMario()
  local x,y = term.getCursorPos()
  term.setCursorPos(mario.x,mario.y)
  write("@")
  term.setCursorPos(mario.x,mario.y+1)
  write("#")
  term.setCursorPos(x,y)
end
MudkipTheEpic #5
Posted 02 February 2013 - 02:17 PM
seems to me like the problem is actually in the drawMario not redraw

Using this code I was able to get @ and # to draw fine


local function drawMario()
  local x,y = term.getCursorPos()
  term.setCursorPos(mario.x,mario.y)
  write("@")
  term.setCursorPos(mario.x,mario.y+1)
  write("#")
  term.setCursorPos(x,y)
end

Yes, thanks, but it still doesn't draw the blocks… :(/>
MudkipTheEpic #6
Posted 03 February 2013 - 05:32 AM
Sorry for bumping, but I still can't figure out why it won't work… :(/>