This commit is contained in:
Waylon Walker 2023-06-11 10:58:45 -05:00
parent 5101d4c478
commit 6749925f11

View file

@ -11,8 +11,10 @@ import atexit
import pygame import pygame
import typer import typer
from typer import Typer from typer import Typer
from websocket import create_connection
from learn_sql_model.models.hero import Hero, HeroCreate, HeroDelete, HeroUpdate from learn_sql_model.config import get_config
from learn_sql_model.models.hero import Hero, HeroCreate, HeroDelete, HeroUpdate, Heros
speed = 10 speed = 10
@ -20,13 +22,17 @@ pygame.font.init() # you have to call this at the start,
# if you want to use this module. # if you want to use this module.
my_font = pygame.font.SysFont("Comic Sans MS", 30) my_font = pygame.font.SysFont("Comic Sans MS", 30)
config = get_config()
class Client: class Client:
def __init__(self, name, secret_name): def __init__(self, name, secret_name):
self.hero = Hero(name=name, secret_name=secret_name, x=400, y=300, size=50) self.hero = Hero(name=name, secret_name=secret_name, x=400, y=300, size=50)
self.hero = HeroCreate(**self.hero.dict()).post() self.hero = HeroCreate(**self.hero.dict()).post()
pygame.init()
self.screen = pygame.display.set_mode((800, 600)) self.screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Learn SQL Model")
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.running = True self.running = True
self.screen.fill((0, 0, 0)) self.screen.fill((0, 0, 0))
@ -35,12 +41,19 @@ class Client:
self.moving_down = False self.moving_down = False
self.moving_left = False self.moving_left = False
self.moving_right = False self.moving_right = False
self.ws = create_connection(
f"ws://{config.api_client.host}:{config.api_client.port}/wsecho"
)
self.others = [hero for hero in Heros.list() if hero.id != self.hero.id]
self.ticks = 0 self.ticks = 0
atexit.register(self.quit) atexit.register(self.quit)
@property
def others(self):
raw_heros = self.ws.recv()
return Heros.parse_raw(raw_heros)
def run(self): def run(self):
while self.running: while self.running:
self.handle_events() self.handle_events()
@ -67,13 +80,12 @@ class Client:
HeroUpdate( HeroUpdate(
**{k: v for k, v in self.hero.dict().items() if v is not None} **{k: v for k, v in self.hero.dict().items() if v is not None}
).update() ).update()
self.others = [hero for hero in Heros.list() if hero.id != self.hero.id]
def render(self): def render(self):
# Console().print(self.hero) # Console().print(self.hero)
self.screen.fill((0, 0, 0)) self.screen.fill((0, 0, 0))
for other in self.others: for other in self.others.heros:
pygame.draw.circle(self.screen, (255, 0, 0), (other.x, other.y), other.size) pygame.draw.circle(self.screen, (255, 0, 0), (other.x, other.y), other.size)
self.screen.blit( self.screen.blit(
my_font.render(other.name, False, (255, 255, 255), 1), my_font.render(other.name, False, (255, 255, 255), 1),