0

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)

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • What prompt message? You have a couple of problems. The `if player.xcor()...` block should be indented so it's inside the `while` loop. Otherwise, the loop will just run forever. Also, you are only generating ONE random color here. `rand_c` just returns `rand_color`, which is a constant throughout the code. – Tim Roberts May 19 '22 at 03:47
  • sm.write("Stealth Mode", align="center", font=("Courirer new", 10, "bold")) sleep(2) sm.clear() – MASTER MIND May 19 '22 at 03:48
  • "Stealth Mode" then the screen pauses for 2 seconds, after that the message is cleared and the game continues – MASTER MIND May 19 '22 at 03:49
  • I'll also point out that there's only a 1 in 16 million chance of matching a single color. Also, just do `score = 0`, not `score *= 0`. – Tim Roberts May 19 '22 at 03:50
  • oh sorry, everything under the while True is indented in my IDE i forgot to indent here in stack so there is no problem in that, however Sir how can I fix the code for my condition statement for turtle.color() – MASTER MIND May 19 '22 at 03:52
  • Okay Sir, but i intend to add this feature as a 1 in 16 million probability, how can i do that Sir since in testing ill just set the randomly generating color to lime green to see if it works – MASTER MIND May 19 '22 at 03:55

2 Answers2

0

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):
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

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
cdlane
  • 40,441
  • 5
  • 32
  • 81