Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

1 Player Pong game made by a n00b

I’m brand spankin new to Codea. And just as new to Lua. I’m coming from Python and a web development background. I don’t know any Lua except for the simple circle I made move around by touch. I watch a LOT of YouTube and came across a game I thought would be easy(er) to make. But I don’t know ANY Lua syntax and I don’t know the correct terminology for what I’m trying to do. I wanna make a single player Pong game where the user is a rectangle that rotates in a circle and the ball starts in the center and the player has to keep it from going outside ofc. So here are my questions.

- How do a make a point of rotation and set a radius for it?
- How do I set a rectangle on the edge of the circle and only rotate along the edge?
- How do I make a ball that moves across the screen?
- I know I’ll need to set a speed variable for the ball, so how do I implement it to the ball?

Sorry for the long post, I just wanna get started with Lua and I’m very excited to create games. The only problem is there’s not much documentation on Lua game development. Least I can’t find much. Thanks for the help.

Comments

  • Posts: 1,547

    I don't know the answers off the top of my head, but I'll tell you exactly what I'd do to find them: use the forum search. There is a ton, ton, ton of helpful information available that way. And there are helpful people here too who will probably jump in and are able to rattle off the answers right away.

    I'm just chiming in to say, my personal avenue of problem solving is always to try the search first.

  • Posts: 1,275

    Here's a little sample, just a starting thing to talk about:

    -- Rotator
    
    function setup()
        center = vec2(WIDTH/2, HEIGHT/2)
        paddleRadius = vec2(300,0)
        angle = 0
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(40, 40, 50)
    
        strokeWidth(3)
        fill(255,0,0)
        stroke(255,255,0)
        local paddlePoint = paddleRadius:rotate(angle) + center
        angle = angle + 0.01
        if angle > math.pi*2 then angle = 0 end
        rect(paddlePoint.x, paddlePoint.y, 40,20) 
    end
    

    I'm not sure what level to pitch, but as we chat we'll sync up.

    The idea here is that a vector of two values, x and y, can be rotated by an angle in radians (zero to two times pi). So I create a vector to rotate. Then in draw, I rotate by the current angle, add screen center, increment the angle, setting it back to zero if it goes over two pi.

    Then I draw the rectangle. You can remove the setting to center, or change it, to see what that does.

    I imagine that a next step might be to make the rectangle always face the center. I'll do that next.

    Ask questions, so we can get a sense of where you need help. I apologize if this reply isn't aimed at the right level.

    Enjoy!

  • Posts: 1,275

    Here's v2:

    ~~~lua-- Rotator v0.2

    function setup()
    center = vec2(WIDTH/2, HEIGHT/2)
    paddleRadius = vec2(300,0)
    angle = 0
    end

    -- This function gets called once every frame
    function draw()
    pushMatrix()
    background(40, 40, 50)

    strokeWidth(3)
    fill(255,0,0)
    stroke(255,255,0)
    translate(center.x, center.y)
    local paddlePoint = paddleRadius:rotate(angle)
    angle = angle + 0.01
    if angle > math.pi*2 then angle = 0 end
    rect(paddlePoint.x, paddlePoint.y, 40,20) 
    popMatrix()
    

    end

    ~~~

    The difference here is the push and pop matrix around the drawing, and the translated call to center. That starts drawing at the center of the screen, everything offset to there. So I don't add center to the radius any more.

    The rectangle still goes around the same way.

  • Posts: 1,275

    Here's v 0.3

    -- Rotator v0.3
    
    function setup()
        center = vec2(WIDTH/2, HEIGHT/2)
        paddleRadius = vec2(300,0)
        angle = 0
    end
    
    -- This function gets called once every frame
    function draw()
        pushMatrix()
        background(40, 40, 50)
    
        strokeWidth(3)
        fill(255,0,0)
        stroke(255,255,0)
        translate(center.x, center.y)
        local paddlePoint = paddleRadius:rotate(angle)
        angle = angle + 0.01
        if angle > math.pi*2 then angle = 0 end
        translate(paddlePoint.x, paddlePoint.y)
        rotate(math.deg(angle) + 90)
        rect(0,0, 40,20) 
        popMatrix()
    end
    
    

    Now I translate again (this could be done in one step, but I showed two to make the point that translates add to each other). The second translate puts the drawing center wherever the paddle will be.

    Then the rotate (this one is in degrees, thanks Codea) turns the screen by whatever the current angle is, plus 90 degrees, which makes the rectangle always face the center.

    Questions welcome! I'll stop here for now.

  • dave1707dave1707 Mod
    Posts: 9,725

    Here’s an update to Ron’s code. To move the paddle, slide your finger back and forth on the screen.

    viewer.mode=FULLSCREEN
    
    function setup()
        center = vec2(WIDTH/2, HEIGHT/2)
        paddleRadius = vec2(WIDTH/2-10,0)
        angle = 0
    end
    
    function draw()
        background(40, 40, 50)
        pushMatrix()    
        strokeWidth(3)
        fill(255,0,0)
        stroke(255,255,0)
        translate(center.x, center.y)
        local paddlePoint = paddleRadius:rotate(angle)
        translate(paddlePoint.x, paddlePoint.y)
        rotate(math.deg(angle) + 90)
        rect(0,0, 80,20) 
        popMatrix()
    end
    
    function touched(t)
        if t.state==CHANGED then
            angle=angle+t.deltaX/50
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 9,725

    @rockinspots See the latest code I PM’d you. This is a very interesting idea.

Sign In or Register to comment.