From 76788b1c24fd64ecb2e5fcf86f9b30f42320b230 Mon Sep 17 00:00:00 2001 From: djib Date: Tue, 16 Nov 2021 19:24:19 +0100 Subject: [PATCH] Improving game speed (better code) --- pyras.p8 | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/pyras.p8 b/pyras.p8 index ad78de3..5741442 100644 --- a/pyras.p8 +++ b/pyras.p8 @@ -60,12 +60,6 @@ function initpyras() end end -function drawpyras() - for pyra in all(pyras) do - pset(pyra.x,pyra.y,8) - end -end - function movepyras() for pyra in all(pyras) do if (pyra.x>guy.x) pyra.x-=1 @@ -76,27 +70,24 @@ function movepyras() pyra.y==guy.y then guy.dead=true end - end + end end -function eatpyras() - local toeat={} - for i,pyra in ipairs(pyras) do - for j=i+1,#pyras do - if pyra.x==pyras[j].x and pyra.y==pyras[j].y then - add(toeat,j) - end +function drawandeatpyras() + for index,pyra in ipairs(pyras) do + if pget(pyra.x,pyra.y)==8 then + sfx(0) + deli(pyras,index) + else + pset(pyra.x,pyra.y,8) end end - for index in all(toeat) do - sfx(0) - deli(pyras,index) - end end + -->8 -- game game={ - level=1, + level=7, slowdown=0.1 } @@ -114,10 +105,11 @@ function drawbg() if(#pyras>99)offset+=1 rectfill(0,0,127,127,1) print(#pyras,128-4*offset,1,7) + print("l:"..game.level,1,1,7) end function waitormove() - if game.tick>min(10,game.slowdown*#pyras) then + if game.tick>min(30,game.slowdown*#pyras) then moveguy() movepyras() game.tick=0 @@ -130,7 +122,6 @@ function updategame() if(btnp(❎))game.state=1 elseif game.state==1 then waitormove() - eatpyras() if guy.dead then game.state=3 game.level=max(0,game.level-1) @@ -149,7 +140,7 @@ function drawgame() cls() drawbg() drawguy() - drawpyras() + drawandeatpyras() if game.state==0 then cursor(48,61,7) print "press ❎"