From 515e476657472221184779349acfd992eeaaad03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=91=97=E6=9D=B0?= <3695888@qq.com> Date: Wed, 8 Sep 2021 23:38:34 +0800 Subject: [PATCH] V0.5.2 commit not done --- Difficult_Rocket.py | 11 +- Difficult_Rocket/Game_threads/__init__.py | 11 +- Difficult_Rocket/Game_threads/orbit_demo.py | 11 +- Difficult_Rocket/__init__.py | 8 + Difficult_Rocket/api/Exp.py | 7 + Difficult_Rocket/api/__init__.py | 7 + Difficult_Rocket/api/delivery.py | 11 +- Difficult_Rocket/api/new_thread.py | 7 +- Difficult_Rocket/api/thread.py | 23 ++ Difficult_Rocket/api/tools.py | 10 +- Difficult_Rocket/api/translate.py | 6 + Difficult_Rocket/client.py | 24 +- Difficult_Rocket/configs.py | 11 +- Difficult_Rocket/crash.py | 27 +- Difficult_Rocket/main.py | 13 +- Difficult_Rocket/server.py | 12 +- Difficult_Rocket/unpack_textures.py | 16 +- tests/{config_file.py => config_file_test.py} | 0 tests/{crash.py => crash_test.py} | 0 tests/{decorators.py => decorators_test.py} | 0 tests/{dict_test.py => dict_test_test.py} | 0 tests/{hit_box.py => hit_box_test.py} | 0 tests/{image.py => image_test.py} | 0 tests/{import.py => import_test (2).py} | 0 tests/{import_test.py => import_test_test.py} | 0 tests/{json5.py => json5_test.py} | 0 ...near_algebra.py => linear_algebra_test.py} | 0 .../{logging_conf.py => logging_conf_test.py} | 0 tests/{logging.config => logging_test.config} | 0 tests/{logging.py => logging_test.py} | 0 tests/{mod_tick.py => mod_tick_test.py} | 0 tests/{mutiproser.py => mutiproser_test.py} | 0 tests/openGL_test.py | 352 ++++++++++++++++++ tests/{process.py => process_test.py} | 0 tests/re.py | 5 - tests/{speed.py => speed_test.py} | 0 ..._config.config => test_config_test.config} | 0 tests/{test.py => test_test.py} | 0 tests/{threading.py => threading_test.py} | 0 tests/{tools.py => tools_test.py} | 0 tests/奇怪的东西.py | 70 ++++ 41 files changed, 589 insertions(+), 53 deletions(-) create mode 100644 Difficult_Rocket/api/thread.py rename tests/{config_file.py => config_file_test.py} (100%) rename tests/{crash.py => crash_test.py} (100%) rename tests/{decorators.py => decorators_test.py} (100%) rename tests/{dict_test.py => dict_test_test.py} (100%) rename tests/{hit_box.py => hit_box_test.py} (100%) rename tests/{image.py => image_test.py} (100%) rename tests/{import.py => import_test (2).py} (100%) rename tests/{import_test.py => import_test_test.py} (100%) rename tests/{json5.py => json5_test.py} (100%) rename tests/{linear_algebra.py => linear_algebra_test.py} (100%) rename tests/{logging_conf.py => logging_conf_test.py} (100%) rename tests/{logging.config => logging_test.config} (100%) rename tests/{logging.py => logging_test.py} (100%) rename tests/{mod_tick.py => mod_tick_test.py} (100%) rename tests/{mutiproser.py => mutiproser_test.py} (100%) create mode 100644 tests/openGL_test.py rename tests/{process.py => process_test.py} (100%) delete mode 100644 tests/re.py rename tests/{speed.py => speed_test.py} (100%) rename tests/{test_config.config => test_config_test.config} (100%) rename tests/{test.py => test_test.py} (100%) rename tests/{threading.py => threading_test.py} (100%) rename tests/{tools.py => tools_test.py} (100%) create mode 100644 tests/奇怪的东西.py diff --git a/Difficult_Rocket.py b/Difficult_Rocket.py index 69c2a57..c544efd 100644 --- a/Difficult_Rocket.py +++ b/Difficult_Rocket.py @@ -32,6 +32,7 @@ if __name__ == '__main__': print(hi) try: from Difficult_Rocket import main + from Difficult_Rocket import crash game = main.Game() game.start() @@ -39,13 +40,11 @@ if __name__ == '__main__': print('the game has error , now outputting error message') error = traceback.format_exc() print(error) - from Difficult_Rocket import crash + from Difficult_Rocket.api import thread - crash_thread = threading.Thread(target=crash.create_crash_report, args=(error,), name='Crash report thread') + crash_thread = thread.Threads(target=crash.create_crash_report, args=(error,), name='Crash report thread') crash_thread.start() crash_thread.join() - print(crash_thread.__str__()) else: - for thread in threading.enumerate(): - if thread == threading.main_thread(): continue - print(thread.__str__()) + crash.record_thread = False + print(crash.all_thread) diff --git a/Difficult_Rocket/Game_threads/__init__.py b/Difficult_Rocket/Game_threads/__init__.py index 0b6ee8d..44b906c 100644 --- a/Difficult_Rocket/Game_threads/__init__.py +++ b/Difficult_Rocket/Game_threads/__init__.py @@ -1,7 +1,16 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + # import folders # import in this folder diff --git a/Difficult_Rocket/Game_threads/orbit_demo.py b/Difficult_Rocket/Game_threads/orbit_demo.py index 0ba9695..b20c3a1 100644 --- a/Difficult_Rocket/Game_threads/orbit_demo.py +++ b/Difficult_Rocket/Game_threads/orbit_demo.py @@ -1,8 +1,17 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + import threading diff --git a/Difficult_Rocket/__init__.py b/Difficult_Rocket/__init__.py index ca6a129..c7208f3 100644 --- a/Difficult_Rocket/__init__.py +++ b/Difficult_Rocket/__init__.py @@ -1,3 +1,9 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie mail: 3695888@qq.com @@ -5,6 +11,8 @@ github: @shenjackyuanjie gitee: @shenjackyuanjie """ + + from .api import * __all__ = [ diff --git a/Difficult_Rocket/api/Exp.py b/Difficult_Rocket/api/Exp.py index f959e35..f5d0fa5 100644 --- a/Difficult_Rocket/api/Exp.py +++ b/Difficult_Rocket/api/Exp.py @@ -1,3 +1,9 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie mail: 3695888@qq.com @@ -6,6 +12,7 @@ gitee: @shenjackyuanjie """ + class Error(Exception): """基础 Exception""" pass diff --git a/Difficult_Rocket/api/__init__.py b/Difficult_Rocket/api/__init__.py index 6ac259c..5998488 100644 --- a/Difficult_Rocket/api/__init__.py +++ b/Difficult_Rocket/api/__init__.py @@ -1,3 +1,9 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie mail: 3695888@qq.com @@ -5,6 +11,7 @@ github: @shenjackyuanjie gitee: @shenjackyuanjie """ + from .Exp import TexturesError, LanguageError from .delivery import Delivery from .new_thread import new_thread diff --git a/Difficult_Rocket/api/delivery.py b/Difficult_Rocket/api/delivery.py index b7e04f0..bbad546 100644 --- a/Difficult_Rocket/api/delivery.py +++ b/Difficult_Rocket/api/delivery.py @@ -1,9 +1,18 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + class Delivery: def __init__(self): # bool diff --git a/Difficult_Rocket/api/new_thread.py b/Difficult_Rocket/api/new_thread.py index 928eb7c..842a32f 100644 --- a/Difficult_Rocket/api/new_thread.py +++ b/Difficult_Rocket/api/new_thread.py @@ -3,6 +3,7 @@ import inspect import threading import time from typing import Optional, Callable +from Difficult_Rocket.api import thread """ This part of code come from MCDReforged(https://github.com/Fallen_Breath/MCDReforged) @@ -21,9 +22,9 @@ def new_thread(thread_name: Optional[str or Callable] = None): def wrapper(func): @functools.wraps(func) # to preserve the origin function information def wrap(*args, **kwargs): - thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=thread_name) - thread.setDaemon(False) - thread.start() + thread_ = thread.Threads(target=func, args=args, kwargs=kwargs, name=thread_name) + thread_.setDaemon(False) + thread_.start() return thread # bring the signature of the func to the wrap function diff --git a/Difficult_Rocket/api/thread.py b/Difficult_Rocket/api/thread.py new file mode 100644 index 0000000..aea09d5 --- /dev/null +++ b/Difficult_Rocket/api/thread.py @@ -0,0 +1,23 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + +""" +writen by shenjackyuanjie +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie +""" + +import threading + +from Difficult_Rocket import crash + + +class Threads(threading.Thread): + def run(self) -> None: + if crash.record_thread: + crash.all_thread[self.name] = self + super(Threads, self).run() diff --git a/Difficult_Rocket/api/tools.py b/Difficult_Rocket/api/tools.py index ba8018e..288b898 100644 --- a/Difficult_Rocket/api/tools.py +++ b/Difficult_Rocket/api/tools.py @@ -1,9 +1,17 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + import configparser import decimal import logging diff --git a/Difficult_Rocket/api/translate.py b/Difficult_Rocket/api/translate.py index 1ffbf12..58dec66 100644 --- a/Difficult_Rocket/api/translate.py +++ b/Difficult_Rocket/api/translate.py @@ -1,3 +1,9 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie mail: 3695888@qq.com diff --git a/Difficult_Rocket/client.py b/Difficult_Rocket/client.py index d31aebc..7025c50 100644 --- a/Difficult_Rocket/client.py +++ b/Difficult_Rocket/client.py @@ -1,7 +1,16 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + import configparser import logging import os @@ -12,11 +21,10 @@ if __name__ == '__main__': # been start will not run this sys.path.append('/bin/libs') sys.path.append('/bin') -from Difficult_Rocket.libs import pyglet -from Difficult_Rocket.libs.pyglet.window import key, mouse -from Difficult_Rocket.api import Exp, tools, config -from drag_sprite import DragSprite -from api.new_thread import new_thread +import pyglet +from pyglet.window import key, mouse +from Difficult_Rocket.drag_sprite import DragSprite +from Difficult_Rocket.api import Exp, tools, config, new_thread class Client: @@ -90,17 +98,16 @@ class ClientWindow(pyglet.window.Window): self.label_batch = pyglet.graphics.Batch() # frame self.frame = pyglet.gui.Frame(self) - self.logger.info(self.lang['setup.done']) # setup self.setup() self.info_label = pyglet.text.Label(x=10, y=self.height - 10, anchor_x='left', anchor_y='top', batch=self.label_batch) - self.fps_display = pyglet.window.FPSDisplay(self) pyglet.clock.schedule_interval(self.update, self.SPF) self.times.append(time.time()) self.times.append(self.times[1] - self.times[0]) self.logger.debug(self.times[2]) + self.logger.info(self.lang['setup.done']) @new_thread('client_load_environment') def load_environment(self) -> None: @@ -157,7 +164,6 @@ class ClientWindow(pyglet.window.Window): def draw_batch(self): self.part_batch.draw() self.label_batch.draw() - self.fps_display.draw() """ keyboard and mouse input diff --git a/Difficult_Rocket/configs.py b/Difficult_Rocket/configs.py index 9732f5b..e3f111d 100644 --- a/Difficult_Rocket/configs.py +++ b/Difficult_Rocket/configs.py @@ -1,8 +1,17 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + import decimal import logging # import re diff --git a/Difficult_Rocket/crash.py b/Difficult_Rocket/crash.py index 7c7c8c1..3f372bd 100644 --- a/Difficult_Rocket/crash.py +++ b/Difficult_Rocket/crash.py @@ -1,3 +1,9 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie mail: 3695888@qq.com @@ -5,6 +11,7 @@ github: @shenjackyuanjie gitee: @shenjackyuanjie """ + import os import threading import time @@ -26,22 +33,8 @@ Thread_message = """## Thread info System_message = """## System info """ - -def thread_crash_check() -> None: - """ - Need to run in a new thread - will raise error when any thread raise error - maybe will create a crash report - """ - all_thread = {} - thread_local = threading.local() - for thread in threading.enumerate(): - if thread not in all_thread: - all_thread[thread.name] = thread - for any_thread in all_thread: - if not all_thread[any_thread].is_alive(): - pass - # if all_thread[any_thread].exitcode +all_thread = {threading.main_thread().name: threading.main_thread()} +record_thread = True def crash_info_handler(info: str = None) -> str: @@ -71,7 +64,7 @@ def create_crash_report(info: str = None) -> None: crash_file.write(Head_message) # 开头信息 crash_file.write(crash_info) crash_file.write(Thread_message) - for thread in threading.enumerate(): + for thread in all_thread: crash_file.write(markdown_line_handler(thread.name, code=True)) crash_file.write(markdown_line_handler(f'Ident: {thread.ident}', level=2)) crash_file.write(markdown_line_handler(f'Daemon: {thread.isDaemon()}', level=2)) diff --git a/Difficult_Rocket/main.py b/Difficult_Rocket/main.py index e1501bc..75899e1 100644 --- a/Difficult_Rocket/main.py +++ b/Difficult_Rocket/main.py @@ -1,8 +1,17 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + import logging import os import sys @@ -14,7 +23,7 @@ if __name__ == '__main__': # been start will not run this sys.path.append('/bin') from Difficult_Rocket import client, server -from Difficult_Rocket.libs import pyglet +from libs import pyglet from Difficult_Rocket.api import tools diff --git a/Difficult_Rocket/server.py b/Difficult_Rocket/server.py index bf960ea..f5292d0 100644 --- a/Difficult_Rocket/server.py +++ b/Difficult_Rocket/server.py @@ -1,7 +1,17 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ + + import logging import os import sys diff --git a/Difficult_Rocket/unpack_textures.py b/Difficult_Rocket/unpack_textures.py index 4008df1..75854b1 100644 --- a/Difficult_Rocket/unpack_textures.py +++ b/Difficult_Rocket/unpack_textures.py @@ -1,12 +1,18 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + """ writen by shenjackyuanjie -mail: 3695888@qq.com +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie """ -try: - from bin import tools -except (ModuleNotFoundError, ImportError, ImportWarning): - import tools + +from Difficult_Rocket.api import tools import os import PIL.Image diff --git a/tests/config_file.py b/tests/config_file_test.py similarity index 100% rename from tests/config_file.py rename to tests/config_file_test.py diff --git a/tests/crash.py b/tests/crash_test.py similarity index 100% rename from tests/crash.py rename to tests/crash_test.py diff --git a/tests/decorators.py b/tests/decorators_test.py similarity index 100% rename from tests/decorators.py rename to tests/decorators_test.py diff --git a/tests/dict_test.py b/tests/dict_test_test.py similarity index 100% rename from tests/dict_test.py rename to tests/dict_test_test.py diff --git a/tests/hit_box.py b/tests/hit_box_test.py similarity index 100% rename from tests/hit_box.py rename to tests/hit_box_test.py diff --git a/tests/image.py b/tests/image_test.py similarity index 100% rename from tests/image.py rename to tests/image_test.py diff --git a/tests/import.py b/tests/import_test (2).py similarity index 100% rename from tests/import.py rename to tests/import_test (2).py diff --git a/tests/import_test.py b/tests/import_test_test.py similarity index 100% rename from tests/import_test.py rename to tests/import_test_test.py diff --git a/tests/json5.py b/tests/json5_test.py similarity index 100% rename from tests/json5.py rename to tests/json5_test.py diff --git a/tests/linear_algebra.py b/tests/linear_algebra_test.py similarity index 100% rename from tests/linear_algebra.py rename to tests/linear_algebra_test.py diff --git a/tests/logging_conf.py b/tests/logging_conf_test.py similarity index 100% rename from tests/logging_conf.py rename to tests/logging_conf_test.py diff --git a/tests/logging.config b/tests/logging_test.config similarity index 100% rename from tests/logging.config rename to tests/logging_test.config diff --git a/tests/logging.py b/tests/logging_test.py similarity index 100% rename from tests/logging.py rename to tests/logging_test.py diff --git a/tests/mod_tick.py b/tests/mod_tick_test.py similarity index 100% rename from tests/mod_tick.py rename to tests/mod_tick_test.py diff --git a/tests/mutiproser.py b/tests/mutiproser_test.py similarity index 100% rename from tests/mutiproser.py rename to tests/mutiproser_test.py diff --git a/tests/openGL_test.py b/tests/openGL_test.py new file mode 100644 index 0000000..c7363e8 --- /dev/null +++ b/tests/openGL_test.py @@ -0,0 +1,352 @@ + +from pyglet.gl import * +from pyglet.gl.gl import * +from pyglet.gl.glu import * + +import numpy as np + +IS_PERSPECTIVE = True # 透视投影 + +VIEW = np.array([-0.8, 0.8, -0.8, 0.8, 1.0, 20.0]) # 视景体的left/right/bottom/top/near/far六个面 + +SCALE_K = np.array([1.0, 1.0, 1.0]) # 模型缩放比例 + +EYE = np.array([0.0, 0.0, 2.0]) # 眼睛的位置(默认z轴的正方向) + +LOOK_AT = np.array([0.0, 0.0, 0.0]) # 瞄准方向的参考点(默认在坐标原点) + +EYE_UP = np.array([0.0, 1.0, 0.0]) # 定义对观察者而言的上方(默认y轴的正方向) + +WIN_W, WIN_H = 640, 480 # 保存窗口宽度和高度的变量 + +LEFT_IS_DOWNED = False # 鼠标左键被按下 + +MOUSE_X, MOUSE_Y = 0, 0 # 考察鼠标位移量时保存的起始位置 + +def getposture(): + + global EYE, LOOK_AT + + dist = np.sqrt(np.power((EYE-LOOK_AT), 2).sum()) + + if dist > 0: + + phi = np.arcsin((EYE[1]-LOOK_AT[1])/dist) + + theta = np.arcsin((EYE[0]-LOOK_AT[0])/(dist*np.cos(phi))) + + else: + + phi = 0.0 + + theta = 0.0 + + return dist, phi, theta + +DIST, PHI, THETA = getposture() # 眼睛与观察目标之间的距离、仰角、方位角 + +def init(): + + glClearColor(0.0, 0.0, 0.0, 1.0) # 设置画布背景色。注意:这里必须是4个参数 + + glEnable(GL_DEPTH_TEST) # 开启深度测试,实现遮挡关系 + + glDepthFunc(GL_LEQUAL) # 设置深度测试函数(GL_LEQUAL只是选项之一) + +def draw(): + + global IS_PERSPECTIVE, VIEW + + global EYE, LOOK_AT, EYE_UP + + global SCALE_K + + global WIN_W, WIN_H + + # 清除屏幕及深度缓存 + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + # 设置投影(透视投影) + + glMatrixMode(GL_PROJECTION) + + glLoadIdentity() + + if WIN_W > WIN_H: + + if IS_PERSPECTIVE: + + glFrustum(VIEW[0]*WIN_W/WIN_H, VIEW[1]*WIN_W/WIN_H, VIEW[2], VIEW[3], VIEW[4], VIEW[5]) + + else: + + glOrtho(VIEW[0]*WIN_W/WIN_H, VIEW[1]*WIN_W/WIN_H, VIEW[2], VIEW[3], VIEW[4], VIEW[5]) + + else: + + if IS_PERSPECTIVE: + + glFrustum(VIEW[0], VIEW[1], VIEW[2]*WIN_H/WIN_W, VIEW[3]*WIN_H/WIN_W, VIEW[4], VIEW[5]) + + else: + + glOrtho(VIEW[0], VIEW[1], VIEW[2]*WIN_H/WIN_W, VIEW[3]*WIN_H/WIN_W, VIEW[4], VIEW[5]) + + + # 设置模型视图 + + glMatrixMode(GL_MODELVIEW) + + glLoadIdentity() + + # 几何变换 + + glScaled(SCALE_K[0], SCALE_K[1], SCALE_K[2]) + + # 设置视点 + + gluLookAt( + + EYE[0], EYE[1], EYE[2], + + LOOK_AT[0], LOOK_AT[1], LOOK_AT[2], + + EYE_UP[0], EYE_UP[1], EYE_UP[2] + + ) + + # 设置视口 + + glViewport(0, 0, WIN_W, WIN_H) + + # --------------------------------------------------------------- + glBegin(GL_LINES) # 开始绘制线段(世界坐标系) + # 以红色绘制x轴 + + glColor4f(1.0, 0.0, 0.0, 1.0) # 设置当前颜色为红色不透明 + + glVertex3f(-0.8, 0.0, 0.0) # 设置x轴顶点(x轴负方向) + + glVertex3f(0.8, 0.0, 0.0) # 设置x轴顶点(x轴正方向) + + # 以绿色绘制y轴 + + glColor4f(0.0, 1.0, 0.0, 1.0) # 设置当前颜色为绿色不透明 + + glVertex3f(0.0, -0.8, 0.0) # 设置y轴顶点(y轴负方向) + + glVertex3f(0.0, 0.8, 0.0) # 设置y轴顶点(y轴正方向) + + # 以蓝色绘制z轴 + + glColor4f(0.0, 0.0, 1.0, 1.0) # 设置当前颜色为蓝色不透明 + + glVertex3f(0.0, 0.0, -0.8) # 设置z轴顶点(z轴负方向) + + glVertex3f(0.0, 0.0, 0.8) # 设置z轴顶点(z轴正方向) + + glEnd() # 结束绘制线段 + # --------------------------------------------------------------- + glBegin(GL_TRIANGLES) # 开始绘制三角形(z轴负半区) + + glColor4f(1.0, 0.0, 0.0, 1.0) # 设置当前颜色为红色不透明 + + glVertex3f(-0.5, -0.366, -0.5) # 设置三角形顶点 + + glColor4f(0.0, 1.0, 0.0, 1.0) # 设置当前颜色为绿色不透明 + + glVertex3f(0.5, -0.366, -0.5) # 设置三角形顶点 + + glColor4f(0.0, 0.0, 1.0, 1.0) # 设置当前颜色为蓝色不透明 + + glVertex3f(0.0, 0.5, -0.5) # 设置三角形顶点 + + glEnd() # 结束绘制三角形 + # --------------------------------------------------------------- + glBegin(GL_TRIANGLES) # 开始绘制三角形(z轴正半区) + glColor4f(1.0, 0.0, 0.0, 1.0) # 设置当前颜色为红色不透明 + + glVertex3f(-0.5, 0.5, 0.5) # 设置三角形顶点 + + glColor4f(0.0, 1.0, 0.0, 1.0) # 设置当前颜色为绿色不透明 + + glVertex3f(0.5, 0.5, 0.5) # 设置三角形顶点 + + glColor4f(0.0, 0.0, 1.0, 1.0) # 设置当前颜色为蓝色不透明 + + glVertex3f(0.0, -0.366, 0.5) # 设置三角形顶点 + + glEnd() # 结束绘制三角形 + + # --------------------------------------------------------------- + + # glutSwapBuffers() # 切换缓冲区,以显示绘制内容 + +def reshape(width, height): + + global WIN_W, WIN_H + + WIN_W, WIN_H = width, height + + glutPostRedisplay() + +def mouseclick(button, state, x, y): + + global SCALE_K + + global LEFT_IS_DOWNED + + global MOUSE_X, MOUSE_Y + + MOUSE_X, MOUSE_Y = x, y + + if button == GLUT_LEFT_BUTTON: + + LEFT_IS_DOWNED = state==GLUT_DOWN + + elif button == 3: + + SCALE_K *= 1.05 + + glutPostRedisplay() + + elif button == 4: + + SCALE_K *= 0.95 + + glutPostRedisplay() + +def mousemotion(x, y): + + global LEFT_IS_DOWNED + + global EYE, EYE_UP + + global MOUSE_X, MOUSE_Y + + global DIST, PHI, THETA + + global WIN_W, WIN_H + + if LEFT_IS_DOWNED: + + dx = MOUSE_X - x + + dy = y - MOUSE_Y + + MOUSE_X, MOUSE_Y = x, y + + PHI += 2*np.pi*dy/WIN_H + + PHI %= 2*np.pi + + THETA += 2*np.pi*dx/WIN_W + + THETA %= 2*np.pi + + r = DIST*np.cos(PHI) + + EYE[1] = DIST*np.sin(PHI) + + EYE[0] = r*np.sin(THETA) + + EYE[2] = r*np.cos(THETA) + + if 0.5*np.pi < PHI < 1.5*np.pi: + + EYE_UP[1] = -1.0 + + else: + + EYE_UP[1] = 1.0 + + glutPostRedisplay() + +def keydown(key, x, y): + + global DIST, PHI, THETA + + global EYE, LOOK_AT, EYE_UP + + global IS_PERSPECTIVE, VIEW + + if key in [b'x', b'X', b'y', b'Y', b'z', b'Z']: + + if key == b'x': # 瞄准参考点 x 减小 + + LOOK_AT[0] -= 0.01 + + elif key == b'X': # 瞄准参考 x 增大 + + LOOK_AT[0] += 0.01 + + elif key == b'y': # 瞄准参考点 y 减小 + + LOOK_AT[1] -= 0.01 + + elif key == b'Y': # 瞄准参考点 y 增大 + + LOOK_AT[1] += 0.01 + + elif key == b'z': # 瞄准参考点 z 减小 + + LOOK_AT[2] -= 0.01 + + elif key == b'Z': # 瞄准参考点 z 增大 + + LOOK_AT[2] += 0.01 + + DIST, PHI, THETA = getposture() + + glutPostRedisplay() + + elif key == b'\r': # 回车键,视点前进 + + EYE = LOOK_AT + (EYE - LOOK_AT) * 0.9 + + DIST, PHI, THETA = getposture() + + glutPostRedisplay() + + elif key == b'\x08': # 退格键,视点后退 + + EYE = LOOK_AT + (EYE - LOOK_AT) * 1.1 + + DIST, PHI, THETA = getposture() + + glutPostRedisplay() + + elif key == b' ': # 空格键,切换投影模式 + + IS_PERSPECTIVE = not IS_PERSPECTIVE + + glutPostRedisplay() + +if __name__ == "__main__": + + glutInit() + + displayMode = GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH + + glutInitDisplayMode(displayMode) + + glutInitWindowSize(WIN_W, WIN_H) + + glutInitWindowPosition(300, 200) + + glutCreateWindow('Quidam Of OpenGL') + + init() # 初始化画布 + + glutDisplayFunc(draw) # 注册回调函数draw() + + glutReshapeFunc(reshape) # 注册响应窗口改变的函数reshape() + + glutMouseFunc(mouseclick) # 注册响应鼠标点击的函数mouseclick() + + glutMotionFunc(mousemotion) # 注册响应鼠标拖拽的函数mousemotion() + + glutKeyboardFunc(keydown) # 注册键盘输入的函数keydown() + + glutMainLoop() # 进入glut主循环 \ No newline at end of file diff --git a/tests/process.py b/tests/process_test.py similarity index 100% rename from tests/process.py rename to tests/process_test.py diff --git a/tests/re.py b/tests/re.py deleted file mode 100644 index 57f205c..0000000 --- a/tests/re.py +++ /dev/null @@ -1,5 +0,0 @@ -import re - -ipv4 = re.compile('[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}') - -print(re.match(ipv4, '1.2.3.2')) diff --git a/tests/speed.py b/tests/speed_test.py similarity index 100% rename from tests/speed.py rename to tests/speed_test.py diff --git a/tests/test_config.config b/tests/test_config_test.config similarity index 100% rename from tests/test_config.config rename to tests/test_config_test.config diff --git a/tests/test.py b/tests/test_test.py similarity index 100% rename from tests/test.py rename to tests/test_test.py diff --git a/tests/threading.py b/tests/threading_test.py similarity index 100% rename from tests/threading.py rename to tests/threading_test.py diff --git a/tests/tools.py b/tests/tools_test.py similarity index 100% rename from tests/tools.py rename to tests/tools_test.py diff --git a/tests/奇怪的东西.py b/tests/奇怪的东西.py new file mode 100644 index 0000000..0defabc --- /dev/null +++ b/tests/奇怪的东西.py @@ -0,0 +1,70 @@ +from math import sin, cos, sqrt +import pyglet +from pyglet.gl import * + + +def vec(*args): + return (GLfloat * len(args))(*args) + + +class GameEventHandler(object): # 这里用GameEventHandler把事件包装了一下 + rx, ry = 0, 0 + track = [] + + def on_draw(self): + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + # glLoadIdentity() + position = (sin(integral_drift[0] / 2) / sqrt(integral_drift[0]), + cos(integral_drift[1] / 2) / sqrt(integral_drift[1])) + self.track.append(position) + + + glColor3f(1, 1, 1) + glBegin(GL_LINE_STRIP) + for v in self.track: + glVertex3f(v[0], v[1], 0) + glEnd() + + @staticmethod + def on_resize(width, height): + return pyglet.event.EVENT_HANDLED + + +def scene_init(): + # One-time GL setup + glClearColor(0, 0, 0, 1) + glEnable(GL_DEPTH_TEST) + glEnable(GL_CULL_FACE) + glEnable(GL_BLEND) # 启用混合功能,将图形颜色同周围颜色相混合 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) + + glEnable(GL_POLYGON_SMOOTH) # 多边形抗锯齿 + # glHint(GL_POLYGON_SMOOTH, GL_NICEST) + + glEnable(GL_LINE_SMOOTH) # 线抗锯齿 + # glHint(GL_LINE_SMOOTH_HINT, GL_NICEST) + + glEnable(GL_POINT_SMOOTH) # 点抗锯齿 + # glHint(GL_POINT_SMOOTH, GL_NICEST) + + pass + + +def update(dt): + delta_x = 5 * dt + delta_y = 5 * dt + delta_z = 5 * dt + integral_drift[0] += delta_x + integral_drift[1] += delta_y + integral_drift[2] += delta_z + pass + + +window = pyglet.window.Window(resizable=True) +scene_init() +integral_drift = [0.1, 0.1, 0.1] # 位移的总和 +game_event = GameEventHandler() +window.push_handlers(game_event.on_draw) +pyglet.app.event_loop.clock.schedule(update) # 单位时间触发update +pyglet.app.run() \ No newline at end of file