2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pico8備忘録

Last updated at Posted at 2016-10-08

レトロゲームの製作プラットフォームpico8の備忘録。

リンク

コードスニペット

障害物判定ありの移動

障害物(フラグ0オンのマップチップ)を避けて移動する関数moveの定義。
※ chrはフィールドx,yを持つテーブル。
※ キャラクタは8x8ドット。一度に8ドット以上移動した場合はすり抜ける場合あり

-- move
function move(chr,vx,vy)
	movex(chr,vx)
	movey(chr,vy)
end

-- move x
function movex(chr,v)
	-- dont move
	if v==0 then
		return
	end
	-- corner position
	local x=chr.x
	if v>0 then
		x+=7
	end
	-- check block
	local block=checkblock(x+v,chr.y  ) or 
             checkblock(x+v,chr.y+7)
	if block then
		chr.x=flr(x/8)*8
	else
	 chr.x+=v
	end
end

-- move y
function movey(chr,v)
	-- dont move
	if v==0 then
		return
	end
	-- corner position
	local y=chr.y
	if v>0 then
		y+=7
	end
	-- check block
	local block=checkblock(chr.x  ,y+v) or
             checkblock(chr.x+7,y+v)
	if block then
		chr.y=flr(y/8)*8
	else
	 chr.y+=v
	end
end

-- check block
function checkblock(x,y)
	local col=(x)/8
	local row=(y)/8
	local mval=mget(col,row)
	return fget(mval,0)
end
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?