Help with a scoring system?

Hi, I’m 17 and new to Corona. I want to go to college and get a degree in Computer Science so I thought learning Corona would help me a lot. Right now, I’m making a Pong-like game as practice before making something more complicated. However, I can’t seem to figure out how to code a scoring system in such a way that the score increases by 1 every time the ball/puck goes past one of the paddles.

Could someone write the code and explain its parts to me? I would greatly appreciate it. Thank you.

if ball.x < paddle1.x then player1Score = player1Score + 1

if ball.x > paddle2.x then player2Score = player2Score + 1

maybe something like this without seeing some of your code its hard to say exactly

Oh right, my bad. Here are my images and whatnot: 

local puck = display.newImage ("images/puck.png"); puck.x = \_W; puck.y = \_H; local paddle1 = display.newImage ("images/paddle1.png"); paddle1.x = \_W; paddle1.y = 455; local paddle2 = display.newImage ("images/paddle2.png"); paddle2.x = \_W; paddle2.y = 20;

And here is the scoring I’m trying to implement: 

local player1Score = display.newText("p1: 0", 0, 0, native.systemFont, 16) player1Score:setReferencePoint(display.CenterLeftReferencePoint); local player2Score = display.newText("p2: 0", 300, 0, native.systemFont, 16) player2Score:setReferencePoint(display.CenterRightReferencePoint); if puck.y \< paddle1.y then player1Score = player1Score + 1 else if puck.y \> paddle2.y then player2Score = player2Score + 1 end end

As you can see, I tried doing what you suggested but I get an error saying “attempt to perform arithmetic on local player1Score (a table value)”

Thank you for your help.

Hi there,

I think there’s two issues here.

The first issue is that a score is a number, but the variable player1Score is not the score, it’s a text object that displays the score.  As a result, you can’t do arithmetic with it.

What you need is two different variables (for each player).  One variable is a number that stores the score itself.  You can do arithmetic on that.  The other is the text object that displays the score.  When the score changes, you would update the text object’s .text property to show the new score.

The second issue is that you need to place your score-checking code in an enterFrame listener, so that it checks the puck’s position every frame.

Hope this helps.

  • Andrew

Hi Andrew, I see what you mean. I’ve managed to get it so that there’s no more errors displayed, but the score still doesn’t update (and I’m not sure if I did it correctly because I’m not quite familiar with events and listeners  :wacko:

local p1Score = 0 local p2Score = 0 local player1Score = display.newText( p1Score , 0, 0, native.systemFont, 16) player1Score:setReferencePoint(display.CenterLeftReferencePoint); local player2Score = display.newText( p2Score , 300, 0, native.systemFont, 16) player2Score:setReferencePoint(display.CenterRightReferencePoint); if puck.y \> paddle1.y then p1Score = p1Score + 1 else if puck.y \< paddle2.y then p2Score = p2Score + 1 end end Runtime:addEventListener( "enterFrame", puck );

Could you show me an example, if it’s not too much hassle?

Thanks.

what code are you using to move puck

Just this: 

physics.setGravity(0, 0); physics.setDrawMode("normal"); physics.addBody (puck, {bounce = .9, radius = 18, friction = 0, speed = 1}); puck.angularVelocity = 3; puck:applyLinearImpulse( .5, .1, puck.x, puck.y );

Something like this should do the trick:

[lua]

local p1Score = 0

local p2Score = 0

local gameActive = true

local player1Score = display.newText( p1Score , 0, 0, native.systemFont, 16)

player1Score:setReferencePoint(display.CenterLeftReferencePoint);

local player2Score = display.newText( p2Score , 300, 0, native.systemFont, 16)

player2Score:setReferencePoint(display.CenterRightReferencePoint);

local gameLoop = function (event)

    if gameActive then

        if puck.y > paddle1.y then

            p1Score = p1Score + 1

            player1Score.text = p1score

            gameActive= false

        else

            if puck.y < paddle2.y then

                p2Score = p2Score + 1

                player1Score.text = p1score

                gameActive = false

            end

    else

 – reset puck position and set up next point here, then reset gameActive to true

    end

end

Runtime:addEventListener( “enterFrame”, gameLoop );

[/lua]

Thank you, that seems to change the score, but it either disappears or changes the text color to black or changes the text location (not sure because my background is black).

I noticed the corona simulator output window says “attempting to set property (text) with nil” after the puck goes past either of the paddles.
Could someone help me fix this error and make the score count?

Also, how would I go about resetting the position of the puck once it goes past one of the paddles and a point is added one of the scores?

Thanks so much for any help.

Would the resetting of the puck have to be a function?  :wacko:

if ball.x < paddle1.x then player1Score = player1Score + 1

if ball.x > paddle2.x then player2Score = player2Score + 1

maybe something like this without seeing some of your code its hard to say exactly

Oh right, my bad. Here are my images and whatnot: 

local puck = display.newImage ("images/puck.png"); puck.x = \_W; puck.y = \_H; local paddle1 = display.newImage ("images/paddle1.png"); paddle1.x = \_W; paddle1.y = 455; local paddle2 = display.newImage ("images/paddle2.png"); paddle2.x = \_W; paddle2.y = 20;

And here is the scoring I’m trying to implement: 

local player1Score = display.newText("p1: 0", 0, 0, native.systemFont, 16) player1Score:setReferencePoint(display.CenterLeftReferencePoint); local player2Score = display.newText("p2: 0", 300, 0, native.systemFont, 16) player2Score:setReferencePoint(display.CenterRightReferencePoint); if puck.y \< paddle1.y then player1Score = player1Score + 1 else if puck.y \> paddle2.y then player2Score = player2Score + 1 end end

As you can see, I tried doing what you suggested but I get an error saying “attempt to perform arithmetic on local player1Score (a table value)”

Thank you for your help.

Hi there,

I think there’s two issues here.

The first issue is that a score is a number, but the variable player1Score is not the score, it’s a text object that displays the score.  As a result, you can’t do arithmetic with it.

What you need is two different variables (for each player).  One variable is a number that stores the score itself.  You can do arithmetic on that.  The other is the text object that displays the score.  When the score changes, you would update the text object’s .text property to show the new score.

The second issue is that you need to place your score-checking code in an enterFrame listener, so that it checks the puck’s position every frame.

Hope this helps.

  • Andrew

Hi Andrew, I see what you mean. I’ve managed to get it so that there’s no more errors displayed, but the score still doesn’t update (and I’m not sure if I did it correctly because I’m not quite familiar with events and listeners  :wacko:

local p1Score = 0 local p2Score = 0 local player1Score = display.newText( p1Score , 0, 0, native.systemFont, 16) player1Score:setReferencePoint(display.CenterLeftReferencePoint); local player2Score = display.newText( p2Score , 300, 0, native.systemFont, 16) player2Score:setReferencePoint(display.CenterRightReferencePoint); if puck.y \> paddle1.y then p1Score = p1Score + 1 else if puck.y \< paddle2.y then p2Score = p2Score + 1 end end Runtime:addEventListener( "enterFrame", puck );

Could you show me an example, if it’s not too much hassle?

Thanks.

what code are you using to move puck

Just this: 

physics.setGravity(0, 0); physics.setDrawMode("normal"); physics.addBody (puck, {bounce = .9, radius = 18, friction = 0, speed = 1}); puck.angularVelocity = 3; puck:applyLinearImpulse( .5, .1, puck.x, puck.y );

Something like this should do the trick:

[lua]

local p1Score = 0

local p2Score = 0

local gameActive = true

local player1Score = display.newText( p1Score , 0, 0, native.systemFont, 16)

player1Score:setReferencePoint(display.CenterLeftReferencePoint);

local player2Score = display.newText( p2Score , 300, 0, native.systemFont, 16)

player2Score:setReferencePoint(display.CenterRightReferencePoint);

local gameLoop = function (event)

    if gameActive then

        if puck.y > paddle1.y then

            p1Score = p1Score + 1

            player1Score.text = p1score

            gameActive= false

        else

            if puck.y < paddle2.y then

                p2Score = p2Score + 1

                player1Score.text = p1score

                gameActive = false

            end

    else

 – reset puck position and set up next point here, then reset gameActive to true

    end

end

Runtime:addEventListener( “enterFrame”, gameLoop );

[/lua]

Thank you, that seems to change the score, but it either disappears or changes the text color to black or changes the text location (not sure because my background is black).

I noticed the corona simulator output window says “attempting to set property (text) with nil” after the puck goes past either of the paddles.
Could someone help me fix this error and make the score count?

Also, how would I go about resetting the position of the puck once it goes past one of the paddles and a point is added one of the scores?

Thanks so much for any help.

Would the resetting of the puck have to be a function?  :wacko: