Python 3.2 与 Python 2.7 代码问题

标签 python python-2.7 python-3.x pygame

我有一个代码可以使用 pygame 绘制 mandlebrot 集。这是代码。

import pygame, sys, math
from decimal import *
window=pygame.display.set_mode((1000, 1000))
window.fill((255, 255, 255))
pygame.display.update()
winrect=window.get_rect()
hq=3
getcontext().prec=20
colors=((255, 0, 0), (255, 128, 0), (255, 255, 0), (128, 255, 0), (0, 255, 0), (0, 255, 128), (0, 255, 255), (0, 128, 255), (0, 0, 255), (128, 0, 255), (255, 0, 255), (255, 0, 128))
def graph(scale):#left, right, bottom, top
    window.fill((0, 0, 0))
    minimum=-1
    y=((scale[3]-scale[2]))/(winrect.height)+scale[2]
    for a in range(winrect.width):
        x=((scale[1]-scale[0])*(a))/(winrect.width)+scale[0]
        d, e=x**2-y**2+x, 2*x*y+y
        for i in range(int(1/(50*(scale[1]-scale[0]))+25)):
            d, e=d**2-e**2+x, 2*d*e+y
            if math.sqrt(d**2+e**2)>2:
                if i<minimum or minimum==-1:
                    minimum=i
                break
    y=((scale[3]-scale[2])*winrect.height)/(winrect.height)+scale[2]
    for a in range(winrect.width):
        x=((scale[1]-scale[0])*a)/winrect.width+scale[0]
        d, e=x**2-y**2+x, 2*x*y+y
        for i in range(int(1/(50*(scale[1]-scale[0]))+25)):
            d, e=d**2-e**2+x, 2*d*e+y
            if math.sqrt(d**2+e**2)>2:
                if i<minimum or minimum==-1:
                    minimum=i
                break
    x=((scale[1]-scale[0])*1)/winrect.width+scale[0]
    for b in range(winrect.height):
        y=((scale[3]-scale[2])*b)/winrect.height+scale[2]
        d, e=x**2-y**2+x, 2*x*y+y
        for i in range(int(1/(50*(scale[1]-scale[0]))+25)):
            d, e=d**2-e**2+x, 2*d*e+y
            if math.sqrt(d**2+e**2)>2:
                if i<minimum or minimum==-1:
                    minimum=i
                break
    x=((scale[1]-scale[0])*winrect.width)/winrect.width+scale[0]
    for b in range(winrect.height):
        y=((scale[3]-scale[2])*b)/winrect.height+scale[2]
        d, e=x**2-y**2+x, 2*x*y+y
        for i in range(int(1/(50*(scale[1]-scale[0]))+25)):
            d, e=d**2-e**2+x, 2*d*e+y
            if math.sqrt(d**2+e**2)>2:
                if i<minimum or minimum==-1:
                    minimum=i
                break
    for a in range(winrect.width):
        for b in range(winrect.height):
            x=((scale[1]-scale[0])*a)/winrect.width+scale[0]
            y=((scale[3]-scale[2])*b)/winrect.height+scale[2]
            d, e=x**2-y**2+x, 2*x*y+y
            for i in range(minimum):
                d, e=d**2-e**2+x, 2*d*e+y
            for i in range(20*hq):
                d, e=d**2-e**2+x, 2*d*e+y
                if math.sqrt(d**2+e**2)>2:
                    window.set_at((a, b), colors[i-(int(i/len(colors))*len(colors))])
                    break
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()
        pygame.display.update()
    pygame.display.update()
graph([-3, 2, -2.5, 2.5, 0])#
scale=[-3, 2, -2.5, 2.5, 0]
scalea=scale[:]
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key==pygame.K_r:
                graph([-3, 2, -2.5, 2.5, 0])
                scale=[-3, 2, -2.5, 2.5, 0]
                scalea=scale[:]
            if event.key==pygame.K_p:
                hq+=1
                graph(scale)
            if event.key==pygame.K_o:
                if not hq==1:
                    hq-=1
                    graph(scale)
            if event.key==pygame.K_SPACE:
                print(scale)
                print(scale[1]-scale[0])
        if event.type==pygame.MOUSEBUTTONDOWN:
            if not scalea[4]:
                scalea[0]=(((scale[1]-scale[0])*event.pos[0])/winrect.width)+scale[0]
                scalea[2]=(((scale[3]-scale[2])*event.pos[1])/winrect.height)+scale[2]
                scalea[4]=1
            else:
                scalea[1]=(((scale[1]-scale[0])*event.pos[0])/winrect.width)+scale[0]
                scalea[3]=(((scale[3]-scale[2])*event.pos[1])/winrect.height)+scale[2]
                scalea[4]=0
                if scalea[1]<scalea[0]:
                    scalea=[scalea[1], scalea[0], scalea[2], scalea[3], 0]
                if scalea[3]<scalea[2]:
                    scalea=[scalea[0], scalea[1], scalea[3], scalea[2], 0]
                scale=scalea[:]
                if scale[1]-scale[0]<scale[3]-scale[2]:
                    scale[1]+=((scalea[3]-scalea[2])-(scalea[1]-scalea[0]))/2
                    scale[0]-=((scalea[3]-scalea[2])-(scalea[1]-scalea[0]))/2
                else:
                    scale[2]-=((scalea[1]-scalea[0])-(scalea[3]-scalea[2]))/2
                    scale[3]+=((scalea[1]-scalea[0])-(scalea[3]-scalea[2]))/2
                graph(scale)

当我在 python 3.2 中运行它时,它工作正常。图像看起来像这样:

[IMG]http://i47.tinypic.com/2ps0d8n.jpg[/IMG]

但是,当我在 python 2.7 中运行它时,我得到一个看起来很糟糕的图像,如下所示:

[IMG]http://i47.tinypic.com/2a0nskk.jpg[/IMG]

有什么办法可以解决这个问题吗?

最佳答案

是的,将其添加到文件顶部:

from __future__ import division

Python 2 在默认情况下使用整数输入时使用整数除法(底除法);即使在使用整数输入时,Python 3 也切换到浮点除法。

参见 PEP 238 ,它记录了变化:

The current division (/) operator has an ambiguous meaning for numerical arguments: it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex. This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.

关于Python 3.2 与 Python 2.7 代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16085679/

相关文章:

python - 通过django url传递2个变量

传递了附加参数的 Python argparse 自定义操作

python - 为什么 Python 的线程模块中有两种具有相同功能的方法?

python - PyQt6在QSlider的paintEvent中设置自定义矩形

python-3.x - 拆开 pandas 数据框

python - 使用 Matplotlib 和 PyQt 进行动态绘图 - 卡住窗口

python - 重 I/O 和 python 多处理/多线程

python - .decode ('utf-8' ).upper() 和特殊字符的问题(但仅限于字符串内部)

python - 优化给定移位的数组距离的成对计算

python - 与内置指纹传感器 Python 接口(interface)