Esempio n. 1
0
    def __init__(self, **kwargs):
        super(Client, self).__init__()
        self.listener = pyglet.media.listener

        # create window
        try:
            # try and create a window with multisampling (antialiasing)
            config = Config(sample_buffers=1, samples=4,
                depth_size=16, double_buffer=True)
            self.window = pyglet.window.Window(resizable=True, config=config,
                width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
        except pyglet.window.NoSuchConfigException:
            # fall back to no multisampling for old hardware
            print "window configuration failed"
            self.window = pyglet.window.Window(resizable=True,
                width=SCREEN_WIDTH, height=SCREEN_HEIGHT)

        # register window events
        self.window.set_exclusive_mouse(True)
        self.on_draw = self.window.event(self.on_draw)
        self.on_key_press = self.window.event(self.on_key_press)
        self.on_key_release = self.window.event(self.on_key_release)
        self.on_mouse_press = self.window.event(self.on_mouse_press)
        self.on_mouse_release = self.window.event(self.on_mouse_release)
        self.on_resize = self.window.event(self.on_resize)
        self.keyboard = pyglet.window.key.KeyStateHandler()
        self.window.push_handlers(self.keyboard)

        # init graphics
        setup_opengl()

        # init misc?
        if self.hud is None:
            self.hud = Hud()
        #if self.sound is None:
        self.sound = Sound()

        # self.fps = FPSDisplay()
        # self.room.elements.append(self.fps)
        # self.fps_display = pyglet.clock.ClockDisplay() # see programming guide pg 48

        # network must be last to avoid race condition with the initialization callback
        self.network = NetClient(callbacks={
            "player_init": self.init_callback,
            "packed_room": self.packed_room_callback,
            "sound_play": self.sound_play_callback,
            "explosion": self.explosion_callback,
            "laser": self.laser_callback,
            "puff" : self.puff_callback,
            "game_event": self.game_event
        }, **kwargs)

        # start game
        pyglet.clock.set_fps_limit(60)
        pyglet.clock.schedule(self.update)
        pyglet.app.run()
Esempio n. 2
0
    def __init__(self, **kwargs):
        super(Client, self).__init__()
        self.listener = pyglet.media.listener

        # create window
        try:
            # try and create a window with multisampling (antialiasing)
            config = Config(sample_buffers=1,
                            samples=4,
                            depth_size=16,
                            double_buffer=True)
            self.window = pyglet.window.Window(resizable=True,
                                               config=config,
                                               width=SCREEN_WIDTH,
                                               height=SCREEN_HEIGHT)
        except pyglet.window.NoSuchConfigException:
            # fall back to no multisampling for old hardware
            print "window configuration failed"
            self.window = pyglet.window.Window(resizable=True,
                                               width=SCREEN_WIDTH,
                                               height=SCREEN_HEIGHT)

        # register window events
        self.window.set_exclusive_mouse(True)
        self.on_draw = self.window.event(self.on_draw)
        self.on_key_press = self.window.event(self.on_key_press)
        self.on_key_release = self.window.event(self.on_key_release)
        self.on_mouse_press = self.window.event(self.on_mouse_press)
        self.on_mouse_release = self.window.event(self.on_mouse_release)
        self.on_resize = self.window.event(self.on_resize)
        self.keyboard = pyglet.window.key.KeyStateHandler()
        self.window.push_handlers(self.keyboard)

        # init graphics
        setup_opengl()

        # init misc?
        if self.hud is None:
            self.hud = Hud()
        #if self.sound is None:
        self.sound = Sound()

        # self.fps = FPSDisplay()
        # self.room.elements.append(self.fps)
        # self.fps_display = pyglet.clock.ClockDisplay() # see programming guide pg 48

        # network must be last to avoid race condition with the initialization callback
        self.network = NetClient(callbacks={
            "player_init": self.init_callback,
            "packed_room": self.packed_room_callback,
            "sound_play": self.sound_play_callback,
            "explosion": self.explosion_callback,
            "laser": self.laser_callback,
            "puff": self.puff_callback,
            "game_event": self.game_event
        },
                                 **kwargs)

        # start game
        pyglet.clock.set_fps_limit(60)
        pyglet.clock.schedule(self.update)
        pyglet.app.run()
Esempio n. 3
0
class Client(Manager):
    camera = None
    hud = None
    graphics_cache = {}
    initialized = False
    game_state_messages = []
    player_state_alive = False
    player_state_hit = 0
    hits = []

    def __init__(self, **kwargs):
        super(Client, self).__init__()
        self.listener = pyglet.media.listener

        # create window
        try:
            # try and create a window with multisampling (antialiasing)
            config = Config(sample_buffers=1,
                            samples=4,
                            depth_size=16,
                            double_buffer=True)
            self.window = pyglet.window.Window(resizable=True,
                                               config=config,
                                               width=SCREEN_WIDTH,
                                               height=SCREEN_HEIGHT)
        except pyglet.window.NoSuchConfigException:
            # fall back to no multisampling for old hardware
            print "window configuration failed"
            self.window = pyglet.window.Window(resizable=True,
                                               width=SCREEN_WIDTH,
                                               height=SCREEN_HEIGHT)

        # register window events
        self.window.set_exclusive_mouse(True)
        self.on_draw = self.window.event(self.on_draw)
        self.on_key_press = self.window.event(self.on_key_press)
        self.on_key_release = self.window.event(self.on_key_release)
        self.on_mouse_press = self.window.event(self.on_mouse_press)
        self.on_mouse_release = self.window.event(self.on_mouse_release)
        self.on_resize = self.window.event(self.on_resize)
        self.keyboard = pyglet.window.key.KeyStateHandler()
        self.window.push_handlers(self.keyboard)

        # init graphics
        setup_opengl()

        # init misc?
        if self.hud is None:
            self.hud = Hud()
        #if self.sound is None:
        self.sound = Sound()

        # self.fps = FPSDisplay()
        # self.room.elements.append(self.fps)
        # self.fps_display = pyglet.clock.ClockDisplay() # see programming guide pg 48

        # network must be last to avoid race condition with the initialization callback
        self.network = NetClient(callbacks={
            "player_init": self.init_callback,
            "packed_room": self.packed_room_callback,
            "sound_play": self.sound_play_callback,
            "explosion": self.explosion_callback,
            "laser": self.laser_callback,
            "puff": self.puff_callback,
            "game_event": self.game_event
        },
                                 **kwargs)

        # start game
        pyglet.clock.set_fps_limit(60)
        pyglet.clock.schedule(self.update)
        pyglet.app.run()

    # callbacks
    def init_callback(self, tag, data):
        cache = data["persistent_cache"]
        player_room_id = data["player_room_id"]

        # if room is not default_room, initialize it here

        # first, switch this client's player object to a camera object
        cache[player_room_id] = (Camera.__name__, cache[player_room_id][1])

        # unpack persistent cache and set camera
        self.room.unpack_add(data["persistent_cache"])
        self.camera = self.room.instances[player_room_id]
        self.camera.on_mouse_motion = self.window.event(
            self.camera.on_mouse_motion)
        self.camera.on_mouse_drag = self.window.event(
            self.camera.on_mouse_drag)

        self.last_pposition = Vector3(0.0, 0.0, 0.0)

        # start game
        self.initialized = True

    def packed_room_callback(self, tag, data):
        newRoom = copy.copy(self.room)
        newRoom.elements = copy.copy(self.room.elements)
        newRoom.classes = copy.copy(self.room.classes)
        newRoom.instances = copy.copy(self.room.instances)
        newRoom.players = copy.copy(self.room.players)
        newRoom.balistics = copy.copy(self.room.balistics)
        newRoom.unpack(data)
        self.room = newRoom

    def sound_play_callback(self, tag, data):
        sound_source = data["sound_source"]
        sound_type = data["sound_type"]
        self.sound.play_sound(sound_type, sound_source)

    def explosion_callback(self, tag, data):
        data["sound_source"] = data["pos"]
        data["sound_type"] = "explosion"
        self.sound_play_callback("sound", data)
        self.room.explosions.new_explosion(data["pos"])

    def laser_callback(self, tag, data):
        # print "laser_callback: %s" % data

        if "hits" in data:
            if data["hits"] is not None and data["hits"] != self.camera.id:
                self.hits.append(data["hits"])
            del data["hits"]

        data["shot_team"] = self.room.instances[data["shot_by"]].team.team_name
        self.room.lasers.add_laser(**data)

    def puff_callback(self, tag, data):
        color = (1.0, 0.0,
                 0.0) if (self.camera.team.team_name is not None
                          and self.camera.team.team_name == "RED") else (0.0,
                                                                         0.0,
                                                                         1.0)
        if data["pos"] == self.last_pposition:
            pass
        else:
            self.last_pposition = data["pos"]
            self.room.puffs.new_puff(data["pos"],
                                     color=color,
                                     fire_vector=data["fire"],
                                     right_vector=data["up"])
            data["sound_source"] = data["pos"]
            data["sound_type"] = "knock"
            self.sound_play_callback("sound", data)

    def game_event(self, tag, data):
        self.game_state = data["game_state"]
        if data["game_state"] == "game_start":
            team_message = "You have joined team " + self.camera.team.team_name
            self.game_state_messages.append(team_message)
        if data["game_state"] == "game_over":
            end_message = ("All of " + data["team"][0].team_name +
                           "'s gates have been destroyed.")
            self.game_state_messages.append(end_message)

    def player_event(self, data):
        pass

    # window events
    def on_key_press(self, symbol, modifiers):
        # self.camera.check_orientation_keys(symbol)
        if symbol == key.ESCAPE:
            #sys.exit()
            pass
        else:
            self.network.send("keypress", symbol)

    def on_key_release(self, symbol, modifiers):
        self.network.send("keyrelease", symbol)

    def on_mouse_press(self, x, y, button, modifiers):
        self.network.send("mousepress", button)

    def on_mouse_release(self, x, y, button, modifiers):
        self.network.send("mouserelease", button)

    def on_resize(self, width, height):
        self.hud.window_resize(width, height)

    # main update loop
    def update(self, dt):
        if self.initialized:
            # print self.camera.hit_by
            self.player_state_hit = 0
            if (self.camera.health < self.camera.last_health):
                try:
                    attacker = self.room.instances[self.camera.hit_by].position
                except:
                    attacker = Vector3(0.0, 0.0, 0.0)
                attack_vector = (attacker - self.camera.position).normalize()
                attack_angle = self.camera.forward.angle(attack_vector)

                if attack_angle >= 2.0:
                    self.player_state_hit = 4
                elif attack_angle >= 1.0:
                    self.player_state_hit = 2 if (
                        self.camera.right.angle(attack_vector) < 1.5) else 3
                else:
                    self.player_state_hit = 1

            if self.camera.dead is True and self.player_state_alive is True:
                self.player_state_alive = False
                self.game_state_messages.append("You have died, respawning.")
            elif self.camera.dead is False and self.player_state_alive is False:
                self.player_state_alive = True

            functions = self.get_scheduled_tasks()
            for func in functions:
                func()

            self.camera.check_orientation_keys(self.keyboard)
            if self.camera.mod:
                self.network.send("orientation", {
                    "forward": self.camera.forward,
                    "up": self.camera.up
                })
                self.camera.mod = False
            self.hud.update(self.camera)
            pos = self.camera.position
            self.listener.position = (pos[0] / 100, pos[1] / 100, pos[2] / 100)
            self.listener.up_orientation = self.camera.up
            self.listener.forward_orientation = self.camera.forward

    def on_draw(self):
        # if self.very_first:
        #     load_img = pyglet.image.load('../images/loadingpage.png')#loadingpage.png')
        #     load_img.anchor_x = load_img.width / 2
        #     load_img.anchor_y = load_img.height / 2
        #     self.arg = pyglet.sprite.Sprite(load_img, x = 300, y = 250)
        #     self.arg.draw()
        #     self.very_first = False
        #
        # elif self.loading:
        #     time.sleep(5)
        #     self.loading = False

        if self.initialized:
            begin_draw()
            self.camera.place_camera()
            self.room.render()
            self.hud.draw()
Esempio n. 4
0
class Client(Manager):
    camera = None
    hud = None
    graphics_cache = {}
    initialized = False
    game_state_messages = []
    player_state_alive = False
    player_state_hit = 0
    hits = []

    def __init__(self, **kwargs):
        super(Client, self).__init__()
        self.listener = pyglet.media.listener

        # create window
        try:
            # try and create a window with multisampling (antialiasing)
            config = Config(sample_buffers=1, samples=4,
                depth_size=16, double_buffer=True)
            self.window = pyglet.window.Window(resizable=True, config=config,
                width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
        except pyglet.window.NoSuchConfigException:
            # fall back to no multisampling for old hardware
            print "window configuration failed"
            self.window = pyglet.window.Window(resizable=True,
                width=SCREEN_WIDTH, height=SCREEN_HEIGHT)

        # register window events
        self.window.set_exclusive_mouse(True)
        self.on_draw = self.window.event(self.on_draw)
        self.on_key_press = self.window.event(self.on_key_press)
        self.on_key_release = self.window.event(self.on_key_release)
        self.on_mouse_press = self.window.event(self.on_mouse_press)
        self.on_mouse_release = self.window.event(self.on_mouse_release)
        self.on_resize = self.window.event(self.on_resize)
        self.keyboard = pyglet.window.key.KeyStateHandler()
        self.window.push_handlers(self.keyboard)

        # init graphics
        setup_opengl()

        # init misc?
        if self.hud is None:
            self.hud = Hud()
        #if self.sound is None:
        self.sound = Sound()

        # self.fps = FPSDisplay()
        # self.room.elements.append(self.fps)
        # self.fps_display = pyglet.clock.ClockDisplay() # see programming guide pg 48

        # network must be last to avoid race condition with the initialization callback
        self.network = NetClient(callbacks={
            "player_init": self.init_callback,
            "packed_room": self.packed_room_callback,
            "sound_play": self.sound_play_callback,
            "explosion": self.explosion_callback,
            "laser": self.laser_callback,
            "puff" : self.puff_callback,
            "game_event": self.game_event
        }, **kwargs)

        # start game
        pyglet.clock.set_fps_limit(60)
        pyglet.clock.schedule(self.update)
        pyglet.app.run()

    # callbacks
    def init_callback(self, tag, data):
        cache = data["persistent_cache"]
        player_room_id = data["player_room_id"]

        # if room is not default_room, initialize it here

        # first, switch this client's player object to a camera object
        cache[player_room_id] = (Camera.__name__, cache[player_room_id][1])

        # unpack persistent cache and set camera
        self.room.unpack_add(data["persistent_cache"])
        self.camera = self.room.instances[player_room_id]
        self.camera.on_mouse_motion = self.window.event(self.camera.on_mouse_motion)
        self.camera.on_mouse_drag = self.window.event(self.camera.on_mouse_drag)

        self.last_pposition = Vector3(0.0,0.0,0.0)

        # start game
        self.initialized = True

    def packed_room_callback(self, tag, data):
        newRoom = copy.copy(self.room)
        newRoom.elements = copy.copy(self.room.elements)
        newRoom.classes = copy.copy(self.room.classes)
        newRoom.instances = copy.copy(self.room.instances)
        newRoom.players = copy.copy(self.room.players)
        newRoom.balistics = copy.copy(self.room.balistics)
        newRoom.unpack(data)
        self.room=newRoom

    def sound_play_callback(self, tag, data):
        sound_source = data["sound_source"]
        sound_type = data["sound_type"]
        self.sound.play_sound(sound_type,sound_source)

    def explosion_callback(self, tag, data):
        data["sound_source"] = data["pos"]
        data["sound_type"] = "explosion"
        self.sound_play_callback("sound", data)
        self.room.explosions.new_explosion(data["pos"])

    def laser_callback(self, tag, data):
        # print "laser_callback: %s" % data
        
        if "hits" in data:
            if data["hits"] is not None and data["hits"] != self.camera.id:
                self.hits.append(data["hits"])
            del data["hits"]
            
        data["shot_team"] = self.room.instances[data["shot_by"]].team.team_name
        self.room.lasers.add_laser(**data)
    
    def puff_callback(self, tag, data):
        color = (1.0,0.0,0.0) if (self.camera.team.team_name is not None and self.camera.team.team_name == "RED") else (0.0,0.0,1.0)
        if data["pos"] == self.last_pposition:
            pass
        else:
            self.last_pposition = data["pos"]
            self.room.puffs.new_puff(data["pos"], color=color, fire_vector=data["fire"], right_vector=data["up"])
            data["sound_source"] = data["pos"]
            data["sound_type"] = "knock"
            self.sound_play_callback("sound",data)
    
    def game_event(self, tag, data):
        self.game_state = data["game_state"]
        if data["game_state"] == "game_start":
            team_message = "You have joined team " + self.camera.team.team_name
            self.game_state_messages.append(team_message)
        if data["game_state"] == "game_over":
            end_message = ("All of " + data["team"][0].team_name + "'s gates have been destroyed.")
            self.game_state_messages.append(end_message)
            
    def player_event(self, data):
        pass

    # window events
    def on_key_press(self, symbol, modifiers):
        # self.camera.check_orientation_keys(symbol)
        if symbol == key.ESCAPE:
            #sys.exit()
            pass
        else:
            self.network.send("keypress", symbol)

    def on_key_release(self, symbol, modifiers):
        self.network.send("keyrelease", symbol)

    def on_mouse_press(self, x, y, button, modifiers):
        self.network.send("mousepress", button)

    def on_mouse_release(self, x, y, button, modifiers):
        self.network.send("mouserelease", button)
    
    def on_resize(self, width, height):
        self.hud.window_resize(width, height)
    
    # main update loop
    def update(self, dt):
        if self.initialized:
            # print self.camera.hit_by
            self.player_state_hit = 0
            if (self.camera.health < self.camera.last_health):
                try:
                    attacker = self.room.instances[self.camera.hit_by].position
                except:
                    attacker = Vector3(0.0,0.0,0.0)
                attack_vector = (attacker - self.camera.position).normalize()
                attack_angle = self.camera.forward.angle(attack_vector)
                
                if attack_angle >= 2.0:
                    self.player_state_hit = 4
                elif attack_angle >= 1.0:
                    self.player_state_hit = 2 if (self.camera.right.angle(attack_vector) < 1.5) else 3
                else:
                    self.player_state_hit = 1
            
            if self.camera.dead is True and self.player_state_alive is True:
                self.player_state_alive = False
                self.game_state_messages.append("You have died, respawning.")
            elif self.camera.dead is False and self.player_state_alive is False:
                self.player_state_alive = True
                
            functions = self.get_scheduled_tasks()
            for func in functions:
                func()

            self.camera.check_orientation_keys(self.keyboard)
            if self.camera.mod:
                self.network.send("orientation", {
                    "forward": self.camera.forward,
                    "up":      self.camera.up
                })
                self.camera.mod = False
            self.hud.update(self.camera)
            pos = self.camera.position
            self.listener.position = (pos[0]/100,pos[1]/100,pos[2]/100)
            self.listener.up_orientation = self.camera.up
            self.listener.forward_orientation = self.camera.forward


    def on_draw(self):
        # if self.very_first:
        #     load_img = pyglet.image.load('../images/loadingpage.png')#loadingpage.png')
        #     load_img.anchor_x = load_img.width / 2
        #     load_img.anchor_y = load_img.height / 2
        #     self.arg = pyglet.sprite.Sprite(load_img, x = 300, y = 250)
        #     self.arg.draw()
        #     self.very_first = False
        #     
        # elif self.loading:    
        #     time.sleep(5)
        #     self.loading = False
            
        if self.initialized:
            begin_draw()
            self.camera.place_camera()
            self.room.render()
            self.hud.draw()