python-coder12345
python-coder12345

Reputation: 188

What is wrong with my turtle code? Cannot understand

I was doing a school project about pixelart and wanted to create a landscape. What is wrong?

""" Landscape Drawer.py
    Draws landscape on a turtle screen"""

import turtle as t
import random as r

pen = t.Pen()
pen.hideturtle()
pen.speed('fastest')

pixelno = 90000

def controls():
    if pen.xcor() == 150 or pen.xcor() == 150 or pen.ycor() == 150 or pen.ycor() == 150:
        pen.right(90)
        pen.forward(1)
        pen.right(90)
        
def allcode():
    while True:
        t.colormode(255)
        controls()
        if pen.ycor() < -75:
            rX = r.randint(0, 150)
            gX = r.randint(0, 255)
            bX = r.randint(0, 150)
            pen.pencolor(rX, gX, bX)
            pen.pendown()
            pen.penup()
            pen.forward(1)
        elif pen.ycor() > -150 or pen.ycor < 150:
            cloud = r.randint(1, 7)
            if cloud == 7:
                pen.pencolor(255, 255, 255)
                t.pendown()
            rY = r.randint(0, 150)
            gY = r.randint(0, 150)
            bY = r.randint(0, 255)
            pen.pencolor(rY, gY, bY)
            pen.pendown()
            pen.penup()
            pen.forward(1)
        else:
            print('Your landscape, look nice? ')
            
            
pen.penup()
pen.goto(-300, -300)
allcode()
pen.exitonclick() 

P.S. I am using Python 3.8.2.

There are no errors, but the turtle screen is completely blank.

I have actually tried deleting the pen.hideturtle() and pen.speed('fastest') but it still won't work.

Thanks for your help!

Upvotes: 0

Views: 75

Answers (1)

cdlane
cdlane

Reputation: 41925

Your code is riddled with errors:

pen.pencolor(255, 255, 255)
t.pendown()

Here you're talking to two different turtles, your pen and the default turtle which you haven't touched before now nor ever again. What's going on here:

pen.xcor() == 150 or pen.xcor() == 150

is there a minus sign missing? Ditto for pen.ycor(). These statements are in the wrong order:

pen.pendown()
pen.penup()
pen.forward(1)

To actually make a mark on the screen you want:

pen.pendown()
pen.forward(1)
pen.penup()

Look for that error in two places in your code. This logic:

if cloud == 7:
    pen.pencolor(255, 255, 255)

is completely undone by what follows next:

rY = r.randint(0, 150)
gY = r.randint(0, 150)
bY = r.randint(0, 255)
pen.pencolor(rY, gY, bY)

You're missing an else clause there. In this else clause:

else:
    print('Your landscape, look nice? ')

you probably also want a break statement otherwise you'll never get out of your infinite while True: loop.

Below is my rework of your code to fix the above, but I make no claim that it does anything useful:

"""
Landscape Drawer.py
Draws landscape on a turtle screen
"""

from turtle import Screen, Pen
from random import randint

def controls():
    if pen.xcor() == -150 or pen.xcor() == 150 or pen.ycor() == -150 or pen.ycor() == 150:
        pen.right(90)
        pen.forward(1)
        pen.right(90)

def allcode():
    while True:
        controls()

        if pen.ycor() < -75:
            r = randint(0, 150)
            g = randint(0, 255)
            b = randint(0, 150)
            pen.pencolor(r, g, b)

            pen.pendown()
            pen.forward(1)
            pen.penup()
        elif pen.ycor() > -150 or pen.ycor() < 150:
            cloud = randint(1, 7)

            if cloud == 7:
                pen.pencolor(255, 255, 255)
            else:
                r = randint(0, 150)
                g = randint(0, 150)
                b = randint(0, 255)
                pen.pencolor(r, g, b)

            pen.pendown()
            pen.forward(1)
            pen.penup()
        else:
            print('Your landscape, look nice? ')
            break

screen = Screen()
screen.colormode(255)

pen = Pen()
pen.hideturtle()
pen.speed('fastest')

pen.penup()
pen.goto(-300, -300)

allcode()

screen.exitonclick()

Upvotes: 4

Related Questions