Log In  
Follow
Worms

I'm trying to make a jumping mechanic for an animated sprite and i wanted to know if my code is good

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 < ball.jumping_max_frames then

        if ball.jumping_frame < 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

[ Continue Reading.. ]

1
0 comments



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:

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

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.

*I do consider anti-patterns as wrong.

1
6 comments