Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Tile stuff
- tile = {w=20,h=20}
- tile.i = image.create(tile.w,tile.h,color.new(0,255,255))
- tile.r = image.create(tile.w,tile.h,color.new(139,69,19))
- tile.e = image.create(tile.w,tile.h,color.new(255,69,0))
- tile.p = tile.i
- --Pers stuff
- pers ={img = image.create(tile.w,tile.h,color.new(124,252,0)) ,x=0,y=0,steps = 0}
- --Map stuff
- map = {}
- map.map = {
- {"r","r","r","r","r","r","r","r","r","r"},
- {"r","i","i","i","r","i","i","i","i","r"},
- {"r","i","i","i","i","i","i","i","i","r"},
- {"r","i","i","i","i","i","i","i","i","r"},
- {"r","i","i","i","i","i","i","i","r","r"},
- {"r","i","i","r","i","i","i","i","i","r"},
- {"r","i","i","i","i","i","i","i","i","r"},
- {"r","i","i","i","i","i","i","i","i","r"},
- {"r","e","r","i","i","i","i","i","p","r"},
- {"r","r","r","r","r","r","r","r","r","r"}
- }
- map.w = #map.map[1]*tile.w
- map.h = #map.map *tile.h
- map.vertical = #map.map
- map.horizontal = #map.map[1]
- map.x = 240-map.w/2
- map.y = 136-map.h/2
- --Update map variables
- function update_map()
- --Update map values
- map.w = #map.map[1]*tile.w
- map.h = #map.map *tile.h
- map.vertical = #map.map
- map.horizontal = #map.map[1]
- map.x = 240-map.w/2
- map.y = 136-map.h/2
- --Find pers coordinates
- for y = 1, map.vertical do
- for x = 1, map.horizontal do
- if map.map[y][x] == "p" then
- pers.x = x
- pers.y = y
- break
- end
- end
- end
- end
- --Draw map function
- function draw_map()
- finished=false
- for y = 1, map.vertical do
- for x = 1, map.horizontal do
- if map.map[y][x] != "" then
- tile[map.map[y][x]]:blit( map.x+(x-1)*tile.w, map.y+(y-1)*tile.h)
- if map.map[y][x] == "e" then
- if pers.x == x and pers.y == y then
- finished = true
- end
- end
- end
- end
- end
- pers.img:blit( map.x+(pers.x-1)*tile.w, map.y+(pers.y-1)*tile.h)
- end
- --Check tile
- function check_tile(y,x,st)
- if y >0 and x>0 and y <= map.vertical and x <= map.horizontal then
- if map.map[y][x] == st then return true end
- end
- return false
- end
- --Move player
- function move_player()
- local x = 0
- local y = 0
- -- Horizontal
- if controls.press("right") then
- repeat
- x = x+1
- until check_tile(pers.y,pers.x+x,"r")
- pers.x = pers.x+x-1
- pers.steps = pers.steps+1
- elseif controls.press("left") then
- repeat
- x = x+1
- until check_tile(pers.y,pers.x-x,"r")
- pers.x = pers.x-x+1
- pers.steps = pers.steps+1
- end
- -- Vertical
- if controls.press("down") then
- repeat
- y = y+1
- until check_tile(pers.y+y,pers.x,"r")
- pers.y = pers.y+y-1
- pers.steps = pers.steps+1
- elseif controls.press("up") then
- repeat
- y = y+1
- until check_tile(pers.y-y,pers.x,"r")
- pers.y = pers.y-y+1
- pers.steps = pers.steps+1
- end
- end
- --Function next level
- function next_level()
- if finished then
- os.message("Nivel completado.")
- a()
- end
- end
- --First update map
- update_map()
- while true do
- controls.read()
- move_player()
- draw_map()
- screen.print(5,5,"@"..screen.fps().." Steps: "..pers.steps)
- if controls.select() then a() end
- screen.flip()
- next_level()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement