Log In  


I'm a bit of a beginner in Pico 8 programming and I wanted to know how to implement a gravity system that doesn't activate the collision function.
(I saw some tutorials on YouTube and I think I will have to change a large part of my code)

Could anyone tell me how to fix this? (ps: typos due to Google Translate)

Cart #satadomeku-0 | 2024-05-24 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

player code (related only to movement):

--ben 10

ben = {

 x = 4*8,
 y = 9*8,
 hp= 3,
 spd= 1,
 dx=0,
 dy=0,
 timer=1,
 oframe=80,
 sframe=80,
 eframe=83,
 atimer=4,
 d=false,
 form=0,
 head=96,

}

 local benwx=1
 local benwy=2
 local omini=0
  gravity=1

--ben sprite  
function _draw_ben()
 spr(ben.head,ben.x,ben.y-8,1,1,ben.d)
 spr(ben.sframe,ben.x,ben.y,1,1,ben.d)
end

function benupdate()

  lx=ben.x
  ly=ben.y
  ben.dy=gravity
  --gravity check??
  if mget(nil,ben.y+7/8)~=2 then
  ben.y+=ben.dy
  end
  --walk buttons
  if (btn(0)) then
  ben.x-=ben.spd  _animate() ben.d=true  end
  if (btn(1)) then 
  ben.x+=ben.spd  _animate() ben.d=false end   

  if collide() then 
  ben.x=lx
  ben.y=ly
  end

  --trasform button
  if (btnp(3)) then
  omini+=1  ben.form=1 _transform()
  if ben.head~=96 and omini==2 then _untrans() omini=0 end

end

end

--collision
function collide()

 local benwx=ben.x/8
 local benwx2=(ben.x+7)/8
 local benwy=ben.y/8
 local benwy2=(ben.y+7)/8

 if fget(mget(benwx,benwy),1) or fget(mget(benwx2,benwy2),1) then 
  return true
 else
  return false
 end
end

1


1

Your code already has gravity. Right now, it's linear. If you want more realistic gravity, then it would need to be an acceleration which would mean adding gravity to ben.dy instead of setting ben.dy equal to gravity.

The line after the "gravity check" comment is checking if the map tile underneath ben is solid. It's also missing the x value needed to work. I'm pretty sure the y value is wrong too. The line after the check is for updating the vertical position, which is necessary for gravity to have any meaning.

If you don't want the map check to occur, get rid of the line after the "gravity check" comment and also the "end" right after it. More work will be done for a good gravity system, but I can't advise on that without more understanding regarding what you want the game to do.


It may be better to fix the problems in which the nest of the function is unnaturally codeed before working on gravity and collision.
This is because if you proceed in this state, you may be unintended in an unexpected place.

Currently, the function of benupdate() is closed immediately after _untrans().

One of the end of _untrans () must be added behind benupdate().

function benupdate()
︙
  --trasform button
  if (btnp(3)) then
  omini+=1  ben.form=1 _transform()
  if ben.head~=96 and omini==2 then _untrans() omini=0 end

end
--There is few "end".
︙
function _untrans()
 ben.oframe=80 ben.eframe=83 ben.head=96 ben.sframe=ben.oframe   end --There is more "end".

end


[Please log in to post a comment]