使用easygraphics的RENDER_MODE_AUTO时仅显示空白屏幕求助
问题分析与解决方案
核心问题定位
RENDER_MODE_AUTO无显示原因
- 你使用
time.sleep()阻塞主线程,破坏了easygraphics自动渲染依赖的事件循环,导致渲染线程无法刷新画面。 rotateVector函数参数错误:math.cos()/math.sin()需要传入弧度值,但你传入了转成度数的结果,导致分支旋转角度异常,可能绘制到屏幕外。
- 你使用
RENDER_MODE_MANUAL之前的问题
- 未掌握手动渲染的核心流程:清屏→绘制→刷新缓冲区→控制帧率,且误用
time.sleep()代替库自带的延迟函数。
- 未掌握手动渲染的核心流程:清屏→绘制→刷新缓冲区→控制帧率,且误用
修复后的代码(自动渲染模式)
branch.py修正
from easygraphics import * import numpy as np import math sizeMult = 0.67 def rotateVector(vector, radians): # 参数改为弧度 newVector = np.empty(2) newVector[0] = math.cos(radians) * vector[0] - math.sin(radians) * vector[1] newVector[1] = math.sin(radians) * vector[0] + math.cos(radians) * vector[1] return newVector class Branch: def __init__(self, x1, y1, x2, y2): self.x1 = x1 self.x2 = x2 self.y1 = y1 self.y2 = y2 def draw(self): line(self.x1, self.y1, self.x2, self.y2) def branchR(self): vec = np.array([self.x2, self.y2]) - np.array([self.x1, self.y1]) vec = rotateVector(vec, math.pi / 4) # 直接传入弧度值 vec *= sizeMult newEnd = np.array([self.x2, self.y2]) + vec return Branch(self.x2, self.y2, newEnd[0], newEnd[1]) def branchL(self): vec = np.array([self.x2, self.y2]) - np.array([self.x1, self.y1]) vec = rotateVector(vec, -math.pi / 4) # 直接传入弧度值 vec *= sizeMult newEnd = np.array([self.x2, self.y2]) + vec return Branch(self.x2, self.y2, newEnd[0], newEnd[1])
main.py修正
from easygraphics import * import math from branch import Branch width = 1280 height = 720 length = 150 tree = [] def mainloop(): n = 0 # 初始化根分支 a = [width / 2, height] b = [width / 2, height - length] tree.append(Branch(a[0], a[1], b[0], b[1])) while is_run(): if n < 5: delay_ms(2000) # 用库自带延迟替代time.sleep,不阻塞渲染 brancher() elif n == 5: delay_ms(100000) n += 1 # 绘制所有分支(自动模式会自动刷新画面) for branch in tree: branch.draw() def brancher(): new_branches = [] for branch in tree[:]: new_branches.append(branch.branchR()) new_branches.append(branch.branchL()) tree.extend(new_branches) def main(): init_graph(width, height) set_background_color("#333333") set_color("#ffffff") set_line_width(2) set_render_mode(RenderMode.RENDER_AUTO) mainloop() close_graph() easy_run(main)
RENDER_MODE_MANUAL 完整使用指南
手动模式需要完全控制渲染流程,步骤如下:
- 每帧开始调用
clear_device()清除缓冲区(如需保留历史绘制可跳过) - 执行所有绘制操作
- 调用
flush()将缓冲区内容显示到屏幕 - 用
delay_fps()控制帧率,避免CPU占用过高
手动模式示例代码(main.py)
from easygraphics import * import math from branch import Branch width = 1280 height = 720 length = 150 tree = [] def mainloop(): n = 0 a = [width / 2, height] b = [width / 2, height - length] tree.append(Branch(a[0], a[1], b[0], b[1])) while is_run(): # 1. 清屏(用预设背景色填充) clear_device() if n < 5: delay_ms(2000) brancher() elif n == 5: delay_ms(100000) n += 1 # 2. 绘制所有分支 for branch in tree: branch.draw() # 3. 刷新缓冲区到屏幕 flush() # 4. 控制帧率(每秒30帧) delay_fps(30) def brancher(): new_branches = [] for branch in tree[:]: new_branches.append(branch.branchR()) new_branches.append(branch.branchL()) tree.extend(new_branches) def main(): init_graph(width, height) set_background_color("#333333") set_color("#ffffff") set_line_width(2) set_render_mode(RenderMode.RENDER_MANUAL) mainloop() close_graph() easy_run(main)
关键注意事项
- 永远不要在easygraphics主循环中使用
time.sleep(),必须用delay_ms()或delay_fps(),避免阻塞事件循环。 - 手动模式下,
flush()是必调用的,否则绘制内容不会显示到屏幕。 - 若需保留历史绘制(比如逐步生长的树),可跳过
clear_device(),直接绘制新内容后调用flush()刷新。
内容的提问来源于stack exchange,提问作者Pascal Emanuel
相关产品推荐
相关产品推荐

