[Tutor] Python Dice Game

jake_k2011 jk_kriegel at yahoo.com
Sat Apr 11 06:38:51 CEST 2009


http://www.nabble.com/file/p22982720/button.py button.py 
http://www.nabble.com/file/p22982720/dieview.py dieview.py 
http://www.nabble.com/file/p22982720/graphics.py graphics.py 
http://www.nabble.com/file/p22982720/msdie.py msdie.py 
http://www.nabble.com/file/p22982720/roller.py roller.py 


Im' trying to get this dice game to work and i'm having the worst time to
get it to work :\ I'm a total noob and just a BEGINNER. I can't seem to get
the buttons to work.Basically they are constructed and maybe activated in
the main program, andI  need to be able to use them in the functions roll()
and turn() i believe.

CAn anyone help to get this to work??


roller.py
#!/usr/bin/python
# roller.py
# Graphics program to roll a pair of dice. Uses custom widgets
# Button and GDie.

from random import randrange
from graphics import GraphWin, Point
from graphics import*


from button import Button
from dieview import DieView 

def main():

    intro()
    scoreA = 0
    scoreB = 0


    # create the application window
    win = GraphWin("Dice Roller", 600,400)
    win.setCoords(0, 0, 400, 150)
    win.setBackground("green2")

    # Draw the interface widgets
    die1 = DieView(win, Point(90,120), 40)
    die2 = DieView(win, Point(210,120), 40)
    die3 = DieView(win, Point(90,60), 40)
    die4 = DieView(win, Point(210,60), 40)
    die5 = DieView(win, Point(150,90), 40)
    rollButton = Button(win, Point(150,30), 150, 10, "Roll Dice")
    rollButton.activate()
    passButton = Button(win, Point(150,10), 150, 10, "Pass Turn")
    passButton.activate()
    scoretext1 = Text(Point(300,120),"Player A Score:").draw(win)
    scoretext2 = Text(Point(300,100),scoreA).draw(win)
    scoretext3 = Text(Point(300, 70),"Player B Score:").draw(win)
    scoretext4 = Text(Point(300, 50),scoreB).draw(win)                      


    turn = "A"
    while scoreA<1000 and scoreB<1000:
        tempscore = turn(passButton)
        if turn == "A":
            scoreA = scoreA+tempscore
            scoretext2.undraw()
            scoretext2 = Text(Point(300,100),scoreA).draw(win)
            turn = "B"
        else:
            scoreB = scoreB+tempscore
            scoretext4.undraw()
            scoretext4 = Text(Point(300, 50),scoreB).draw(win)
            turn = "A"
    if scoreA >= 1000:
        print "Congratulations, Player A WINS!!!"
    else:
        print "Congratulations, Player B WINS!!!"
    raw_input("Press <Enter> to close game.")


def intro():
    intro = GraphWin("Introduction", 500, 500)
    intro.setCoords(0,0,100,100)
    text1=Text(Point(50,90), "Player A begins by clicking the 'Roll'
button.").draw(intro)
    text2=Text(Point(50,80), "The player has up to three rolls to score as
many points").draw(intro)
    text3=Text(Point(50,70), "as possible. If 'snake-eyes' (two ones) are
rolled,").draw(intro)
    text4=Text(Point(50,60), "the player loses all points from the current
round.").draw(intro)
    text5=Text(Point(50,50), "At any time, a player can choose to protect
his or her").draw(intro)
    text6=Text(Point(50,40), "score by passing the roll to the other
player.").draw(intro)
    text7=Text(Point(50,30), "The game ends when the first player reaches
1,000 points.").draw(intro)
    text8=Text(Point(50,10), "Click screen to begin game.").draw(intro)
    intro.getMouse()
    intro.close()
    

def turn(passButton):
    pt = win.getMouse()
    score = 0
    rollcount = 0
    snakeeyes = 0
    while snakeeyes == 0 and rollcount < 3 and not passButton.clicked(pt):
        tempscore,snakeeyes = scoreroll()
        score = score+tempscore
        rollcount = rollcount+1
    return score


def scoreroll():
    score = 0
    rlist = roll()
    if rlist.count(1) == 2:
        score = 0
        snakeeyes = 1
    else:
        for value in rlist:
            if rlist.count(value)==3:
                score = score+value*100
            elif rlist.count(value)==2:
                score = score+value*10
            else:
                score = score+value
    return score, snakeeyes
    
    
                

def roll():
    pt = win.getMouse()
    while not quitButton.clicked(pt):
        if rollButton.clicked(pt):
            value1 = randrange(1,7)
            die1.setValue(value1)
            value2 = randrange(1,7)
            die2.setValue(value2)
            value3= randrange(1,7)
            die3.setValue(value3)
            value4 = randrange(1,7)
            die4.setValue(value4)
            value5 = randrange(1,7)
            die5.setValue(value5)
    rlist = [value1,value2,value3,value4,value5]
    return rlist



main()
    
            
THANKS 
-- 
View this message in context: http://www.nabble.com/Python-Dice-Game-tp22982720p22982720.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list