Python 使用pygame.transform.rotate时内存不足

Python 使用pygame.transform.rotate时内存不足,python,rotation,pygame,sprite,Python,Rotation,Pygame,Sprite,我写了一个脚本,允许用户控制一只鹰的精灵四处飞行来学习游戏。直到我实现了一个旋转函数,使精灵按照它飞行的方向旋转,这一切看起来都很好。精灵在移动了一小段时间后变得非常模糊,很快就会弹出一个错误,说内存不足(在这行:鹰\u img=pygame.transform.rotate(鹰\u img,新角度)) 我的代码: # eagle movement script import pygame, math, sys from pygame.locals import * pygame.init()

我写了一个脚本,允许用户控制一只鹰的精灵四处飞行来学习游戏。直到我实现了一个旋转函数,使精灵按照它飞行的方向旋转,这一切看起来都很好。精灵在移动了一小段时间后变得非常模糊,很快就会弹出一个错误,说内存不足(在这行:鹰\u img=pygame.transform.rotate(鹰\u img,新角度)

我的代码:

# eagle movement script
import pygame, math, sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()

# terminate function
def terminate():
    pygame.quit()
    sys.exit()

# incircle function, check if mouse click is inside controller
def incircle(coordinates,cir_center,cir_out_rad):
    if math.sqrt((coordinates[0]-cir_center[0])**2+\
                 (coordinates[1]-cir_center[1])**2) <= cir_out_rad:
        return True
    return False

# speed function, translates the controller movement into eagle movement
def speed(position,cir_center,eagle_speed):
    x_dist = position[0] - cir_center[0]
    y_dist = position[1] - cir_center[1]
    dist = math.sqrt(x_dist**2+y_dist**2)   # distance from controller knob to center
    if dist != 0:
        return [(x_dist/dist)*eagle_speed,(y_dist/dist)*eagle_speed]
    else:
        return [0,0]

# rotation function, rotates the eagle image
def rotation(position,cir_center):
    x_dist = position[0] - cir_center[0]
    y_dist = position[1] - cir_center[1]
    new_radian = math.atan2(-y_dist,x_dist)
    new_radian %= 2*math.pi
    new_angle = math.degrees(new_radian)
    return new_angle

# screen
screenw = 1000
screenh = 700
screen = pygame.display.set_mode((screenw,screenh),0,32)
pygame.display.set_caption('eagle movement')

# variables
green = (0,200,0)
grey = (100,100,100)
red = (255,0,0)
fps = 60

# controller
cir_out_rad = 150    # circle controller outer radius
cir_in_rad = 30     # circle controller inner radius
cir_center = [screenw-cir_out_rad,int(screenh/2)]
position = cir_center    # mouse position

# eagle
eaglew = 100
eagleh = 60
eagle_speed = 3
eagle_pos = [screenw/2-eaglew/2,screenh/2-eagleh/2]
eagle = pygame.Rect(eagle_pos[0],eagle_pos[1],eaglew,eagleh)
eagle_img = pygame.image.load('eagle1.png').convert()
eagle_bg_colour = eagle_img.get_at((0,0))
eagle_img.set_colorkey(eagle_bg_colour)
eagle_img = pygame.transform.scale(eagle_img,(eaglew,eagleh))

# eagle controls
stop_moving = False # becomes True when player stops clicking
rotate = False      # becomes True when there is input
angle = 90          # eagle is 90 degrees in the beginning

# game loop
while True:
    # controls
    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()
        if event.type == MOUSEBUTTONUP:
            stop_moving = True
            rotate = False

    mouse_input = pygame.mouse.get_pressed()
    if mouse_input[0]:
        coordinates = pygame.mouse.get_pos()    # check if coordinates is inside controller
        if incircle(coordinates,cir_center,cir_out_rad):
            position = pygame.mouse.get_pos()
            stop_moving = False
            rotate = True
        else:
            cir_center = pygame.mouse.get_pos()
            stop_moving = False
            rotate = True
    key_input = pygame.key.get_pressed()
    if key_input[K_ESCAPE] or key_input[ord('q')]:
        terminate()

    screen.fill(green)

    [dx,dy] = speed(position,cir_center,eagle_speed)
    if stop_moving:
        [dx,dy] = [0,0]
    if eagle.left > 0:
        eagle.left += dx
    if eagle.right < screenw:
        eagle.right += dx
    if eagle.top > 0:
        eagle.top += dy
    if eagle.bottom < screenh:
        eagle.bottom += dy

    if rotate:
        new_angle = rotation(position,cir_center)
        if new_angle != angle:
            eagle_img = pygame.transform.rotate(eagle_img,new_angle-angle)
            eagle = eagle_img.get_rect(center=eagle.center)
        rotate = False
        angle = new_angle

    outer_circle = pygame.draw.circle(screen,grey,(cir_center[0],cir_center[1]),\
                                  cir_out_rad,3)
    inner_circle = pygame.draw.circle(screen,grey,(position[0],position[1]),\
                                  cir_in_rad,1)
    screen.blit(eagle_img,eagle)
    pygame.display.update()
    clock.tick(fps)
#鹰的运动脚本
导入pygame、数学、系统
从pygame.locals导入*
pygame.init()
clock=pygame.time.clock()
#终止函数
def terminate():
pygame.quit()
sys.exit()
#在循环功能中,检查鼠标点击是否在控制器内
def incircle(坐标、cir_中心、cir_out_弧度):
如果数学sqrt((坐标[0]-cir_中心[0])**2+\
(坐标[1]-cir_中心[1])**2)0:
eagle.left+=dx
如果eagle.right<屏幕W:
eagle.right+=dx
如果eagle.top>0:
eagle.top+=dy
如果eagle.bottom<屏幕H:
eagle.bottom+=dy
如果旋转:
新角度=旋转(位置、cir\U中心)
如果是新角度!=角度:
eagle\u img=pygame.transform.rotate(eagle\u img,new\u angle-angle)
eagle=eagle\u img.get\u rect(中心=eagle.center)
旋转=假
角度=新角度
外圆=pygame.draw.circle(屏幕,灰色,(循环中心[0],循环中心[1])\
cir_out_rad,3)
内圆=pygame.draw.circle(屏幕,灰色,(位置[0],位置[1])\
cir_in_rad,1)
屏幕。blit(鹰牌,鹰牌)
pygame.display.update()
时钟滴答声(fps)

请告诉我这里出了什么问题,以及如何改进。您可以使用名为“eagle1.png”的精灵来尝试代码。谢谢

您的eagle图片变得模糊的原因是您不断旋转同一个png,而不是复制和旋转该副本,同时始终保留未编辑的图片。这是我在旋转方形图像时经常使用的一个函数

def rot_center(image, angle):
    """rotate an image while keeping its center and size"""
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image
这里有一张适合任何形状的图片

def rot_center(image, rect, angle):
        """rotate an image while keeping its center"""
        rot_image = pygame.transform.rotate(image, angle)
        rot_rect = rot_image.get_rect(center=rect.center)
        return rot_image,rot_rect
来源于

至于“内存不足”错误,我不确定,但我假设泄漏是由同一个png文件反复旋转引起的。从一些似乎经常发生在其他人身上的研究中


希望这有助于澄清一些问题:)

哇,这个解决方案似乎可以解决图像模糊和内存不足的问题,谢谢D