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

添加文本代码后Processing绘图内容消失,求恢复显示方法

问题:添加文本交互后Processing项目所有内容消失,求恢复方法

我开发这个Processing项目已经快两周了,之前星星、行星这些元素都能正常显示,但加了文本交互代码后,所有内容都从屏幕上消失了。我改了好几次setup()函数和交互代码模块都没解决,想知道怎么恢复所有内容的显示。

相关代码如下:

def setup():
 size (700, 700)
 background('#0c1445')

#stars
fill('#fbf4d9')
circle(300, 300, 9)
circle(460, 240, 9)
circle(500, 680, 9)
circle(28, 64, 9)
circle(150, 530, 9)
circle(50, 340, 9)
circle(600, 590, 9)
circle(280, 100, 9)
circle(450, 600, 9)
circle(440, 400, 9)
circle(580, 120, 9)
circle(590, 330, 9)
circle(160, 140, 9)
circle(280, 640, 9)
circle(290, 500, 9)
circle(170, 350, 9)
circle(45, 630, 9)

#arrow key 1
arrowone = createShape(GROUP)
#make two shapes
leftpoint = createShape(TRIANGLE, 150, 520, 130, 545, 150, 570)
labody = createShape(RECT, 150, 530, 54, 20)
#add the two "child" shapes to the parent group
arrowone.addChild(leftpoint)
arrowone.addChild(labody)

#arrow key 2
arrowtwo = createShape(GROUP)
#make two shapes
rightpoint = createShape(TRIANGLE, 504, 520, 524, 545, 504, 570)
rabody = createShape(RECT, 450, 530, 54, 20)
#add the two "child" shapes to the parent group
arrowtwo.addChild(rightpoint)
arrowtwo.addChild(rabody)

#show keys
shape(arrowone)
shape(arrowtwo)

def display(itself):
#sun
 fill('#FDB813')
 circle(350, 250, 200)

 #planet one(neptune)
  fill('#4b70dd')
  circle(635, 250, 70)

 #planet two(mercury)
  fill('#e5e5e5')
  circle(75, 250, 20)

 #planet three(venus)
  fill('#a57c1b')   
  circle(125, 250, 40)

 #planet four(mars)
  fill('#ad6242')
  circle(275, 250, 30)

 #planet five(uranus)
  fill('#e1eeee')
  circle(550, 250, 80)

 #planet six(saturn)
  fill('#ead6b8')
  circle(450, 250, 90)

 #planet seven(jupiter)
  fill('#bcafb2')
  circle(350, 250, 100)

 #planet eight(earth) 
  fill('#34A56F') 
  circle(200, 250, 50)

#attempt to make something interactive(p1)
def draw():
 fill('#D0D0D0')
 textSize(30)
 textAlign(CENTER)
 text(fillVal)
def keyPressed():
        global fillVal
        if keyCode == Coded:
            if keyCode == LEFT:
                fillVal = ("Did you know that space is silent?", 350, 450)
            elif keyCode == RIGHT:
                fillVal = ("Did you know that there are more trees on earth than there are stars in the milky way", 350, 450)
            else:
                fillVal = ("Fun facts about space!", 350, 450)

#making the keys work?

解决方法

1. 修正代码执行逻辑

  • Processing Python模式仅自动执行setup()、draw()等官方指定函数,你写在这些函数外的星星、箭头绘制代码不会运行,需移到setup()或draw()中。
  • display()函数定义后未被调用,导致行星和太阳完全没被绘制,必须在draw()里调用该函数。

2. 修复文本绘制错误

  • fillVal被赋值为元组,但text()函数需要分别传入文本内容、x坐标、y坐标三个参数,不能直接传元组。应将坐标固定在text()中,仅让fillVal存储文本内容。
  • 必须初始化fillVal的初始值,否则程序启动时会因变量未定义报错。
  • draw()每秒刷新60次,每次需先绘制背景,再重新绘制所有元素,避免内容被覆盖或残留。

3. 修正按键判断逻辑

  • 特殊按键判断应使用if key == CODED(CODED为大写常量),而非keyCode == Coded。
  • 调整fillVal赋值逻辑,仅存储文本字符串。

修改后的完整代码

fillVal = "Fun facts about space!"
arrowone = None
arrowtwo = None

def setup():
    global arrowone, arrowtwo
    size(700, 700)
    
    # 创建箭头形状
    arrowone = createShape(GROUP)
    leftpoint = createShape(TRIANGLE, 150, 520, 130, 545, 150, 570)
    labody = createShape(RECT, 150, 530, 54, 20)
    arrowone.addChild(leftpoint)
    arrowone.addChild(labody)
    
    arrowtwo = createShape(GROUP)
    rightpoint = createShape(TRIANGLE, 504, 520, 524, 545, 504, 570)
    rabody = createShape(RECT, 450, 530, 54, 20)
    arrowtwo.addChild(rightpoint)
    arrowtwo.addChild(rabody)

def draw():
    background('#0c1445')
    
    # 绘制星星
    fill('#fbf4d9')
    circle(300, 300, 9)
    circle(460, 240, 9)
    circle(500, 680, 9)
    circle(28, 64, 9)
    circle(150, 530, 9)
    circle(50, 340, 9)
    circle(600, 590, 9)
    circle(280, 100, 9)
    circle(450, 600, 9)
    circle(440, 400, 9)
    circle(580, 120, 9)
    circle(590, 330, 9)
    circle(160, 140, 9)
    circle(280, 640, 9)
    circle(290, 500, 9)
    circle(170, 350, 9)
    circle(45, 630, 9)
    
    # 绘制箭头
    shape(arrowone)
    shape(arrowtwo)
    
    # 绘制行星与太阳
    display()
    
    # 绘制交互文本
    fill('#D0D0D0')
    textSize(30)
    textAlign(CENTER)
    text(fillVal, 350, 450)

def display():
    # 太阳
    fill('#FDB813')
    circle(350, 250, 200)

    # 海王星
    fill('#4b70dd')
    circle(635, 250, 70)

    # 水星
    fill('#e5e5e5')
    circle(75, 250, 20)

    # 金星
    fill('#a57c1b')   
    circle(125, 250, 40)

    # 火星
    fill('#ad6242')
    circle(275, 250, 30)

    # 天王星
    fill('#e1eeee')
    circle(550, 250, 80)

    # 土星
    fill('#ead6b8')
    circle(450, 250, 90)

    # 木星
    fill('#bcafb2')
    circle(350, 250, 100)

    # 地球
    fill('#34A56F') 
    circle(200, 250, 50)

def keyPressed():
    global fillVal
    if key == CODED:
        if keyCode == LEFT:
            fillVal = "Did you know that space is silent?"
        elif keyCode == RIGHT:
            fillVal = "Did you know that there are more trees on earth than there are stars in the milky way?"
        else:
            fillVal = "Fun facts about space!"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 16:52:56