I"ve been working on this dice game for the past few days. I made sure the game itself worked properly then I added a draw dice function that would show you the dice that were rolled on the canvas. I needed a continue button to make the rest of the game run, but when I press my button I have a couple problems. One it does not update the canvas so for example if you roll a 4 on dice one then next turn a 2 it's still going to look like a 4. second it does not give me messages for when a one is rolled or 2 is rolled, nor does it go on to the next players turn like its suppose too. here is my code any help would be greatley appricated.

from tkinter import*
master=Tk()
canvas = Canvas(master, width = 300, height = 200);
canvas.pack();
from random import randint
#### Dice
def draw(rolled,rolled2):
    dice = [rolled,rolled2]
    for i in range(2):
        canvas.create_rectangle(5+195*i,50,105+195*i,150)
        if dice[i] == 1:
            canvas.create_oval(45+195*i,85,65+195*i,105, fill="black")
        elif dice[i] == 2:
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
        elif dice[i] == 3:
            canvas.create_oval(45+195*i,85,65+195*i,105, fill="black")
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
        elif dice[i] == 4:
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
            canvas.create_oval(12+195*i,122,32+195*i,142, fill="black")
            canvas.create_oval(82+195*i,52,102+195*i,72, fill="black")
        elif dice[i] == 5:
            canvas.create_oval(45+195*i,85,65+195*i,105, fill="black")
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
            canvas.create_oval(12+195*i,122,32+195*i,142, fill="black")
            canvas.create_oval(82+195*i,52,102+195*i,72, fill="black")
        elif dice[i] == 6:
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
            canvas.create_oval(12+195*i,122,32+195*i,142, fill="black")
            canvas.create_oval(82+195*i,52,102+195*i,72, fill="black")
            canvas.create_oval(47+195*i,52,67+195*i,72, fill="black")
            canvas.create_oval(47+195*i,122,67+195*i,142, fill="black")

    button1 = Button(master, text = "continue", command =lambda:game(), anchor = W)
    button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button1_window = canvas.create_window(10, 180, anchor=SW, window=button1)


#####game    
def game():
    playercount = 2
    maxscore = 100
    safescore = [0] * playercount
    player = 0
    score=0

    while max(safescore) < maxscore:
        if player == 0:
            rolling = 0
            if score < 17 and score + safescore[player] < maxscore:
                rolling = 1
        else:
            rolling = input("Player %i: (%i, %i) Rolling? (Y) "
                % (player, safescore[player], score)).strip().lower() in {'yes', 'y', ''}
        if rolling:
            rolled = randint(1, 6)
            rolled2 = randint(1, 6)
            print('  Rolled %i' % rolled)
            print('  Rolled %i' % rolled2)
            draw(rolled,rolled2)   
            mainloop()
            if rolled ==1 and rolled2 ==1:
                    print('  Snake Eyes!! your score is set to 0')
                    safescore[player] = 0    
                    player = (player + 1) % playercount

            elif rolled == 1:
                print('  Bust! you lose %i but still keep your previous %i'
                      % (score, safescore[player]))
                score, player = 0, (player + 1) % playercount
            elif rolled2 == 1:
                print('  Bust! you lose %i but still keep your previous %i'
                      % (score, safescore[player]))
                score, player = 0, (player + 1) % playercount


            else:
                score += rolled + rolled2
        else:
            safescore[player] += score
            if safescore[player] >= maxscore:
                break
            print('  Sticking with %i' % safescore[player])
            score, player = 0, (player + 1) % playercount

    print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))
game()

Recommended Answers

All 6 Replies

second it does not give me messages for when a one is rolled or 2 is rolled

Do you mean one or two dice are rolled, or the dice rolled total one or two

nor does it go on to the next players turn like its suppose too

The function game() always assigns zero to player

I added one line as the previous roll was covering up the current roll

canvas.delete(ALL)  ## remove all items

and modified another as you were calling the function incorrectly

button1 = Button(master, text = "continue", command=game)

Complete code, modified, and if this does not do it, post back

from tkinter import*
from functools import partial

master=Tk()
canvas = Canvas(master, width = 300, height = 200);
canvas.pack();
from random import randint
#### Dice
def draw(rolled,rolled2):
    dice = [rolled,rolled2]
    for i in range(2):
        canvas.create_rectangle(5+195*i,50,105+195*i,150)
        if dice[i] == 1:
            canvas.create_oval(45+195*i,85,65+195*i,105, fill="black")
        elif dice[i] == 2:
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
        elif dice[i] == 3:
            canvas.create_oval(45+195*i,85,65+195*i,105, fill="black")
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
        elif dice[i] == 4:
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
            canvas.create_oval(12+195*i,122,32+195*i,142, fill="black")
            canvas.create_oval(82+195*i,52,102+195*i,72, fill="black")
        elif dice[i] == 5:
            canvas.create_oval(45+195*i,85,65+195*i,105, fill="black")
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
            canvas.create_oval(12+195*i,122,32+195*i,142, fill="black")
            canvas.create_oval(82+195*i,52,102+195*i,72, fill="black")
        elif dice[i] == 6:
            canvas.create_oval(12+195*i,52,32+195*i,72, fill="black")
            canvas.create_oval(82+195*i,122,102+195*i,142, fill="black")
            canvas.create_oval(12+195*i,122,32+195*i,142, fill="black")
            canvas.create_oval(82+195*i,52,102+195*i,72, fill="black")
            canvas.create_oval(47+195*i,52,67+195*i,72, fill="black")
            canvas.create_oval(47+195*i,122,67+195*i,142, fill="black")

    button1 = Button(master, text = "continue", command=game)
    button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
    button1_window = canvas.create_window(10, 180, anchor=SW, window=button1)


#####game    
def game():
    playercount = 2
    maxscore = 100
    safescore = [0] * playercount
    player = 0
    score=0

    while max(safescore) < maxscore:
        if player == 0:
            rolling = 0
            if score < 17 and score + safescore[player] < maxscore:
                rolling = 1
        else:
            rolling = input("Player %i: (%i, %i) Rolling? (Y) "
                % (player, safescore[player], score)).strip().lower() in {'yes', 'y', ''}
        if rolling:
            rolled = randint(1, 6)
            rolled2 = randint(1, 6)
            print('  Rolled %i' % rolled)
            print('  Rolled %i' % rolled2)
            draw(rolled,rolled2)   
            mainloop()
            if rolled ==1 and rolled2 ==1:
                    print('  Snake Eyes!! your score is set to 0')
                    safescore[player] = 0    
                    player = (player + 1) % playercount

            elif rolled == 1:
                print('  Bust! you lose %i but still keep your previous %i'
                      % (score, safescore[player]))
                score, player = 0, (player + 1) % playercount
            elif rolled2 == 1:
                print('  Bust! you lose %i but still keep your previous %i'
                      % (score, safescore[player]))
                score, player = 0, (player + 1) % playercount


            else:
                score += rolled + rolled2
        else:
            safescore[player] += score
            if safescore[player] >= maxscore:
                break
            print('  Sticking with %i' % safescore[player])
            score, player = 0, (player + 1) % playercount

    print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))
game()

It kinda works the only problem is it still doesn't play the rest of the game. the dice show up correctly now. I don't get me messages for when a 1 or two ones are rolled, nor does it go to the next players turn. For example when a one is rolled on just one dice you get a message saying bust! you lose you (points rolled) but keep your previous score of (points) then goes to the next players turn. If two ones are rolled it says snake eyes!! your score is set to 0.

so, because I posted it two other places to try and get help on this means your done helping me?

It means check those other forums before answering, or we may be wasting our time answering a question that has already been answered elsewhere. We are volunteers and have limited time to spend.

In your particular case, you posted again that it does not change players, which I addressed with a hint in the first response. This implies that you are not reading our responses. Not reading the responses usually means that are we wasting our time because the OP is just looking for someone to code it for them instead of figuring it out for themselves, so isn't reading, just waiting for code that they can copy and paste.

well sorry if that what you thought I was intendding on doing, but no it's not. I was trying to get help simple as does not mean I'm waiting on code I'm deffently reading all this trying to make sense of everything. I'm obivosuly new to progamining, and I'm try to learn. Some peoples explnations make sense more than others. I did no think this would be a problem obvisouly I know now. If it means anything no one has posted anything on the other two places that are helpfull.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.