在使用字典定义巴恩斯利蕨类植物的参数时遇到问题

2024-09-30 08:21:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用Python和Pygame对Barnsley Fern进行了呈现,并希望在字典中包含所有参数以便于访问,但这不起作用。你知道吗

原来的代码,我已经写了预期的作品。在生成Barnsley蕨类植物的过程中,我尝试使用一个带有键'a'、'b'、'c'、'd'、'e'、'f'和'p'的字典作为所有参数,如https://en.wikipedia.org/wiki/Barnsley_fern所示。你知道吗

当我运行代码时,窗口冻结并且:

Traceback (most recent call last):  
  File "C:/Users/owner/Desktop/Will's Stuff/Python/Pygame/Bransley'sFernDictionary.py", line 105, in <module>
    DrawPoint(fernx,ferny)  
  File "C:/Users/owner/Desktop/Will's Stuff/Python/Pygame/Bransley'sFernDictionary.py", line 47, in DrawPoint
    pygame.gfxdraw.pixel(screen,cartesianx,cartesiany,white)
OverflowError: signed short integer is less than minimum

我预期的是维基百科文章中所示的Bransley蕨类植物,它是在没有字典的情况下在代码中创建的,但是在绘制了一些像素之后窗口就崩溃了。你知道吗

import pygame  
import random  
import pygame.gfxdraw  
import math  
import sys  
pygame.init()  

def GeneralAlgorithm(a,b,c,d,e,f,x,y):  
    outputx = a*x + b*y + e  
    outputy = c*x + d*y + f  
    return outputx,outputy  

def GenerateNextPoint(x,y,constants):  
    randompercentage = random.randint(0,100)  
    for n in constants['p']:  
        if randompercentage <= n:  
            i = constants['p'].index(n)  
            print(i)  
            break  
    nextx, nexty = GeneralAlgorithm(constants['a'][i],constants['b'][i],constants['c'][i],constants['d'][i],constants['e'][i],constants['f'][i],x,y)  
    return nextx, nexty  

def DrawPoint(fernx,ferny):  
    cartesianx = int((fernx+2.1820+margin) * (width /(2.1820+2.6558+2*margin)))  
    cartesiany = int((ferny*-1+9.9983+margin) * (height/(9.9983+2*margin)))  
    pygame.gfxdraw.pixel(screen,cartesianx,cartesiany,white)  
    pygame.display.update()  


white = 255,255,255  
background = 20,40,100  

variables1 = {  
    'a':[0   ,0.85 ,0.2  ,-0.15],  
    'b':[0   ,0.04 ,-0.26,0.28 ],  
    'c':[0   ,-0.04,0,23 ,0.26 ],  
    'd':[0.16,0.85 ,0.22 ,0.24 ],  
    'e':[0   ,0    ,0    ,0    ],  
    'f':[0   ,1.6  ,1.6  ,0.44 ],  
    'p':[1   ,86   ,93   ,100  ]  
}  

fernx = 0  
ferny = 0  
width = int(500)  
height = int(500)  
margin = 0.5  

screen = pygame.display.set_mode((width,height))  
screen.fill(background)  
pygame.display.update()  

while True:  

    fernx,ferny = GenerateNextPoint(fernx,ferny,variables1)
    DrawPoint(fernx,ferny)

Tags: 代码inmarginimport字典screenpygameint
1条回答
网友
1楼 · 发布于 2024-09-30 08:21:22

天哪,我是个傻瓜。你知道吗

variables1 = {  
    'a':[0   ,0.85 ,0.2  ,-0.15],  
    'b':[0   ,0.04 ,-0.26,0.28 ],  
    'c':[0   ,-0.04,0,23 ,0.26 ],  
    'd':[0.16,0.85 ,0.22 ,0.24 ],  
    'e':[0   ,0    ,0    ,0    ],  
    'f':[0   ,1.6  ,1.6  ,0.44 ],  
    'p':[1   ,86   ,93   ,100  ]  
}  

仔细看“c”

    'c':[0   ,-0.04,0,23 ,0.26 ],

在应该放句点的地方,我用了逗号。你知道吗

只是改变了我的算法。你知道吗

variables1 = {  
    'a':[0   ,0.85 ,0.2  ,-0.15],  
    'b':[0   ,0.04 ,-0.26,0.28 ],  
    'c':[0   ,-0.04,0.23 ,0.26 ],  
    'd':[0.16,0.85 ,0.22 ,0.24 ],  
    'e':[0   ,0    ,0    ,0    ],  
    'f':[0   ,1.6  ,1.6  ,0.44 ],  
    'p':[1   ,86   ,93   ,100  ]  
}  

答案很简单。你知道吗

FML公司

相关问题 更多 >

    热门问题