def main(): screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Engine RPG") clock = pygame.time.Clock() #rejilla = load_image('resources/graphics/rejilla.png', True) map_loaded = Map("pruebas.tmx") heroe = Actor(map_loaded) camara = Camera(map_loaded, heroe) inp = Input() while True: time = clock.tick(40) inp.update() salir(inp.get_key_list()) id = heroe.mover(map_loaded, inp) heroe.update(id) camara.update(screen, map_loaded, heroe) camara.show_fps(screen, clock.get_fps()) #screen.blit(rejilla, (0, 0)) pygame.display.flip() return 0
def _mainloop(self, fps): step = 1. / fps clock = pygame.time.Clock() while not Input.quitflag: Input.update() map(GameObject.update, GameObject._gameobjects) #self.scene.lateupdate() self._renderloop() PhysicsEngine.step(step * Game.scale) delta = clock.tick(fps) Game.delta = delta / 1000.
class Game(object): def __init__(self, screen, width, height, debug=False, start_level=1): self.target_fps = 60 self.clock = pygame.time.Clock() self.running = False self.test_text = "Hello world" self.font = pygame.font.Font('./assets/font/vcr.ttf', 28) self.screen = screen self.width = width self.height = height self.current_scene = None self.restarting = False self.input = Input(pygame.key, pygame.mouse) self.current_scene = MenuScene(self, start_level) self.next_scene = None self.debug = debug # self.background = Background((800, 600), 'assets/img/bgs.png') def run(self): self.running = True current_time = 0 previous_time = time.time() while self.running: self.clock.tick( self.target_fps ) self.input.update() if not self.next_scene is None: self.current_scene = copy.copy(self.next_scene) self.next_scene = None for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False self.restarting = False self.input.handle_event(event) if game.input.is_down(pygame.K_ESCAPE): self.running = False self.restarting = False current_time = time.time() delta_time = (current_time - previous_time) self.update(delta_time) self.draw() if self.debug: fps = round(1.0 / delta_time) fps_text = 'FPS: {0}'.format(fps) fps_w, fps_h = self.font.size(fps_text) fps_text_pos = (self.width - fps_w - 40, 20) self.screen.blit(self.font.render(fps_text, 1, WHITE), fps_text_pos) pygame.display.update() previous_time = current_time if self.restarting: game.run() else: game.quit() def restart(self): self.running = False self.restarting = True def quit(self): pygame.quit() quit() def update(self, dt): self.current_scene.update(dt) def draw(self): self.current_scene.draw()
class SceneManager(object): _instance2 = None _count2 = 0 def __new__(cls, *args, **kwargs): if not cls._instance2: cls._instance2 = super(SceneManager, cls).__new__( cls, *args, **kwargs) # _count += 1 print "Created new instance of SceneManger" return cls._instance2 def __init__(self): """ Initializes the scenemanager """ # Display some text if SceneManager._count2 == 0: #Init self.initPygame() self.initEventReaders() self.initScreen() self.initClock() self.initInput() self.initGame() SceneManager._count2 += 1 def initGame(self): """ Initializes the first scene """ self.gamemode = GameModes.MENU_MAIN self.scene = None self.running = True def initPygame(self): """ Initializes pygame """ result = pygame.init() return result[1] def initEventReaders(self): """ Initializes the event readers """ self.eventReaders = [] def initScreen(self): """ Initializes the screen """ self.surface = pygame.display.set_mode([Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT]) #retourneert Surface pygame.display.set_caption(Settings.GAME_TITLE) def initClock(self): """ Initializes the clock """ self.clock = pygame.time.Clock() def initInput(self): """ Initializes the input class """ self.input = Input() def run(self): """ Run the scene """ while(self.running): self.clock.tick(90) self.input.update() #E Event for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False else: for eventReader in self.eventReaders: eventReader(event) self.scene.update(self.input) self.surface.fill(Settings.SCREEN_COLOR) self.scene.draw(self.surface) pygame.display.flip() self.scene.clean() def setScene(self, scene): """ Change the scene @param scene: The new scene """ if self.scene: self.scene.clean() if not scene == None: self.scene = scene else: self.running = False def registerEventReader(self, callback): """ Register a event reader @param callback: The function that should be called """ self.eventReaders.append(callback) def unregisterEventReader(self, callback): """ Unregisters a event reader @param callback: The function that should be unregistered """ self.eventReaders.remove(callback)