Programming Pong

Hi,
I decided to start learning Codea Lua by writing a game of Pong. The enclosed code is only my initial idea of movement of bat and ball, and therein a couple of questions…

Firstly there is probably a much more elegant way of moving the ball around than the way I’ve done it.
Secondly, if you increase velocity of the ball it seriously blurs, moreso horizontally than vertically for some reason.

Any suggestions would be most appreciated… next step is collision detection with the bat.

Many thanks,

David

PS Is there a specific way to cut and paste code to stop this strange formatting?

-- Pong 1.4

function setup()
    x = 100    -- (x,y) ball coords
    y = 100
    a = 25    -- (a,b) bat coords
    b = 100
    moveRight = 1     -- flag 1 is right, 0 left
    moveUp = 1        -- flag 1 is up, 0 down
    watch("x")
    watch("y")
    watch("moveRight")
    watch ("moveUp")
    parameter("horiz_vel", 0, 10, 1)
    parameter("vert_vel", 0, 10, 1)
    end

function draw()
    
    background(0,0,0)
    fill(255, 255, 255, 255)
    rect(a,b,15,50)            -- bat
    
    if moveRight == 1 then
    fill(255, 255, 255, 255)
    rect(x,y,10,10)            -- ball l-r
    x = x + horiz_vel
    end
    
   if x > 750 then
    moveRight = 0
    end

    if moveRight == 0 then
    fill(255, 255, 255, 255)
    rect(x,y,10,10)            -- ball r-l
    x = x - horiz_vel
    end
    
    if x == 0 then
    moveRight = 1
    end

    if moveUp == 1 then
    fill(255, 255, 255, 255)
    rect(x,y,10,10)            -- ball move up
    y = y + vert_vel
    end
    
    if y > 750 then
    moveUp = 0                 
    end

    if moveUp == 0 then
    fill(255, 255, 255, 255)
    rect(x,y,10,10)            -- ball move down
    y = y - vert_vel
    end
    
    if x < 0 then
    moveRight = 1
    end

    if y < 0 then
    moveUp = 1
    end     
    
end

    function touched (touch)
    b = touch.y
end

(Use fences for code blocks: ~~~ on a line by themselves before and after the code. Plus a blank line outside the fences. I added them to your post.)

Hi @David I hope you don’t mind, but I removed code that you really didn’t need. I also changed the touch code to move the bat just by sliding or flicking your finger anywhere up or down the screen. I also added code to check if the ball hit the bat. Look over the code and make your additions to it. I also moved the edges in where the ball bounces off them.


-- Pong 1.4

function setup()
    x = 100    -- (x,y) ball coords
    y = 100
    a = 25    -- (a,b) bat coords
    b = 100
    horiz_vel=1
    vert_vel=1
    watch("x")
    watch("y")
    parameter("horiz_vel", 0, 10, 1)
    parameter("vert_vel", 0, 10, 1)
end

function draw()
    background(0,0,0)
    fill(255, 255, 255, 255)
    rect(a,b,15,50)            -- bat
    rect(x,y,10,10)            -- ball
    x=x+horiz_vel
    y=y+vert_vel
    
    if x>WIDTH-10 or x<10 then    -- is x at edge
        horiz_vel=horiz_vel*-1    -- change direction
    end
    
    if y>HEIGHT-10 or y<10 then    -- is y at edge
        vert_vel=vert_vel*-1       -- change direction
    end
    
    if x<35 and y>b and y<b+50 then    -- check if ball hit bat
        horiz_vel=horiz_vel*-1         -- change direction
    end
end

function touched (touch)
    b=b+touch.deltaY    -- move bat smoothly
end

Thank you so much Dave - that is great! I knew there would be a much easier way than writing the ball draw code 4 times :wink: That is so much more logical. I guess deltaY is an inbuilt smoothing function?

I will undoubtedly need more help and advice as I go along. I’m starting with Pong, then having a go at Breakout, Classic Space Invaders… if you or anyone know or hear of any Codea tutorials for these, please let me know. I’m looking at other people’s code but it’s usually not easy for a beginner to follow what is going on.

David

Hi @David If you’re looking to learn from others, start looking thru the other threads. There is a wealth of information and examples. The quickest way to learn is to try different things, then when you reach a point that something doesn’t make sense, there are plenty of people here that will help explain things.

Also, Codea has a breakout example in the code samples installed in Codea itself…

Hey @David I am also pretty new at this, but one interesting thing I have learned is using vec2 (vector 2) for coordinates of the ball’s position. Example:

ballPos = vec2(WIDTH/2,HEIGHT/2)

Then you can refer to the x and y of the ball like this:

ballPos.x and ballPos.y

There is an excellent vec2 tutorial in the wiki here:
https://bitbucket.org/TwoLivesLeft/core/wiki/bvec2