wip
This commit is contained in:
parent
78cc5ff0cd
commit
70f4a89e30
10 changed files with 564 additions and 182 deletions
|
|
@ -1,24 +1,24 @@
|
|||
import atexit
|
||||
|
||||
import pygame
|
||||
from typer import Typer
|
||||
from websocket import create_connection
|
||||
|
||||
from learn_sql_model.game.menu import Menu
|
||||
from learn_sql_model.config import get_config
|
||||
from learn_sql_model.console import console
|
||||
from learn_sql_model.factories.hero import HeroFactory
|
||||
from learn_sql_model.models.hero import HeroCreate, HeroDelete, HeroUpdate, Heros
|
||||
from learn_sql_model.game.map import Map
|
||||
from learn_sql_model.game.menu import Menu
|
||||
from learn_sql_model.game.player import Player
|
||||
from learn_sql_model.optional import _optional_import_
|
||||
|
||||
pygame = _optional_import_("pygame", group="game")
|
||||
|
||||
speed = 10
|
||||
|
||||
config = get_config()
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self):
|
||||
hero = HeroFactory().build(size=50, x=100, y=100)
|
||||
self.hero = HeroCreate(**hero.dict()).post()
|
||||
|
||||
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
|
||||
pygame.display.set_caption("Learn SQL Model")
|
||||
self.clock = pygame.time.Clock()
|
||||
|
|
@ -30,13 +30,15 @@ class Client:
|
|||
self.moving_left = False
|
||||
self.moving_right = False
|
||||
self.ticks = 0
|
||||
self.others = []
|
||||
self.player = Player(self)
|
||||
self.menu = Menu(self)
|
||||
self.map = Map(self)
|
||||
self.font = pygame.font.SysFont("", 50)
|
||||
self.joysticks = {}
|
||||
|
||||
atexit.register(self.quit)
|
||||
|
||||
@ property
|
||||
@property
|
||||
def ws(self):
|
||||
def connect():
|
||||
self._ws = create_connection(
|
||||
|
|
@ -59,67 +61,23 @@ class Client:
|
|||
console.print("render")
|
||||
self.render()
|
||||
time = self.clock.tick(60)
|
||||
self.elapsed = time / 100
|
||||
self.ticks += 1
|
||||
console.print(f"time: {time}")
|
||||
console.print(f"ticks: {self.ticks}")
|
||||
self.quit()
|
||||
|
||||
def quit(self):
|
||||
try:
|
||||
HeroDelete(id=self.hero.id).delete()
|
||||
except:
|
||||
pass
|
||||
self.running = False
|
||||
self.player.quit()
|
||||
|
||||
def update(self):
|
||||
if self.moving_up:
|
||||
self.hero.y -= speed
|
||||
if self.moving_down:
|
||||
self.hero.y += speed
|
||||
if self.moving_left:
|
||||
self.hero.x -= speed
|
||||
if self.moving_right:
|
||||
self.hero.x += speed
|
||||
|
||||
if self.hero.x < 0 + self.hero.size:
|
||||
self.hero.x = 0 + self.hero.size
|
||||
if self.hero.x > self.screen.get_width() - self.hero.size:
|
||||
self.hero.x = self.screen.get_width() - self.hero.size
|
||||
if self.hero.y < 0 + self.hero.size:
|
||||
self.hero.y = 0 + self.hero.size
|
||||
if self.hero.y > self.screen.get_height() - self.hero.size:
|
||||
self.hero.y = self.screen.get_height() - self.hero.size
|
||||
|
||||
if self.ticks % 5 == 0 or self.ticks == 0:
|
||||
console.print("updating")
|
||||
update = HeroUpdate(**self.hero.dict(exclude_unset=True))
|
||||
console.print(update)
|
||||
self.ws.send(update.json())
|
||||
console.print("sent")
|
||||
|
||||
raw_heros = self.ws.recv()
|
||||
console.print(raw_heros)
|
||||
self.others = Heros.parse_raw(raw_heros)
|
||||
...
|
||||
|
||||
def render(self):
|
||||
self.screen.fill((0, 0, 0))
|
||||
|
||||
for other in self.others.heros:
|
||||
if other.id != self.hero.id:
|
||||
pygame.draw.circle(
|
||||
self.screen, (255, 0, 0), (other.x, other.y), other.size
|
||||
)
|
||||
self.screen.blit(
|
||||
self.font.render(other.name, False, (255, 255, 255), 1),
|
||||
(other.x, other.y),
|
||||
)
|
||||
|
||||
pygame.draw.circle(
|
||||
self.screen, (0, 0, 255), (self.hero.x, self.hero.y), self.hero.size
|
||||
)
|
||||
self.screen.blit(
|
||||
self.font.render(self.hero.name, False, (255, 255, 255)),
|
||||
(self.hero.x, self.hero.y),
|
||||
)
|
||||
self.map.render()
|
||||
self.player.render()
|
||||
|
||||
# update the screen
|
||||
self.menu.render()
|
||||
|
|
@ -127,52 +85,18 @@ class Client:
|
|||
|
||||
def handle_events(self):
|
||||
self.events = pygame.event.get()
|
||||
self.menu.handle_events(self.events)
|
||||
self.player.handle_events()
|
||||
for event in self.events:
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
self.running = False
|
||||
if event.key == pygame.K_LEFT:
|
||||
self.moving_left = True
|
||||
if event.key == pygame.K_RIGHT:
|
||||
self.moving_right = True
|
||||
if event.key == pygame.K_UP:
|
||||
self.moving_up = True
|
||||
if event.key == pygame.K_DOWN:
|
||||
self.moving_down = True
|
||||
# wasd
|
||||
if event.key == pygame.K_w:
|
||||
self.moving_up = True
|
||||
if event.key == pygame.K_s:
|
||||
self.moving_down = True
|
||||
if event.key == pygame.K_a:
|
||||
self.moving_left = True
|
||||
if event.key == pygame.K_d:
|
||||
self.moving_right = True
|
||||
# controller left joystick
|
||||
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_LEFT:
|
||||
self.moving_left = False
|
||||
if event.key == pygame.K_RIGHT:
|
||||
self.moving_right = False
|
||||
if event.key == pygame.K_UP:
|
||||
self.moving_up = False
|
||||
if event.key == pygame.K_DOWN:
|
||||
self.moving_down = False
|
||||
# wasd
|
||||
if event.key == pygame.K_w:
|
||||
self.moving_up = False
|
||||
if event.key == pygame.K_s:
|
||||
self.moving_down = False
|
||||
if event.key == pygame.K_a:
|
||||
self.moving_left = False
|
||||
if event.key == pygame.K_d:
|
||||
self.moving_right = False
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if event.button == 1: # Left mouse button
|
||||
self.menu.handle_click()
|
||||
if event.type == pygame.JOYDEVICEADDED:
|
||||
# This event will be generated when the program starts for every
|
||||
# joystick, filling up the list without needing to create them manually.
|
||||
joy = pygame.joystick.Joystick(event.device_index)
|
||||
self.joysticks[joy.get_instance_id()] = joy
|
||||
if event.type == pygame.JOYDEVICEREMOVED:
|
||||
del self.joysticks[event.instance_id]
|
||||
|
||||
def check_events(self):
|
||||
pass
|
||||
|
|
@ -184,7 +108,7 @@ class Client:
|
|||
game_app = Typer()
|
||||
|
||||
|
||||
@ game_app.command()
|
||||
@game_app.command()
|
||||
def run():
|
||||
client = Client()
|
||||
client.run()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue