如何测试由Random.randint在Python Turtle中产生的特定RGB值?

发布于 2025-01-30 05:34:07 字数 1485 浏览 2 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ら栖息 2025-02-06 05:34:07

如果您想要生成新的颜色,则必须这样做。

# random color generator

def rand_c():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    turtle.color( (r,g,b) )
    return r, g, b

rand_color = rand_c()

在您的循环中:

        rand_color = rand_c()

        if rand_color == (50, 205, 50):

If you want a new color generated, then you have to DO that.

# random color generator

def rand_c():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    turtle.color( (r,g,b) )
    return r, g, b

rand_color = rand_c()

and in your loop:

        rand_color = rand_c()

        if rand_color == (50, 205, 50):
沉默的熊 2025-02-06 05:34:07

您可以将随机颜色存储在全局a @timroberts中,也可以询问乌龟的当前颜色,即pencolor()。但是,还有许多其他问题可以使您的代码无法正常工作。

首先,一个负号,您的意思是一个等于标志:

g = random.randint(0, 255)
b - random.randint(0, 255)

的方法中分配结果:

rand_color = turtle.color(r, g, b)

或者,可以:

turtle.color(r, g, b)
rand_color = turtle.pencolor()

或::::::::::::::::::::

rand_color = (r, g, b)
turtle.color(rand_color)

接下来,您是从没有返回任何内容

from turtle import Screen, Turtle
from random import randrange
from time import sleep

# random color generator
# it will update the rgb values set above every time a condition is satisfied
def rand_c():
    r = randrange(256)
    g = randrange(256)
    b = randrange(256)
    rand_color = (r, g, b)

    player.color(rand_color)

screen = Screen()
screen.colormode(255)

player = Turtle()

# for the prompt message inside turtle.Screen()
prompt = Turtle()
prompt.hideturtle()

while True:
    player.forward(player.speed())

    # boundary checking/ putting effects on the border/ player
    if not -275 <= player.xcor() <= 275:
        player.home()  # 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  # sets the score to zero if the player hits border
        rand_c()  # change the player color every time it hits the left and right border

        if player.pencolor() == (50, 205, 50):
            prompt.write("Stealth Mode", align='center', font=('Courirer new', 10, 'bold'))
            sleep(2)
            prompt.clear()

screen.mainloop()  # never reached

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:

g = random.randint(0, 255)
b - random.randint(0, 255)

Next, you're assigning a result from a method that doesn't return anything:

rand_color = turtle.color(r, g, b)

Alternatively, could be:

turtle.color(r, g, b)
rand_color = turtle.pencolor()

Or:

rand_color = (r, g, b)
turtle.color(rand_color)

A rework of your code that addresses these and other issues:

from turtle import Screen, Turtle
from random import randrange
from time import sleep

# random color generator
# it will update the rgb values set above every time a condition is satisfied
def rand_c():
    r = randrange(256)
    g = randrange(256)
    b = randrange(256)
    rand_color = (r, g, b)

    player.color(rand_color)

screen = Screen()
screen.colormode(255)

player = Turtle()

# for the prompt message inside turtle.Screen()
prompt = Turtle()
prompt.hideturtle()

while True:
    player.forward(player.speed())

    # boundary checking/ putting effects on the border/ player
    if not -275 <= player.xcor() <= 275:
        player.home()  # 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  # sets the score to zero if the player hits border
        rand_c()  # change the player color every time it hits the left and right border

        if player.pencolor() == (50, 205, 50):
            prompt.write("Stealth Mode", align='center', font=('Courirer new', 10, 'bold'))
            sleep(2)
            prompt.clear()

screen.mainloop()  # never reached
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文