Improving game speed (better code)

This commit is contained in:
2021-11-16 19:24:19 +01:00
parent 54d072abbc
commit 76788b1c24

View File

@ -60,12 +60,6 @@ function initpyras()
end end
end end
function drawpyras()
for pyra in all(pyras) do
pset(pyra.x,pyra.y,8)
end
end
function movepyras() function movepyras()
for pyra in all(pyras) do for pyra in all(pyras) do
if (pyra.x>guy.x) pyra.x-=1 if (pyra.x>guy.x) pyra.x-=1
@ -79,24 +73,21 @@ function movepyras()
end end
end end
function eatpyras() function drawandeatpyras()
local toeat={} for index,pyra in ipairs(pyras) do
for i,pyra in ipairs(pyras) do if pget(pyra.x,pyra.y)==8 then
for j=i+1,#pyras do
if pyra.x==pyras[j].x and pyra.y==pyras[j].y then
add(toeat,j)
end
end
end
for index in all(toeat) do
sfx(0) sfx(0)
deli(pyras,index) deli(pyras,index)
else
pset(pyra.x,pyra.y,8)
end
end end
end end
-->8 -->8
-- game -- game
game={ game={
level=1, level=7,
slowdown=0.1 slowdown=0.1
} }
@ -114,10 +105,11 @@ function drawbg()
if(#pyras>99)offset+=1 if(#pyras>99)offset+=1
rectfill(0,0,127,127,1) rectfill(0,0,127,127,1)
print(#pyras,128-4*offset,1,7) print(#pyras,128-4*offset,1,7)
print("l:"..game.level,1,1,7)
end end
function waitormove() function waitormove()
if game.tick>min(10,game.slowdown*#pyras) then if game.tick>min(30,game.slowdown*#pyras) then
moveguy() moveguy()
movepyras() movepyras()
game.tick=0 game.tick=0
@ -130,7 +122,6 @@ function updategame()
if(btnp(❎))game.state=1 if(btnp(❎))game.state=1
elseif game.state==1 then elseif game.state==1 then
waitormove() waitormove()
eatpyras()
if guy.dead then if guy.dead then
game.state=3 game.state=3
game.level=max(0,game.level-1) game.level=max(0,game.level-1)
@ -149,7 +140,7 @@ function drawgame()
cls() cls()
drawbg() drawbg()
drawguy() drawguy()
drawpyras() drawandeatpyras()
if game.state==0 then if game.state==0 then
cursor(48,61,7) cursor(48,61,7)
print "press ❎" print "press ❎"