예제 #1
0
class Game(Box):
    def timerCountdown(self):
        if self.config["two_players"]:
            if self.player1Turn:
                self.player1Timer = self.player1Timer - 1
                self.timerText.value = str(self.player1Timer)
            else:
                self.player2Timer = self.player2Timer - 1
                self.timerText.value = str(self.player2Timer)

            if (self.player1Timer <= 0) and (self.player2Timer <= 0):
                self.showGameOver()
            elif self.player1Timer == 0:
                self.timerText.value = self.player2Timer
                self.player1Turn = False
                self.player1Timer = -1
                self.generateNewBoard()
            elif self.player2Timer == 0:
                self.timerText.value = self.player1Timer
                self.player1Turn = True
                self.player2Timer = -1
                self.generateNewBoard()

        else:
            self.timerText.value = int(self.timerText.value) - 1
            if self.timerText.value == "0":
                self.showGameOver()

    def is_correct_guess(self, pushButton):
        if self.config["two_players"]:
            if self.player1Turn:
                if self.same_image == pushButton.image:
                    self.player1ScoreText.value = int(
                        self.player1ScoreText.value) + 1
                else:
                    self.player1ScoreText.value = int(
                        self.player1ScoreText.value) - 1

                if (self.player2Timer > 0):
                    self.timerText.value = self.player2Timer
                    self.player1Turn = False

                self.generateNewBoard()

            else:
                if self.same_image == pushButton.image:
                    self.player2ScoreText.value = int(
                        self.player2ScoreText.value) + 1
                else:
                    self.player2ScoreText.value = int(
                        self.player2ScoreText.value) - 1

                if (self.player1Timer > 0):
                    self.timerText.value = self.player1Timer
                    self.player1Turn = True

                self.generateNewBoard()

        else:
            if self.same_image == pushButton.image:
                self.player1ScoreText.value = int(
                    self.player1ScoreText.value) + 1
                self.generateNewBoard()
            else:
                self.player1ScoreText.value = int(
                    self.player1ScoreText.value) - 1

    def generateNewBoard(self):

        if self.emojiShowBox != None:
            self.emojiShowBox.destroy()
        if self.emojiGuessBox != None:
            self.emojiGuessBox.destroy()

        self.emojiShowBox = Box(self, layout="grid")
        self.emojiGuessBox = Box(self, layout="grid")

        emojis_path = os.listdir("images")

        show_images = []
        guess_images = []

        # Get random images for both boxes
        for i in range(0, self.config["board_size"] - 1):
            show_images.append(
                "images/" +
                emojis_path.pop(random.randint(0,
                                               len(emojis_path) - 1)))
            guess_images.append(
                "images/" +
                emojis_path.pop(random.randint(0,
                                               len(emojis_path) - 1)))

        # Get image that is equal between both boxes
        rand_index = random.randint(0, len(emojis_path) - 1)
        self.same_image = "images/" + emojis_path[rand_index]
        show_images.append(self.same_image)
        guess_images.append(self.same_image)

        random.shuffle(show_images)
        random.shuffle(guess_images)

        board_size_rows = math.floor(math.sqrt(self.config["board_size"]))

        for i in range(0, self.config["board_size"]):
            x = i // board_size_rows
            y = i % board_size_rows

            picture = Picture(self.emojiShowBox,
                              grid=[x, y],
                              image=show_images[i])

            pushButton = PushButton(self.emojiGuessBox,
                                    grid=[x, y],
                                    image=guess_images[i])
            pushButton.update_command(command=self.is_correct_guess,
                                      args=[pushButton])

    def showGameOver(self):
        self.emojiShowBox.destroy()
        self.emojiGuessBox.destroy()
        self.cancel(self.timerCountdown)
        self.timerText.destroy()

        Text(self, text="Game Over")
        self.mainMenuButton.visible = True

        if not self.config["two_players"]:
            self.nameForLeaderBoard.visible = True
            self.submitScoreButton.visible = True

    def __init__(self, master, config):
        super().__init__(master=master)

        self.config = config

        self.emojiShowBox = None
        self.emojiGuessBox = None

        self.timerText = Text(self, text=self.config["round_timer"])

        self.playerScoreBox = Box(self, align="left")

        self.player1ScoreLabel = Text(self.playerScoreBox,
                                      text="Player 1 Score")
        self.player1ScoreText = Text(self.playerScoreBox, text="0")

        self.player1Turn = True

        if (self.config["two_players"]):
            self.player2ScoreLabel = Text(self.playerScoreBox,
                                          text="Player 2 Score")
            self.player2ScoreText = Text(self.playerScoreBox, text="0")

            self.player1Timer = self.config["round_timer"]
            self.player2Timer = self.config["round_timer"]

        self.same_image = ""

        self.mainMenuButton = PushButton(self, text="Main Menu", visible=False)

        self.nameForLeaderBoard = TextBox(self,
                                          text="Enter your name:",
                                          visible=False)
        self.submitScoreButton = PushButton(self, text="Submit", visible=False)

        self.generateNewBoard()

        self.repeat(1000, self.timerCountdown)
예제 #2
0
파일: Main.py 프로젝트: blgutie1/EECS159A
def Make_keys():
    global Keyboard_box
    global keys_exist
    global uppercase
    global lower_letters
    if (keys_exist == 0):
        Weather_drawing.grid = [0, 7, 2, 1]
        Keyboard_box = Box(app,
                           width=500,
                           height=300,
                           layout="grid",
                           grid=[0, 6, 4, 1])
        keys_exist = 1
        uppercase = 0
    elif (keys_exist == 1 and uppercase == 1):
        Keyboard_box.destroy()
        Keyboard_box = Box(app,
                           width=500,
                           height=300,
                           layout="grid",
                           grid=[0, 6, 2, 1])
        uppercase = 0
#Keyboard
#1st Row
    Q_button = PushButton(Keyboard_box,
                          grid=[0, 0],
                          width=2,
                          height=2,
                          text=lower_letters[0],
                          command=type_function,
                          args=lower_letters[0])
    Q_button.bg = "light gray"
    Q_button.text_size = 10

    W_button = PushButton(Keyboard_box,
                          grid=[1, 0],
                          width=2,
                          height=2,
                          text=lower_letters[1],
                          command=type_function,
                          args=lower_letters[1])
    W_button.bg = "light gray"
    W_button.text_size = 10

    E_button = PushButton(Keyboard_box,
                          grid=[2, 0],
                          width=2,
                          height=2,
                          text=lower_letters[2],
                          command=type_function,
                          args=lower_letters[2])
    E_button.bg = "light gray"
    E_button.text_size = 10

    R_button = PushButton(Keyboard_box,
                          grid=[3, 0],
                          width=2,
                          height=2,
                          text=lower_letters[3],
                          command=type_function,
                          args=lower_letters[3])
    R_button.bg = "light gray"
    R_button.text_size = 10

    T_button = PushButton(Keyboard_box,
                          grid=[4, 0],
                          width=2,
                          height=2,
                          text=lower_letters[4],
                          command=type_function,
                          args=lower_letters[4])
    T_button.bg = "light gray"
    T_button.text_size = 10

    Y_button = PushButton(Keyboard_box,
                          grid=[5, 0],
                          width=2,
                          height=2,
                          text=lower_letters[5],
                          command=type_function,
                          args=lower_letters[5])
    Y_button.bg = "light gray"
    Y_button.text_size = 10

    U_button = PushButton(Keyboard_box,
                          grid=[6, 0],
                          width=2,
                          height=2,
                          text=lower_letters[6],
                          command=type_function,
                          args=lower_letters[6])
    U_button.bg = "light gray"
    U_button.text_size = 10

    I_button = PushButton(Keyboard_box,
                          grid=[7, 0],
                          width=2,
                          height=2,
                          text=lower_letters[7],
                          command=type_function,
                          args=lower_letters[7])
    I_button.bg = "light gray"
    I_button.text_size = 10

    O_button = PushButton(Keyboard_box,
                          grid=[8, 0],
                          width=2,
                          height=2,
                          text=lower_letters[8],
                          command=type_function,
                          args=lower_letters[8])
    O_button.bg = "light gray"
    O_button.text_size = 10

    P_button = PushButton(Keyboard_box,
                          grid=[9, 0],
                          width=2,
                          height=2,
                          text=lower_letters[9],
                          command=type_function,
                          args=lower_letters[9])
    P_button.bg = "light gray"
    P_button.text_size = 10

    #2nd Row
    A_button = PushButton(Keyboard_box,
                          grid=[0, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[10],
                          command=type_function,
                          args=lower_letters[10])
    A_button.bg = "light gray"
    A_button.text_size = 10

    S_button = PushButton(Keyboard_box,
                          grid=[1, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[11],
                          command=type_function,
                          args=lower_letters[11])
    S_button.bg = "light gray"
    S_button.text_size = 10

    D_button = PushButton(Keyboard_box,
                          grid=[2, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[12],
                          command=type_function,
                          args=lower_letters[12])
    D_button.bg = "light gray"
    D_button.text_size = 10

    F_button = PushButton(Keyboard_box,
                          grid=[3, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[13],
                          command=type_function,
                          args=lower_letters[13])
    F_button.bg = "light gray"
    F_button.text_size = 10

    G_button = PushButton(Keyboard_box,
                          grid=[4, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[14],
                          command=type_function,
                          args=lower_letters[14])
    G_button.bg = "light gray"
    G_button.text_size = 10

    H_button = PushButton(Keyboard_box,
                          grid=[5, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[15],
                          command=type_function,
                          args=lower_letters[15])
    H_button.bg = "light gray"
    H_button.text_size = 10

    J_button = PushButton(Keyboard_box,
                          grid=[6, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[16],
                          command=type_function,
                          args=lower_letters[16])
    J_button.bg = "light gray"
    J_button.text_size = 10

    K_button = PushButton(Keyboard_box,
                          grid=[7, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[17],
                          command=type_function,
                          args=lower_letters[17])
    K_button.bg = "light gray"
    K_button.text_size = 10

    L_button = PushButton(Keyboard_box,
                          grid=[8, 1, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[18],
                          command=type_function,
                          args=lower_letters[18])
    L_button.bg = "light gray"
    L_button.text_size = 10

    #3rd Row
    shift_button = PushButton(Keyboard_box,
                              grid=[0, 2, 2, 1],
                              align="left",
                              width=2,
                              height=2,
                              padx=15,
                              pady=10,
                              text="Shift",
                              command=Make_upperkeys)
    shift_button.bg = "light gray"
    shift_button.text_size = 10

    Z_button = PushButton(Keyboard_box,
                          grid=[1, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[19],
                          command=type_function,
                          args=lower_letters[19])
    Z_button.bg = "light gray"
    Z_button.text_size = 10

    X_button = PushButton(Keyboard_box,
                          grid=[2, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[20],
                          command=type_function,
                          args=lower_letters[20])
    X_button.bg = "light gray"
    X_button.text_size = 10

    C_button = PushButton(Keyboard_box,
                          grid=[3, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[21],
                          command=type_function,
                          args=lower_letters[21])
    C_button.bg = "light gray"
    C_button.text_size = 10

    V_button = PushButton(Keyboard_box,
                          grid=[4, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[22],
                          command=type_function,
                          args=lower_letters[22])
    V_button.bg = "light gray"
    V_button.text_size = 10

    B_button = PushButton(Keyboard_box,
                          grid=[5, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[23],
                          command=type_function,
                          args=lower_letters[23])
    B_button.bg = "light gray"
    B_button.text_size = 10

    N_button = PushButton(Keyboard_box,
                          grid=[6, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[24],
                          command=type_function,
                          args=lower_letters[24])
    N_button.bg = "light gray"
    N_button.text_size = 10

    M_button = PushButton(Keyboard_box,
                          grid=[7, 2, 2, 1],
                          width=2,
                          height=2,
                          text=lower_letters[25],
                          command=type_function,
                          args=lower_letters[25])
    M_button.bg = "light gray"
    M_button.text_size = 10

    backspace_button = PushButton(Keyboard_box,
                                  grid=[8, 2, 2, 1],
                                  align="right",
                                  width=2,
                                  height=2,
                                  padx=15,
                                  pady=10,
                                  text="<-",
                                  command=type_function,
                                  args=lower_letters[27])
    backspace_button.bg = "light gray"
    backspace_button.text_size = 10

    #4th Row
    Space_button = PushButton(Keyboard_box,
                              grid=[2, 3, 6, 1],
                              width=28,
                              height=2,
                              padx=5,
                              pady=1,
                              text="Space",
                              command=type_function,
                              args=lower_letters[26])
    Space_button.bg = "light gray"
    Space_button.text_size = 10

    Hide_button = PushButton(Keyboard_box,
                             grid=[8, 3, 6, 1],
                             width=5,
                             height=2,
                             padx=5,
                             pady=1,
                             text="Hide",
                             command=Destroy_keys)
    Hide_button.text_size = 10
    Hide_button.bg = "light gray"
예제 #3
0
class ControlPanel():
    def __init__(self):
        self.components = {
            "AND": And,
            "OR": Or,
            "XOR": Xor,
            "NOT": Not,
            "Half Adder": HalfAdder,
            "Full Adder": FullAdder,
            "8 bit Ripple Carry Adder": EightBitRippleCarryAdder,
            "8 bit Ripple Carry Adder Subtractor":
            EightBitRippleCarryAdderSubtractor,
            "ALU": ALU,
            "2 to 1 Multiplexer": TwoToOneMultiplexer,
            "4 to 1 Multiplexer": FourToOneMultiplexer,
            "2 to 4 Decoder": TwoToFourDecoder,
            "And Or Latch": AndOrLatch,
            "Gated Latch": GatedLatch,
            "RAM Cell": RAMCell,
            "Eight Bit Register": EightBitRegister,
        }

        self.app = App(title="Philbin SS Control Panel", layout="grid")
        self.box_component = Box(self.app, grid=[0, 1])

        self.combo = Combo(self.box_component,
                           options=list(self.components.keys()),
                           command=self.reload_component)

        self.setup_component()

        self.app.display()

    def setup_component(self):
        #create the component
        self.component = self.components[self.combo.get()]()
        self.box_inputs = Box(self.app, layout="grid", grid=[0, 0])
        self.box_outputs = Box(self.app, layout="grid", grid=[0, 2])

        self.input_power = []
        self.input_checks = []
        self.output_checks = []

        #setup the inputs and power switches
        #for i in range(len(self.component.inputs)):
        i = 0
        for input_key in self.component.inputs:
            #is this input a list of inputs?
            if isinstance(self.component.inputs[input_key], list):
                s = 0
                for sub_input in self.component.inputs[input_key]:
                    p = Power()
                    p.connect(sub_input)
                    self.input_power.append(p)
                    self.input_checks.append(
                        CheckBox(self.box_inputs,
                                 text=input_key + ":" + str(s),
                                 grid=[i, 0],
                                 command=self.update_values))
                    s += 1
                    i += 1
            else:
                p = Power()
                p.connect(self.component.inputs[input_key])
                self.input_power.append(p)
                self.input_checks.append(
                    CheckBox(self.box_inputs,
                             text=input_key,
                             grid=[i, 0],
                             command=self.update_values))
                i += 1

        #setup the outputs
        o = 0
        for output_key in self.component.outputs:
            if isinstance(self.component.outputs[output_key], list):
                s = 0
                for sub_output in self.component.outputs[output_key]:
                    output_check = CheckBox(self.box_outputs,
                                            text=output_key + ":" + str(s),
                                            grid=[o, 2])
                    output_check.configure(state="disabled")
                    self.output_checks.append(output_check)
                    s += 1
                    o += 1
            else:
                output_check = CheckBox(self.box_outputs,
                                        text=output_key,
                                        grid=[o, 2])
                output_check.configure(state="disabled")
                self.output_checks.append(output_check)
                o += 1

        self.update_values()

    def update_values(self):
        #set the power switched based on the check boxes
        for i in range(len(self.input_checks)):
            self.input_power[i].value = (self.input_checks[i].get_value() == 1)

        #update the outputs
        #for o in range(len(self.component.outputs)):
        o = 0
        for output_key in self.component.outputs:
            if isinstance(self.component.outputs[output_key], list):
                for sub_output in self.component.outputs[output_key]:
                    if sub_output.value:
                        self.output_checks[o].select()
                    else:
                        self.output_checks[o].deselect()
                    o += 1
            else:
                if self.component.outputs[output_key].value:
                    self.output_checks[o].select()
                else:
                    self.output_checks[o].deselect()
                o += 1

    def reload_component(self, x):
        self.box_inputs.destroy()
        self.box_outputs.destroy()
        self.setup_component()
        self.app.display()