Пример #1
0
class FinishWindow(Window):
    def __init__(self, master):
        Window.__init__(self, master=master)
        self.master = master
        self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170))
        self.frame = RectangleShape(0.5 * self.width, 0.5 * self.height, GREEN_DARK, outline=2)
        self.victory = None
        self.text_finish = Text(font=(None, 70))
        self.button_restart = Button(self, "Restart", font=(None, 40), callback=self.restart)
        self.button_return_to_menu = Button(self, "Return to menu", font=(None, 40), callback=self.stop)
        self.ask_restart = False
        self.bind_key(pygame.K_ESCAPE, lambda event: self.stop())

    def start(self, victory: bool) -> None:
        self.victory = victory
        if victory is not None:
            self.text_finish.message = "{winner} won".format(winner="You" if victory else "Enemy")
        else:
            self.text_finish.message = "The enemy has left\nthe game"
        self.ask_restart = False
        self.mainloop()

    def update(self):
        if self.ask_restart:
            if self.master.client_socket.recv("restart"):
                self.master.restart = True
                self.stop()
            elif self.master.client_socket.recv("quit"):
                self.text_finish.message = "The enemy has left\nthe game"

    def place_objects(self):
        self.frame.center = self.center
        self.text_finish.center = self.frame.center
        if self.victory is not None:
            self.button_restart.move(bottom=self.frame.bottom - 20, centerx=self.frame.centerx - (self.frame.w // 4))
            self.button_return_to_menu.move(bottom=self.frame.bottom - 20, centerx=self.frame.centerx + (self.frame.w // 4))
        else:
            self.button_restart.hide()
            self.button_return_to_menu.move(bottom=self.frame.bottom - 20, centerx=self.frame.centerx)

    def restart(self):
        if self.master.client_socket.connected():
            self.ask_restart = True
            self.master.client_socket.send("restart")
            self.text_finish.message = "Waiting for\nenemy response"
            self.button_restart.hide()
            self.button_return_to_menu.move(bottom=self.frame.bottom - 20, centerx=self.frame.centerx)
        else:
            self.master.restart = True
            self.stop()
Пример #2
0
class Garage(Window):
    def __init__(self):
        Window.__init__(self,
                        bg_color=GRAY,
                        bg_music=RESOURCES.MUSIC["garage"])
        params_for_all_buttons = {
            "bg": GREEN,
            "hover_bg": GREEN_LIGHT,
            "active_bg": GREEN_DARK,
            "highlight_color": YELLOW,
            "hover_sound": RESOURCES.SFX["select"],
        }
        params_for_button_except_back = {
            "on_click_sound": RESOURCES.SFX["validate"],
            "disabled_sound": RESOURCES.SFX["block"],
            "disabled_bg": GRAY_LIGHT,
        }
        params_for_button_except_back.update(params_for_all_buttons)
        params_for_car_viewer = {
            k: params_for_button_except_back[k]
            for k in ["hover_sound", "on_click_sound"]
        }
        self.button_back = ImageButton(self,
                                       RESOURCES.IMG["blue_arrow"],
                                       **params_for_all_buttons,
                                       on_click_sound=RESOURCES.SFX["back"],
                                       callback=self.stop)
        self.car_viewer = CarViewer(self, SAVE["car"], **params_for_car_viewer)

        size_progress_bar = (300, 30)
        self.speed_bar = ProgressBar(*size_progress_bar, TRANSPARENT, GREEN)
        self.maniability_bar = ProgressBar(*size_progress_bar, TRANSPARENT,
                                           GREEN)
        self.braking_bar = ProgressBar(*size_progress_bar, TRANSPARENT, GREEN)

        self.left_arrow = ImageButton(
            self,
            img=RESOURCES.IMG["left_arrow"],
            active_img=RESOURCES.IMG["left_arrow_hover"],
            **params_for_button_except_back,
            callback=self.car_viewer.decrease_id)
        self.right_arrow = ImageButton(
            self,
            img=RESOURCES.IMG["right_arrow"],
            active_img=RESOURCES.IMG["right_arrow_hover"],
            **params_for_button_except_back,
            callback=self.car_viewer.increase_id)
        for arrow in [self.left_arrow, self.right_arrow]:
            arrow.take_focus(False)
        self.button_price = Button(self,
                                   font=(RESOURCES.FONT["algerian"], 40),
                                   img=Image(RESOURCES.IMG["piece"], size=40),
                                   compound="right",
                                   callback=self.buy_car,
                                   **params_for_button_except_back)
        self.button_play = Button(self,
                                  "Play",
                                  font=(RESOURCES.FONT["algerian"], 70),
                                  callback=self.play,
                                  **params_for_button_except_back)
        self.text_money = Text(format_number(SAVE["money"]),
                               (RESOURCES.FONT["algerian"], 50),
                               YELLOW,
                               img=Image(RESOURCES.IMG["piece"], height=40),
                               compound="right")
        self.text_highscore = Text(
            "Highscore: {}".format(format_number(SAVE["highscore"])),
            (RESOURCES.FONT["algerian"], 50), YELLOW)
        self.padlock = Image(RESOURCES.IMG["padlock"])
        self.bind_key(pygame.K_ESCAPE,
                      lambda event: self.stop(sound=RESOURCES.SFX["back"]))
        self.bind_joystick(
            0, "B", lambda event: self.stop(sound=RESOURCES.SFX["back"]))

    def update(self):
        self.left_arrow.set_visibility(self.car_viewer.id > 1)
        self.right_arrow.set_visibility(
            self.car_viewer.id < len(self.car_viewer))
        if not SAVE["owned_cars"][self.car_viewer.id]:
            self.padlock.show()
            price = self.car_viewer["price"]
            if isinstance(price, int):
                self.button_price.show()
                self.button_price.text = format_number(price)
                self.button_price.state = Button.NORMAL if SAVE[
                    "money"] >= price else Button.DISABLED
            else:
                self.button_price.hide()
            self.button_play.state = Button.DISABLED
        else:
            self.padlock.hide()
            self.button_price.hide()
            self.button_play.state = Button.NORMAL
            SAVE["car"] = self.car_viewer.id
        max_s = self.car_viewer.max_speed
        min_a = self.car_viewer.min_acceleration
        max_m = self.car_viewer.max_maniability
        min_b = self.car_viewer.min_braking
        s = self.car_viewer["max_speed"]
        a = self.car_viewer["acceleration"]
        m = self.car_viewer["maniability"]
        b = self.car_viewer["braking"]
        self.speed_bar.percent = (s + min_a) / (max_s + a)
        self.maniability_bar.percent = m / max_m
        self.braking_bar.percent = min_b / b

    def place_objects(self):
        self.button_back.topleft = (5, 5)
        self.car_viewer.move(center=self.center)
        self.padlock.center = self.car_viewer.center

        self.braking_bar.move(bottom=self.car_viewer.top - 40,
                              centerx=self.car_viewer.centerx + 100)
        self.maniability_bar.move(bottom=self.braking_bar.top - 10,
                                  centerx=self.car_viewer.centerx + 100)
        self.speed_bar.move(bottom=self.maniability_bar.top - 10,
                            centerx=self.car_viewer.centerx + 100)

        self.speed_bar.show_label("Speed/Acc.",
                                  ProgressBar.S_LEFT,
                                  font=(RESOURCES.FONT["algerian"], 40))
        self.maniability_bar.show_label("Maniability",
                                        ProgressBar.S_LEFT,
                                        font=(RESOURCES.FONT["algerian"], 40))
        self.braking_bar.show_label("Braking",
                                    ProgressBar.S_LEFT,
                                    font=(RESOURCES.FONT["algerian"], 40))

        self.left_arrow.move(left=self.left + 50, centery=self.centery)
        self.right_arrow.move(right=self.right - 50, centery=self.centery)
        self.button_price.move(centerx=self.centerx,
                               top=self.car_viewer.bottom + 25)
        self.button_play.move(bottom=self.bottom - 50, right=self.right - 10)

        self.text_money.move(top=5, right=self.right - 10)
        self.text_highscore.move(bottom=self.bottom - 50, left=5)

    def set_grid(self):
        self.button_back.set_obj_on_side(on_bottom=self.car_viewer)
        self.car_viewer.set_obj_on_side(on_top=self.button_back,
                                        on_bottom=self.button_price)
        self.button_price.set_obj_on_side(on_top=self.car_viewer,
                                          on_bottom=self.button_play)
        self.button_play.set_obj_on_side(on_top=self.button_price)

    def buy_car(self):
        confirm_window = ConfirmPayement(self)
        confirm_window.mainloop()
        if confirm_window.buyed:
            SAVE["money"] -= self.car_viewer["price"]
            SAVE["owned_cars"][self.car_viewer.id] = True
            self.text_money.message = format_number(SAVE["money"])
            if Clickable.MODE != Clickable.MODE_MOUSE:
                self.car_viewer.focus_set()

    def play(self):
        environment_chooser = EnvironmentChooser(self)
        environment_chooser.mainloop()
        self.text_money.message = format_number(SAVE["money"])
        self.text_highscore.message = "Highscore: {}".format(
            format_number(SAVE["highscore"]))
Пример #3
0
class FinishWindow(Window):
    def __init__(self, master, victory: bool):
        Window.__init__(
            self,
            master=master,
            bg_music=None if victory is None else RESOURCES.MUSIC["end"])
        self.master = master
        self.bg = RectangleShape(self.width, self.height, (0, 0, 0, 170))
        self.frame = RectangleShape(0.5 * self.width,
                                    0.5 * self.height,
                                    GREEN_DARK,
                                    outline=2)
        self.victory = victory
        if victory is not None:
            message = "{winner} won".format(
                winner="You" if victory else "Enemy")
        else:
            message = "The enemy has left\nthe game"
        self.text_finish = Text(message, font=(None, 70))
        params_for_all_buttons = {
            "font": (None, 40),
            "bg": GREEN,
            "hover_bg": GREEN_LIGHT,
            "active_bg": GREEN_DARK,
            "highlight_color": YELLOW
        }
        self.button_restart = Button(self,
                                     "Restart",
                                     callback=self.restart,
                                     **params_for_all_buttons)
        self.button_return_to_menu = Button(self,
                                            "Return to menu",
                                            callback=self.stop,
                                            **params_for_all_buttons)
        self.ask_restart = False
        self.bind_key(pygame.K_ESCAPE, lambda event: self.stop())

    def update(self):
        if self.ask_restart:
            if self.master.client_socket.recv("restart"):
                self.master.restart = True
                self.stop()
            elif self.master.client_socket.recv("quit"):
                self.text_finish.message = "The enemy has left\nthe game"

    def place_objects(self):
        self.frame.center = self.center
        self.text_finish.center = self.frame.center
        if self.victory is not None:
            self.button_restart.move(bottom=self.frame.bottom - 20,
                                     centerx=self.frame.centerx -
                                     (self.frame.w // 4))
            self.button_return_to_menu.move(bottom=self.frame.bottom - 20,
                                            centerx=self.frame.centerx +
                                            (self.frame.w // 4))
        else:
            self.button_restart.hide()
            self.button_return_to_menu.move(bottom=self.frame.bottom - 20,
                                            centerx=self.frame.centerx)

    def restart(self):
        if self.master.client_socket.connected():
            self.ask_restart = True
            self.master.client_socket.send("restart")
            self.text_finish.message = "Waiting for\nenemy response"
            self.button_restart.hide()
            self.button_return_to_menu.move(bottom=self.frame.bottom - 20,
                                            centerx=self.frame.centerx)
        else:
            self.master.restart = True
            self.stop()