diff --git a/beamclsh.p8 b/beamclsh.p8 index 710f469..d11c1e2 100644 --- a/beamclsh.p8 +++ b/beamclsh.p8 @@ -10,9 +10,9 @@ function _init() -- 1: action -- 2: score -- 3: over - if debounce==nil then - debounce=0 - end + if debounce==nil then debounce=0 end + if timer==nil then timer=0 end + cur_state=0 init_field() init_player() @@ -23,8 +23,12 @@ function _update() if cur_state==0 then ply:title_input() elseif cur_state==1 then - for p in all(plys) do - p:input() + for p in all(plys) do + if p.human then + p:input() + else + p:think() + end p:update() p:check() end @@ -35,6 +39,7 @@ function _update() ply:end_input() end ply:tick_debounce() + timer+=1 end function _draw() @@ -54,10 +59,10 @@ function init_field() field={ x1=0, - y1=8, - x2=127, - y2=127 - } + y1=8, + x2=127, + y2=127 + } function field:width() return self.x2-self.x1 @@ -90,7 +95,7 @@ function init_field() y=rndint(pfield.y2), len=rndint(10), spd=rndint(5) - } + } end for i=11,50 do @@ -99,7 +104,7 @@ function init_field() y=rndint(field.y2)+8, len=0, spd=0 - } + } end end @@ -118,7 +123,7 @@ function upd_bg() stars[i].x+stars[i].len, stars[i].y+pfield.y1, 13 - ) + ) end end @@ -136,6 +141,7 @@ function init_player() ply={ id=1, + human=true, scrx=10, scry=1, x=pfield.x1, @@ -144,7 +150,7 @@ function init_player() yspd=0, col=10, crash=0, - } + } function ply:input() id=self.id-1 @@ -158,6 +164,92 @@ function init_player() self:redirect(0,1) end end + + function ply:think() + if rndint(100)<5 then + self:check_around() + else + self:check_ahead() + end + end + + function ply:check_ahead() + if self.xspd!=0 then + if self:check_x(self.xspd) then self:check_around() end + else + if self:check_y(self.yspd) then self:check_around() end + end + end + + function ply:check_x(spd) + return sget(self.x+spd,self.y)!=0 or self.x+spdpfield.x2 + end + + function ply:check_y(spd) + return sget(self.x,self.y+spd)!=0 or self.y+spdpfield.y2 + end + + function ply:check_around() + -- basically narrow down the list of possible directions and randomly select a random remaining one. + local possible_dirs={ + {spd=-1,axis="y"}, -- north + {spd=1,axis="y"}, -- south + {spd=1,axis="x"}, -- east + {spd=-1,axis="x"} -- west + } + + local available_dirs={} + + for d in all(possible_dirs) do + d.spd=self:check_dir(d) + if d.spd!=0 then + add(available_dirs,d) + end + end + + local count=0 + + for d in all(available_dirs) do + count+=1 + end + + if count>0 then + self:favour_direction(available_dirs, count) + end + end + + function ply:check_dir(dir) + for c=1,2 do + local spd=0 + if dir.spd!=0 then + spd=(c*dir.spd) + end + + if dir.axis=="x" then + if self:check_x(spd) then + return 0 + end + elseif dir.axis=="y" then + if self:check_y(spd) then + return 0 + end + end + end + return dir.spd + end + + function ply:favour_direction(dirs, length) + dir=dirs[rndint(length)] + + if dir.axis=="x" then + self.xspd=dir.spd + self.yspd=0 + else + self.xspd=0 + self.yspd=dir.spd + end + + end function ply:score_input() if btn(🅾️) and debounce==0 then @@ -169,6 +261,7 @@ function init_player() function ply:title_input() if btn(🅾️) and debounce==0 then cur_state=1 + srand(timer) debounce=10 end end @@ -199,9 +292,9 @@ function init_player() function ply:check() if self.xpfield.x2 or - self.ypfield.y2 then + self.x>pfield.x2 or + self.ypfield.y2 then self:halt() end @@ -226,11 +319,12 @@ function init_player() plys={ copy(ply), copy(ply) - } + } p2=plys[2] p2.scrx=74 p2.id=2 + p2.human=false p2.x=pfield.x2 p2.xspd=-1 p2.col=12 @@ -258,7 +352,9 @@ function next_round() pfield:clear() init_field() init_player() + cur_state=1 + srand(timer) for p in all(plys) do if scores[p.id]>=win_limit then @@ -290,8 +386,8 @@ end -->8 -- ui function draw_hud() - for p in all(plys) do - print(scores[p.id],p.scrx,p.scry,p.col) + for p in all(plys) do + print(scores[p.id],p.scrx,p.scry,p.col) end if cur_state==0 then @@ -319,7 +415,7 @@ function report_winner(id) end function draw_title() - print("beam",64-(4*4),60,11) + print("beam",64-(4*4),60,10) print("clash",64-(4*2),70,12) print("press 🅾️",64-(4*4),80,7)