Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Backgammon Board - Python Code Bank


Backgammon Board
This is an assignment I did for my programming class. It creates a backgammon board with the size based on input from the user. It requires the cs1graphics module to run. cs1graphics.org
                from cs1graphics import *

size = int(raw_input('How many pixels do you want per grid-cell? '))

paper = Canvas(15 * size, 13 * size)
paper.setBackgroundColor('burlywood4')

side1 = Rectangle(6 * size, 11 * size, Point(4 * size, 6 * size + .5 * size))
side1.setFillColor('navajowhite')
side1.setDepth(51)
paper.add(side1)

side2 = Rectangle(6 * size, 11 * size, Point(11 * size, 6 * size + .5 * size))
side2.setFillColor('navajowhite')
side2.setDepth(51)
paper.add(side2)

line = Path(Point(size * 15 /2,0), Point(size * 15 /2,size*13))
line.setBorderWidth(size/10)
paper.add(line)

x = size
y = size * 12

for p in range(24):
    point = Polygon(Point(x,y), Point(x + size/2, y - size*5), Point(x + size, y))


    text = Text(str(p+1), size/3)               # Numbers for each point
    if p <= 11:
        text.moveTo(x + size/2, y + size/2)
    else:
        text.moveTo(x + size/2, y - size/2)
    paper.add(text)



    if p % 2 == 0:                              #alternates colors
        point.setFillColor('darkorange3')
    else:
        point.setFillColor('tan')


    if x == 6 * size and p < 11:             #skips over middle bar
        x += size
    elif x == 8 * size and p > 11:
        x -= size
    
    
    if p < 11:                   #x shift and y shift for bottom and top rows
        x += size
    elif p == 11:
        y = size
    else:
        x -= size
        point.flip(90)


    paper.add(point)




for num,pt,whiteOnTop in [(2,1,True), (5,6,False), (3,8,False), (5,12,True)]:

    for p in range(num):

        x = pt * size + (.5 * size)
        y1 = (size * 12 - (.9*size/2)) - (p * (.9*size))    #two y variables for top and bottom
        y2 = (size + (.9*size/2)) + (p * (.9*size))
        
        if pt > 6:                      # jumps over middle bar
            x += size

        checker1 = Circle(.9*size/2, Point(x, y1))
        checker2 = Circle(.9*size/2, Point(x, y2))
        
        if whiteOnTop == True:              #decides colors for both checkers
            checker1.setFillColor('black')
            checker2.setFillColor('white')
        else:
            checker1.setFillColor('white')
            checker2.setFillColor('black')

        checker1.setDepth(1)
        checker2.setDepth(1)
        paper.add(checker1)
        paper.add(checker2)
            
Comments
Sorry but there are no comments to display