-1

I'm creating a game where you need to catch balls in a box. And I had the following problem: I want to make the counter count the hit ONLY in the hole of the box (I drew this hit zone with a red marker).

But it turned out that the counter counted the touch of the ball with the box anywhere, and I don't need it that way. I want to make the counter count only getting into the hole of the box (so that it is logical).

A piece of code:

# Class for objects
class GameObjects(sprite.Sprite):
    def __init__(self, img, x, y, width, height, speed):
        sprite.Sprite.__init__(self)
        self.image = transform.scale(image.load(img), (width, height))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.width = width
        self.height = height
        self.speed = speed
    # Метод прорисовки
    def reset(self):
        window.blit(self.image, (self.rect.x, self.rect.y))

# Class for player
class Player(GameObjects):
    def update(self):
        keys = key.get_pressed()
        if keys[K_a] and self.rect.x > 5:
            self.rect.x -= self.speed
        if keys[K_d] and self.rect.x < window_width - self.width - 2:
            self.rect.x += self.speed    
        

# Class for balls
class Balls(Player):
    def update(self):
        self.rect.y += self.speed
        if self.rect.y > window_height:
            self.rect.x = randint(5, window_width - 85)
            self.rect.y = randint(-200, -50)            

# Making objects
box = Player('box.png', 450, 660, 250, 180, 25)
balls = sprite.Group()
for i in range(2):
    ball = Balls('ball.png', randint(5, window_width - 85), randint(-200, -50), 85, 85, randint(3, 4))
    balls.add(ball)

# Score
score = 0 

# Game cycle
game = True
finish = False
clock = time.Clock()

while game:
    for e in event.get():
        if e.type == QUIT:
            game = False

    # Displaying sprites
    if finish != True:
        window.blit(background, (0,0))
        box.reset()
        box.update()

        balls.draw(window)
        balls.update()

    # The appearance of balls
    if randint(1, 200) == 1:
        ball = Balls("ball.png", randint(5, window_width - 85),randint(-200, -50), 85, 85, randint(3, 4))
        balls.add(ball)
        display.update()

    # Touching the ball with the platform
    if sprite.spritecollide(box, balls, True):
            ball.remove()
            score += 1

That's where the hit should count: https://i.stack.imgur.com/DhQIS.jpg The game itself: https://skr.sh/sKmvhu1YsqS?a

Merex
  • 1
  • 1

1 Answers1

0

I'd recommend customizing the sprite rectangle for the box, so that it only includes the surface (by which I assume you mean the top surface).

class Player(GameObjects):
    def __init__(self, img, x, y, width, height, speed):
        super().__init__(self, img, x, y, width, height, speed)
        self.rect.update(self.rect.left, self.rect.top, self.rect.width, 1)  # <--

    def update(self):
        keys = key.get_pressed()
        if keys[K_a] and self.rect.x > 5:
            self.rect.x -= self.speed
        if keys[K_d] and self.rect.x < window_width - self.width - 2:
            self.rect.x += self.speed

Additionally, Balls should probably not be inheriting from Player and should be inheriting from GameObjects instead, both for convention and so this new Player functionality doesn't apply to balls as well.

Hope this helps!

Krishnan Shankar
  • 780
  • 9
  • 29