Can anyone tell me, why there is a white space between the ground and the wall? I cant find the mistakte.
First it worked, but it seems like I changed amything and no i dont the what I did wrong. Thanks for helping!
Heres the code:
from turtle import Turtle, Screen
import random
from random import randint
screen = Screen ()
screen.setup (1920, 1080, 0, 0)
t = Turtle ()
t.color ("red")
t.speed (0)
s = Turtle()
s.color("black")
s.speed(0)
# Ego-Perspektive
t.up ()
t.goto (-950, -450)
t.down ()
t.color ("#DFCBAF")
t.begin_fill ()
t.goto(950,-450)
t.goto(999.01,-0.99)
t.goto(-400.99,-0.99)
t.goto(-950,-450)
t.end_fill()
t.color ("#d28c5b")
t.begin_fill ()
t.up ()
t.goto (-950, -450)
t.down ()
t.goto(-950,510)
t.goto(670,510)
t.goto(670,-1)
t.goto(-498.5,-1)
t.goto(-950,-450)
t.end_fill ()
t.color ("black")
t.goto(-950,510)
t.goto(670,510)
t.goto(670,-1)
t.goto(-498.5,-1)
t.goto(-950,-450)
t.up()
t.goto(-498.5,-1)
t.down()
t.setheading(90)
t.forward(500)
t.up()
t.goto(-415,60)
t.down()
t.setheading(0)
t.color("#000c1a")
t.begin_fill()
for x in range(2):
t.forward(1000)
t.left(90)
t.forward(290)
t.left(90)
t.end_fill()
t.up()
t.goto(-415,60)
t.down()
t.setheading(0)
t.color("black")
t.width(5)
for x in range(2):
t.forward(1000)
t.left(90)
t.forward(290)
t.left(90)
t.up()
t.goto(85,60)
t.down()
t.setheading(90)
t.forward(290)
def himmel1():
s.up()
s.goto(random.randint(-400,570),random.randint(100,360))
s.down()
s.dot(5, "#DFE6FF")
def himmel2():
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
s.dot(5, "#757196")
def himmel3():
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
s.dot(5, "#CED5FF")
def himmel4():
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
def stern():
links = 144
vorne = 12
s.color("yellow")
s.begin_fill()
for x in range(1):
s.begin_fill()
s.up()
s.goto(random.randint(-400,570),random.randint(100,350))
s.down()
for x in range(5):
s.forward(vorne)
s.left(links)
s.end_fill()
for x in range(17):
himmel1()
himmel3()
himmel2()
himmel3()
for i in range(10):
stern()
This text is only here because the website tells me izs to much code and to less text so just ignore this.
Change all occurrences of -498.5 to -400. Then the wall and the floor will be aligned.
For such drawings, you can be easily confused by the coordinates of the points. It can get really complicated to debug and change the code. You might want to store the coordinates of the points in some variables such that you won't get lost. In your case, the coordinate point (-400, -1) is miswritten as (-498.5, -1) when you are drawing the wall.
Related
I am working on an assignment for school and want to draw a large building with repeating windows. I now you would probably use a for loop for the windows to repeat, but I don't know how to set it up. Any help would be great! Here is my code:
import turtle
def draw_rectangle(t, x, y, width, height, tilt, pen, fill):
t.hideturtle()
t.up()
t.goto(x,y)
t.setheading(0 + tilt)
t.fillcolor(fill)
t.pencolor(pen)
t.down()
t.begin_fill()
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)
t.end_fill()
t.up()
t.down()
t.begin_fill()
draw_rectangle(t, -80, 100, 20, 30, 0, "white", "white")
t.end_fill()
t.up()
By making the window for loop into a function, it will be much easier to call and won't be as long. Declare this function outside of the draw_rectangle function and call it inside the draw_rectangle function with draw_window() whenever you need to draw a window.
def draw_window():
for i in range (2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
The fillcolor() is not working at all in this function -- I can not figure out why. It worked in all of my other functions:
from turtle import Turtle
from random import randint
t = Turtle()
def rocks():
for i in range(5):
t.penup()
t.goto(randint(-300,0), randint(-200,0))
for x in range(40):
t.pendown()
t.fillcolor("gray")
t.fillcolor()
t.begin_fill()
t.forward(5)
t.left(25)
t.right(27)
t.forward(5)
t.right(20)
t.end_fill()
t.speed("fastest")
rocks()
The problem is that you have the begin_fill() & end_fill() inside your loop which means they are trying to fill short line segments. You need them around your loop to fill the entire shape:
from turtle import Turtle, Screen
from random import randint
def rocks(t):
t.fillcolor('gray')
for _ in range(5):
t.penup()
t.goto(randint(-300, 0), randint(-200, 0))
t.begin_fill()
for _ in range(15):
t.pendown()
t.forward(5)
t.right(2)
t.forward(5)
t.right(22)
t.end_fill()
screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
rocks(turtle)
turtle.hideturtle()
screen.exitonclick()
I am trying to make a random game in which whichever character reaches the end first wins. I have basically the entire code made, but I need help with the end to determine who crosses the finish line first. So how would I do that? My code is:
from turtle import Turtle
from random import randint
t = Turtle()
t.speed(0)
t.up()
t.goto(-200,0)
t.down()
t.forward(900)
t.up()
t.goto(-200,100)
t.down()
t.forward(900)
t.up()
t.goto(-200,200)
t.down()
t.forward(1000)
t.up()
t.goto(-200,-100)
t.down()
t.forward(900)
t.up()
t.goto(-200,-200)
t.down()
t.forward(1000)
t.up()
t.goto(-200,200)
t.down()
t.right(90)
t.forward(400)
t.left(90)
t.up()
t.goto(-100, -200)
t.left(90)
t.down()
t.forward(400)
t.up()
t.goto(800,-200)
t.down()
t.forward(400)
t.up()
t.goto(700,-200)
t.down()
t.forward(400)
d = Turtle()
d.speed(5)
d.color('red')
d.shape('arrow')
d.up()
d.goto(-155,150)
d.right(360)
c = Turtle()
c.speed(5)
c.color('blue')
c.shape('turtle')
c.up()
c.goto(-155,50)
c.right(360)
b = Turtle()
b.speed(5)
b.color('yellow')
b.shape('arrow')
b.up()
b.goto(-155,-50)
b.right(360)
a = Turtle()
a.speed(5)
a.color('green')
a.shape('turtle')
a.up()
a.goto(-155,-150)
a.right(360)
for i in range(350):
a.forward(randint(1,6))
b.forward(randint(1,6))
d.forward(randint(1,6))
c.forward(randint(1,6))
Any suggestions to make the code smaller would also be appreciated. I am trying to use a while loop so that I can stop the game as soon as the finish line is crossed.
We can test if a turtle's .xcor() value is greater than the x coordinate of the finish line to see if someone has won the race. I've reworked your code accordingly below along with some code compacting (but not as much as I could do, just to keep things nicely simple):
from turtle import Screen, Turtle
from random import randint
START_LINE = -300
FINISH_LINE = 300
screen = Screen()
screen.setup(1000, 600)
t = Turtle(visible=False)
t.speed('fastest')
# Race Lanes
for y in range(-200, 300, 100):
t.up()
t.goto(START_LINE - 100, y)
t.down()
t.forward((FINISH_LINE + 90) - (START_LINE - 100))
# Starting and Finishing Gates
for x in [START_LINE - 100, FINISH_LINE - 10]:
t.up()
t.goto(x, 200)
t.right(90)
t.down()
t.forward(400)
t.left(90)
t.forward(100)
t.left(90)
t.forward(400)
t.right(90)
d = Turtle('arrow')
d.color('red')
d.speed(5)
d.up()
d.goto(START_LINE - 50, 150)
d.right(360)
c = Turtle('turtle')
c.color('blue')
c.speed(5)
c.up()
c.goto(START_LINE - 50, 50)
c.right(360)
b = Turtle('arrow')
b.color('yellow')
b.speed(5)
b.up()
b.goto(START_LINE - 50, -50)
b.right(360)
a = Turtle('turtle')
a.color('green')
a.speed(5)
a.up()
a.goto(START_LINE - 50, -150)
a.right(360)
while a.xcor() < FINISH_LINE and b.xcor() < FINISH_LINE and c.xcor() < FINISH_LINE and d.xcor() < FINISH_LINE:
a.forward(randint(1, 6))
b.forward(randint(1, 6))
c.forward(randint(1, 6))
d.forward(randint(1, 6))
# The race is over
screen.mainloop()
It's good to define key locations with variables so that you don't have to work out numbers for every step in the code -- you can instead calculate and adjust as needed.
I can't figure out how to calculate the coordinates for the dots inside the circle. Right now my program is supposed to calculate the dots inside the circle within the square and divide that by the total amount of dots.
import turtle
import random
t=turtle.Turtle()
insideCircleCount = 0
def square():
for y in range(4):
t.forward(200)
t.left(90)
def circle():
t.penup()
t.setpos(100,0)
t.pendown()
t.circle(100)
def randomDot():
z=int(input("iterations:"))
for y in range(z):
t.penup()
t.pensize(1)
x=random.randint(0,200)
y=random.randint(0,200)
t.goto(x,y)
t.pendown()
t.dot()
insideCircle(x,y)
def insideCircle(x,y):
if ((x*x + y*y)<100):
insideCircleCount+=1
#main
square()
circle()
randomDot()
print('Dots inside circle account for ', insideCircleCount)
import turtle
import random
t=turtle.Turtle()
insideCircleCount = 0
def square():
for y in range(4):
t.forward(200)
t.left(90)
def circle():
t.penup()
t.setpos(100,0)
t.pendown()
t.circle(100)
def randomDot():
z=int(input("iterations:"))
for y in range(z):
t.penup()
t.pensize(1)
x=random.randint(0,200)
y=random.randint(0,200)
t.goto(x,y)
t.pendown()
t.dot()
insideCircle(x,y)
def insideCircle(x,y):
# Here the circle has a offset with center at (100,100)
#(x – h)^2 + (y – k)^2 = r2
if (((x-100)**2 + (y-100)**2) < 100**2):
# you were trying to access a local variable before assignment
#this fixes that
global insideCircleCount
insideCircleCount+=1
#main
square()
circle()
randomDot()
print('Dots inside circle account for ', insideCircleCount)
turtle.mainloop() #stops the window from closing
I am new to Python and I am struggling with a lesson in our book where we have to change the colors in a bar graph. I am not sure what I am doing wrong. There is no error message the colors are just printing black.
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
if xs is 48:
tess.color("blue")
if xs is 117:
tess.color("yellow")
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()
This is what I have so far.
Thanks for the help!
I think you need to have a color assigned before you begin_fill(). Because the computer is like: "Well, I'll fill the box, but I don't have a color", so it uses the default of black.
The if statement you provided doesn't make sense, so I moved it up in the function and modified it where you can examine the height within the function. Instead of using if/else, I'd recommend dictionaries.
This new code works:
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
# Select Color
if height is 48:
color = "blue"
elif height is 117:
color = "yellow"
else:
color = "black"
# Assign color
t.color(color)
# The rest of your code
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()