#Jonathan Hudson #Student Number 12347890 #Date: 2020-08-26 #Lecture Practice #Draw stars at locations given by user #Import turtle import turtle #CONSTANTS WIDTH = 800 HEIGHT = 600 #Main function (Loops taking two integer inputs from user for (x,y) cooridinate in (800,600) window #Draws star at that location indicated colour #Loop stops once one of coordinate inputs is the empty line "" (i.e. pressing Enter for input) def main(): pointer = setup() #Get (x, y) sXLocation = input("Enter new x coordinate in (800,600) window: ") sYLocation = input("Enter new y coordinate in (800,600) window: ") #Loop as long as not empty string while sXLocation != "" and sYLocation != "": #Change to integer (we assume they are valid numbers) x = int(sXLocation) y = int(sYLocation) #Set color (prompts for it's own input) setColor(pointer) #Go to x,y and draw a star gotoAndDrawStar(pointer,x,y) #Get next location sXLocation = input("Enter new x coordinate in (800,600) window: ") sYLocation = input("Enter new y coordinate in (800,600) window: ") #Done main loop print("Done") #Sets up window and gets turtle pointer (uses WIDTH AND HEIGHT constants) #Pointer is hidden and delay set to 0 to speed up drawing #Pen is set to up #Parameters: # None #Returns: # Turtle drawing pointer def setup(): pointer = turtle.Turtle() screen = turtle.getscreen() screen.setup(WIDTH, HEIGHT, 0, 0) screen.setworldcoordinates(0, 0, WIDTH, HEIGHT) pointer.hideturtle() screen.delay(delay=0) pointer.up() return pointer #Sets pen color (for star color), takes its own input as a string (doesn't do cast so no crash possible) #Parameters: # Turtle drawing pointer #Returns: # None (however, state of turtle pen color is changed def setColor(pointer): sColor = input("Enter color [1:red 2:green 3:blue otherwise:black]: ") if sColor == "1": pointer.color("red") elif sColor == "2": pointer.color("green") elif sColor == "3": pointer.color("blue") else: pointer.color("black") #Moves pen to indicated (x,y) location and draws a star with edges of parameter length #Parameters: # Turtle drawing pointer # (x,y) coordinate to draw star at, (400,300) centre of screen is default # length of edge (point to point), 100 is default #Returns: # None (however window will have star drawn in it) def gotoAndDrawStar(pointer,x=400,y=300,length=100): pointer.goto(x,y) pointer.setheading(0) pointer.down() drawStar(pointer, length) pointer.up() #Draws a star with pen at current position with edges of parameter length #Parameters: # Turtle drawing pointer # length of edge (point to point), 100 is default #Returns: # None (however window will have star drawn in it) def drawStar(pointer, length=100): for i in range(5): pointer.forward(length) pointer.right(144) main()