You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arcade框架中Sprite的.stop()方法失效问题求助

问题分析与解决

你的乒乓球游戏中,生命值为0时停止Sprite运动的功能失效,且调用ball.stop()/arcade.stop()报错,核心原因是:

  1. Arcade Sprite无内置stop()方法:你自定义的ball和bar类继承自arcade.Sprite,但该类并没有提供stop()方法,直接调用必然报错。
  2. 错误调用类而非实例:ball.stop()是尝试调用类的静态方法,但你需要操作的是self.ball和self.bar这两个实例对象。
  3. 逻辑顺序问题:原代码中self.live == 0的判断在self.ball.update()之后执行,导致即使生命值为0,球和挡板仍会先完成一次移动更新。

修复方案

利用你自定义的stepx/stepy变量控制运动,直接将这些速度变量设为0即可停止Sprite,同时调整逻辑顺序,确保生命值为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()
        arcade.set_background_color(arcade.color.DARK_GRAY)  # 背景色先设置,避免被覆盖
        self.ball.draw()
        self.bar.draw()
        life = f'Жизней: {self.live}'
        score = f'Счёт: {self.score}'  # 修复拼写错误scrore→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, anchor_x='center')  # 文字居中显示
        arcade.finish_render()

    def update(self, delta_time: float):
        # 生命值为0时,跳过所有更新逻辑
        if self.live == 0:
            return
        
        self.ball.update()
        self.bar.update()
        colliding = arcade.check_for_collision(self.ball, self.bar)
        if colliding:
            self.ball.bottom = self.bar.top  # 修复原代码self.example的拼写错误
            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
            # 生命值变为0时,立即停止所有运动
            if self.live == 0:
                self.ball.stepx = 0
                self.ball.stepy = 0
                self.bar.stepx = 0

    def on_key_press(self, key, modifiers):
        if self.live == 0:  # 生命值为0时,禁用按键控制
            return
        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()

关键修改点

  • 停止运动逻辑:在update方法开头判断self.live == 0,直接返回跳过所有更新;同时在生命值变为0时,将self.ball.stepx、self.ball.stepy、self.bar.stepx设为0。
  • 修复拼写错误:修正scrore为score,self.example为self.bar,避免额外报错。
  • 优化显示:将背景色设置放在draw方法最开头,确保背景正常显示;添加anchor_x='center'让结束文字居中。
  • 禁用按键:生命值为0时,on_key_press直接返回,防止玩家继续移动挡板。

内容的提问来源于stack exchange,提问作者Firy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:35:15