How do I implement a tennis scoring system to my game?
Posted: Fri Jan 06, 2023 3:22 am
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
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?
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
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?