使用PictoBlox开发Python游戏:气球到达Y轴211时无法重生
PictoBlox Python游戏:气球重生功能失效修复
我用PictoBlox开发一款Python游戏,包含箭头和气球元素。预期逻辑:
- 气球到达Y轴211位置时,从底部重新生成
- 箭头触碰气球时,气球消失(模拟戳破)
目前箭头触碰气球的功能正常,但气球到达Y轴211时的重生功能未生效。
气球相关代码
balloon = Sprite('Balloon1') import time import random balloon.gotoxy(random.randint(-210, 200), -257) print("Let's get poppin!") balloon.show() while True: balloon.changey(2) if balloon.y == 211: balloon.hide() time.sleep(1) balloon.gotoxy(random.randint(-210, 200), -257) balloon.show() balloon.changey(2) if balloon.istouching('Arrow1'): balloon.hide() balloon.gotoxy(random.randint(-210, 200), -257) time.sleep(1.5) balloon.show() print("Point!")
箭头相关代码
sprite = Sprite('Arrow1') import time sprite.setrotationstyle('left-right') while True: if sprite.iskeypressed('up arrow'): sprite.changey(6.5) if sprite.iskeypressed('down arrow'): sprite.changey(-6.5) if sprite.iskeypressed('right arrow'): sprite.changex(6.5) if sprite.iskeypressed('left arrow'): sprite.changex(-6.5)
问题原因与修复方案
问题根源
气球每次执行changey(2),Y值会以步长2递增,这意味着Y值可能直接跳过211(例如从210→212),永远触发不了balloon.y == 211的判断条件。
修复代码
将气球循环中的判断条件从==改为>=,确保气球到达或超过211位置时触发重生逻辑,同时删除冗余的changey(2)调用:
while True: balloon.changey(2) # 修改判断条件为>=211 if balloon.y >= 211: balloon.hide() time.sleep(1) balloon.gotoxy(random.randint(-210, 200), -257) balloon.show() if balloon.istouching('Arrow1'): balloon.hide() balloon.gotoxy(random.randint(-210, 200), -257) time.sleep(1.5) balloon.show() print("Point!")
内容的提问来源于stack exchange,提问作者MaLJeT
相关产品推荐
相关产品推荐

