Difficult-Rocket/Difficult_Rocket/client.py

240 lines
9.1 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021 by shenjackyuanjie
# All rights reserved
# -------------------------------
2021-02-20 21:46:14 +08:00
"""
2020-12-13 18:39:12 +08:00
writen by shenjackyuanjie
2021-09-08 23:38:34 +08:00
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
2021-02-20 21:46:14 +08:00
"""
2021-09-08 23:38:34 +08:00
# system function
2021-02-20 21:46:14 +08:00
import os
2021-03-25 23:15:18 +08:00
import sys
2021-04-10 22:51:08 +08:00
import time
import logging
import configparser
2021-09-24 00:04:56 +08:00
from decimal import Decimal
2021-08-24 22:31:52 +08:00
if __name__ == '__main__': # been start will not run this
sys.path.append('/bin/libs')
sys.path.append('/bin')
2021-07-08 20:42:22 +08:00
# Difficult_Rocket function
from Difficult_Rocket.api.translate import tr
from Difficult_Rocket.api import tools, new_thread, translate
2021-02-03 22:04:34 +08:00
# libs function
local_lib = True
if local_lib:
from libs import pyglet
from libs.pyglet.window import key, mouse
else:
import pyglet
from pyglet.window import key, mouse
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
class Client:
def __init__(self, net_mode='local'):
2021-10-01 23:12:01 +08:00
start_time = time.time_ns()
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
# config
self.config = tools.load_file('configs/main.config')
# value
self.process_id = 'Client'
self.process_name = 'Client process'
2021-02-20 21:46:14 +08:00
self.process_pid = os.getpid()
self.net_mode = net_mode
2021-08-13 12:25:29 +08:00
self.caption = tools.name_handler(self.config['window']['caption'], {'version': self.config['runtime']['version']})
self.window = ClientWindow(net_mode=self.net_mode,
2021-08-13 12:25:29 +08:00
width=int(self.config['window']['width']),
height=int(self.config['window']['height']),
fullscreen=tools.format_bool(self.config['window']['full_screen']),
caption=self.caption,
resizable=tools.format_bool(self.config['window']['resizable']),
visible=tools.format_bool(self.config['window']['visible']))
2021-10-01 23:12:01 +08:00
self.logger.info(tr.lang('client', 'setup.done'))
end_time = time.time_ns()
self.use_time = end_time - start_time
self.logger.info(tr.lang('client', 'setup.use_time').format(Decimal(self.use_time) / 1000000000))
self.logger.debug(tr.lang('client', 'setup.use_time_ns').format(self.use_time))
2021-02-20 21:46:14 +08:00
def start(self):
self.window.start_game() # 游戏启动
2021-08-24 22:31:52 +08:00
# TODO 写一下服务端启动相关,还是需要服务端啊
2021-01-11 21:58:51 +08:00
2021-08-13 12:25:29 +08:00
class ClientWindow(pyglet.window.Window):
def __init__(self, net_mode='local', *args, **kwargs):
2021-10-01 23:12:01 +08:00
start_time = time.time_ns()
2021-08-13 12:25:29 +08:00
super().__init__(*args, **kwargs)
2021-02-16 12:51:07 +08:00
"""
2021-02-03 22:04:34 +08:00
:param dev_list: 共享内存
:param dev_dic: 共享内存
:param logger: logger
:param net_mode: 网络模式 # local / ip
2021-02-16 12:51:07 +08:00
"""
2021-01-25 12:22:55 +08:00
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
2021-02-03 22:04:34 +08:00
# value
self.net_mode = net_mode
self.run_input = False
2021-08-24 22:31:52 +08:00
# configs
pyglet.resource.path = ['textures']
pyglet.resource.reindex()
self.config_file = tools.load_file('configs/main.config')
self.game_config = tools.load_file('configs/game.config')
2021-09-24 00:04:56 +08:00
self.FPS = Decimal(int(self.config_file['runtime']['fps']))
self.SPF = Decimal('1') / self.FPS
2020-12-16 06:33:33 +08:00
# dic
2021-09-02 22:47:10 +08:00
self.environment = {}
self.textures = {} # all textures
self.runtime = {}
# FPS
self.max_fps = [self.FPS, time.time()]
self.min_fps = [self.FPS, time.time()]
self.fps_wait = 5
2021-02-20 21:46:14 +08:00
# batch
self.part_batch = pyglet.graphics.Batch()
self.label_batch = pyglet.graphics.Batch()
2021-08-24 22:31:52 +08:00
# frame
self.frame = pyglet.gui.Frame(self)
2021-10-01 23:12:01 +08:00
self.M_frame = pyglet.gui.MovableFrame(self, modifier=key.LCTRL)
2021-02-03 22:04:34 +08:00
# setup
self.setup()
# 命令显示
self.command_label = [pyglet.text.Label(x=10, y=10 + 20 * line,
anchor_x='left', anchor_y='center',
font_name=translate.鸿蒙简体, font_size=12,
batch=self.label_batch)
for line in range(int(self.game_config['command']['show']) + 1)]
# fps显示
self.fps_label = pyglet.text.Label(x=10, y=self.height - 10,
anchor_x='left', anchor_y='top',
font_name=translate.鸿蒙简体, font_size=20,
batch=self.label_batch)
# 设置刷新率
2021-09-24 00:04:56 +08:00
pyglet.clock.schedule_interval(self.update, float(self.SPF))
# 完成设置后的信息输出
2021-10-01 23:12:01 +08:00
self.logger.info(tr.lang('window', 'setup.done'))
self.logger.info(tr.lang('window', 'os.pid_is').format(os.getpid(), os.getppid()))
2021-10-01 23:12:01 +08:00
end_time = time.time_ns()
self.use_time = end_time - start_time
self.logger.info(tr.lang('window', 'setup.use_time').format(Decimal(self.use_time) / 1000000000))
self.logger.debug(tr.lang('window', 'setup.use_time_ns').format(self.use_time))
2021-09-02 22:47:10 +08:00
2020-12-16 06:33:33 +08:00
def setup(self):
2021-10-01 23:12:01 +08:00
self.load_fonts()
# print(pyglet.font.have_font('HarmonyOS_Sans_Black'))
2021-10-01 23:12:01 +08:00
# @new_thread('client load_fonts')
2021-10-01 23:12:01 +08:00
def load_fonts(self):
file_path = './libs/fonts/HarmonyOS Sans/'
ttf_files = os.listdir(file_path)
self.logger.info(tr.lang('window', 'fonts.found').format(ttf_files))
for ttf_file in ttf_files:
pyglet.font.add_directory(f'{file_path}{ttf_file}')
@new_thread('client load_editor')
def load_Editor(self):
pass
2021-02-20 21:46:14 +08:00
def start_game(self) -> None:
self.run_input = True
2021-10-01 23:12:01 +08:00
# self.read_thread = threading.Thread(target=self.read_input, name='client_read_input')
# self.read_thread.start()
# crash.all_thread.append(self.read_thread)
pyglet.app.run()
def read_input(self):
self.logger.debug('read_input start')
while self.run_input:
get = input('<<<')
self.logger.info(get)
if get == 'stop':
self.run_input = False
self.logger.debug('read_input end')
"""
draws and some event
"""
2021-01-29 14:07:40 +08:00
2021-09-02 22:47:10 +08:00
def update(self, tick: float):
2021-10-01 23:12:01 +08:00
decimal_tick = Decimal(tick)
self.FPS_update(decimal_tick)
2021-02-22 21:32:13 +08:00
2021-10-01 23:12:01 +08:00
def FPS_update(self, tick: Decimal):
2021-02-22 12:28:13 +08:00
now_FPS = pyglet.clock.get_fps()
2021-02-22 21:32:13 +08:00
if now_FPS > self.max_fps[0]:
self.max_fps = [now_FPS, time.time()]
elif now_FPS < self.min_fps[0]:
self.min_fps = [now_FPS, time.time()]
else:
if (time.time() - self.max_fps[1]) > self.fps_wait:
self.max_fps = [self.FPS, time.time()]
elif (time.time() - self.min_fps[1]) > self.fps_wait:
self.min_fps = [self.FPS, time.time()]
self.fps_label.text = 'FPS: {:.1f} {:.1f} ({:.1f}/{:.1f}) | MSPF: {:.5f} '.format(now_FPS, 1 / tick, self.max_fps[0], self.min_fps[0], tick)
2021-02-21 11:58:47 +08:00
2020-12-16 06:33:33 +08:00
def on_draw(self):
2021-02-22 21:32:13 +08:00
self.clear()
2021-02-20 21:46:14 +08:00
self.draw_batch()
2021-09-02 22:47:10 +08:00
def on_resize(self, width: int, height: int):
super().on_resize(width, height)
self.fps_label.y = height - 10
2021-09-02 22:47:10 +08:00
2021-02-20 21:46:14 +08:00
def draw_batch(self):
self.part_batch.draw()
self.label_batch.draw()
"""
2021-01-28 22:50:28 +08:00
keyboard and mouse input
"""
2021-01-28 22:50:28 +08:00
2021-08-13 12:25:29 +08:00
def on_mouse_motion(self, x, y, dx, dy) -> None:
2021-04-10 22:48:22 +08:00
# self.logger.debug('按键移动 %s %s %s %s' % (x, y, dx, dy))
pass
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers) -> None:
2021-08-24 22:31:52 +08:00
# self.logger.debug('按键拖拽 %s %s %s %s %s %s' % (x, y, dx, dy, buttons, modifiers))
2021-04-10 22:48:22 +08:00
pass
2021-08-13 12:25:29 +08:00
def on_mouse_press(self, x, y, button, modifiers) -> None:
2021-10-01 23:12:01 +08:00
self.logger.debug(tr.lang('window', 'mouse.press').format([x, y], tr.lang('window', 'mouse.{}'.format(mouse.buttons_string(button)))))
2021-08-13 12:25:29 +08:00
def on_mouse_release(self, x, y, button, modifiers) -> None:
2021-10-01 23:12:01 +08:00
self.logger.debug(tr.lang('window', 'mouse.release').format([x, y], tr.lang('window', 'mouse.{}'.format(mouse.buttons_string(button)))))
2021-08-13 12:25:29 +08:00
def on_key_press(self, symbol, modifiers) -> None:
2021-02-22 23:17:16 +08:00
if symbol == key.ESCAPE and not (modifiers & ~(key.MOD_NUMLOCK |
key.MOD_CAPSLOCK |
key.MOD_SCROLLLOCK)):
self.dispatch_event('on_close')
self.logger.debug(tr.lang('window', 'key.press').format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
def on_key_release(self, symbol, modifiers) -> None:
self.logger.debug(tr.lang('window', 'key.release').format(key.symbol_string(symbol), key.modifiers_string(modifiers)))
def on_text(self, text):
if text == '':
self.logger.debug(tr.lang('window', 'text.new_line'))
else:
self.logger.debug(tr.lang('window', 'text.input').format(text))
2021-08-13 12:25:29 +08:00
def on_close(self) -> None:
self.run_input = False
# self.input_line.join()
2021-08-13 12:25:29 +08:00
config_file = configparser.ConfigParser()
config_file.read('configs/main.config')
config_file['window']['width'] = str(self.width)
config_file['window']['height'] = str(self.height)
config_file.write(open('configs/main.config', 'w', encoding='utf-8'))
super().on_close()