# this is a graphical version of the "Pig" dice game
from graphics import *
import random
import time
def makeWindow():
# make the window
win = GraphWin("Pig!", 1000, 1000)
win.setBackground("lightblue")
return win
def makeLabel(x, y, size, color, message, win):
text = Text(Point(x, y), message)
text.setSize(size)
text.setTextColor(color)
text.draw(win)
return text
def makeButton(x, y, text, win):
rect = Rectangle(Point(x, y), Point(x + 125, y + 150))
mesg = Text(Point(x + 62, y + 75), text)
rect.draw(win)
mesg.draw(win)
def makePip(x, y, win):
pip = Circle(Point(x, y) , 15)
pip.setFill("red")
pip.draw(win)
return pip
def drawDice(dice, pips, win):
# hide any old pips
for pip in pips:
pip.undraw()
pips = []
if dice == 1:
pips.append(makePip(500, 620, win))
elif dice == 2:
pips.append(makePip(420, 700, win))
pips.append(makePip(580, 540, win))
elif dice == 3:
pips.append(makePip(420, 700, win))
pips.append(makePip(580, 540, win))
pips.append(makePip(500, 620, win))
elif dice == 4:
pips.append(makePip(420, 700, win))
pips.append(makePip(580, 540, win))
pips.append(makePip(580, 700, win))
pips.append(makePip(420, 540, win))
elif dice == 5:
pips.append(makePip(420, 700, win))
pips.append(makePip(580, 540, win))
pips.append(makePip(580, 700, win))
pips.append(makePip(420, 540, win))
pips.append(makePip(500, 620, win))
elif dice == 6:
pips.append(makePip(420, 700, win))
pips.append(makePip(580, 540, win))
pips.append(makePip(580, 700, win))
pips.append(makePip(420, 540, win))
pips.append(makePip(580, 620, win))
pips.append(makePip(420, 620, win))
return pips
# call the function to make the window
win = makeWindow()
# make the labels we will need
p_score = makeLabel(125, 25, 20, "green", "Player: 0", win)
c_score = makeLabel(775, 25, 20, "purple", "Computer: 0", win)
turn_label = makeLabel(500, 250, 30, "gold", "Player's Turn", win)
round_text = makeLabel(500, 900, 25, "red", "Round = 0", win)
# make our two buttons
makeButton(250, 350, "Roll", win)
makeButton(625, 350, "Stay", win)
# the die in the screen
dice_square = Rectangle(Point(380, 500), Point(620, 740))
dice_square.setOutline("black")
dice_square.setFill("green")
dice_square.draw(win)
# return true if the player is rolling, false if staying
def playerRoll():
while True:
coords = win.getMouse()
x = coords.getX()
y = coords.getY()
if x >= 250 and x <= 375 and y >= 350 and y <= 500:
return True
elif x >= 625 and x <= 750 and y <= 500 and y >= 350:
return False
# return true if the computer is rolling
def computerRoll(round_score):
time.sleep(1)
if round_score < 20:
return True
else:
return False
# perform one round of the game
def gameRound(playerTurn, pips):
if playerTurn == True:
turn_label.setText("Player's Turn")
else:
turn_label.setText("Computer's Turn")
round_score = 0
round_text.setText("Round = " + str(round_score))
roll = True
# keep going until they choose Stay
while roll != False:
if playerTurn == True:
roll = playerRoll()
else:
roll = computerRoll(round_score)
if roll == True:
# roll the dice, and add it to round score
dice = random.randint(1, 6)
print(dice)
# handle the dice
pips = drawDice(dice, pips, win)
# add the roll into the round score
round_score = round_score + dice
round_text.setText("Round = " + str(round_score))
# if they got a 1, they lose their points, and rest of turn
if dice == 1:
round_score = 0
round_text.setText("Round = " + str(round_score))
roll = False
time.sleep(1)
# remove the pips
for pip in pips:
pip.undraw()
return round_score
# they chose stay
else:
for pip in pips:
pip.undraw()
roll = False
return round_score
def endGame(message, win):
screen = Rectangle(Point(0, 0), Point(1000, 1000))
screen.setFill("black")
screen.draw(win)
mesg = Text(Point(500, 500), message)
mesg.setSize(30)
mesg.setTextColor("white")
mesg.draw(win)
time.sleep(3)
win.close()
# these keep track of the total score for each player
player_score = 0
computer_score = 0
# our list of pips for drawing the dice
pips = []
# keep going until someone wins
playerTurn = True
while player_score < 100 and computer_score < 100:
round_score = gameRound(playerTurn, pips)
print(round_score)
if playerTurn == True:
player_score = player_score + round_score
p_score.setText("Player: " + str(player_score))
playerTurn = False
else:
computer_score = computer_score + round_score
c_score.setText("Computer: " + str(computer_score))
playerTurn = True
# check who won
if player_score > computer_score:
endGame("Yay you won!")
else:
endGame("Sorry you lost :(")