尝试模拟洛伦兹吸引子时出错

问题描述

这是我从 YouTube 视频中得到的: 唯一的问题是,当我运行代码时,会弹出一个窗口,然后立即关闭,然后在终端中显示一条消息:进程已完成,退出代码为 0。 有人可以帮忙吗? 这是从 QuantativeBytes 的视频中截取的,在 YouTube 上搜索 QuantativeBytes。

代码

    import pygame


class LAttractor:

    def __init__(self):
        self.xMin,self.xMax = -30,30
        self.yMin,self.yMax = -30,30
        self.zMin,self.zMax = 0,50
        self.X,self.Y,self.Z = 0.1,0.0,0.0
        self.oX,self.oY,self.oZ = self.X,self.Z
        self.dt = 0.01
        self.a,self.b,self.c = 10,28,8 / 3
        self.pixelColour = (235,192,52)

    def step(self):
        self.oX,self.Z
        self.X = self.X + (self.dt * self.a * (self.Y - self.X))
        self.Y = self.Y + (self.dt * (self.X * (self.b - self.Z) - self.Y))
        self.Z = self.Z + (self.dt * (self.X * self.Y - self.c * self.Z))

    def draw(self,displaySurface):
        width,height = displaySurface.get_size()
        oldPos = self.ConvertToScreen(self.oX,self.xMin,self.xMax,self.yMin,self.yMax,width,height)
        newPos = self.ConvertToScreen(self.X,height)

        # Draw the active line segment:
        newRect = pygame.draw.line(displaySurface,self.pixelColour,oldPos,newPos,2)

        # Return the bounding rect:
        return newRect

    def ConvertToScreen(self,x,y,xMin,xMax,yMin,yMax,height):
        newX = width * ((x - xMin) / (xMax - width))
        newY = height * ((y - yMin) / (yMax - height))
        return round(newX),round(newY)


class Application:
    def __init__(self):
        self.isRunning = True
        self.displaySurface = None
        self.fpsClock = None
        self.attractors = []
        self.size = self.width,self.height = 1920,1080
        self.count = 0
        self.outputCount = 1

    def on_init(self):
        pygame.init()
        pygame.display.set_caption("Lorenz Attractor Simulation")
        self.displaySurface = pygame.display.set_mode(self.size)
        self.isRunning = True
        self.fpsClock = pygame.time.Clock()

        # Configure the attractor
        self.attractors = LAttractor()

    def on_event(self,event):
        if event.type == pygame.QUIT:
            self.isRunning = False

    def on_loop(self):
        # Call the step method for the attractor
        self.attractors.step()

    def on_render(self):
        # Draw the attractor
        newRect = self.attractors.draw(self.displaySurface)
        pygame.display.update(newRect)

    def on_execute(self):
        if self.on_init() == False:
            self.isRunning = False

            while self.isRunning:
                for event in pygame.event.get():
                    self.on_event(event)

                self.on_loop()
                self.on_render()

                self.fpsClock.tick()
                self.count += 1

            pygame.quit()


if __name__ == "__main__":
    q = Application()
    q.on_execute()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)