0

Im making a Pong game that displays the player and enemy score based on the screen width. Both scores should keep a 20 pixels distance from the middle of the screen and everything was working just fine but i've ran in to some problems:

First, i've made the game in a way that the player can be in both sides, left or right. When the scores are initialized, some attributes are passed to a Text class (like top or centerx) and these attributes will define where it should be placed. I tried to do it like:

self.player_score = Text(self.player.score, 32, WHITE,
                         top = 10, right = SCREEN_WIDTH/2 - 20)
self.enemy_score = Text(self.player.score, 32, WHITE,
                         top = 10, left = SCREEN_WIDTH/2 + 20)

but since the player and sides can change thats not a good solution.

Second, I want the text to always be at the same distance from the center of the screen, but when the score grows to two or more digits the text occupies more space. For the right side its not a problem, but for the left side the text keeps getting closer and closer to the middle of the screen. Here's the set_value method from my Text class:

def set_value(self, new_value):
        if new_value != self._value: 
            self._value = new_value
            self.image = self._create_surface()
            self.rect = self.image.get_rect(top     = self.rect.top,
                                            bottom  = self.rect.bottom,
                                            left    = self.rect.left,
                                            right   = self.rect.right,
                                            centerx = self.rect.centerx,
                                            centery = self.rect.centery)

How can I solve these problems?

2 Answers 2

1

Why don't you call them self.score_left and self.score_right and, depending on the player's side, put the value on the left or right instance:

# prepare the Text fields
self.score_right = Text(left, 32, WHITE, top = 10, right=SCREEN_WIDTH/2-20)
self.score_left = Text(right, 32, WHITE, top= 10, left=SCREEN_WIDTH/2+20)

# then, when updating:
a, b = playerScore, enemyScore
if playerSide == "right": a, b = b, a   # swap `a` and `b`
self.score_left.text = a
self.score_right.text = b

For the second one, to right-align text, you can render it (with its x-coordinate origin at the left side) at x = right - textWidth. You can get the width of your text using Font.size(text).

Sign up to request clarification or add additional context in comments.

2 Comments

This will swap a and b back and forth if player.side == right
Well, no. Before swapping, you set a and b, so it will swap them every tick -- wich does not matter, since you reset them before the next... thus, they will always be swapped when required.
1

I want the text to always be at the same distance from the center of the screen, but when the score grows to two or more digits the text occupies more space

To offset relative the center of a rect, you can use the virtual properties. Make sure to update rect when you re-render the cache'd text, and grab it's new rect width

For the score on the left side:

left_score.rect.centery = screen_rect.centery
left_score.rect.right = screen_rect.centerx - 20

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.