nebula49dev
nebula49dev

Reputation: 43

Faulty random generation in the wrong area

weird thing

This is an assignment in one of my classes. This is a grape vine that is randomly generated, but whenever I generate it there is always a stray grape that is in the bottom of the vine. How can I get rid of it? I can't find a single line that could possibly place it there, and I am clueless as of what to do.

# imports and variables
from decimal import localcontext
import turtle as trtl
import random as rng

# starting vine coords, global vars
global y 
y = -300
global x 
x = -70

# color list
rCol = ["red" , "green"]

# speed change
trtl.tracer(True)
trtl.tracer(0,5) # actual speed

# init vine
trtl.penup()
trtl.goto(x,300)
trtl.pendown()
trtl.pencolor("brown")
trtl.pensize(20)
trtl.pendown()
trtl.goto(-70,y)
trtl.penup()

# definitions
def drawGrape(rad): # take args for radius
    locX = x
    locY = y
    trtl.pensize(3)
    trtl.pencolor("green")
    trtl.fillcolor(rng.choice(rCol))
    trtl.left(45)
    for oLoop in range(2):
        trtl.begin_fill()
        trtl.circle(rad,90)
        trtl.circle(rad//2,90)
        trtl.end_fill()
    trtl.right(180)
    trtl.penup()
    trtl.pendown()
    
# code with established functions
# starting grape coords
x = -70
y = -290

trtl.penup()

for dLoop in range(15):
    trtl.pendown()
    drawGrape(rng.randint(10,30))
    trtl.goto(-70,y)
    trtl.penup()
    trtl.goto(0,y)
    y += 40

# stop program from closing after finished
input()

Upvotes: 0

Views: 39

Answers (1)

CryptoFool
CryptoFool

Reputation: 23139

Your problem seems to be that before you draw your first grape, the turtle is in the wrong position fro having drawn the vine. So the position of the first grape that you want is wrong because you don't set the position of the turtle before drawing the first grape. Subsequent grapes show up in the right place because you do set the position of the turtle AFTER drawing a grape.

Try adding this line before the for loop that draws your grapes:

trtl.goto(0, y-40)

This got me what looks like what you want. I can't be sure because you say that you want the grape to "disappear". I'm guessing that you actually want it to be drawn in the right place. If you really want it to disappear, then change your logic to not draw the first grape that you are currently drawing. There's no mystery why you're getting that grape drawn. That's just what your code is doing.

If you aren't debugging your code by running it in a debugger, you should be. You could have stepped through your code, checking what the turtle position was at each step, and you should have been able to reason out your problem.

Upvotes: 1

Related Questions