Question: import turtle from turtle import Turtle import random from time import sleep WIDTH = 1 6 0 0 HEIGHT = 9 0 0 backgroundColor =

import turtle
from turtle import Turtle
import random
from time import sleep
WIDTH =1600
HEIGHT =900
backgroundColor ="#696969"
buildingColor ="#4c2020"
windowColor ="#FFFFFF"
unveilTheCurtain = False
starTurtles =[]
buildingTurtle = Turtle()
buildings =[]
def drawBuildings(buildingTurtle):
while buildingTurtle.xcor() WIDTH:
buildings.append(drawBuilding(buildingTurtle))
buildingTurtle.goto(buildingTurtle.xcor(),0)
def drawBuilding(buildingTurtle, wallHeight=None, floorWidth=None):
buildingTurtle.pen(fillcolor=buildingColor, pencolor=buildingColor, pensize=3)
buildingTurtle.begin_fill()
previousWallHeight = constructWall(buildingTurtle, wallHeight=wallHeight)
floorWidth = constructFloor(buildingTurtle, floorWidth)
constructWall(buildingTurtle, previousWallHeight, wallHeight)
buildingTurtle.end_fill()
return (previousWallHeight, floorWidth)
def redrawBuildings(buildingTurtle):
buildingTurtle.penup()
buildingTurtle.goto(0,0)
buildingTurtle.pendown()
for building in buildings:
drawBuilding(buildingTurtle, wallHeight=building[0], floorWidth=building[1])
def setupBuilding(colorset):
colorset.penup()
colorset.pen(fillcolor=buildingColor, pencolor=buildingColor, pensize=3)
colorset.goto(0,0)
colorset.pendown()
def constructFloor(construct, floorWidth=None):
construct.setheading(0)
if floorWidth is None:
floorWidth = random.randint(20,(WIDTH //6))
construct.forward(floorWidth)
return floorWidth
def constructWall(construct, previousWallHeight=None, wallHeight=None):
if previousWallHeight is None:
construct.setheading(90)
wallHeight = random.randint(20, int(HEIGHT /1.5)) if wallHeight is None else wallHeight
construct.forward(wallHeight)
return wallHeight
else:
construct.setheading(270)
construct.forward(previousWallHeight)
def draw_window(x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.fillcolor(windowColor)
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.end_fill()
def drawStars():
starAmount = WIDTH //20
stars =[randomStar() for _ in range(starAmount)]
for star in stars:
drawStar(star[0], star[1])
def randomStar():
return random.randint(0, WIDTH), random.randint(HEIGHT //2, HEIGHT)
def updateStars():
for star in starTurtles:
x = star.xcor()
if x >= WIDTH:
x =0
star.setx(x)
star.sety(randomStar()[1])
star.clear()
drawStar(x +(WIDTH //80), starTurtle=star)
def drawStar(x, y=None, starTurtle=None):
if starTurtle is None:
starTurtle = defaultTurtle()
starTurtles.append(starTurtle)
starTurtle.hideturtle()
starTurtle.color("yellow")
starTurtle.penup()
safeGoto(x, y, starTurtle)
drawLine(10, starTurtle)
starTurtle.right(45)
drawLine(10, starTurtle)
starTurtle.left(90)
drawLine(10, starTurtle)
starTurtle.left(45)
drawLine(10, starTurtle)
starTurtle.penup()
def drawLine(length, turtle=turtle):
halfLength = length //2
turtle.pendown()
turtle.forward(halfLength)
turtle.backward(length)
turtle.left(180)
turtle.backward(halfLength)
turtle.penup()
def safeGoto(x, y=None, turtle=turtle):
turtle.goto(x, turtle.ycor() if y is None else y)
turtle.setheading(90)
def defaultTurtle(newTurtle=None):
if newTurtle is None:
newTurtle = Turtle()
if not unveilTheCurtain:
newTurtle.hideturtle()
return newTurtle
def main():
screen = turtle.Screen()
if not unveilTheCurtain:
screen.tracer(0)
defaultTurtle(turtle)
global buildingTurtle
defaultTurtle(buildingTurtle)
setupBuilding(buildingTurtle)
turtle.setup(WIDTH, HEIGHT)
turtle.setworldcoordinates(0,0, WIDTH, HEIGHT)
turtle.bgcolor(backgroundColor)
drawStars()
drawBuildings(buildingTurtle)
while True:
screen.update()
sleep(0.5)
updateStars()
redrawBuildings(buildingTurtle)
if __name__=="__main__":
main()
Intro Python level..l'm struggling to add windows to my code to match the 'After Result' picture. Keep it simple-just use imports already given. Add non-overlapping windows within the building borders. If possible, make windows randomly turn on/off one by one. Stars should move slowly in the background. Please comment changes with '\# changed '. Upload the output with Pycharm or any Python|compiler to prove it works well. Thanks!
After Results (Goal)
import turtle from turtle import Turtle import

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!