我已经为此工作了将近两个星期,但我仍然无法让它工作,我想知道如何让它出现在屏幕上,因为星星和行星会毫无问题地弹出,但是一旦我添加文本代码一切都不再在屏幕上可见。我想知道如何让屏幕上的所有内容再次可见:
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?
我一直在重写 def setup(): 部分并尝试制作一些交互部分,但它仍然无法正常工作..
看来您正在尝试将较新的代码与较旧的代码合并到一个草图中。
在这种情况下,最好的方法是独立测试每个部分,并确保首先没有遗漏任何错误。
之后,检查您的代码并删除任何不需要的东西(或者如果您发现有问题,请先删除代码中的损坏部分)。
最后,您应该继续合并代码,一次添加一个部分,并运行它进行测试,然后再添加下一个。这将使问题更容易解决。
FWIW,这是我在上面的代码中发现的一些问题:
itself
是模棱两可的:听起来类似于 self
,但是您没有使用类,因此您可能不适用。它似乎没有在任何地方用作函数参数,所以如果你不需要它,只需将它删除以简化你的代码global
用法不一致。您应该将 fillVal
定义为全局变量,然后重用 draw()
中的关键字来引用更新后的变量。 (你可能想对箭头做同样的事情)display
这样做了。你可以做类似的事情来画星星)这是您的代码的修改版本:
arrowone = None
arrowtwo = None
fillVal = "Fun facts about space!"
def setup():
size (700, 700)
background('#0c1445')
noStroke()
#arrow key 1
global arrowone,arrowtwo
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)
def drawStars():
#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)
def display():
drawStars()
#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():
global arrowone, arrowtwo, fillVal
background(0, 0, 32)
display()
#show keys
shape(arrowone)
shape(arrowtwo)
fill('#D0D0D0')
textSize(30)
textAlign(CENTER)
text(fillVal, 350, 450)
def keyPressed():
global fillVal
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!"
不要让以上问题让你气馁。这是学习过程的一部分。 顺便说一句,在使用创建形状和重用它们方面做得很好。 祝您学习编码并玩得开心!