add new debug config, create and implement FPSCounter class

This commit is contained in:
Xevion
2020-04-20 21:43:07 -05:00
parent 625fe85527
commit 2feb1d98b3
2 changed files with 29 additions and 2 deletions

View File

@@ -42,6 +42,9 @@ class Config(object):
BOTTOM_VIEWPORT_MARGIN = 50 BOTTOM_VIEWPORT_MARGIN = 50
TOP_VIEWPORT_MARGIN = 100 TOP_VIEWPORT_MARGIN = 100
# All debug statements and renderings should use this
DEBUG = True
class Enums(Enum): class Enums(Enum):
""" """

View File

@@ -3,7 +3,9 @@ main.py
The main class used to load the game. The main class used to load the game.
Holds the main game window, as well as manages basic functions for organizing the game. Holds the main game window, as well as manages basic functions for organizing the game.
""" """
import collections
import random import random
import time
import arcade import arcade
@@ -12,6 +14,25 @@ from map import Dungeon
from mobs import Player 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): class Game(arcade.Window):
""" """
Main application class. Main application class.
@@ -45,9 +66,10 @@ class Game(arcade.Window):
# Create the Sprite lists # Create the Sprite lists
self.enemy_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList()
self.fps = FPSCounter()
# Create the dungeon # Create the dungeon
self.dungeon = Dungeon(0, 3) self.dungeon = Dungeon(0, 5)
# Set up the player, specifically placing it at these coordinates. # Set up the player, specifically placing it at these coordinates.
self.player = Player() self.player = Player()
@@ -74,7 +96,6 @@ class Game(arcade.Window):
def on_draw(self): def on_draw(self):
""" Render the screen. """ """ Render the screen. """
try: try:
# Clear the screen to the background color # Clear the screen to the background color
arcade.start_render() arcade.start_render()
@@ -86,6 +107,9 @@ class Game(arcade.Window):
self.player.draw_hit_box() self.player.draw_hit_box()
x, y = self.player.center_x, self.player.center_y 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(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: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()