How do I implement a tennis scoring system to my game?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

How do I implement a tennis scoring system to my game?

Post by MaxGamz »

I basically have most of the logic for my game completed, but my biggest issue is with tennis. My game isn't pong but what I am trying to do is to get the ball to bounce once on one side of the field without the player losing a point like how regular tennis works. However, I had trouble getting lua to recognize when the ball bounced twice in the same area instead of bouncing twice in general. For example, the ball will bounce once in player 1's area but when it is bounces again in player 2's area, the ball will respawn in player 2's area and player 1 scores a point.

This is the code I have now

Code: Select all

function Ball:winOrLose()
    if bounceCounter == 2  and self.x < 320 then
        playerTwoScore = playerTwoScore + 1
        
        switch = 600
        Ball:respawn()
        bounceCounter = 0
    elseif bounceCounter == 2 and self.x > 340 then
        playerOneScore = playerOneScore + 1

        switch = 690
        Ball:respawn()
        bounceCounter = 0
    end
end
The if statements check which position the ball is and keeps track of how many times it bounced

What I tried to do is make 2 pairs of if statements to check if the ball is in the same position for the first bounce as in the second one. I got no errors but the ball bounced normally, and no points were gained or lost. Essentially the code didn't accomplish its task. Is there a way where I can implement this logic in a way where it can work as intended?
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: How do I implement a tennis scoring system to my game?

Post by Andlac028 »

You can try to save bounce count and bounce position (index of player’s side) and if you add bounce, check if side is same, if yes, check bounces count, if not, reset side and set bounce count to 1. Plus as it is optional to bounce the ball, make sure, if player prevents ball from bouncing, you also resets counter and position.
RNavega
Party member
Posts: 418
Joined: Sun Aug 16, 2020 1:28 pm

Re: How do I implement a tennis scoring system to my game?

Post by RNavega »

Code: Select all

local bounceCounter = 0

(...)

-- When a bounce happens:
if self.x < 320 then
    -- The bounce happened on the left.
    if bounceCounter < 0 then
        -- Counter is already negative, it's a repeated left bounce.
        -- (Add a point to player 2, respawn the ball.)
    else
        -- First bounce on the left.
        bounceCounter = -1
    end
elseif self.x > 340 then
    if bounceCounter > 0 then
        -- Counter is already positive, it's a repeated right bounce.
        -- (Add a point to player 1, respawn the ball.)
    else
        bounceCounter = 1
    end
else
    -- Bounce happened in the X space between (320, 340).
    bounceCounter = 0
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot], dusoft and 7 guests