From b3e8a92cdb0be0ffa056a5c81ad08a300b7477e0 Mon Sep 17 00:00:00 2001 From: ThisBirchWood Date: Sun, 9 Mar 2025 21:48:14 +0100 Subject: [PATCH] FEAT: Added pygame, scene now renders; Bug when round changes --- .gitignore | 3 ++- game.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ match.py | 4 +-- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 game.py diff --git a/.gitignore b/.gitignore index 1fd1563..477838a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -demo.dem \ No newline at end of file +demo.dem +__pycache__/ \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..e927b92 --- /dev/null +++ b/game.py @@ -0,0 +1,77 @@ +import pygame +from match import Match +from player import Player +from utils import mapped_value + +WIDTH, HEIGHT = 800, 600 +FPS = 60 + +class Game: + def __init__(self, match: Match): + pygame.init() + self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) + self.font = pygame.font.Font(None, 36) + self.small_font = pygame.font.Font(None, 15) + + pygame.display.set_caption("CS2 Demo Mapper") + + self.clock = pygame.time.Clock() + self.running = True + + self.match = match + + def handle_events(self): + """Handles user inputs.""" + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.running = False + + def update(self): + """Updates game objects.""" + self.match.next_tick() + + def draw(self): + """Draws everything on screen.""" + self.screen.fill((30, 30, 30)) # Clear screen + + # Draw current tick + text = self.font.render(f"Tick: {self.match.tick}/{self.match.max_tick}", True, (255, 255, 255)) + self.screen.blit(text, (10, 10)) + + for player in self.match.players: + if player.dead: + continue + mapped_x = mapped_value(player.x, -4000, 4000, 0, WIDTH) + mapped_y = mapped_value(player.y, -4000, 4000, 0, HEIGHT) + pygame.draw.circle(self.screen, (255, 255, 255), (mapped_x, mapped_y), 5) + + # Draw player name + text = self.small_font.render(player.name, True, (255, 255, 255)) + self.screen.blit(text, (mapped_x + 10, mapped_y)) + + pygame.display.flip() # Update display + + def run(self): + """Main game loop.""" + while self.running: + self.handle_events() + self.update() + self.draw() + self.clock.tick(self.match.tick_rate) + + pygame.quit() + +if __name__ == "__main__": + import demoparser2 + + demo_parser = demoparser2.DemoParser("demo.dem") + game_info = demo_parser.parse_ticks(["X", "Y", "Z", "pitch", "yaw", "is_alive", "team", "player_steamid"]) + players = demo_parser.parse_player_info() + + m = Match("de_dust2", game_info) + for index, row in players.iterrows(): + m.add_player(Player(row["name"], row["steamid"])) + + game = Game(m) + + game.run() diff --git a/match.py b/match.py index 0cd4361..dae77fa 100644 --- a/match.py +++ b/match.py @@ -1,13 +1,14 @@ from player import Player class Match: - def __init__(self, map_name, game_info): + def __init__(self, map_name, game_info, tick_rate=64): self.players = [] self.map_name = map_name self.round = 0 self.tick = 0 self.max_tick = game_info.index[-1] self.game_info = game_info # pd dataframe sorted by tick + self.tick_rate = tick_rate def add_player(self, player: Player) -> None: self.players.append(player) @@ -20,7 +21,6 @@ class Match: # inefficient, might need to change current_tick = self.game_info[self.game_info["tick"] == self.tick] for player in self.players: - print("Updated player position of ", player.name) player.x = current_tick[current_tick["player_steamid"] == player.steam_id]["X"].values[0] player.y = current_tick[current_tick["player_steamid"] == player.steam_id]["Y"].values[0] player.z = current_tick[current_tick["player_steamid"] == player.steam_id]["Z"].values[0]