Exemplo n.º 1
0
    def __init__(self, x=0.0, y=0.0, w=480, h=768, ball=None, gamer1=None, gamer2=None, interval=0.02):
        self.__x = x
        self.__y = y
        self.__w = w
        self.__h = h
        self.interval = interval

        self.NNCTR = NNCTR()
        self.gui = GUI(self)
        self.game_gui = PingPongGUI()

        self.pp_settings = self.gui.new_window("Ping Pong Settings", 70.0, 100.0, 240, 350)
        self.win = self.gui.new_window("Ping Pong", x, y, w, h)

        self.pp_settings.iconbitmap('./favicon/game.ico')
        self.win.iconbitmap('./favicon/game.ico')

        self.space = self.game_gui.new_gamespace(self.win, x=x, y=y, w=w, h=h)

        self.__ball = self.create_ball(ball)
        self.__gamer1 = self.create_gamer(gamer1, id=1)
        self.__gamer2 = self.create_gamer(gamer2, id=2)

        self.win.bind('<Key>', self.key_press)
        self.win.bind('<KeyRelease>', self.key_release)

        frame = self.gui.frame(self.pp_settings, x=0, y=0, w=240, h=360, bind=None, bg="#77AA77")
        frame_control = self.gui.frame(frame, x=10, y=0, w=220, h=70, bind=None, bg="#669966")
        frame_settings = self.gui.frame(frame, x=10, y=80, w=220, h=270, bind=None, bg="#669966")

        self.gui.text(frame_control, x=60, y=5, w=100, h=20, text=self.NNCTR.localization("game_control_title"),
                      bg="#669966", font='Arial 14')

        self.gui.button(frame_control, x=20, y=30, w=80, h=30, text=self.NNCTR.localization("game_start_button"),
                        bind=("<Button-1>", lambda event:  self.start()))

        self.gui.button(frame_control, x=120, y=30, w=80, h=30, text=self.NNCTR.localization("game_stop_button"),
                        bind=("<Button-1>", lambda event:  self.stop()))

        self.gui.text(frame_settings, x=60, y=0, w=100, h=20, text=self.NNCTR.localization("game_settings_title"),
                      bg="#669966", font='Arial 14')

        self.gui.text(frame_settings, x=10, y=40, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_ball"),
                      bg="#669966")

        x_speed_ball = self.gui.input(frame_settings, x=120, y=40, w=40, h=20)

        self.gui.set_text_input(x_speed_ball, "")

        y_speed_ball = self.gui.input(frame_settings, x=170, y=40, w=40, h=20)

        self.gui.set_text_input(y_speed_ball, "")

        self.gui.text(frame_settings, x=10, y=70, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_of_p1"),
                      bg="#669966")

        x_speed_g1 = self.gui.input(frame_settings, x=170, y=70, w=40, h=20)

        self.gui.set_text_input(x_speed_g1, "")

        self.gui.text(frame_settings, x=10, y=100, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_of_p2"),
                      bg="#669966")

        x_speed_g2 = self.gui.input(frame_settings, x=170, y=100, w=40, h=20)

        self.gui.set_text_input(x_speed_g2, "")

        self.gui.text(frame_settings, x=10, y=130, w=110, h=20, text=self.NNCTR.localization("game_settings_radius"),
                      bg="#669966")

        r_ball = self.gui.input(frame_settings, x=170, y=130, w=40, h=20)

        self.gui.set_text_input(r_ball, "10")

        self.gui.text(frame_settings, x=10, y=160, w=130, h=20, text=self.NNCTR.localization("game_settings_collision"),
                      bg="#669966")

        k_ball = self.gui.input(frame_settings, x=170, y=160, w=40, h=20)

        self.gui.set_text_input(k_ball, "1.2")

        self.gui.text(frame_settings, x=10, y=190, w=110, h=20, text=self.NNCTR.localization("game_settings_cords"),
                      bg="#669966")

        x_ball = self.gui.input(frame_settings, x=120, y=190, w=40, h=20)

        self.gui.set_text_input(x_ball, "")

        y_ball = self.gui.input(frame_settings, x=170, y=190, w=40, h=20)

        self.gui.set_text_input(y_ball, "")

        self.gui.button(frame_settings, x=20, y=230, w=80, h=30, text=self.NNCTR.localization("game_get_button"),
                        bind=("<Button-1>", lambda event: self.get_settings(x_speed_ball, y_speed_ball,
                                                                            x_speed_g1, x_speed_g2, x_ball, y_ball)))

        self.gui.button(frame_settings, x=120, y=230, w=80, h=30, text=self.NNCTR.localization("game_set_button"),
                        bind=("<Button-1>", lambda event:
                        self.update_settings(x_speed_ball.get(),
                                             y_speed_ball.get(), x_speed_g1.get(),
                                             x_speed_g2.get(), r_ball.get(), k_ball.get(),
                                             x_ball.get(), y_ball.get())))
Exemplo n.º 2
0
class PingPong:
    __game = False
    i1 = 0
    i2 = 0
    __x = 0.0
    __y = 0.0
    __w = 0.0
    __h = 0.0
    __ball = None
    __gamer1 = None
    __gamer2 = None
    NNCTR = None
    gui = None
    game_gui = None
    win = None
    space = None
    inp = None
    outp = None
    network = None
    test = None
    pp_settings = None

    def __init__(self, x=0.0, y=0.0, w=480, h=768, ball=None, gamer1=None, gamer2=None, interval=0.02):
        self.__x = x
        self.__y = y
        self.__w = w
        self.__h = h
        self.interval = interval

        self.NNCTR = NNCTR()
        self.gui = GUI(self)
        self.game_gui = PingPongGUI()

        self.pp_settings = self.gui.new_window("Ping Pong Settings", 70.0, 100.0, 240, 350)
        self.win = self.gui.new_window("Ping Pong", x, y, w, h)

        self.pp_settings.iconbitmap('./favicon/game.ico')
        self.win.iconbitmap('./favicon/game.ico')

        self.space = self.game_gui.new_gamespace(self.win, x=x, y=y, w=w, h=h)

        self.__ball = self.create_ball(ball)
        self.__gamer1 = self.create_gamer(gamer1, id=1)
        self.__gamer2 = self.create_gamer(gamer2, id=2)

        self.win.bind('<Key>', self.key_press)
        self.win.bind('<KeyRelease>', self.key_release)

        frame = self.gui.frame(self.pp_settings, x=0, y=0, w=240, h=360, bind=None, bg="#77AA77")
        frame_control = self.gui.frame(frame, x=10, y=0, w=220, h=70, bind=None, bg="#669966")
        frame_settings = self.gui.frame(frame, x=10, y=80, w=220, h=270, bind=None, bg="#669966")

        self.gui.text(frame_control, x=60, y=5, w=100, h=20, text=self.NNCTR.localization("game_control_title"),
                      bg="#669966", font='Arial 14')

        self.gui.button(frame_control, x=20, y=30, w=80, h=30, text=self.NNCTR.localization("game_start_button"),
                        bind=("<Button-1>", lambda event:  self.start()))

        self.gui.button(frame_control, x=120, y=30, w=80, h=30, text=self.NNCTR.localization("game_stop_button"),
                        bind=("<Button-1>", lambda event:  self.stop()))

        self.gui.text(frame_settings, x=60, y=0, w=100, h=20, text=self.NNCTR.localization("game_settings_title"),
                      bg="#669966", font='Arial 14')

        self.gui.text(frame_settings, x=10, y=40, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_ball"),
                      bg="#669966")

        x_speed_ball = self.gui.input(frame_settings, x=120, y=40, w=40, h=20)

        self.gui.set_text_input(x_speed_ball, "")

        y_speed_ball = self.gui.input(frame_settings, x=170, y=40, w=40, h=20)

        self.gui.set_text_input(y_speed_ball, "")

        self.gui.text(frame_settings, x=10, y=70, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_of_p1"),
                      bg="#669966")

        x_speed_g1 = self.gui.input(frame_settings, x=170, y=70, w=40, h=20)

        self.gui.set_text_input(x_speed_g1, "")

        self.gui.text(frame_settings, x=10, y=100, w=110, h=20, text=self.NNCTR.localization("game_settings_speed_of_p2"),
                      bg="#669966")

        x_speed_g2 = self.gui.input(frame_settings, x=170, y=100, w=40, h=20)

        self.gui.set_text_input(x_speed_g2, "")

        self.gui.text(frame_settings, x=10, y=130, w=110, h=20, text=self.NNCTR.localization("game_settings_radius"),
                      bg="#669966")

        r_ball = self.gui.input(frame_settings, x=170, y=130, w=40, h=20)

        self.gui.set_text_input(r_ball, "10")

        self.gui.text(frame_settings, x=10, y=160, w=130, h=20, text=self.NNCTR.localization("game_settings_collision"),
                      bg="#669966")

        k_ball = self.gui.input(frame_settings, x=170, y=160, w=40, h=20)

        self.gui.set_text_input(k_ball, "1.2")

        self.gui.text(frame_settings, x=10, y=190, w=110, h=20, text=self.NNCTR.localization("game_settings_cords"),
                      bg="#669966")

        x_ball = self.gui.input(frame_settings, x=120, y=190, w=40, h=20)

        self.gui.set_text_input(x_ball, "")

        y_ball = self.gui.input(frame_settings, x=170, y=190, w=40, h=20)

        self.gui.set_text_input(y_ball, "")

        self.gui.button(frame_settings, x=20, y=230, w=80, h=30, text=self.NNCTR.localization("game_get_button"),
                        bind=("<Button-1>", lambda event: self.get_settings(x_speed_ball, y_speed_ball,
                                                                            x_speed_g1, x_speed_g2, x_ball, y_ball)))

        self.gui.button(frame_settings, x=120, y=230, w=80, h=30, text=self.NNCTR.localization("game_set_button"),
                        bind=("<Button-1>", lambda event:
                        self.update_settings(x_speed_ball.get(),
                                             y_speed_ball.get(), x_speed_g1.get(),
                                             x_speed_g2.get(), r_ball.get(), k_ball.get(),
                                             x_ball.get(), y_ball.get())))

    def __getattr__(self, item):
        if item == "x":
            return self.__x
        elif item == "y":
            return self.__y
        elif item == "w":
            return self.__w
        elif item == "h":
            return self.__h
        elif item == "g1":
            return self.__gamer1
        elif item == "g2":
            return self.__gamer2
        elif item == "g":
            return self.__game

    def update_settings(self, x_speed_ball, y_speed_ball, x_speed_g1, x_speed_g2, r_ball, k_ball, x_ball, y_ball):
        try:
            x_speed_ball = float(x_speed_ball)
            y_speed_ball = float(y_speed_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_speed_ball"))
            return None

        try:
            x_speed_g1 = float(x_speed_g1)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_speed_of_p1"))
            return None

        try:
            x_speed_g2 = float(x_speed_g2)
        except ValueError:
            self.gui.message("warning", "Warning!",self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_speed_of_p2"))
            return None

        try:
            r_ball = float(r_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_radius"))
            return None

        try:
            k_ball = float(k_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_collision"))
            return None

        try:
            x_ball = float(x_ball)
            y_ball = float(y_ball)
        except ValueError:
            self.gui.message("warning", "Warning!", self.NNCTR.localization("ii_error")
                             % self.NNCTR.localization("game_settings_cords"))
            return None

        self.__ball.set_speed(x_speed_ball, y_speed_ball)
        self.__gamer1.set_speed(x_speed_g1)
        self.__gamer2.set_speed(x_speed_g2)
        self.__ball.set_r(float(r_ball), float(k_ball))
        self.__ball.set_cords(x_ball, y_ball)
        self.__game = True
        self.paint()
        self.__game = False

    def get_settings(self, x_speed_ball, y_speed_ball, x_speed_g1, x_speed_g2, x_ball, y_ball):
        x, y = self.__ball.get_speed()
        self.gui.set_text_input(x_speed_ball, round(x, 2))
        self.gui.set_text_input(y_speed_ball, round(y, 2))
        x = self.__gamer1.get_speed()
        self.gui.set_text_input(x_speed_g1, round(x, 2))
        x = self.__gamer2.get_speed()
        self.gui.set_text_input(x_speed_g2, round(x, 2))
        x, y = self.__ball.get_cords()
        self.gui.set_text_input(x_ball, round(x, 2))
        self.gui.set_text_input(y_ball, round(y, 2))

    def create_ball(self, ball):
        pass
        return Ball(self, ball)

    def create_gamer(self, gamer, id):
        pass
        return Gamer(self, gamer, id=id)

    def game_loop(self):
        self.__game = True
        while self.__game:
            self.__ball.move()
            self.__gamer1.move()
            self.__gamer2.move()
            sleep(self.interval)
            self.space.update()

    def stop_all(self):
        self.__game = False
        self.NNCTR.stop_all()

    def stop(self):
        self.__game = False

    def start(self):
        if self.NNCTR.network is None:
            self.gui.message("warning", "Warning!", "Network is not loaded!")
            return None
        if not self.__game:
            self.game_loop()

    def paint(self):
        if self.__game:
            self.__ball.paint()
            self.__gamer1.paint()
            self.__gamer2.paint()

    def key_press(self, event):
        if event.keycode == (65 if os.name == "nt" else 38):
            self.__gamer1.mul_speed(-1)
        elif event.keycode == (68 if os.name == "nt" else 40):
            self.__gamer1.mul_speed(1)
        elif event.keycode == (37 if os.name == "nt" else 113):
            self.__gamer2.mul_speed(-1)
        elif event.keycode == (39 if os.name == "nt" else 114):
            self.__gamer2.mul_speed(1)
        elif event.keycode == (32 if os.name == "nt" else 65):
            self.stop() if self.__game else self.start()

    def key_release(self, event):
        if event.keycode == (65 if os.name == "nt" else 38) or event.keycode == (68 if os.name == "nt" else 40):
            self.__gamer1.mul_speed(0)
        elif event.keycode == (37 if os.name == "nt" else 113) or event.keycode == (39 if os.name == "nt" else 114):
            self.__gamer2.mul_speed(0)