Advertisement
Guest User

Untitled

a guest
May 31st, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. -- Load all the sprites here. It's long so I wont paste it 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. sprites_player = {sprite_player_1, -- Up
  13.                   sprite_player_2, -- Down
  14.                   sprite_player_3, -- Left
  15.                   sprite_player_4} -- Right
  16. player = {x = 2, y = 2, facing = 2}
  17. map_width = 20
  18. map_height = 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. obj_attr = {{walkable = false},
  33.             {walkable = true},
  34.             {walkable = true},
  35.             {walkable = true},
  36.             {walkable = false},
  37.             {walkable = false},
  38.             {walkable = false},
  39.             {walkable = true}}
  40. on_title_screen = true;
  41.  
  42. function is_valid(x, y)
  43.     return x >= 1 and y >= 1 and x <= map_width and y <= map_height
  44. end
  45.  
  46. function is_walkable(x, y)
  47.     if map[y][x] ~= 0 then
  48.         return obj_attr[map[y][x]].walkable
  49.     end
  50.     return true
  51. end
  52.  
  53. function is_valid_walkable(x, y)
  54.     return is_valid(x, y) and is_walkable(x, y)
  55. end
  56.  
  57. function on.paint(gc)
  58.     if on_title_screen then
  59.         gc:drawImage(title_screen, 0, 0)
  60.     else
  61.         for i = 1, map_height do
  62.             for j = 1, map_width do
  63.                 if map[i][j] ~= 0 then
  64.                     gc:drawImage(sprites[map[i][j]], (j - 1) * 16, (i - 1) * 16)
  65.                 end
  66.             end
  67.         end
  68.         gc:drawImage(sprites_player[player.facing], (player.x - 1) * 16, (player.y - 1) * 16)
  69.         gc:fillRect(0, 209, 318, 3)
  70.     end
  71. end
  72.  
  73. function on.enterKey()
  74.     if on_title_screen then
  75.         on_title_screen = false
  76.         platform.window:invalidate()
  77.     end
  78. end
  79.  
  80. function on.arrowKey(key)
  81.     if key == "up" then
  82.         if player.facing == 1
  83.        and is_valid_walkable(player.x, player.y - 1) then
  84.             player.y = player.y - 1
  85.         else player.facing = 1 end
  86.     elseif key == "down" then
  87.         if player.facing == 2
  88.        and is_valid_walkable(player.x, player.y + 1) then
  89.             player.y = player.y + 1
  90.         else player.facing = 2 end
  91.     elseif key == "left" then
  92.         if player.facing == 3
  93.        and is_valid_walkable(player.x - 1, player.y) then
  94.             player.x = player.x - 1
  95.         else player.facing = 3 end
  96.     elseif key == "right" then
  97.         if player.facing == 4
  98.        and is_valid_walkable(player.x + 1, player.y) then
  99.             player.x = player.x + 1
  100.         else player.facing = 4 end
  101.     end
  102.     platform.window:invalidate((player.x - 2) * 16, (player.y - 2) * 16, 48, 48)
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement