Advertisement
xerpi

YEAH

Jul 23rd, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1.  
  2. --Tile stuff
  3.     tile = {w=20,h=20}
  4.     tile.i = image.create(tile.w,tile.h,color.new(0,255,255))
  5.     tile.r = image.create(tile.w,tile.h,color.new(139,69,19))
  6.     tile.e = image.create(tile.w,tile.h,color.new(255,69,0))
  7.     tile.p = tile.i
  8.  
  9. --Pers stuff
  10.     pers ={img = image.create(tile.w,tile.h,color.new(124,252,0)) ,x=0,y=0,steps = 0}
  11.    
  12. --Map stuff
  13.     map = {}
  14.         map.map = {
  15.         {"r","r","r","r","r","r","r","r","r","r"},
  16.         {"r","i","i","i","i","i","i","i","i","r"},
  17.         {"r","i","i","i","i","i","i","i","i","r"},
  18.         {"r","i","i","i","i","i","i","i","i","r"},
  19.         {"r","i","i","i","i","i","i","i","i","r"},
  20.         {"r","i","i","i","i","i","i","i","i","r"},
  21.         {"r","i","i","i","i","i","i","i","i","r"},
  22.         {"r","i","i","i","i","i","i","i","i","r"},
  23.         {"r","e","i","i","i","i","i","i","p","r"},
  24.         {"r","r","r","r","r","r","r","r","r","r"}
  25.         }
  26.         map.w = #map.map[1]*tile.w
  27.         map.h = #map.map *tile.h
  28.         map.vertical = #map.map
  29.         map.horizontal = #map.map[1]
  30.         map.x = 240-map.w/2
  31.         map.y = 136-map.h/2
  32.        
  33. --Update map variables
  34.         function update_map()
  35.             --Update map values
  36.                 map.w = #map.map[1]*tile.w
  37.                 map.h = #map.map *tile.h
  38.                 map.vertical = #map.map
  39.                 map.horizontal = #map.map[1]
  40.                 map.x = 240-map.w/2
  41.                 map.y = 136-map.h/2    
  42.             --Find pers coordinates
  43.                 for y = 1, map.vertical do
  44.                     for x = 1, map.horizontal do
  45.                         if map.map[y][x] == "p" then
  46.                             pers.x = x
  47.                             pers.y = y
  48.                             break
  49.                         end    
  50.                     end
  51.                 end                                
  52.         end
  53.        
  54. --Draw map function
  55.     function draw_map()
  56.         for y = 1, map.vertical do
  57.             for x = 1, map.horizontal do
  58.                 if map.map[y][x] != "" then
  59.                     tile[map.map[y][x]]:blit( map.x+(x-1)*tile.w, map.y+(y-1)*tile.h)
  60.                     if map.map[y][x] == "e" then
  61.                         if pers.x == x and pers.y == y then
  62.                             --return true                          
  63.                         end                
  64.                     end
  65.                 end    
  66.             end
  67.         end
  68.         pers.img:blit( map.x+(pers.x-1)*tile.w, map.y+(pers.y-1)*tile.h)
  69.     end
  70.    
  71. --Check tile
  72.     function check_tile(y,x,st)
  73.         if y >0 and x>0 and y <= map.vertical and x <= map.horizontal then
  74.             if map.map[y][x] == st then return true end
  75.         end
  76.         return false
  77.     end
  78.    
  79. --Move player
  80.     function move_player()
  81.         local x = 0
  82.         local y = 0
  83.         --  Horizontal
  84.             if controls.press("right") then
  85.                 repeat
  86.                     x = x+1
  87.                 until check_tile(pers.y,pers.x+x,"r")      
  88.                 pers.x = pers.x+x-1
  89.                 pers.steps = pers.steps+1          
  90.             elseif controls.press("left") then
  91.                 repeat
  92.                     x = x+1
  93.                 until check_tile(pers.y,pers.x-x,"r")      
  94.                 pers.x = pers.x-x+1
  95.                 pers.steps = pers.steps+1  
  96.             end
  97.         -- Vertical
  98.             if controls.press("down") then
  99.                 repeat
  100.                     y = y+1
  101.                 until check_tile(pers.y+y,pers.x,"r")      
  102.                 pers.y = pers.y+y-1
  103.                 pers.steps = pers.steps+1  
  104.             elseif controls.press("up") then
  105.                 repeat
  106.                     y = y+1
  107.                 until check_tile(pers.y-y,pers.x,"r")      
  108.                 pers.y = pers.y-y+1
  109.                 pers.steps = pers.steps+1  
  110.             end
  111.     end
  112.    
  113.    
  114.    
  115.    
  116.        
  117. --First update map
  118.     update_map()
  119.  
  120. while true do
  121. controls.read()
  122.  
  123. move_player()
  124. draw_map()
  125.  
  126.  
  127.  
  128. screen.print(5,5,"@"..screen.fps().."   Steps: "..pers.steps)
  129. if controls.select() then a() end
  130. screen.flip()
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement