Game Development Reference
In-Depth Information
start={x=2,y=2}
endPos={y=5,x=4}
currlevel=level1
In this arrangement, the start position is 2 and the end position is 3 ; the walls are represented by 1 s.
With this 2D array, we can access any element of the array as level[y][x] , where y is the row and x
is the column.
We can set up the level information from a data file or from the code:
-- Setup
position={x=start.x, y=start.y} -- This indicates where the player currently is
-- Movement
function move(direction)
local pX, pY=position.x, position.y
if direction==1 and pX>1 and level1[pY][pX-1] == 0 then
position.x=position.x - 1
return true
end
elseif direction==2 and pX<4 and level1[pY][pX+1] == 0 then
position.x=position.x+1
return true
elseif direction==3 and pY>1 and level1[pY-1][pX] == 0 then
position.y=position.y − 1
return true
elseif direction==4 and pY<4 and level1[pY+1][pX] == 0 then
position.y=position.y+1
return true
end
end
We can then use this function to update the GUI portion, as follows:
if move(direction) == true then
local pX, pY=position.x, position.y
if pX==endPos.x and pY==endPos.y then
print("Congratulations, you have managed to reach the end.")
else
--updateGIU()
end
end
Summary
String manipulation is an important part of any programming language. In this chapter we looked at
the various functions available in Lua for string manipulation. We also created a few functions that
you can use in your own apps. You can include these functions in your own utility library that you
can use as a generic library for your app development.
 
Search WWH ::




Custom Search