FEAT: Added map image (though inaccurate), and added round attr to match
This commit is contained in:
23
game.py
23
game.py
@@ -1,9 +1,10 @@
|
||||
import pygame
|
||||
from match import Match
|
||||
from player import Player
|
||||
from utils import mapped_value
|
||||
from coordinate_manager import CoordinateManager
|
||||
|
||||
WIDTH, HEIGHT = 800, 600
|
||||
WIDTH, HEIGHT = 800, 800
|
||||
MAP_SIZE = 700
|
||||
FPS = 60
|
||||
|
||||
class Game:
|
||||
@@ -19,6 +20,7 @@ class Game:
|
||||
self.running = True
|
||||
|
||||
self.match = match
|
||||
self.coordinate_manager = CoordinateManager(WIDTH, HEIGHT)
|
||||
|
||||
def handle_events(self):
|
||||
"""Handles user inputs."""
|
||||
@@ -34,15 +36,26 @@ class Game:
|
||||
"""Draws everything on screen."""
|
||||
self.screen.fill((30, 30, 30)) # Clear screen
|
||||
|
||||
# Draw map from image
|
||||
map_image = pygame.image.load(f"maps/{self.match.map_name}.png")
|
||||
map_x, map_y = self.coordinate_manager.get_top_left(WIDTH, HEIGHT, MAP_SIZE)
|
||||
|
||||
map_image = pygame.transform.scale(map_image, (MAP_SIZE, MAP_SIZE))
|
||||
map_image = pygame.transform.rotate(map_image, 270)
|
||||
self.screen.blit(map_image, (map_x, map_y))
|
||||
|
||||
# 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))
|
||||
|
||||
# Draw current round
|
||||
text = self.font.render(f"Round: {self.match.round}", True, (255, 255, 255))
|
||||
self.screen.blit(text, (10, 50))
|
||||
|
||||
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)
|
||||
mapped_x, mapped_y = self.coordinate_manager.coord_to_pixel(player.x, player.y)
|
||||
pygame.draw.circle(self.screen, (255, 255, 255), (mapped_x, mapped_y), 5)
|
||||
|
||||
# Draw player name
|
||||
@@ -65,7 +78,7 @@ 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"])
|
||||
game_info = demo_parser.parse_ticks(["X", "Y", "Z", "pitch", "yaw", "is_alive", "team", "player_steamid", "team_rounds_total"])
|
||||
header_info = demo_parser.parse_header()
|
||||
map_name = header_info['map_name']
|
||||
players = demo_parser.parse_player_info()
|
||||
|
||||
30
match.py
30
match.py
@@ -4,8 +4,12 @@ class Match:
|
||||
def __init__(self, map_name, game_info, tick_rate=64):
|
||||
self.players = []
|
||||
self.map_name = map_name
|
||||
self.round = 0
|
||||
self.tick = 0
|
||||
|
||||
self.tick = 1
|
||||
self.current_tick = game_info[game_info["tick"] == self.tick]
|
||||
|
||||
self.round = self.current_tick["team_rounds_total"].values[0]
|
||||
|
||||
self.max_tick = game_info.index[-1]
|
||||
self.game_info = game_info # pd dataframe sorted by tick
|
||||
self.tick_rate = tick_rate
|
||||
@@ -15,20 +19,26 @@ class Match:
|
||||
|
||||
def next_tick(self) -> None:
|
||||
self.tick += 1
|
||||
self.current_tick = self.game_info[self.game_info["tick"] == self.tick]
|
||||
self._update_player_positions()
|
||||
self._update_round()
|
||||
|
||||
def _update_player_positions(self) -> None:
|
||||
# inefficient, might need to change
|
||||
current_tick = self.game_info[self.game_info["tick"] == self.tick]
|
||||
|
||||
# empty tick
|
||||
if current_tick.empty:
|
||||
if self.current_tick.empty:
|
||||
return
|
||||
|
||||
for player in self.players:
|
||||
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]
|
||||
player.pitch = current_tick[current_tick["player_steamid"] == player.steam_id]["pitch"].values[0]
|
||||
player.yaw = current_tick[current_tick["player_steamid"] == player.steam_id]["yaw"].values[0]
|
||||
player.dead = current_tick[current_tick["player_steamid"] == player.steam_id]["is_alive"].values[0] == 0
|
||||
player.x = self.current_tick[self.current_tick["player_steamid"] == player.steam_id]["Y"].values[0]
|
||||
player.y = self.current_tick[self.current_tick["player_steamid"] == player.steam_id]["X"].values[0]
|
||||
player.z = self.current_tick[self.current_tick["player_steamid"] == player.steam_id]["Z"].values[0]
|
||||
player.pitch = self.current_tick[self.current_tick["player_steamid"] == player.steam_id]["pitch"].values[0]
|
||||
player.yaw = self.current_tick[self.current_tick["player_steamid"] == player.steam_id]["yaw"].values[0]
|
||||
player.dead = self.current_tick[self.current_tick["player_steamid"] == player.steam_id]["is_alive"].values[0] == 0
|
||||
|
||||
def _update_round(self) -> None:
|
||||
if self.current_tick.empty:
|
||||
return
|
||||
self.round = self.current_tick["team_rounds_total"].values[0]
|
||||
Reference in New Issue
Block a user