'''
Simulates rolling multiple dice and draws them in a window.

Created on Dec 12, 2010

@author: clifton
'''
from zellegraphics import GraphWin, Point, Rectangle, Text
from random import random

def rollAndDrawDie(centerX, ctrY, dieSize, win):
    '''Simulates a single die roll and draws the result on the given window with the given
    location and size.'''
    # roll one die
    dieValue = int(random() * 6) + 1

    # draw the die
    dieBorder = Rectangle(Point(centerX - dieSize/2, ctrY - dieSize/2), 
                          Point(centerX + dieSize/2, ctrY + dieSize/2))
    dieBorder.draw(win)
    label = Text(Point(centerX, ctrY), str(dieValue))
    label.draw(win)


def main():
    width, height = 800, 320
    win = GraphWin("Roll 'em", width, height)
    
    firstX, firstY = 35, 70
    dieSize = 40
    dieSpacing = 10
    numberOfDice = 40 #int(input("Number of dice: "))
    horizontalNumberThatFit = (width - 5) // (dieSize + dieSpacing)
    
    
    for i in range(numberOfDice):
        horizontalIndex = i % horizontalNumberThatFit
        verticalIndex = i // horizontalNumberThatFit
        rollAndDrawDie(firstX + horizontalIndex * (dieSize + dieSpacing), 
                       firstY + verticalIndex * (dieSize + dieSpacing), dieSize, win)
    
    win.getMouse()
    win.close()

if __name__ == '__main__':
    main()