Esempio n. 1
0
def create_world():
    global _player, _Bg, _Enemy1, timer, GameScore, font, _EBullet, _PBullet, _Life, client_sock, tcp_controller, E_NUM, _Enemy_Data
    global MyNumber, AnotherPlayer, unpacked_all_player_data, game_data, P_NUM, Player_Count, _LifeList, iScore, bgm
    global miScore
    _Bg = BackGround()
    E_NUM = 0
    bgm = load_music('Resource\BGM_B.mp3')
    bgm.set_volume(64)
    bgm.repeat_play()
    _player = Player1(_Bg)
    _LifeList = []
    miScore = CScore()
    iScore = 0
    AnotherPlayer = []
    _Enemy1 = []
    client_sock = GameData.client_socket
    MyNumber = GameData.player_number
    t1 = threading.Thread(target=recv_thread, args=(client_sock, ))
    GameScore = 0
    Player_Count = 0
    font = load_font('Resource\ENCR10B.TTF')

    Player2(0, 0, 0, 0, 0, 0, 0)
    Enemy1(0, 0, 0, 0, 0)
    Enemy2(0, 0, 0, 0, 0)
    EBullet(0, 0)
    PBullet(0, 0)
    t1.start()
Esempio n. 2
0
def create_world():
    global _DS, _player, _Bg, _Enemy1, timer, GameScore, font, _EBullet, _PBullet, _Life, client_socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((SERVER_IP_ADDR, SERVER_PORT))
    _player = Player1()
    _Bg = BackGround()
    _DS = DataStruct()
    _Enemy1 = []
    _Life = Life(_player.life)
    timer = Timer()
    GameScore = 0
    font = load_font('ENCR10B.TTF')
    _EBullet = EBullet.get_list()
    _PBullet = PBullet.get_list()
Esempio n. 3
0
def create_world():
    global _player, _Bg, _Enemy1, timer,GameScore, font, _EBullet, _PBullet, _Life,client_sock,tcp_controller,E_NUM, _Enemy_Data
    _Bg = BackGround()
    E_NUM =0
    _player = Player1(_Bg)
    _Enemy1 = []
    _Life = Life(_player.life)
    #timer = Timer()
    client_sock = GameData.client_socket
    #tcp_controller = TcpContoller()
    readystate = 0
    #client_sock = tcp_controller.tcp_client_init()
    t1 = threading.Thread(target=recv_thread, args=(client_sock,))
    t1.start()
    readystate = 0
    GameScore =0
    font = load_font('ENCR10B.TTF')
    _EBullet= EBullet.get_list()
    _PBullet = PBullet.get_list()
Esempio n. 4
0
 def __init__(self):
     global font
     font = load_font('..\ENCR10B.TTF')
     self.imagenum = State.C_CharSellect_State.select_witchs()
     global _Bg
     global _Enemy
     self.x, self.y = 400,400
     self.life = 3
     _Bg = BackGround()
     self.xdir = 0
     self.ydir =0
     self.state = self.DOWN_RUN
     self.bg = 0
     self.beforestate = 1
     self.sx = self.x - _Bg.window_left
     self.sy = self.y - _Bg.window_bottom
     self.iSpace = False
     if Player1.image == None:
         if self.imagenum ==1 :
             Player1.image = load_image('..\Player\Image_Player.png')
         elif self.imagenum == 2:
             Player1.image = load_image('..\Player\Image_Player2.png')
         else :
             Player1.image = load_image('..\Player\Image_Player3.png')
Esempio n. 5
0
class TcpController:
    global GAME_STATE, player_count, _Bg, Enemy_packed, E_NUM, ready_state, game_sys_main, Image_size, Canvas_size
    GAME_STATE = 0
    Image_size = [0, 3500, 0, 1800]
    Canvas_size = [0, 1200, 0, 900]

    player_count = 0
    PORT = 19000
    ready_state = 0
    _Bg = BackGround()
    IP = ''
    E_NUM = 0
    player_number = 0
    MAX_BIND = 5

    def tcp_server_init(self):
        global game_sys_main
        game_sys_main = GameSysMain()
        TcpController.server_socket = socket.socket(socket.AF_INET,
                                                    socket.SOCK_STREAM)
        TcpController.server_socket.bind((self.IP, self.PORT))
        TcpController.server_socket.listen(self.MAX_BIND)
        print('TCPServer Waiting for client on port %d' % self.PORT)
        print("=" * 50)
        print("[Thread version] 서버 초기화 완료")

    def accept_loof(self):
        global SEND_TIME, _Bg, player_number

        print("[정보] 접속 대기중...")
        print("=" * 50)
        player_number = 0

        while 1:
            client_socket, address = TcpController.server_socket.accept()

            print("I got a connection from ", address)
            client_thread = []
            client_thread.append(
                threading.Thread(target=TcpController.process_client,
                                 args=(client_socket, )))
            client_thread[-1].start()
            # game_sys_main.join_player(PlayerData)

    def process_client(client_socket):
        global PLAYER_NUM, state, GAME_STATE, E_Data, ready_state
        room_exit_ack = True
        room_number = 0
        game_sys_main.player_count += 1
        player_number = game_sys_main.empty_player_number()

        packed_player_number = data_struct.pack_integer(player_number)
        client_socket.send(packed_player_number)

        while room_exit_ack:
            print('room_number : ', room_number)
            room_number = TcpController.recv_lobby_state(
                client_socket, player_number)  #in Lobby
            if room_number == SOCKET_CLOSE:
                return
            print('player_number : ', player_number)
            room_exit_ack = TcpController.send_in_room_data(
                client_socket, room_number, player_number)  # in Room
        TcpController.send_in_game_data(client_socket, room_number,
                                        player_number)
        leader_board(client_socket, room_number)

    #Lobby
    def recv_lobby_state(client_socket, player_number):
        global game_sys_main
        in_lobby = True
        state = 0

        while in_lobby:
            packed_Selection = client_socket.recv(data_struct.integer_size)
            selection = data_struct.unpack_integer(packed_Selection)

            if selection == LOBBY_DATA:
                TcpController.send_lobby_data(client_socket)
            elif selection == JOIN:
                state = TcpController.send_room_data(client_socket,
                                                     player_number)
                is_none_room = (state == -1)
                if not is_none_room:
                    in_lobby = False
                    for i in range(3):
                        if game_sys_main.waitting_room_data[
                                state - 1]['player_number'][i] == -1:
                            game_sys_main.waitting_room_data[
                                state - 1]['player_number'][i] = player_number
                            break
            elif selection == CREATE:
                state = TcpController.recv_create_room(client_socket,
                                                       player_number)
                if state != -1:
                    in_lobby = False
                    for i in range(3):  # 추후 수정
                        if game_sys_main.waitting_room_data[
                                state - 1]['player_number'][i] == -1:
                            game_sys_main.waitting_room_data[
                                state - 1]['player_number'][i] = player_number
                            break
            elif selection == EXIT:
                state = TcpController.recv_exit_server(client_socket,
                                                       player_number)
                break

        return state

    def recv_create_room(client_socket, player_number):
        global game_sys_main

        packed_create_room_data = client_socket.recv(
            data_struct.room_data_size)  #수정
        create_room_data = data_struct.unpack_room_data(
            packed_create_room_data)
        rooms_count = game_sys_main.exist_room_count()
        if rooms_count < game_sys_main.maxroomcount:
            game_sys_main.rooms_data[rooms_count] = create_room_data
            game_sys_main.rooms_data[rooms_count][
                'room_number'] = game_sys_main.empty_room_number()
            game_sys_main.players_data[player_number][
                'player_name1'] = create_room_data['player_name1']
            room_is_not_full = game_sys_main.rooms_data[rooms_count][
                'room_number']
        else:
            room_is_not_full = -1
        packed_room_is_full = data_struct.pack_integer(room_is_not_full)
        client_socket.send(packed_room_is_full)
        return room_is_not_full

    def send_room_data(client_socket, player_number):
        global game_sys_main
        packed_request_data = client_socket.recv(
            data_struct.join_request_data_size)
        unpack_join_request_data = data_struct.unpack_join_request_data(
            packed_request_data)

        room_count = game_sys_main.exist_room_count()
        for i in range(room_count):
            if game_sys_main.rooms_data[i][
                    'room_number'] == unpack_join_request_data['room_number']:
                packed_room_data = data_struct.pack_room_data(
                    game_sys_main.rooms_data[i])
                client_socket.send(packed_room_data)
                if game_sys_main.rooms_data[i]['is_started']:
                    break
                full_plyaer = game_sys_main.rooms_data[i]['full_player']
                if (full_plyaer >= 2
                        and game_sys_main.rooms_data[i]['player_name2']
                        == 'default_name'):
                    game_sys_main.rooms_data[i][
                        'player_name2'] = unpack_join_request_data[
                            'player_name']
                    return game_sys_main.rooms_data[i]['room_number']
                elif (full_plyaer >= 3
                      and game_sys_main.rooms_data[i]['player_name3']
                      == 'default_name'):
                    game_sys_main.rooms_data[i][
                        'player_name3'] = unpack_join_request_data[
                            'player_name']
                    return game_sys_main.rooms_data[i]['room_number']
                elif (full_plyaer == 4
                      and game_sys_main.rooms_data[i]['player_name4']
                      == 'default_name'):
                    game_sys_main.rooms_data[i][
                        'player_name4'] = unpack_join_request_data[
                            'player_name']
                    return game_sys_main.rooms_data[i]['room_number']
        game_sys_main.players_data[player_number][
            'player_name1'] = unpack_join_request_data['player_name']
        return -1

    def send_lobby_data(socket):
        global game_sys_main

        room_count = game_sys_main.exist_room_count()
        packed_room_count = data_struct.pack_integer(room_count)
        socket.send(packed_room_count)
        for i in range(room_count):
            packed_room_data = data_struct.pack_room_data(
                game_sys_main.rooms_data[i])
            socket.send(packed_room_data)

    def recv_exit_server(socket, player_number):
        global game_sys_main

        game_sys_main.player_exit(player_number)

        socket.send(data_struct.pack_boolean(True))
        socket.close()
        return -2

    # ROOM
    def send_in_room_data(client_socket, room_number, player_number):
        global game_sys_main
        player_name = [
            'player_name1', 'player_name2', 'player_name3', 'player_name4'
        ]
        in_room = True
        data_reset = False

        while in_room:
            packed_in_room_data = client_socket.recv(
                data_struct.in_room_data_size)
            in_room_data = data_struct.unpack_in_room_data(packed_in_room_data)

            if in_room_data['is_exit'] == True:
                for i in range(3):
                    if data_reset and game_sys_main.waitting_room_data[
                            room_number - 1]['player_number'][i] != -1:
                        if game_sys_main.rooms_data[room_number -
                                                    1]['host_number'] == -1:
                            game_sys_main.rooms_data[room_number - 1][
                                'host_number'] = game_sys_main.waitting_room_data[
                                    room_number - 1]['player_number'][i]
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_number'][
                                i - 1] = game_sys_main.waitting_room_data[
                                    room_number - 1]['player_number'][i]
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_witch_select'][
                                i - 1] = game_sys_main.waitting_room_data[
                                    room_number - 1]['player_witch_select'][i]
                        game_sys_main.rooms_data[room_number - 1][player_name[
                            i -
                            1]] = game_sys_main.rooms_data[room_number -
                                                           1][player_name[i]]
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_number'][i] = -1
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_witch_select'][i] = 0
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_ready_state'][i] = False
                        game_sys_main.rooms_data[room_number - 1][player_name[
                            i -
                            1]] = game_sys_main.rooms_data[room_number -
                                                           1][player_name[i]]
                    elif game_sys_main.waitting_room_data[
                            room_number -
                            1]['player_number'][i] == player_number:
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_number'][i] = -1
                        if game_sys_main.rooms_data[
                                room_number -
                                1]['host_number'] == player_number:
                            game_sys_main.rooms_data[room_number -
                                                     1]['host_number'] = -1
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_witch_select'][i] = 0
                        game_sys_main.waitting_room_data[
                            room_number - 1]['player_ready_state'][i] = False
                        game_sys_main.rooms_data[room_number - 1][
                            player_name[i]] = 'default_name'
                        data_reset = True

            in_room_player_count = 0
            ready_count = 0
            for i in range(3):
                if game_sys_main.waitting_room_data[
                        room_number - 1]['player_number'][i] == player_number:
                    game_sys_main.waitting_room_data[
                        room_number - 1]['player_witch_select'][
                            i] = in_room_data['character_select']
                    game_sys_main.waitting_room_data[
                        room_number -
                        1]['player_ready_state'][i] = in_room_data['is_ready']
                    game_sys_main.waitting_room_data[
                        room_number -
                        1]['emotion'][i] = in_room_data['emotion']

            for i in range(3):
                if game_sys_main.waitting_room_data[
                        room_number - 1]['player_number'][i] != -1:
                    in_room_player_count += 1
                if game_sys_main.waitting_room_data[
                        room_number - 1]['player_ready_state'][i]:
                    ready_count += 1

            game_sys_main.waitting_room_data[
                room_number - 1]['player_count'] = in_room_player_count

            if in_room_player_count <= 0:
                game_sys_main.rooms_data[room_number - 1]['room_number'] = -1
                game_sys_main.rooms_data[room_number -
                                         1]['room_name'] = 'default_name'
                game_sys_main.rooms_data[room_number - 1]['full_player'] = 0

            if data_reset:
                packed_exit_ack = data_struct.pack_boolean(True)
                client_socket.send(packed_exit_ack)
                return True
            else:
                packed_in_room_data_server = data_struct.pack_in_room_data_server(
                    game_sys_main.waitting_room_data[room_number - 1],
                    GAME_STATE)
                client_socket.send(packed_in_room_data_server)

            if in_room_player_count == ready_count:
                in_room = False
                game_sys_main.rooms_data[room_number - 1]['is_started'] = True
        return False

    def game_thread(client_socket, room_number):
        global main_time
        room_player = game_sys_main.waitting_room_data[room_number -
                                                       1]['player_number']
        room_player_data = game_sys_main.all_player_data[room_number]
        while 1:
            for i in range(3):
                if (room_player[i] == -1):
                    break
                to_all_event[room_player[i]].wait()
                to_all_event[room_player[i]].clear()
            frame_time = time.clock() - main_time
            main_time += frame_time
            if timer.update(frame_time) == True:  # 적 만드는 부분
                EnemyDirNum = random.randint(0, 8)
                if EnemyDirNum <= 3:
                    newEnemy = Enemy1(EnemyDirNum)
                    dis = []
                    for i in range(3):
                        if (room_player[i] == -1):
                            break
                        dis.append(
                            newEnemy.get_distance(
                                room_player_data['player_x'][i],
                                room_player_data['player_y'][i]))
                    near = dis.index(min(dis))
                    newEnemy.set_dir(room_player_data['player_x'][near],
                                     room_player_data['player_y'][near])
                    _EnemyList.append(newEnemy)

                if EnemyDirNum >= 4:
                    newEnemy = Enemy2(EnemyDirNum)
                    dis = []
                    for i in range(3):
                        if (room_player[i] == -1):
                            break
                        dis.append(
                            newEnemy.get_distance(
                                room_player_data['player_x'][i],
                                room_player_data['player_y'][i]))
                    near = dis.index(min(dis))
                    newEnemy.set_dir(room_player_data['player_x'][near],
                                     room_player_data['player_y'][near])
                    _EnemyList.append(newEnemy)
            for i in range(3):
                if (room_player[i] == -1):
                    break
                if room_player_data['player_isShoot'][i] == True:
                    newBullet = PBullet(room_player_data['player_x'][i],
                                        room_player_data['player_y'][i],
                                        room_player_data['player_dir'][i])
                    _Bullet.append(newBullet)

            for enemy in _EnemyList:
                if enemy.ADD_Bullet() == True:
                    newBullet = EBullet(enemy.x, enemy.y)
                    dis = []
                    for i in range(3):
                        if (room_player[i] == -1):
                            break
                        dis.append(
                            newBullet.get_distance(
                                room_player_data['player_x'][i],
                                room_player_data['player_y'][i]))
                    near = dis.index(min(dis))
                    newBullet.set_dir(room_player_data['player_x'][near],
                                      room_player_data['player_y'][near])
                    _Bullet.append(newBullet)

            for enemy in _EnemyList:
                enemy.update(frame_time)
                for i in range(3):
                    if (room_player[i] == -1):
                        break
                    if collide2(room_player_data['player_x'][i],
                                room_player_data['player_y'][i], enemy.x,
                                enemy.y):
                        enemy.alive = 0
                        room_player_data['player_life'][i] -= 1

            player = 0
            for bullets in _Bullet:
                bullets.update(frame_time)
                if bullets.shooter == player:
                    for enemys in _EnemyList:
                        if collide(bullets, enemys):
                            bullets.alive = 0
                            enemys.alive = 0
                            room_player_data['Score'] += 300

                else:
                    for i in range(3):
                        if (room_player[i] == -1):
                            break
                        if collide2(room_player_data['player_x'][i],
                                    room_player_data['player_y'][i], bullets.x,
                                    bullets.y):
                            bullets.alive = 0
                            room_player_data['player_life'][i] -= 1

            for enemy in _EnemyList:
                if enemy.alive == 0:
                    _EnemyList.remove(enemy)
            for pbullets in _Bullet:
                if pbullets.alive == 0:
                    _Bullet.remove(pbullets)
            for ebullets in _Bullet:
                if ebullets.alive == 0:
                    _Bullet.remove(ebullets)

            for i in range(3):
                if (room_player[i] == -1):
                    break
                to_one_event[room_player[i]].set()
            gc.collect()

    def send_in_game_data(client_socket, room_number, player_number):
        global main_time, PLAYER_NUM, state, GAME_STATE, timer, E_Data, ready_state, E_NUM, after_time, before_time
        global player_count, Image_size, Canvas_size
        room_player = game_sys_main.waitting_room_data[room_number -
                                                       1]['player_number']
        game_sys_main.all_player_data[room_number]['player_number'][
            player_number] = player_number
        current_time = time.clock()
        E_NUM = 0
        timer = Timer()
        k = 0
        main_time = time.clock()

        if player_number == game_sys_main.rooms_data[room_number -
                                                     1]['host_number']:
            t1 = threading.Thread(target=TcpController.game_thread,
                                  args=(client_socket, room_number))
            t1.start()
        gc.disable()

        while 1:  # When Game Over
            if current_time + 0.033 < time.clock():
                before_time = time.clock()
                current_time = time.clock()
                P_Data = client_socket.recv(struct.calcsize('=ffffiBf'))
                _Player_Packed = data_struct.unpack_player_data(P_Data)
                game_sys_main.all_player_data[room_number]['Score'] += 1
                for i in range(3):  # 임시
                    if game_sys_main.all_player_data[room_number][
                            'player_number'][i] == player_number:
                        game_sys_main.all_player_data[room_number][
                            'player_count'] = game_sys_main.waitting_room_data[
                                room_number - 1]['player_count']
                        game_sys_main.all_player_data[room_number]['player_x'][
                            i] = _Player_Packed[0]
                        game_sys_main.all_player_data[room_number]['player_y'][
                            i] = _Player_Packed[1]
                        game_sys_main.all_player_data[room_number][
                            'player_sx'][i] = _Player_Packed[2]
                        game_sys_main.all_player_data[room_number][
                            'player_sy'][i] = _Player_Packed[3]
                        game_sys_main.all_player_data[room_number][
                            'player_life'][i] = _Player_Packed[4]
                        game_sys_main.all_player_data[room_number][
                            'player_isShoot'][i] = _Player_Packed[5]
                        game_sys_main.all_player_data[room_number][
                            'player_dir'][i] = _Player_Packed[6]
                to_all_event[player_number].set()
                gc.collect()
                to_one_event[player_number].wait()
                to_one_event[player_number].clear()

                dead_count = 0

                for i in range(3):
                    if (room_player[i] == -1):
                        break
                    if game_sys_main.all_player_data[room_number][
                            'player_life'][i] <= 0:
                        dead_count += 1
                if dead_count == game_sys_main.waitting_room_data[
                        room_number - 1]['player_count']:
                    game_sys_main.is_game_over = True

                packed_all_player_data = data_struct.pack_all_player_data(
                    game_sys_main.all_player_data[room_number])
                client_socket.send(packed_all_player_data)

                client_socket.send(struct.pack('?',
                                               game_sys_main.is_game_over))
                if (game_sys_main.is_game_over):
                    break
                client_socket.send(
                    struct.pack('=ii', len(_EnemyList), len(_Bullet)))
                Enemys_IN_Window = []
                Bullets_IN_Window = []
                for enemy in _EnemyList:
                    if k == len(_EnemyList):
                        k = 0
                        k += 1
                    if _Player_Packed[0] < 600:
                        if Canvas_size[1] > enemy.x:
                            if Image_size[0] < enemy.x:
                                if _Player_Packed[1] + 600 > enemy.y:
                                    if _Player_Packed[1] - 600 < enemy.y:
                                        Enemy_packed = data_struct.pack_enemy_data(
                                            enemy, k)
                                        Enemys_IN_Window.append(Enemy_packed)
                    elif _Player_Packed[0] > 2900:
                        if Image_size[1] > enemy.x:
                            if Image_size[1] - Canvas_size[1] + 200 < enemy.x:
                                if _Player_Packed[1] + 600 > enemy.y:
                                    if _Player_Packed[1] - 600 < enemy.y:
                                        Enemy_packed = data_struct.pack_enemy_data(
                                            enemy, k)
                                        Enemys_IN_Window.append(Enemy_packed)
                    if _Player_Packed[1] < 450:
                        if _Player_Packed[0] + 800 > enemy.x:
                            if _Player_Packed[0] - 800 < enemy.x:
                                if Canvas_size[2] < enemy.y:
                                    if Canvas_size[3] > enemy.y:
                                        Enemy_packed = data_struct.pack_enemy_data(
                                            enemy, k)
                                        Enemys_IN_Window.append(Enemy_packed)
                    elif _Player_Packed[1] > 1350:
                        if _Player_Packed[0] + 800 > enemy.x:
                            if _Player_Packed[0] - 800 < enemy.x:
                                if Image_size[3] - Canvas_size[3] < enemy.y:
                                    if Image_size[3] > enemy.y:
                                        Enemy_packed = data_struct.pack_enemy_data(
                                            enemy, k)
                                        Enemys_IN_Window.append(Enemy_packed)
                    else:
                        if _Player_Packed[0] + 800 > enemy.x:
                            if _Player_Packed[0] - 800 < enemy.x:
                                if _Player_Packed[1] + 600 > enemy.y:
                                    if _Player_Packed[1] - 600 < enemy.y:
                                        Enemy_packed = data_struct.pack_enemy_data(
                                            enemy, k)
                                        Enemys_IN_Window.append(Enemy_packed)

                for bullets in _Bullet:
                    if _Player_Packed[0] < 600:
                        if Canvas_size[1] > bullets.x:
                            if Image_size[0] < bullets.x:
                                if _Player_Packed[1] + 600 > bullets.y:
                                    if _Player_Packed[1] - 600 < bullets.y:
                                        bullet_packed = data_struct.pack_bullet_data(
                                            bullets)
                                        Bullets_IN_Window.append(bullet_packed)
                    elif _Player_Packed[0] > 2900:
                        if Image_size[1] > bullets.x:
                            if Image_size[1] - Canvas_size[1] < bullets.x:
                                if _Player_Packed[1] + 600 > bullets.y:
                                    if _Player_Packed[1] - 600 < bullets.y:
                                        bullet_packed = data_struct.pack_bullet_data(
                                            bullets)
                                        Bullets_IN_Window.append(bullet_packed)
                    # 플레이어 y로 계산
                    if _Player_Packed[1] < 450:
                        if _Player_Packed[0] + 800 > bullets.x:
                            if _Player_Packed[0] - 800 < bullets.x:
                                if Canvas_size[2] < bullets.y:
                                    if Canvas_size[3] > bullets.y:
                                        bullet_packed = data_struct.pack_bullet_data(
                                            bullets)
                                        Bullets_IN_Window.append(bullet_packed)
                    elif _Player_Packed[1] > 1350:
                        if _Player_Packed[0] + 800 > bullets.x:
                            if _Player_Packed[0] - 800 < bullets.x:
                                if Image_size[3] - Canvas_size[3] < bullets.y:
                                    if Image_size[3] > bullets.y:
                                        bullet_packed = data_struct.pack_bullet_data(
                                            bullets)
                                        Bullets_IN_Window.append(bullet_packed)
                    else:
                        if _Player_Packed[0] + 800 > bullets.x:
                            if _Player_Packed[0] - 800 < bullets.x:
                                if _Player_Packed[1] + 600 > bullets.y:
                                    if _Player_Packed[1] - 600 < bullets.y:
                                        bullet_packed = data_struct.pack_bullet_data(
                                            bullets)
                                        Bullets_IN_Window.append(bullet_packed)

                client_socket.send(
                    struct.pack('=ii', len(Enemys_IN_Window),
                                len(Bullets_IN_Window)))

                for Enemy_packed in Enemys_IN_Window:
                    client_socket.send(Enemy_packed)

                for bullet_packed in Bullets_IN_Window:
                    client_socket.send(bullet_packed)

        after_time = time.clock()
        gc.enable()
Esempio n. 6
0
 def __init__(self, playerlife):
     global _Bg
     _Bg = BackGround()
     self.Now = playerlife
     if Life.image == None:
         Life.image = load_image('..\Life\Image_Life.png')
Esempio n. 7
0
class TcpController:
    global GAME_STATE, player_count, timer, _Bg, Enemy_packed, E_NUM, ready_state, game_sys_main
    GAME_STATE = 0
    player_count = 0
    PORT = 19000
    timer = Timer()
    ready_state = 0
    _Bg = BackGround()
    IP = ''
    E_NUM = 0
    player_number = 0
    MAX_BIND = 5

    def tcp_server_init(self):
        global game_sys_main
        game_sys_main = GameSysMain()
        TcpController.server_socket = socket.socket(socket.AF_INET,
                                                    socket.SOCK_STREAM)
        TcpController.server_socket.bind((self.IP, self.PORT))
        TcpController.server_socket.listen(self.MAX_BIND)
        print('TCPServer Waiting for client on port %d' % self.PORT)
        print("=" * 50)
        print("[Thread version] 서버 초기화 완료")

    '''
    클라이언트의 접속을 accept()합니다.
    accept()되면 process_client쓰레드에 client소켓을 전달해주고 client소켓을 삭제합니다.
    '''

    def accept_loof(self):
        global SEND_TIME, _Bg, player_number

        print("[정보] 접속 대기중...")
        print("=" * 50)
        player_number = 0
        '''
        Waitting Room
        '''

        while 1:
            client_socket, address = TcpController.server_socket.accept()

            print("I got a connection from ", address)
            client_thread = []
            client_thread.append(
                threading.Thread(target=TcpController.process_client,
                                 args=(client_socket, )))
            client_thread[-1].start()
            # game_sys_main.join_player(PlayerData)

    '''
    클라이언트와 통신하는 스레드입니다.
    로직을 이안에 쓰는것은 지양합니다.
    함수를 호출하여 수정을 최소화 하세요.
    '''

    def process_client(client_socket):
        global PLAYER_NUM, state, GAME_STATE, E_Data, ready_state
        room_number = 0
        game_sys_main.player_count += 1
        player_number = game_sys_main.empty_player_number()

        packed_player_number = data_struct.pack_integer(player_number)
        client_socket.send(packed_player_number)
        istime = time.clock()
        room_number = TcpController.recv_lobby_state(client_socket,
                                                     player_number)  #in Lobby
        print('room_number : ', room_number)
        TcpController.send_in_room_data(client_socket, room_number,
                                        player_number)  # in Room

    #Lobby
    '''
    selection 0 = LOBBY_DATA, selection 1 = ROOM_DATA, selection 2 = JOIN, selection 3 = CREATE
    '''

    def recv_lobby_state(client_socket, player_number):
        global game_sys_main
        print('is Word?')
        in_lobby = True
        room_number = 0
        while in_lobby:
            packed_Selection = client_socket.recv(data_struct.integer_size)
            selection = data_struct.unpack_integer(packed_Selection)

            if selection == 0:
                TcpController.send_lobby_data(client_socket)
            elif selection == 2:
                print('is Workkkkkkkkk?')

                room_number = TcpController.send_room_data(
                    client_socket, player_number)
                if room_number != -1:
                    in_lobby = False
                    for i in range(3):
                        if game_sys_main.waitting_room_data[room_number][
                                'player_number'][i] == -1:
                            game_sys_main.waitting_room_data[room_number][
                                'player_number'][i] = player_number
                            break
            elif selection == 3:
                room_number = TcpController.recv_create_room(
                    client_socket, player_number)
                if room_number != -1:
                    in_lobby = False
                    for i in range(3):  # 추후 수정
                        if game_sys_main.waitting_room_data[room_number][
                                'player_number'][i] == -1:
                            game_sys_main.waitting_room_data[room_number][
                                'player_number'][i] = player_number
                            break
                    print(room_number)

        return room_number

    def recv_create_room(client_socket, player_number):
        global game_sys_main

        packed_create_room_data = client_socket.recv(
            data_struct.room_data_size)  #수정
        create_room_data = data_struct.unpack_room_data(
            packed_create_room_data)
        rooms_count = game_sys_main.exist_room_count()
        if rooms_count < game_sys_main.maxroomcount:
            game_sys_main.rooms_data[rooms_count] = create_room_data
            game_sys_main.rooms_data[rooms_count][
                'room_number'] = game_sys_main.empty_room_number()
            game_sys_main.players_data[player_number][
                'player_name'] = create_room_data['player_name1']
            room_is_not_full = game_sys_main.rooms_data[rooms_count][
                'room_number']
        else:
            room_is_not_full = -1
        packed_room_is_full = data_struct.pack_integer(room_is_not_full)
        client_socket.send(packed_room_is_full)
        return room_is_not_full

    def send_room_data(client_socket, player_number):
        global game_sys_main
        print('is Workkkk?')
        packed_request_data = client_socket.recv(
            data_struct.join_request_data_size)
        unpack_join_request_data = data_struct.unpack_join_request_data(
            packed_request_data)

        room_count = game_sys_main.exist_room_count()
        for i in range(room_count):
            if game_sys_main.rooms_data[i][
                    'room_number'] == unpack_join_request_data['room_number']:
                packed_room_data = data_struct.pack_room_data(
                    game_sys_main.rooms_data[i])
                client_socket.send(packed_room_data)
                full_plyaer = game_sys_main.rooms_data[i]['full_player']
                if (full_plyaer >= 2
                        and game_sys_main.rooms_data[i]['player_name2']
                        == 'default_name'):
                    game_sys_main.rooms_data[i][
                        'player_name2'] = unpack_join_request_data[
                            'player_name']
                    return game_sys_main.rooms_data[i]['room_number']
                elif (full_plyaer >= 3
                      and game_sys_main.rooms_data[i]['player_name3']
                      == 'default_name'):
                    game_sys_main.rooms_data[i][
                        'player_name3'] = unpack_join_request_data[
                            'player_name']
                    return game_sys_main.rooms_data[i]['room_number']
                elif (full_plyaer == 4
                      and game_sys_main.rooms_data[i]['player_name4']
                      == 'default_name'):
                    game_sys_main.rooms_data[i][
                        'player_name4'] = unpack_join_request_data[
                            'player_name']
                    return game_sys_main.rooms_data[i]['room_number']
        game_sys_main.players_data[player_number][
            'player_name'] = unpack_join_request_data['player_name']
        return -1

    def send_lobby_data(socket):
        global game_sys_main

        room_count = game_sys_main.exist_room_count()
        packed_room_count = data_struct.pack_integer(room_count)
        socket.send(packed_room_count)
        for i in range(room_count):
            packed_room_data = data_struct.pack_room_data(
                game_sys_main.rooms_data[i])
            socket.send(packed_room_data)

    # ROOM
    '''
    state 0 = join, 1 = select, 2 = ready, 3 = out, 4 = in game
    '''

    def send_in_room_data(client_socket, room_number, player_number):
        global game_sys_main
        in_room = True
        while in_room:
            packed_in_room_data = client_socket.recv(Pack.in_room_data_size)
            print('Send')
            print(packed_in_room_data)
            in_room_data = data_struct.unpack_in_room_data(packed_in_room_data)

            if in_room_data['is_exit'] == True:
                # 소켓 해제
                pass

            player_count = 0
            ready_count = 0
            # 방내 유저 레디상태 업데이트
            # 방내 유저 캐릭터선택 정보 업데이트
            for i in range(3):  # 임시
                if game_sys_main.waitting_room_data[room_number][
                        'player_number'][i] == player_number:
                    game_sys_main.waitting_room_data[room_number][
                        'player_witch_select'][i] = in_room_data[
                            'character_select']
                    game_sys_main.waitting_room_data[room_number][
                        'player_ready_state'][i] = in_room_data['is_ready']

            for i in range(3):  # 임시
                if game_sys_main.waitting_room_data[room_number][
                        'player_number'][i] != -1:
                    player_count += 1
                if game_sys_main.waitting_room_data[room_number][
                        'player_ready_state'][i]:
                    ready_count += 1

            game_sys_main.waitting_room_data[room_number][
                'player_count'] = player_count
            print(game_sys_main.waitting_room_data[room_number])
            packed_in_room_data_server = Pack.pack_in_room_data_server(
                game_sys_main.waitting_room_data[room_number], GAME_STATE)
            client_socket.send(packed_in_room_data_server)

            if player_count == ready_count:
                in_room = False

    def recv_waitting_room_thread(client_socket, player_number):
        global game_sys_main

        while 1:
            if (game_sys_main.is_start):
                print(player_number, "정상적으로 recv쓰래드 종료")
                return
            else:
                try:
                    TcpController.recv_waitting_room_data(
                        client_socket, player_number)
                except socket.error:
                    print(player_number, game_sys_main.is_start)
                    print("비정상적 종료로 recv쓰래드가 종료됨")
                    return

    def recv_thread(client_socket):
        global GAME_STATE
        print('GAME_STATE:', GAME_STATE)

        while GAME_STATE == 0:
            print('GAME_STATE in Thread', GAME_STATE)
            recv_packed_data = client_socket.recv(struct.calcsize('=BBI'))
            recv_data = struct.unpack('=BBI', recv_packed_data)
            temp = 'player' + str(recv_data[0]) + '_witch_selcet'
            temp2 = 'player' + str(recv_data[0]) + '_ready_state'
            game_sys_main.waitting_room_data[temp] = recv_data[1]
            game_sys_main.waitting_room_data[temp2] = recv_data[2]
            print('recv data :', recv_data)
            print('game_sys_main.waitting_room_data',
                  game_sys_main.waitting_room_data)
            numofready = game_sys_main.waitting_room_data[
                'player1_ready_state'] + game_sys_main.waitting_room_data[
                    'player2_ready_state'] + game_sys_main.waitting_room_data[
                        'player3_ready_state']
            player_count = game_sys_main.waitting_room_data['player_count']
            if player_count <= numofready:
                GAME_STATE = 1

            if (game_sys_main.waitting_room_data['ready_state'] >>
                (player_number - 1) & 0b0001 == 1):
                temp = ''
            else:
                temp = '해제'
            print(player_number, '번 Player가 준비를', temp, '하였습니다.')

        if (game_sys_main.waitting_room_data['ready_state'] == 0b0011):
            print(player_number, '번 Player가 게임을 시작하였습니다.')
            game_sys_main.is_start = True

    def send_in_game_data(client_socket, room_number, player_number):
        global PLAYER_NUM, state, GAME_STATE, E_Data, ready_state, E_NUM
        current_time = time.clock()
        E_NUM = 0
        while 1:  # When Game Over
            P_Data = client_socket.recv(struct.calcsize('=fffff'))

            _Player_Packed = data_struct.unpack_player_data(P_Data)

            frame_time = time.clock() - current_time
            current_time += frame_time
            _Bg.update(_Player_Packed[0], _Player_Packed[1])

            SEND_DATA = b'EMPTY'
            if timer.update(frame_time) == True:
                EnemyDirNum = random.randint(0, 8)
                if EnemyDirNum <= 3:
                    newEnemy = Enemy1(_Player_Packed[2], _Player_Packed[3],
                                      EnemyDirNum, _Bg.window_left,
                                      _Bg.window_bottom)
                    _Enemy.append(newEnemy)
                    SEND_DATA = data_struct.pack_enemy_data(newEnemy)
                    client_socket.send(SEND_DATA)

                if EnemyDirNum >= 4:
                    newEnemy = Enemy2(_Player_Packed[2], _Player_Packed[3],
                                      EnemyDirNum, _Bg.window_left,
                                      _Bg.window_bottom)
                    _Enemy.append(newEnemy)
                    SEND_DATA = data_struct.pack_enemy_data(newEnemy)

                for Enemys in _Enemy:
                    Enemys.A
            client_socket.send(SEND_DATA)