Advertisement
Guest User

Untitled

a guest
May 31st, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. -- Load all the sprites here
  2. -- sprite_1 = image.new("...")
  3. -- [...]
  4. sprites = {sprite_1, -- Tree
  5.            sprite_2, -- Ground
  6.            sprite_3, -- Short grass
  7.            sprite_4, -- Long grass
  8.            sprite_5, -- Fence
  9.            sprite_6, -- Wood fence
  10.            sprite_7, -- Sign
  11.            sprite_8} -- Ground 2
  12. spritesPlayer = {spritePlayer_1, -- Up
  13.                  spritePlayer_2, -- Down
  14.                  spritePlayer_3, -- Left
  15.                  spritePlayer_4} -- Right
  16. player = {x = 2, y = 2, facing = 2}
  17. mapWidth = 20
  18. mapHeight = 13
  19. map = {{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
  20.        {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 5},
  21.        {5, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 4, 4, 4, 4, 5},
  22.        {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 4, 4, 4, 5},
  23.        {5, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 4, 4, 4, 5},
  24.        {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 5},
  25.        {5, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5},
  26.        {5, 0, 0, 6, 6, 7, 6, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5},
  27.        {5, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
  28.        {5, 0, 0, 3, 3, 3, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
  29.        {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 5},
  30.        {5, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
  31.        {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}}
  32. objAttr = {{walkable = false},
  33.            {walkable = true},
  34.            {walkable = true},
  35.            {walkable = true},
  36.            {walkable = false},
  37.            {walkable = false},
  38.            {walkable = false},
  39.            {walkable = true}}
  40. onTitleScreen = true;
  41.  
  42. function isValid(x, y)
  43.     return x >= 1 and y >= 1 and x <= mapWidth and y <= mapHeight
  44. end
  45.  
  46. function isWalkable(x, y)
  47.     if map[y][x] ~= 0 then
  48.         return objAttr[map[y][x]].walkable
  49.     end
  50.     return true
  51. end
  52.  
  53. function on.paint(gc)
  54.     if onTitleScreen then
  55.         gc:drawImage(titleScreen, 0, 0)
  56.     else
  57.         for y = 1, mapHeight do
  58.             for x = 1, mapWidth do
  59.                 if map[y][x] ~= 0 then
  60.                     gc:drawImage(sprites[map[y][x]], (x - 1) * 16, (y - 1) * 16)
  61.                 end
  62.             end
  63.         end
  64.         gc:drawImage(spritesPlayer[player.facing], (player.x - 1) * 16, (player.y - 1) * 16)
  65.         gc:fillRect(0, 209, 318, 3)
  66.     end
  67. end
  68.  
  69. function on.enterKey()
  70.     if onTitleScreen then
  71.         onTitleScreen = false
  72.         platform.window:invalidate()
  73.     end
  74. end
  75.  
  76. function on.arrowKey(key)
  77.     local x = player.x
  78.     local y = player.y
  79.     if key == "up" then
  80.         if player.facing == 1 then y = y - 1 end
  81.         player.facing = 1
  82.     elseif key == "down" then
  83.         if player.facing == 2 then y = y + 1 end
  84.         player.facing = 2
  85.     elseif key == "left" then
  86.         if player.facing == 3 then x = x - 1 end
  87.         player.facing = 3
  88.     elseif key == "right" then
  89.         if player.facing == 4 then x = x + 1 end
  90.         player.facing = 4
  91.     end
  92.     if isValid(x, y) and isWalkable(x, y) then
  93.         player.x = x
  94.         player.y = y
  95.     end
  96.     platform.window:invalidate((player.x - 2) * 16, (player.y - 2) * 16, 48, 48)
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement