如何测试由Random.randint在Python Turtle中产生的特定RGB值?
我想在turtle.screen()
上写一条提示消息,每当乌龟击中边框时会生成随机颜色时,会生成柠檬绿色(50,205,50)< /代码>。
import random
import turtle
import math
turtle.colormode(255)
# random color generator
r = random.randint(0, 255)
g = random.randint(0, 255)
b - random.randint(0, 255)
rand_color = turtle.color(r, g, b)
# it will update the rgb values set above everytime a condition is satisfied
def rand_c():
return rand_color
# for the prompt message inside turtle.Screen()
sm = turtle.Turtle()
sm.color("White")
sm.hideturtle()
sm.goto(0, 0)
# game loop
while True:
player.forward(speed)
# boundary checking/ putting effects on the border/ player
if player.xcor() > 275 or player.xcor() < -275:
player.setposition(x=0, y=0) # player positioned to (0, 0) when it hits L and R border
winsound.PlaySound("KABOOM", winsound.SND_ASYNC) # plays sound when border is hit
score *= 0 # multiplies the score to zero if the player hits border
rand_c() # change the player color every time it hits the left and right border
# What should be the code here? (condition statement for when turtle generates a lime green color)
if rand_color == (50, 205, 50):
sm.write("Stealth Mode", align="center", font=("Courirer new", 10, "bold"))
sleep(2)
sm.clear()
else:
pass
问题在于,它每次撞到边框时都会写下提示消息,而不仅仅是乌龟只能随机生成柠檬绿色(50,205,50)
时写提示消息。
I want to write a prompt message at turtle.Screen()
every time the turtle, that generates random color when hitting the border, generates a lime green color (50, 205, 50)
.
import random
import turtle
import math
turtle.colormode(255)
# random color generator
r = random.randint(0, 255)
g = random.randint(0, 255)
b - random.randint(0, 255)
rand_color = turtle.color(r, g, b)
# it will update the rgb values set above everytime a condition is satisfied
def rand_c():
return rand_color
# for the prompt message inside turtle.Screen()
sm = turtle.Turtle()
sm.color("White")
sm.hideturtle()
sm.goto(0, 0)
# game loop
while True:
player.forward(speed)
# boundary checking/ putting effects on the border/ player
if player.xcor() > 275 or player.xcor() < -275:
player.setposition(x=0, y=0) # player positioned to (0, 0) when it hits L and R border
winsound.PlaySound("KABOOM", winsound.SND_ASYNC) # plays sound when border is hit
score *= 0 # multiplies the score to zero if the player hits border
rand_c() # change the player color every time it hits the left and right border
# What should be the code here? (condition statement for when turtle generates a lime green color)
if rand_color == (50, 205, 50):
sm.write("Stealth Mode", align="center", font=("Courirer new", 10, "bold"))
sleep(2)
sm.clear()
else:
pass
The problem is that it writes the prompt message every time it hits the border, instead of just writing the prompt message when the turtle only randomly generates a lime green color (50, 205, 50)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要生成新的颜色,则必须这样做。
在您的循环中:
If you want a new color generated, then you have to DO that.
and in your loop:
您可以将随机颜色存储在全局a @timroberts中,也可以询问乌龟的当前颜色,即
pencolor()
。但是,还有许多其他问题可以使您的代码无法正常工作。首先,一个负号,您的意思是一个等于标志:
的方法中分配结果:
或者,可以:
或::::::::::::::::::::
接下来,您是从没有返回任何内容
You can store the random color in a global a la @TimRoberts or you can interrogate the turtle as to it's current color, i.e.
pencolor()
. But there is a collection of other issues that keep your code from working.First, a minus sign where you meant an equals sign:
Next, you're assigning a result from a method that doesn't return anything:
Alternatively, could be:
Or:
A rework of your code that addresses these and other issues: