Improving game speed (better code)
This commit is contained in:
31
pyras.p8
31
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
|
||||
@ -79,24 +73,21 @@ function movepyras()
|
||||
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
|
||||
end
|
||||
end
|
||||
for index in all(toeat) do
|
||||
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
|
||||
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 ❎"
|
||||
|
Reference in New Issue
Block a user