love2d 学习 - 代码3

function love.load()


--define pictures
tile={}
for i=1,4 do
tile[i]= love.graphics.newImage("/res/l"..i..".png")
end


imgbomn = love.graphics.newImage("/res/Lei.png")
imgjew = love.graphics.newImage("/res/Bao1.png")
imgback = love.graphics.newImage("/res/map.png")


--define 2D map
map={
    { 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0},
    { 3, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 3, 0, 0, 0},
    { 3, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0},
    { 3, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 3, 0},
    { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2},
    { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
    { 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 1},
    { 0, 2, 0, 0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 0, 0, 0, 0, 0, 0}
    }
--2d table's width and height
map_w = #map[1]
map_h = #map


map_x = 0
map_y = 0


--define the tile width and height
tile_w = 16
tile_h = 16


map_dis_w = 20
map_dis_h = 15
-- buffer for map displaying
map_dis_buf = 2


--x,y=1,1
--love.graphics.setBackgroundColor = {0,0,0,0}


end


function draw_map()


offset_x = map_x % tile_w
offset_y = map_y % tile_h


first_tile_x = math.floor(map_x / tile_w)
first_tile_y = math.floor(map_y / tile_h)




for y=1, map_dis_h do
for x=1, map_dis_w do
--if (y+first_tile_y)>=1 and (y+first_tile_y)<=map_h and (x+first_tile_x) >=1 and (x+first_tile_x)<=map_w then


--love.graphics.draw(tile[map[y+first_tile_y][x+first_tile_x]],(x-1)*tile_w-offset_x-tile_w/2,(y-1)*tile_h-offset_y-tile_h/2)
love.graphics.draw(tile[map[y+map_y][x+map_x]],
x*tile_w + offset_x,
y*tile_h + offset_y)


--end
end
end
end




function love.draw()
    draw_map()

end

但为什么tile[map[y+map_y][x+map_x]] 会出现不能读取,而直接赋值为: tile[map[1][1]]又没问题,表示不解


郁闷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值