def create_text(self): # pg.surface if "bold" in self.kwargs: bold = self.kwargs["bold"] else: bold = False return u.createText(text=self.kwargs["text"], size=self.kwargs["size"], bold=bold)
def loop(self): """pygame main loop.""" while self.running: if self.scene is scenes["startup"]: # events self.handle_events() # drawing render_list.clear(self.scene, self.scene.background) changes = render_list.draw(self.scene) self.app.draw(self.scene) # displaying text fps = u.createText( text="fps: {}".format(int(self.app.clock.get_fps()))) shots = u.createText(text="Shots: {}".format(self.score)) #self.scene.blit(fps, (10, 8)) self.app.draw(shots, (10, fps.get_rect().height + 5)) # updating for each in render_list: each.update() self.scene.update() self.app.update(changes) elif self.scene is scenes["game_over"]: for evt in pg.event.get(): if evt.type is pg.QUIT: self.app.quit() if evt.type is pg.KEYDOWN and evt.key is pg.K_ESCAPE: self.app.quit() self.scene.blit(self.scene.background, (0, 0)) self.scene.blit(gameover_text.image, gameover_text.rect) self.scene.blit(tryagain_button.image, tryagain_button.rect) self.app.draw(self.scene) self.scene.update() tryagain_button.update() self.app.update() # start over if tryagain_button.click: self.scene = scenes["startup"] self.scene.blit(self.scene.background, (0, 0)) healthbar.health = 100 for explosion in render_list: if type(explosion) is Explosion: render_list.remove(explosion)
def __init__(self, pygame, target): pygame.sprite.Sprite.__init__(self) self.target = target self.frames = 0 self.t1 = time.time() self.fps = target self.image, self.rect = createText(str(self.fps), DISPLAY_WIDTH - 20, 20, (0, 200, 0), DISPLAY_HEIGHT // 40)
def update(self): self.frames += 1 if self.frames >= self.target: t_now = time.time() self.fps = self.frames / (t_now - self.t1) self.t1 = t_now self.frames = 0 self.image, self.rect = createText(str(int(self.fps)), DISPLAY_WIDTH - 20, 20, (0, 200, 0), DISPLAY_HEIGHT // 40)
def __init__(self): pygame.sprite.Sprite.__init__(self) self.score = 0 self.image, self.rect = createText("Score: {0}".format(self.score), 60, 20, (0, 200, 0), DISPLAY_HEIGHT // 40)
def update(self): self.image, self.rect = createText("Score: {0}".format(self.score), 60, 20, (0, 200, 0), DISPLAY_HEIGHT // 40)