コード例 #1
0
ファイル: statedriver.py プロジェクト: bitcraft/lpc1
    def run(self):
        """
        run the state driver.
        """

        # deref for speed
        event_poll = pygame.event.poll
        event_pump = pygame.event.pump
        current_state = self.getCurrentState
        clock = pygame.time.Clock()

        # streamline event processing by filtering out stuff we won't use
        allowed = [QUIT, KEYDOWN, KEYUP, \
                   MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION]

        pygame.event.set_allowed(None)
        pygame.event.set_allowed(allowed)

        # set an event to flush out commands
        event_flush = pygame.USEREVENT
        pygame.time.set_timer(event_flush, 20)

        # set an event to update the game state
        #update_state = pygame.USEREVENT + 1
        #pygame.time.set_timer(update_state, 30)

        # make sure our custom events will be triggered
        pygame.event.set_allowed([event_flush])

        # some stuff to handle the command queue
        rawcmds = 0
        cmdlist = []
        checkedcmds = []
        
        currentState = current_state()
        while currentState:
            clock.tick(target_fps)

            event = event_poll()
            while event:

                # check each input for something interesting
                for cmd in [ c.getCommand(event) for c in inputs ]:
                    rawcmds += 1
                    if (cmd != None) and (cmd[:2] not in checkedcmds):
                        checkedcmds.append(cmd[:2])
                        cmdlist.append(cmd)

                # we should quit
                if event.type == QUIT:
                    currentState = None
                    break

                # do we flush input now?
                elif event.type == event_flush:
                    currentState.handle_commandlist(cmdlist)
                    [ currentState.handle_commandlist(i.getHeld())
                    for i in inputs ]
                    rawcmds = 0
                    checkedcmds = []
                    cmdlist = []

                # back out of this state, or send event to the state
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.done()
                        break
                    else:
                        currentState.handle_event(event)
                else:
                    currentState.handle_event(event)

                event = event_poll()

            originalState = current_state()
            if currentState: currentState = originalState

            if currentState:
                dirty = currentState.draw(self._screen)
                gfx.update_display(dirty)

                # looks awkward?  because it is.  forcibly give small updates
                # to each object so we don't draw too often.
                time = target_fps / 10.0

                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
                [ o.update(time) for o in GameObject.population ]
                currentState = current_state()
                if not currentState == originalState: continue
コード例 #2
0
ファイル: statedriver.py プロジェクト: bitcraft/mh
    def run(self):
        """
        run the state driver.
        """

        # deref for speed
        event_poll = pygame.event.poll
        event_pump = pygame.event.pump
        current_state = self.getCurrentState
        clock = pygame.time.Clock()

        # streamline event processing by filtering out stuff we won't use
        pygame.event.set_allowed(None)
        pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])

        # set an event to flush out commands
        event_flush = pygame.USEREVENT
        pygame.time.set_timer(pygame.USEREVENT, 30)

        # make sure our custom events will be triggered
        pygame.event.set_allowed([event_flush])

        # some stuff to handle the command queue
        rawcmds = 0
        cmdlist = []
        checkedcmds = []
        
        currentState = current_state()
        while currentState:
            time = clock.tick(20)

            event = event_poll()
            while event:

                # check each input for something interesting
                for cmd in [ c.getCommand(event) for c in inputs ]:
                    rawcmds += 1
                    if (cmd != None) and (cmd[:2] not in checkedcmds):
                        checkedcmds.append(cmd[:2])
                        cmdlist.append(cmd)

                # we should quit
                if event.type == QUIT:
                    currentState = None
                    break

                # do we flush input now?
                elif event.type == pygame.USEREVENT:
                    currentState.handle_commandlist(cmdlist)
                    [ currentState.handle_commandlist(i.getHeld())
                    for i in inputs ]
                    rawcmds = 0
                    checkedcmds = []
                    cmdlist = []

                # back out of this state, or send event to the state
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.done()
                        break
                    else:
                        currentState.handle_event(event)
                else:
                    currentState.handle_event(event)

                event = event_poll()

            originalState = current_state()
            currentState = originalState

            if currentState:
                time = time / 4.0

                originalState.update(time)
                currentState = current_state()
                if not currentState == originalState: continue

                currentState.update(time)
                currentState = current_state()
                if not currentState == originalState: continue

                currentState.update(time)
                currentState = current_state()
                if not currentState == originalState: continue

                currentState.update(time)
                currentState = current_state()
                if not currentState == originalState: continue

                dirty = currentState.draw(self._screen)
                gfx.update_display(dirty)
コード例 #3
0
ファイル: context.py プロジェクト: MacLeek/mh
    def run(self):
        """
        run the state driver.
        """

        # deref for speed
        event_poll = pygame.event.poll
        event_pump = pygame.event.pump
        current_state = self.getCurrentState
        clock = pygame.time.Clock()

        # streamline event processing by filtering out stuff we won't use
        allowed = [QUIT, KEYDOWN, KEYUP, \
                   MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION]

        pygame.event.set_allowed(None)
        pygame.event.set_allowed(allowed)

        # set an event to update the game state
        debug_output = pygame.USEREVENT
        pygame.time.set_timer(debug_output, 2000)

        # make sure our custom events will be triggered
        pygame.event.set_allowed([debug_output])

        currentState = current_state()       
        lastState = currentState

        # this will loop until the end of the program
        while currentState:

            if self.lameduck:
                self.lameduck = None
                currentState = self._stack[-1]
                if currentState.activated:
                    currentState.reactivate()
                else:
                    currentState.activate()

            elif currentState is not lastState:
                if currentState.activated:
                    currentState.reactivate()
                else:
                    currentState.activate()

            lastState = currentState

            time = clock.tick(self.target_fps)


# =============================================================================
# EVENT HANDLING ==============================================================

            event = event_poll()
            while event:

                # we should quit
                if event.type == QUIT:
                    currentState = None
                    break

                # check each input for something interesting
                for cmd in [ c.getCommand(event) for c in self.inputs ]:
                    if cmd is not None:
                        currentState.handle_command(cmd)

                if event.type == debug_output:
                    print "current FPS: \t{0:.1f}".format(clock.get_fps())

                # back out of this state, or send event to the state
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        currentState = None
                        break

                event = event_poll()

# =============================================================================
# STATE UPDATING AND DRAWING HANDLING =========================================

            if current_state() is currentState:

                dirty = currentState.draw(self._screen)
                gfx.update_display(dirty)
                #gfx.update_display()

                # looks awkward?  because it is.  forcibly give small updates
                # to each object so we don't draw too often.

                time = time / 5.0

                currentState.update(time)
                currentState = current_state()
                if not currentState == lastState: continue
                currentState.update(time)
                currentState = current_state()
                if not currentState == lastState: continue
                currentState.update(time)
                currentState = current_state()
                if not currentState == lastState: continue
                currentState.update(time)
                currentState = current_state()
                if not currentState == lastState: continue
                currentState.update(time)
                currentState = current_state()