def main(): #Initialize the window and windowView (the windowView won't be setup until the state changes!) Init() #These variables will track our position within the game. lCurrentState = ["NULL","NULL"] lNextState = ["Menu","Intro"] #This will be updated when we change to a state. EntityManager = Entity_Manager() ChangeState(lCurrentState, lNextState, config.window, config.windowView, EntityManager) t = sf.Time(0.0) accumulator = sf.Time(0.0) MAX_FRAMESKIP = 5 timer = sf.Clock() lastKeyPress = sf.Clock() #This will be False if the player clicks outside of the program's window and "pause" the program windowIsActive = True bQuit = False while not bQuit: frameTime = timer.elapsed_time timer.restart() #This caps the time inbetween frames to # prevent a spiral of death (which happens when the computer # can't keep up.) if frameTime > sf.Time(0.25): print "preventing spiral of death" frameTime = sf.Time(0.25) accumulator += frameTime #This will loop through all of the events that have been triggered by player input for event in config.window.iter_events(): if event.type == sf.Event.MOUSE_MOVED: Input_Manager._Mouse_Has_Moved(config.window.convert_coords(event.x,event.y)) #elif event.type == sf.Event.TEXT_ENTERED: #Input_Manager._Key_Input(event.unicode, True, lastKeyPress.elapsed_time) #This restarts the Timer for the lastKeyPress since a new key just # got pressed. #lastKeyPress.restart() elif event.type == sf.Event.KEY_PRESSED: Input_Manager._Key_Input(event.code, True, lastKeyPress.elapsed_time) #This restarts the Timer for the lastKeyPress since a new key just # got pressed. lastKeyPress.restart() elif event.type == sf.Event.KEY_RELEASED: #The time elapsed isn't necessary for the released key. Input_Manager._Key_Input(event.code) elif event.type == sf.Event.MOUSE_BUTTON_PRESSED: Input_Manager._Mouse_Input(event.button, True) elif event.type == sf.Event.MOUSE_BUTTON_RELEASED: Input_Manager._Mouse_Input(event.button,False) elif event.type == sf.Event.CLOSED: for stateIndx in xrange(len(lNextState)): lNextState[stateIndx] = "QUIT" bQuit = True elif event.type == sf.Event.LOST_FOCUS: windowIsActive = False elif event.type == sf.Event.GAINED_FOCUS: windowIsActive = True iLoops = 0 #A counter for the amount of game update loops that are made in sucession whilst skipping rendering updates. #This loop will start if it is time to commence the next update and will keep going if we are behind schedule and need to catch up. while accumulator >= sf.Time(1./config.FRAME_RATE) and iLoops < MAX_FRAMESKIP: #This makes the program so that it basically pauses all of its game updates when a user clicks outside of the window. And it waits until the user clicks on the window. if windowIsActive: #We don't want to change lNextState if the game has been set to QUIT if not bQuit: #lNextState will contain "NULL"s when no state change is signaled #lNextState will have all of its elements change when switching to a new state. lNextState = EntityManager._Input_Update() #Check to see if we have signaled to quit the game thus far if lNextState[0] == "QUIT": bQuit = True #If one of the lNextState elements is changed, they all are (just how it goes.) if lNextState[0] != "NULL" and lNextState[0] != "QUIT": ChangeState(lCurrentState, lNextState, config.window, config.windowView, EntityManager) #Finally after we've handled input and have correctly adjusted to the nextState (in most cases it won't happen,) #we can then update our game's model with stuff that will happen in the respective state with each game update. #Notice that DT is a constant variable that represents how much time is going by during # this update. lNextState = EntityManager._Logic_Update(sf.Time(1./config.FRAME_RATE)) #Check to see if we have signaled to quit the game thus far if lNextState[0] == "QUIT": bQuit = True #If one of the lNextState elements is changed, they all are (just how it goes.) if lNextState[0] != "NULL" and lNextState[0] != "QUIT": ChangeState(lCurrentState, lNextState, config.window, config.windowView, EntityManager) #If we have received a quit signal, we should stop our loop and quit the game! if bQuit: break #The accumulator contains the time that hasn't yet been used for the updates. #Each update will assume that dt time is going by, so the accumulator just # needs to subtract by the time that is being used up. accumulator -= sf.Time(1./config.FRAME_RATE) #This counts the Update loop iLoops += 1 #This makes the program so that it basically pauses all of its game updates when a user clicks outside of the window. And it waits until the user clicks on the window. if windowIsActive: EntityManager._Render_Update(config.window, config.windowView) config.window.display() #This closes our RenderWindow! config.window.close()
def main(): #Initialize the window and windowView (the windowView won't be setup until the state changes!) window, windowView = Init() #These variables will track our position within the game. lCurrentState = ["NULL", "NULL"] lNextState = ["Menu", "MainMenu"] #This will be updated when we change to a state. EntityManager = Entity_Manager() #The AssetManager will be used by ChangeState in order to retrieve sounds/textures for a particular type of entity. AssetManager = assets.Asset_Manager() ChangeState(lCurrentState, lNextState, windowView, EntityManager, AssetManager) timer = sf.Clock() SKIP_TICKS = 1 / config.TICKS_PER_SEC MAX_FRAMESKIP = 5 iLoops = None fNextGameTick = timer.elapsed_time #This will be False if the player clicks outside of the program's window and "pause" the program windowIsActive = True bQuit = False while not bQuit: #This will loop through all of the events that have been triggered by player input for event in window.iter_events(): if event.type == sf.Event.MOUSE_MOVED: Input_Manager._Mouse_Has_Moved( window.convert_coords(event.x, event.y)) elif event.type == sf.Event.TEXT_ENTERED: Input_Manager._Key_Input(event.unicode, True, timer.elapsed_time) elif event.type == sf.Event.KEY_PRESSED: Input_Manager._Key_Input(event.code, True, timer.elapsed_time) elif event.type == sf.Event.KEY_RELEASED: Input_Manager._Key_Input(event.code, False, timer.elapsed_time) elif event.type == sf.Event.MOUSE_BUTTON_PRESSED: Input_Manager._Mouse_Input(event.button, True) elif event.type == sf.Event.MOUSE_BUTTON_RELEASED: Input_Manager._Mouse_Input(event.button, False) elif event.type == sf.Event.LOST_FOCUS: windowIsActive = False elif event.type == sf.Event.GAINED_FOCUS: windowIsActive = True elif event.type == sf.Event.CLOSED: for stateIndx in xrange(len(lNextState)): lNextState[stateIndx] = "QUIT" bQuit = True iLoops = 0 #A counter for the amount of game update loops that are made in sucession whilst skipping rendering updates. #This loop will start if it is time to commence the next update and will keep going if we are behind schedule and need to catch up. while timer.elapsed_time > fNextGameTick and iLoops < MAX_FRAMESKIP: #This makes the program so that it basically pauses all of its game updates when a user clicks outside of the window. And it waits until the user clicks on the window. if windowIsActive: #We don't want to change lNextState if the game has been set to QUIT if not bQuit: #lNextState will contain "NULL"s when no state change is signaled #lNextState will have all of its elements change when switching to a new state. lNextState = EntityManager._Input_Update() #Check to see if we have signaled to quit the game thus far if lNextState[0] == "QUIT": bQuit = True #If one of the lNextState elements is changed, they all are (just how it goes.) if lNextState[0] != "NULL" and lNextState[0] != "QUIT": ChangeState(lCurrentState, lNextState, windowView, EntityManager, AssetManager) #Finally after we've handled input and have correctly adjusted to the nextState (in most cases it won't happen,) #we can then update our game's model with stuff that will happen in the respective state with each game update. if not bQuit: EntityManager._Logic_Update( timer.elapsed_time - fNextGameTick ) #This updates our model depending on what is going on in the current state #If we have received a quit signal, we should stop our loop and quit the game! if bQuit: break iLoops += 1 fNextGameTick += sf.Time(seconds=SKIP_TICKS) EntityManager._Render_Update(window, windowView) window.display() #This closes our RenderWindow! window.close()
def main(): #Initialize the window and windowView (the windowView won't be setup until the state changes!) window, windowView = Init() #These variables will track our position within the game. lCurrentState = ["NULL","NULL"] lNextState = ["Menu","MainMenu"] #This will be updated when we change to a state. EntityManager = Entity_Manager() #The AssetManager will be used by ChangeState in order to retrieve sounds/textures for a particular type of entity. AssetManager = assets.Asset_Manager() ChangeState(lCurrentState, lNextState, windowView, EntityManager, AssetManager) timer = sf.Clock() SKIP_TICKS = 1/config.TICKS_PER_SEC MAX_FRAMESKIP = 5 iLoops = None fNextGameTick = timer.elapsed_time #This will be False if the player clicks outside of the program's window and "pause" the program windowIsActive = True bQuit = False while not bQuit: #This will loop through all of the events that have been triggered by player input for event in window.iter_events(): if event.type == sf.Event.MOUSE_MOVED: Input_Manager._Mouse_Has_Moved(window.convert_coords(event.x,event.y)) elif event.type == sf.Event.TEXT_ENTERED: Input_Manager._Key_Input(event.unicode, True, timer.elapsed_time) elif event.type == sf.Event.KEY_PRESSED: Input_Manager._Key_Input(event.code, True, timer.elapsed_time) elif event.type == sf.Event.KEY_RELEASED: Input_Manager._Key_Input(event.code, False, timer.elapsed_time) elif event.type == sf.Event.MOUSE_BUTTON_PRESSED: Input_Manager._Mouse_Input(event.button, True) elif event.type == sf.Event.MOUSE_BUTTON_RELEASED: Input_Manager._Mouse_Input(event.button,False) elif event.type == sf.Event.LOST_FOCUS: windowIsActive = False elif event.type == sf.Event.GAINED_FOCUS: windowIsActive = True elif event.type == sf.Event.CLOSED: for stateIndx in xrange(len(lNextState)): lNextState[stateIndx] = "QUIT" bQuit = True iLoops = 0 #A counter for the amount of game update loops that are made in sucession whilst skipping rendering updates. #This loop will start if it is time to commence the next update and will keep going if we are behind schedule and need to catch up. while timer.elapsed_time > fNextGameTick and iLoops < MAX_FRAMESKIP: #This makes the program so that it basically pauses all of its game updates when a user clicks outside of the window. And it waits until the user clicks on the window. if windowIsActive: #We don't want to change lNextState if the game has been set to QUIT if not bQuit: #lNextState will contain "NULL"s when no state change is signaled #lNextState will have all of its elements change when switching to a new state. lNextState = EntityManager._Input_Update() #Check to see if we have signaled to quit the game thus far if lNextState[0] == "QUIT": bQuit = True #If one of the lNextState elements is changed, they all are (just how it goes.) if lNextState[0] != "NULL" and lNextState[0] != "QUIT": ChangeState(lCurrentState, lNextState, windowView, EntityManager, AssetManager) #Finally after we've handled input and have correctly adjusted to the nextState (in most cases it won't happen,) #we can then update our game's model with stuff that will happen in the respective state with each game update. if not bQuit: EntityManager._Logic_Update(timer.elapsed_time - fNextGameTick) #This updates our model depending on what is going on in the current state #If we have received a quit signal, we should stop our loop and quit the game! if bQuit: break iLoops += 1 fNextGameTick += sf.Time(seconds=SKIP_TICKS) EntityManager._Render_Update(window, windowView) window.display() #This closes our RenderWindow! window.close()
def main(): #Initialize the window and windowView (the windowView won't be setup until the state changes!) Init() #These variables will track our position within the game. lCurrentState = ["NULL", "NULL"] lNextState = ["Menu", "Intro"] #This will be updated when we change to a state. EntityManager = Entity_Manager() ChangeState(lCurrentState, lNextState, config.window, config.windowView, EntityManager) t = sf.Time(0.0) accumulator = sf.Time(0.0) MAX_FRAMESKIP = 5 timer = sf.Clock() lastKeyPress = sf.Clock() #This will be False if the player clicks outside of the program's window and "pause" the program windowIsActive = True bQuit = False while not bQuit: frameTime = timer.elapsed_time timer.restart() #This caps the time inbetween frames to # prevent a spiral of death (which happens when the computer # can't keep up.) if frameTime > sf.Time(0.25): print "preventing spiral of death" frameTime = sf.Time(0.25) accumulator += frameTime #This will loop through all of the events that have been triggered by player input for event in config.window.iter_events(): if event.type == sf.Event.MOUSE_MOVED: Input_Manager._Mouse_Has_Moved( config.window.convert_coords(event.x, event.y)) #elif event.type == sf.Event.TEXT_ENTERED: #Input_Manager._Key_Input(event.unicode, True, lastKeyPress.elapsed_time) #This restarts the Timer for the lastKeyPress since a new key just # got pressed. #lastKeyPress.restart() elif event.type == sf.Event.KEY_PRESSED: Input_Manager._Key_Input(event.code, True, lastKeyPress.elapsed_time) #This restarts the Timer for the lastKeyPress since a new key just # got pressed. lastKeyPress.restart() elif event.type == sf.Event.KEY_RELEASED: #The time elapsed isn't necessary for the released key. Input_Manager._Key_Input(event.code) elif event.type == sf.Event.MOUSE_BUTTON_PRESSED: Input_Manager._Mouse_Input(event.button, True) elif event.type == sf.Event.MOUSE_BUTTON_RELEASED: Input_Manager._Mouse_Input(event.button, False) elif event.type == sf.Event.CLOSED: for stateIndx in xrange(len(lNextState)): lNextState[stateIndx] = "QUIT" bQuit = True elif event.type == sf.Event.LOST_FOCUS: windowIsActive = False elif event.type == sf.Event.GAINED_FOCUS: windowIsActive = True iLoops = 0 #A counter for the amount of game update loops that are made in sucession whilst skipping rendering updates. #This loop will start if it is time to commence the next update and will keep going if we are behind schedule and need to catch up. while accumulator >= sf.Time( 1. / config.FRAME_RATE) and iLoops < MAX_FRAMESKIP: #This makes the program so that it basically pauses all of its game updates when a user clicks outside of the window. And it waits until the user clicks on the window. if windowIsActive: #We don't want to change lNextState if the game has been set to QUIT if not bQuit: #lNextState will contain "NULL"s when no state change is signaled #lNextState will have all of its elements change when switching to a new state. lNextState = EntityManager._Input_Update() #Check to see if we have signaled to quit the game thus far if lNextState[0] == "QUIT": bQuit = True #If one of the lNextState elements is changed, they all are (just how it goes.) if lNextState[0] != "NULL" and lNextState[0] != "QUIT": ChangeState(lCurrentState, lNextState, config.window, config.windowView, EntityManager) #Finally after we've handled input and have correctly adjusted to the nextState (in most cases it won't happen,) #we can then update our game's model with stuff that will happen in the respective state with each game update. #Notice that DT is a constant variable that represents how much time is going by during # this update. lNextState = EntityManager._Logic_Update( sf.Time(1. / config.FRAME_RATE)) #Check to see if we have signaled to quit the game thus far if lNextState[0] == "QUIT": bQuit = True #If one of the lNextState elements is changed, they all are (just how it goes.) if lNextState[0] != "NULL" and lNextState[0] != "QUIT": ChangeState(lCurrentState, lNextState, config.window, config.windowView, EntityManager) #If we have received a quit signal, we should stop our loop and quit the game! if bQuit: break #The accumulator contains the time that hasn't yet been used for the updates. #Each update will assume that dt time is going by, so the accumulator just # needs to subtract by the time that is being used up. accumulator -= sf.Time(1. / config.FRAME_RATE) #This counts the Update loop iLoops += 1 #This makes the program so that it basically pauses all of its game updates when a user clicks outside of the window. And it waits until the user clicks on the window. if windowIsActive: EntityManager._Render_Update(config.window, config.windowView) config.window.display() #This closes our RenderWindow! config.window.close()