alexthescott.bad snakes

-- bad snakes
-- alexthescott
-- 9/2/21

p={8,136,130,11,139,131,12,140,129}
pal(p,1)

function new_snake(c)
	s={}
	-- assign global_color
	s.c=c
	-- assign start point
	s.d=rnd(4)\1
	-- assign steps 'left'
	s.l=(rnd(32)\1)*2
	
	-- top
	if s.d==0 then
		s.x=2*(rnd(64)\1)
		s.y=0
		s.dx=0
		s.dy=1
	-- right
	elseif s.d==1 then
		s.x=128
		s.y=2*(rnd(64)\1)
		s.dx=-1
		s.dy=0
	-- bottom
	elseif s.d==2 then
		s.x=2*(rnd(64)\1)
		s.y=128
		s.dx=0
		s.dy=-1
	else
	-- left
		s.x=0
		s.y=2*(rnd(64)\1)
		s.dx=1
		s.dy=0
	end
	
	s.draw=function(self)
		pset(self.x,self.y,self.c)
	end
	
	s.update=function(self)
		if self.l==0 then
			if abs(self.dx)==1 then
				self.dx=0
				self.dy=1+(-2*(rnd(2)\1))
			else
				self.dx=1+(-2*(rnd(2)\1))
				self.dy=0
			end
			self.l=(rnd(32)\1)*2
		else
			self.l-=1
			self.x+=self.dx
			self.y+=self.dy
		end
	end
	
	s.destroy=function(self)
		if self.x<0 or self.x>129 or self.y<0 or self.y>129 then
			del(snakes,self)
			seen_snakes+=1
		end
	end
	add(snakes,s)
end

function new_clear()
	local clr={}
	clr.i=0
	
	clr.update=function(self)
		self.i+=1
	end
	
	clr.draw=function(self)
		line(self.i,0,self.i,128,0)
		line(128-self.i,0,128-self.i,128,0)
	end
	
	clr.destroy=function(self)
		if self.i>=65 then
			del(screen_wipe,self)
		end
	end
	
	add(screen_wipe,clr)
end

function burn()
	for p=0,256 do
		x=rnd(128)\1
		y=rnd(128)\1
		pc=pget(x,y)
		if pc!=0 and pc%3!=0 then
			if rnd(2)\1==0 then
				pset(x,y,pc+1)
			end
		end
	end
end

screen_wipe={}
global_color=1

snakes={}
seen_snakes=0

-- setup title snakes
for i=1,4 do
	new_snake(global_color)
	snakes[i].y=66
	snakes[i].x=40+i*10
end

cls()
::♥::
if t()<2 then
	print("bad snakes",44,64,1)
else
	burn()
	
	if t()%0.25==0 then
		for i=1,2 do
			new_snake(global_color)
		end
	end
	
	if seen_snakes>=64 then
			if global_color==7 then
				new_clear()
			end
			global_color+=3
			global_color%=9
			seen_snakes=0
	end
	
	for clr in all(screen_wipe) do
			clr:draw()
			clr:update()
			clr:destroy()
	end
	
	for s in all(snakes) do
		s:draw()
		s:update()
		s:destroy()
	end
	
	flip()
end
goto ♥