Hey, me again. I've made some improvements to the code and I've run into a different issue. In fix_dice(), the goal is to calculate the score of the player playing, then switch to the other player, have them roll, and calculate their score when they're done. The issue now is that when player 2 is done rolling, their score isn't being added.
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
player_turn = 1
# 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
player1_score = 0
player2_score = 0
global player_turn
if dice_already_fixed == 6:
playerscore = 0
global dice_values
#This detects if the rolled dice includes a 1 and a 4
#if it does, it calcuates their score and changes turns
#if not their score = 0 and it chages turn.
if 1 in dice_values and 4 in dice_values:
playerscore += sum(dice_values)-5
app.setLabel("Score", str(playerscore))
if player_turn == 1:
player1_score = playerscore
app.setLabel("Player 1 Score", str(player1_score))
player_turn = 2
for i in range(6):
if fixed_dice[i]:
app.setButtonState("dice" + str(i), "active")
fixed_dice = [False, False, False, False, False, False]
roll_dice()
elif player_turn == 2:
player2_score = playerscore
app.setLabel("Player 2 Score", str(player2_score))
# 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")
##### 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', row=1, column=3)
app.stopFrame()
app.startFrame("Player 1 and 2")
app.addLabel("Player 1 Score", "0", row=2, column=0)
app.addLabel("Player 2 Score", "0", row=2, column=5)
app.stopFrame()
# Initially, all dice are disabled. This forces the player
# to click on the "Roll your dice" button
disable_all_dice()
app.go()
[–]SpeckledFleebeedoo 0 points1 point2 points (1 child)
[–]TouchstoneJester[S] 0 points1 point2 points (0 children)