.stop() function doesn't work with sprites

85 Views Asked by At

I was writing a simple ping pong game as an introduction to arcade and in the loop on line 51. It is supposed to stop the sprites moving but it just isn't happening. I tried to remove the self., but got an error, tried replacing self. with arcade., but still getting an error.

import arcade

screen_width = 900
screen_height = 800
screen_title = 'Pingpong'

class MainGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.ball = ball('ball.png', 0.4)
        self.bar = bar('barman.png', 0.5)
        self.live = 3
        self.score = 0

    def setup(self):
        self.ball.center_x = 450
        self.ball.center_y = 400
        self.ball.stepx = 15
        self.ball.stepy = 15

        self.bar.center_x = 400
        self.bar.center_y = 70
        self.bar.stepx = 0

    def on_draw(self):
        arcade.start_render()
        self.ball.draw()
        self.bar.draw()
        arcade.set_background_color(arcade.color.DARK_GRAY)
        life = f'Жизней: {self.live}'
        scrore = f'Счёт: {self.score}'
        arcade.draw_text(life, 750, 740, arcade.color.BLACK, 20)
        arcade.draw_text(scrore, 10, 740, arcade.color.BLACK, 20)
        if self.live == 0:
            arcade.draw_text('Вы лох', 400, 400, arcade.color.RED_DEVIL, 60)
        arcade.finish_render()

    def update(self, delta_time: float):
        self.ball.update()
        self.bar.update()
        colliding = arcade.check_for_collision(self.ball, self.bar)
        if colliding:
            self.ball.bottom = self.example.top
            self.ball.stepy = -self.ball.stepy
            self.score += 1

        if self.ball.bottom < 0:
            self.live -= 1
            self.ball.center_x = 450
            self.ball.center_y = 400

        if self.live == 0:
            ball.stop()
            bar.stop()

    def on_key_press(self, key, modifiers):
        if key == arcade.key.A:
            self.bar.stepx = -15
        if key == arcade.key.D:
            self.bar.stepx = 15

    def on_key_release(self, key, modifiers):
        if key == arcade.key.A or key == arcade.key.D:
            self.bar.stepx = 0

class ball(arcade.Sprite):
    def update(self):
        self.center_x += self.stepx
        self.center_y += self.stepy

        if self.bottom < 0 or self.top > screen_height:
            self.stepy = -self.stepy
        if self.left < 0 or self.right > screen_width:
            self.stepx = -self.stepx

class bar(arcade.Sprite):
    def update(self):
        self.center_x += self.stepx
        if self.left < 0 or self.right > screen_width:
            self.stepx = 0



game = MainGame(screen_width, screen_height, screen_title)
game.setup()
arcade.run()

I tried removing the self. and got an error, tried replacing the self. with an arcade, got an error too. The loop works too it seems

1

There are 1 best solutions below

0
Alderven On

Calling stop doesn't make sense in your code because at the same time you're updating your sprites coordinates. You need to update ball and bar only wnen live is above 0:

import arcade

screen_width = 900
screen_height = 800
screen_title = 'Pingpong'


class MainGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.ball = ball('ball.png', 0.4)
        self.bar = bar('barman.png', 0.5)
        self.live = 3
        self.score = 0

    def setup(self):
        self.ball.center_x = 450
        self.ball.center_y = 400
        self.ball.stepx = 15
        self.ball.stepy = 15

        self.bar.center_x = 400
        self.bar.center_y = 70
        self.bar.stepx = 0

    def on_draw(self):
        arcade.start_render()
        self.ball.draw()
        self.bar.draw()
        arcade.set_background_color(arcade.color.DARK_GRAY)
        life = f'Жизней: {self.live}'
        score = f'Счёт: {self.score}'
        arcade.draw_text(life, 750, 740, arcade.color.BLACK, 20)
        arcade.draw_text(score, 10, 740, arcade.color.BLACK, 20)
        if self.live == 0:
            arcade.draw_text('Вы лох', 400, 400, arcade.color.RED_DEVIL, 60)
        arcade.finish_render()

    def update(self, delta_time: float):
        if self.live > 0:
            self.ball.update()
            self.bar.update()
            colliding = arcade.check_for_collision(self.ball, self.bar)
            if colliding:
                self.ball.bottom = self.bar.top
                self.ball.stepy = -self.ball.stepy
                self.score += 1

            if self.ball.bottom < 0:
                self.live -= 1
                self.ball.center_x = 450
                self.ball.center_y = 400

    def on_key_press(self, key, modifiers):
        if key == arcade.key.A:
            self.bar.stepx = -15
        if key == arcade.key.D:
            self.bar.stepx = 15

    def on_key_release(self, key, modifiers):
        if key == arcade.key.A or key == arcade.key.D:
            self.bar.stepx = 0


class ball(arcade.Sprite):
    def update(self):
        self.center_x += self.stepx
        self.center_y += self.stepy

        if self.bottom < 0 or self.top > screen_height:
            self.stepy = -self.stepy
        if self.left < 0 or self.right > screen_width:
            self.stepx = -self.stepx


class bar(arcade.Sprite):
    def update(self):
        self.center_x += self.stepx
        if self.left < 0 or self.right > screen_width:
            self.stepx = 0


game = MainGame(screen_width, screen_height, screen_title)
game.setup()
arcade.run()

Output:

enter image description here