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

Mac环境VS Code中用Python play库开发Flappy Bird遇导入问题求助

解决VS Code自动替换import play及Flappy Bird代码问题

一、搞定VS Code自动把play换成Playsound的问题

  • 手动输入完整导入语句:输入import play后,不要选VS Code弹出的Playsound补全选项,直接按回车确认导入。
  • 调整VS Code补全设置:如果频繁出现这个问题,按Cmd+,打开设置,搜索“Editor: Suggestions”,暂时降低补全优先级,或者添加import play的自定义代码片段,避免误选。
  • 确认正确安装play库:在Mac终端执行pip3 install play(Mac默认用Python3,优先用pip3),确保安装的是游戏开发用的play库,而非音频播放的playsound。

二、代码里的拼写错误修正

你的代码有两处拼写错误,会导致运行报错:

  • scor.words = str(puan):puan是未定义变量,应改为point
  • obstacle_1.height = 940-randomnnumber:randomnnumber拼写错误,应为randomnumber

修正后的完整代码:

import play
import random

bird = play.new_image(image ="bird.png",x=-100,size=9)
obstacle_1 = play.new_box(x = 440,y = 300 ,height = 350,width =80,color = "blue",border_width= 5,border_color="black")
obstacle_2 = play.new_box(x = 440,y = -300,height = 550,width =80,color = "blue",border_width= 5,border_color="black")
bird.start_physics(stable = True , can_move= True , obeys_gravity= True,bounciness = 0.5)
point = 0
scor = play.new_text(words = str(point),x = 350,y = 250)

@play.repeat_forever
def jump():
    global point
    scor.words = str(point)  # 修正:puan改为point
    if play.key_is_pressed("w","W"):
        bird.physics.y_speed = 30
    if obstacle_1.x > -450:
        obstacle_1.x -= 5
        obstacle_2.x -= 5
    else :
        obstacle_1.x = 440
        obstacle_2.x = 440
        randomnumber = random.randint(0,940)
        obstacle_1.height = 940 - randomnumber  # 修正:拼写错误
        obstacle_2.height = randomnumber
    if obstacle_1.x == -100:
        point += 1
    if bird.is_touching(obstacle_1) or bird.is_touching(obstacle_2):
        point = 0
        obstacle_1.x = 440
        obstacle_2.x = 440

play.start_program()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 06:20:28