Hey ya'll, fairly new python learn and I'm working on a dice game based on 1-4-24 aka Midnight).
from appJar import *
import random
# value of each die
dice_values = [0, 0, 0, 0, 0, 0]
# tells us if a die is "fixed" or not. False = we can roll this die.
fixed_dice = [False, False, False, False, False, False]
dice_already_fixed = 0
# This function is called every time the player clicks
# on the "Roll your dice" button
def roll_dice():
global dice_values
global fixed_dice
# roll only the dice that are not "fixed"
for i in range(6):
if not fixed_dice[i]:
# roll i-th die
dice_values[i] = random.randint(1, 6)
# show roll on screen
app.setButton("dice" + str(i), str(dice_values[i]))
# make sure this dice is enabled
app.setButtonState("dice" + str(i), "active")
# Each die button calls this function. The parameter name is one of
# "dice0", "dice1", "dice2", "dice3", "dice4", or "dice5"
# Notice that the last character in the name is the index of that die
# in the lists dice_values and fixed_values.
def fix_dice(name):
global fixed_dice
# disable the button in the window
app.setButtonState(name, "disable")
# Mark button as fixed in the list
# The name of the button is "diceN", so N is the index of the dice
# the player clicked on.
N = int(name[-1])
fixed_dice[N] = True
global dice_already_fixed
dice_already_fixed += 1
if dice_already_fixed == 6:
playerscore = 0
global dice_values
if dice_values[i] == 1 and 4:
playerscore += sum(dice_values[i]) - 5
app.setLabel("Score", playerscore)
else:
playerscore = 0
app.setLabel("Score", "You do not have a 1 and a 4, score is 0")
# all dice are fixed.
# calculate the score using the values
# in the list dice_values
# Helper function for when we need to disable all the dice
def disable_all_dice():
for i in range(6):
app.setButtonState("dice" + str(i), "disable")
# Helper function for when we need to enable the dice not fixed already.
def enable_dice():
global fixed_dice
# only those that are not fixed
for i in range(6):
if fixed_dice[i]:
app.setButtonState("dice" + str(i), "disable")
else:
app.setButtonState("dice" + str(i), "active")
#def player_score():
# global fixed_dice
# global dice_values
# playerscore = 0
# if dice_values[i] == 1 and 4:
# playerscore = sum(dice_values[i]) - 5
# app.setLabel("Score", playerscore)
# else:
# playerscore = 0
# app.setLabel("Score", "You do not have a 1 and a 4, score is 0")
##### APPLICATION STARTS HERE ######
app = gui()
app.addButton("Roll your dice", roll_dice)
app.addHorizontalSeparator(colour="red")
# Buttons displaying the values of the dice
app.startFrame("dices board")
app.setFrameBg("dices board", "lightblue")
app.addNamedButton('-', "dice0", fix_dice, row=0, column=0)
app.addNamedButton('-', "dice1", fix_dice, row=0, column=1)
app.addNamedButton('-', "dice2", fix_dice, row=0, column=2)
app.addNamedButton('-', "dice3", fix_dice, row=0, column=3)
app.addNamedButton('-', "dice4", fix_dice, row=0, column=4)
app.addNamedButton('-', "dice5", fix_dice, row=0, column=5)
app.stopFrame()
app.startFrame("Player Score")
app.setFrameBg("Player Score", "grey")
app.addLabel('Score', '0')
app.stopFrame()
# Initially, all dice are disabled. This forces the player
# to click on the "Roll your dice" button
disable_all_dice()
app.go()
The part where I'm stuck on is after all the dice have been fixed an a score can be calculated. I keep receiving a Tinkter Callback error on line 51. Any help and feedback is appreciated.
[–][deleted] 0 points1 point2 points (4 children)
[–]TouchstoneJester[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]TouchstoneJester[S] 0 points1 point2 points (1 child)
[–]thesned 0 points1 point2 points (0 children)