From 2feb1d98b3d925f78fa196961019344be37873a8 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 21:43:07 -0500 Subject: [PATCH] add new debug config, create and implement FPSCounter class --- triple-dungeon/config.py | 3 +++ triple-dungeon/main.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 0b82a37..a395e94 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -42,6 +42,9 @@ class Config(object): BOTTOM_VIEWPORT_MARGIN = 50 TOP_VIEWPORT_MARGIN = 100 + # All debug statements and renderings should use this + DEBUG = True + class Enums(Enum): """ diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index ac5d45b..5c2b2a2 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -3,7 +3,9 @@ main.py The main class used to load the game. Holds the main game window, as well as manages basic functions for organizing the game. """ +import collections import random +import time import arcade @@ -12,6 +14,25 @@ from map import Dungeon from mobs import Player +class FPSCounter: + def __init__(self): + self.time = time.perf_counter() + self.frame_times = collections.deque(maxlen=60) + + def tick(self): + t1 = time.perf_counter() + dt = t1 - self.time + self.time = t1 + self.frame_times.append(dt) + + def get_fps(self): + total_time = sum(self.frame_times) + if total_time == 0: + return 0 + else: + return len(self.frame_times) / sum(self.frame_times) + + class Game(arcade.Window): """ Main application class. @@ -45,9 +66,10 @@ class Game(arcade.Window): # Create the Sprite lists self.enemy_list = arcade.SpriteList() + self.fps = FPSCounter() # Create the dungeon - self.dungeon = Dungeon(0, 3) + self.dungeon = Dungeon(0, 5) # Set up the player, specifically placing it at these coordinates. self.player = Player() @@ -74,7 +96,6 @@ class Game(arcade.Window): def on_draw(self): """ Render the screen. """ try: - # Clear the screen to the background color arcade.start_render() @@ -86,6 +107,9 @@ class Game(arcade.Window): self.player.draw_hit_box() x, y = self.player.center_x, self.player.center_y arcade.draw_text(str((x, y)), x - 40, y + 50, arcade.color.WHITE, 15) + arcade.draw_text(f"FPS: {self.fps.get_fps():3.0f}", self.view_left + 50, self.view_bottom + 30, + arcade.color.WHITE, 16) + self.fps.tick() except Exception: import traceback traceback.print_exc()