# A program to roll a pair of dice # Author: Nicholas Coleman # Rolls a randomly chosen pair of dice each time it is run. from turtle import * from random import randrange def main(): # pick two random numbers between 1 and 6 dieFace1 = randrange(1, 7) dieFace2 = randrange(1, 7) # draw the dice drawDie(-150, 0, 100, dieFace1) drawDie(50, 0, 100, dieFace2) hideturtle() # wait for user to click on window exitonclick() def drawSquare(xPos, yPos, size): # get to the right location penup() goto(xPos, yPos) setheading(0) # draw the square pendown() for i in range(4): forward(size) left(90) def drawDot(xPos, yPos, size): # get to the right location penup() goto(xPos, yPos) # draw the dot pendown() dot(size) def drawDie(xPos, yPos, size, faceNum): dotSize = size / 7 xPos1 = xPos + size / 4 xPos2 = xPos + size / 2 xPos3 = xPos + 3 * size / 4 yPos1 = yPos + size / 4 yPos2 = yPos + size / 2 yPos3 = yPos + 3 * size / 4 # draw the square drawSquare(xPos, yPos, size) # If the face number is 1, 3, or 5, draw the middle dot if faceNum % 2 == 1: drawDot(xPos2, yPos2, dotSize) # If the face number is not 2, 3, 4, 5, or 6, draw the upper-right # and lower-left dots if faceNum > 1: drawDot(xPos1, yPos1, dotSize) drawDot(xPos3, yPos3, dotSize) # If the face number is 4, 5, or 6, draw the upper-left and # lower-right dots if faceNum >= 4: drawDot(xPos1, yPos3, dotSize) drawDot(xPos3, yPos1, dotSize) # If the face number is 6 draw the middle-left and middle-right dots if faceNum == 6: drawDot(xPos1, yPos2, dotSize) drawDot(xPos3, yPos2, dotSize) main()