Worms [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=10721 Trying to get this jump mechanic right <p>I'm trying to make a jumping mechanic for an animated sprite and i wanted to know if my code is good</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> ball = {} ball.x = 20 ball.y = 80 ball.initial_sprite = 0 ball.sprite = ball.initial_sprite ball.frames = 10 ball.is_jumping = false ball.jumping_frame = 0 ball.jumping_max_frames = 4 function update_ball_sprite() ball.sprite += 1 ball.sprite %= ball.frames end function ball_jump(speed) if ball.jumping_frame &lt; ball.jumping_max_frames then if ball.jumping_frame &lt; ball.jumping_max_frames/2 then ball.y -= speed else ball.y += speed end ball.jumping_frame+=1 else ball.jumping_frame = 0 ball.is_jumping = false end end function _init() ticker = 0 end function _update() ticker += 1 if (btn(2) and not ball.is_jumping) ball.is_jumping = true if ((ticker % 2 == 0) and ball.is_jumping) ball_jump(6) if (ticker % 3 == 0) update_ball_sprite() if (ticker % 30 == 0) ticker = 0 end function _draw() clear_screen(0) spr(ball.sprite, ball.x, ball.y) end function clear_screen(color) rectfill(0,0,127,127,color) end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>It is a basic setup for an infinite runner with jumping, i feel like i'm doing something wrong but i can't know for sure. Is this good?</p> https://www.lexaloffle.com/bbs/?tid=2378 https://www.lexaloffle.com/bbs/?tid=2378 Mon, 31 Aug 2015 15:19:57 UTC Is this how should i OOP in pico-8? <p>I never used LUA before (I come from JS and Ruby), today a saw a bit about OOP here and since pico doesn't support metatables i'd like to know if this is the right way to OOP:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> local player = {} function Player.new() local self = {} self.x = 64 self.y = 100 self.width = 20 self.height = 5 self.color = 7 self.get_x = function() --one way for method return self.x end function self.set_x(new_value) --another way for method self.x = new_value end function self.draw() rectfill(self.x, self.y, self.x + self.width, self.y + self.height, self.color) end return self end function redraw() rectfill(0,0,128,128,0) end function _init() player1 = Player.new() end function _update() local x = player1.get_x() player1.set_x(x + 1) end function _draw() redraw() player1.draw() end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>So... If there is anything wrong* there please tell me :), i know i could make a player.move method but i wanted to see getters and setters visually.</p> <p>*I do consider anti-patterns as wrong.</p> https://www.lexaloffle.com/bbs/?tid=2339 https://www.lexaloffle.com/bbs/?tid=2339 Wed, 26 Aug 2015 01:01:55 UTC