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()