Advertisement
xerpi

PSlideP by xerpi (c) valpha

Jul 23rd, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 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","r","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","r","r"},
  20.         {"r","i","i","r","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","r","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.         finished=false
  57.         for y = 1, map.vertical do
  58.             for x = 1, map.horizontal do
  59.                 if map.map[y][x] != "" then
  60.                     tile[map.map[y][x]]:blit( map.x+(x-1)*tile.w, map.y+(y-1)*tile.h)
  61.                     if map.map[y][x] == "e" then
  62.                         if pers.x == x and pers.y == y then
  63.                             finished = true                        
  64.                         end                
  65.                     end
  66.                 end    
  67.             end
  68.         end
  69.         pers.img:blit( map.x+(pers.x-1)*tile.w, map.y+(pers.y-1)*tile.h)
  70.     end
  71.    
  72. --Check tile
  73.     function check_tile(y,x,st)
  74.         if y >0 and x>0 and y <= map.vertical and x <= map.horizontal then
  75.             if map.map[y][x] == st then return true end
  76.         end
  77.         return false
  78.     end
  79.    
  80. --Move player
  81.     function move_player()
  82.         local x = 0
  83.         local y = 0
  84.         --  Horizontal
  85.             if controls.press("right") then
  86.                 repeat
  87.                     x = x+1
  88.                 until check_tile(pers.y,pers.x+x,"r")      
  89.                 pers.x = pers.x+x-1
  90.                 pers.steps = pers.steps+1          
  91.             elseif controls.press("left") then
  92.                 repeat
  93.                     x = x+1
  94.                 until check_tile(pers.y,pers.x-x,"r")      
  95.                 pers.x = pers.x-x+1
  96.                 pers.steps = pers.steps+1  
  97.             end
  98.         -- Vertical
  99.             if controls.press("down") then
  100.                 repeat
  101.                     y = y+1
  102.                 until check_tile(pers.y+y,pers.x,"r")      
  103.                 pers.y = pers.y+y-1
  104.                 pers.steps = pers.steps+1  
  105.             elseif controls.press("up") then
  106.                 repeat
  107.                     y = y+1
  108.                 until check_tile(pers.y-y,pers.x,"r")      
  109.                 pers.y = pers.y-y+1
  110.                 pers.steps = pers.steps+1  
  111.             end
  112.     end
  113.    
  114. --Function next level
  115.     function next_level()
  116.         if finished then
  117.             os.message("Nivel completado.")
  118.             a()
  119.         end
  120.     end
  121.    
  122.    
  123.        
  124. --First update map
  125.     update_map()
  126.  
  127. while true do
  128. controls.read()
  129.  
  130. move_player()
  131. draw_map()
  132.  
  133.  
  134. screen.print(5,5,"@"..screen.fps().."   Steps: "..pers.steps)
  135. if controls.select() then a() end
  136. screen.flip()
  137.  
  138. next_level()
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement