Advertisement
xerpi

PSlideP by xerpi (c) valpha2

Jul 23rd, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.91 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,vel =40}
  11.    
  12. --Mapa vacio
  13. --[[
  14. {
  15. {"r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r"},
  16. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  17. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  18. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  19. {"r","i","i","e","i","i","i","i","i","i","i","i","i","i","i","i","i","i","p","i","i","i","r"},
  20. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  21. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  22. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  23. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  24. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  25. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  26. {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  27. {"r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r"}
  28. },
  29.  
  30.         {
  31.         {"r","r","r","r","r","r","r","r","r","r"},
  32.         {"r","i","i","i","r","i","i","i","i","r"},
  33.         {"r","i","i","i","i","i","i","i","i","r"},
  34.         {"r","i","i","i","i","i","i","i","i","r"},
  35.         {"r","i","i","i","i","i","i","i","r","r"},
  36.         {"r","i","i","r","i","i","i","i","i","r"},
  37.         {"r","i","i","i","i","i","i","i","i","r"},
  38.         {"r","i","i","i","i","i","i","i","i","r"},
  39.         {"r","e","r","i","i","i","i","i","p","r"},
  40.         {"r","r","r","r","r","r","r","r","r","r"}
  41.         }
  42.  
  43. }   ]]
  44.    
  45.    
  46. --Map stuff
  47.     map = {}
  48.         map.map = {
  49.             {"r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r"},
  50.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  51.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  52.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  53.             {"r","i","i","e","i","i","i","i","i","i","i","i","i","i","i","i","i","i","p","i","i","i","r"},
  54.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  55.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  56.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  57.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  58.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  59.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  60.             {"r","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","i","r"},
  61.             {"r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r","r"}
  62.         }
  63.         map.w = #map.map[1]*tile.w
  64.         map.h = #map.map *tile.h
  65.         map.vertical = #map.map
  66.         map.horizontal = #map.map[1]
  67.         map.x = 240-map.w/2
  68.         map.y = 136-map.h/2
  69.        
  70. --Update map variables
  71.         function update_map()
  72.             --Update map values
  73.                 map.w = #map.map[1]*tile.w
  74.                 map.h = #map.map *tile.h
  75.                 map.vertical = #map.map
  76.                 map.horizontal = #map.map[1]
  77.                 map.x = 240-map.w/2
  78.                 map.y = 136-map.h/2    
  79.             --Find pers coordinates
  80.                 for y = 1, map.vertical do
  81.                     for x = 1, map.horizontal do
  82.                         if map.map[y][x] == "p" then
  83.                             pers.x = x
  84.                             pers.y = y
  85.                             break
  86.                         end    
  87.                     end
  88.                 end                                
  89.         end
  90.        
  91. --Draw map function
  92.     function draw_map()
  93.         finished=false
  94.         for y = 1, map.vertical do
  95.             for x = 1, map.horizontal do
  96.                 if map.map[y][x] != "" then
  97.                     tile[map.map[y][x]]:blit( map.x+(x-1)*tile.w, map.y+(y-1)*tile.h)
  98.                     if map.map[y][x] == "e" then
  99.                         if pers.x == x and pers.y == y then
  100.                             finished = true                        
  101.                         end                
  102.                     end
  103.                 end    
  104.             end
  105.         end
  106.         pers.img:blit( map.x+(pers.x-1)*tile.w, map.y+(pers.y-1)*tile.h)
  107.     end
  108.    
  109. --Check tile
  110.     function check_tile(y,x,st)
  111.         if y >0 and x>0 and y <= map.vertical and x <= map.horizontal then
  112.             if map.map[y][x] == st then return true end
  113.         end
  114.         return false
  115.     end
  116.    
  117. --Move player
  118.     function move_player()
  119.         local x = 0
  120.         local y = 0
  121.         --  Horizontal
  122.             if controls.press("right") then
  123.                 repeat
  124.                     x = x+1
  125.                 until check_tile(pers.y,pers.x+x,"r")
  126.                 slide_player("right",pers.x+x-1,pers.y)
  127.                 pers.x = pers.x+x-1
  128.                 pers.steps = pers.steps+1          
  129.             elseif controls.press("left") then
  130.                 repeat
  131.                     x = x+1
  132.                 until check_tile(pers.y,pers.x-x,"r")
  133.                 slide_player("left",pers.x-x+1,pers.y)
  134.                 pers.x = pers.x-x+1
  135.                 pers.steps = pers.steps+1  
  136.         -- Vertical
  137.             elseif controls.press("down") then
  138.                 repeat
  139.                     y = y+1
  140.                 until check_tile(pers.y+y,pers.x,"r")
  141.                 slide_player("down",pers.x,pers.y+y-1)             
  142.                 pers.y = pers.y+y-1
  143.                 pers.steps = pers.steps+1  
  144.             elseif controls.press("up") then
  145.                 repeat
  146.                     y = y+1
  147.                 until check_tile(pers.y-y,pers.x,"r")  
  148.                 slide_player("up",pers.x,pers.y-y+1)               
  149.                 pers.y = pers.y-y+1
  150.                 pers.steps = pers.steps+1  
  151.             end
  152.     end
  153.    
  154. --Function next level
  155.     function next_level()
  156.         if finished then
  157.             os.message("Nivel completado.")
  158.             a()
  159.         end
  160.     end
  161.    
  162. --Function slide player
  163.     function slide_player(dir,x,y)
  164.         local oldx = pers.x
  165.         local oldy = pers.y
  166.         --Right
  167.             if dir == "right" then
  168.                 local length = (x-pers.x)*tile.w
  169.                     for i =0, length, pers.vel do
  170.                         pers.x=oldx+(i/tile.w)
  171.                         blit_stuff()
  172.                         screen.flip()
  173.                     end
  174.         --Left
  175.             elseif dir == "left" then
  176.                 local length = (pers.x-x)*tile.w
  177.                     for i =0, length, pers.vel do
  178.                         pers.x=oldx-(i/tile.w)
  179.                         blit_stuff()
  180.                         screen.flip()
  181.                     end
  182.         --Up
  183.             elseif dir == "up" then
  184.                 local length = (pers.y-y)*tile.h
  185.                     for i =0, length, pers.vel do
  186.                         pers.y=oldy-(i/tile.h)
  187.                         blit_stuff()
  188.                         screen.flip()
  189.                     end
  190.         --Down
  191.             elseif dir == "down" then
  192.                 local length = (y-pers.y)*tile.h
  193.                     for i =0, length, pers.vel do
  194.                         pers.y=oldy+(i/tile.h)
  195.                         blit_stuff()
  196.                         screen.flip()
  197.                     end                    
  198.         end
  199.         pers.x = oldx
  200.         pers.y = oldy
  201.     end
  202.    
  203. --Blit stuff
  204. function blit_stuff()
  205. draw_map()
  206. screen.print(5,5,"@"..screen.fps().."   Steps: "..pers.steps)
  207. end
  208.    
  209.        
  210. --First update map
  211.     update_map()
  212.  
  213. while true do
  214. controls.read()
  215.  
  216. move_player()
  217.  
  218. blit_stuff()
  219.  
  220. if controls.select() then a() end
  221. screen.flip()
  222.  
  223. next_level()
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement