alexthescott.random walk ii

-- random walk ii
-- alexthescott
-- 9/27/21 7:22pm

-- new seed every day of the year
srand(31*stat(81)+stat(82))

p1={1,7,0}
p2={131,140,7}
p3={1,2,4}
p4={8,8,0}
p5={1,142,129}

p={p1,p2,p3,p4,p5}
pc=p[1+rnd(#p)\1]

pal(pc,1)

function new_obj(x,y)
	obj={}
	obj.points={}
	obj.points[1]={x=x,y=y}
	obj.points[2]={x=x+6,y=y}
	obj.points[3]={x=x+6,y=y+6}
	obj.points[4]={x=x,y=y+6}
	obj.a=0
	
	obj.draw=function(self)
		local p=self.points
		if self.a==0 then
			line(p[1].x,p[1].y,p[2].x,p[2].y,1)
			line(p[2].x,p[2].y,p[3].x,p[3].y,1)
			line(p[3].x,p[3].y,p[4].x,p[4].y,1)
			line(p[4].x,p[4].y,p[1].x,p[1].y,1)
		else
			local p=self.points[1]
			rectfill(p.x+1,p.y+1,p.x+5,p.y+5,2)
		end
	end
	
	obj.set_off=function(self)
		self.a=0
	end
	
	obj.set_on=function(self)
		self.a=0.5
	end
	
	return obj
end

function new_grid()
	grid={}
	grid.objs={}
	grid.point={x=1+rnd(15)\1,y=1+rnd(15)\1}
	grid.temp_p={x=-1,y=-1}
	for x=1,15 do
		row={}
		for y=1,15 do
			local nx=2+(x-1)*9
			local ny=2+(y-1)*9
			add(row,new_obj(nx,ny))
		end
		 add(grid.objs,row)
	end
	
	grid.draw=function(self)
		local p=self.point
		local spot=self.objs[p.x][p.y]
		
		local x=1
		for row in all(self.objs) do
			local y=1
			for obj in all(row) do
				if (self.point.x==x and
					self.point.y==y) or
					(self.temp_p.x==x and
					self.temp_p.y==y) then
						obj:set_on()
				else
					obj:set_off()
				end
				obj:draw()
				y+=1
			end
			x+=1
		end
		
		grid.temp_p={x=-1,y=-1}
	end
	
 grid.step=function(self)
		r=rnd()
		local x=0
		local y=0
		if r<=0.25 then
			x=1
		elseif r<=0.50 then
			y=1
		elseif r<=0.75 then
		 x=-1
		else
			y=-1
		end
		
		self.point.x+=x
		self.point.y+=y
		
		if self.point.x>#self.objs-1 then
			self.point.x=1
		elseif self.point.x<1 then
			self.point.x=#self.objs-1
		end
		
		if self.point.y>#self.objs-1 then
			self.point.y=1
		elseif self.point.y<1 then
			self.point.y=#self.objs-1
		end
	end
	
	return grid
end

function burn()
	for i=0,64 do
		x=rnd(128)
		y=rnd(128)
		pset(x,y,3)
	end
end

g=new_grid()
fc=0
_set_fps(60)

month=stat(81)
day=stat(82)

cls(3)
::♥::
if t()<2 then
	print("random walk ii",36,59,2)
	print(month.."/"..day,55,65)
elseif t()==2 then
	cls(3)
else
	burn()
	
	if btnp(❎) or btnp(🅾️)then
		local p={}
		p={x=1+rnd(15)\1,y=1+rnd(15)\1}
		g.temp_p=p
	end
	
	g:draw()
	
	fc+=1
	if fc%4==0 then
		g:step()
	end
	
	if fc%1200==0 then
		srand(31*stat(81)+stat(82))
		fc=0
	end
end
flip()
goto ♥