Пример #1
0
def choose_server(assets_obj: assets.Assets, ui_obj: assets.UserInterface,
                  networking_obj: networking.Networking):
    """Choose server screen input handler"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("exit game")
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                if ui_obj.choice > 0:
                    ui_obj.choice -= 1
                return
            if event.key == pygame.K_DOWN:
                if ui_obj.choice < len(networking_obj.ip_result['found']):
                    ui_obj.choice += 1
                return
            if event.key == pygame.K_RETURN:
                print(
                    f"Connect to {networking_obj.ip_result['found'][ui_obj.choice]}"
                )
                networking_obj.connect_to_sever(
                    networking_obj.ip_result['found'][ui_obj.choice])
                return
            if event.key == pygame.K_F5:
                scan_thr = threading.Thread(
                    target=networking_obj.scan_for_server, args=[ui_obj])
                scan_thr.start()
                return
            if event.key == pygame.K_ESCAPE:
                networking_obj.network_disconnect(assets_obj, ui_obj)
                return
Пример #2
0
def client(assets_obj: assets.Assets, networking_obj: networking.Networking,
           ui_obj: assets.UserInterface):
    """Client mode input handler"""
    assets_obj.opponent.previous_speed = assets_obj.opponent.speed
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                assets_obj.opponent.speed -= assets_obj.opponent.control_speed
            if event.key == pygame.K_s:
                assets_obj.opponent.speed += assets_obj.opponent.control_speed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                assets_obj.opponent.speed += assets_obj.opponent.control_speed
            if event.key == pygame.K_s:
                assets_obj.opponent.speed -= assets_obj.opponent.control_speed
            if event.key == pygame.K_ESCAPE:
                networking_obj.network_disconnect(assets_obj, ui_obj)
                print("end tcp client")

        if assets_obj.opponent.speed != assets_obj.opponent.previous_speed:
            networking_obj.send_controls(assets_obj, ui_obj)