简体   繁体   English

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

[英]Out of memory when using pygame.transform.rotate

I wrote a script that allows a user to control the sprite of an eagle to fly around to learn pygame. 我编写了一个脚本,该脚本允许用户控制鹰的精灵来飞来学习pygame。 It seemed fine until i implemented a rotation function that makes the sprite rotate according to the direction it is flying along. 直到我实现了使精灵根据其飞行方向旋转的旋转函数,这似乎还不错。 The sprite becomes really fuzzy after moving a short while and an error soon pops up saying out of memory (at this line: eagle_img = pygame.transform.rotate(eagle_img,new_angle-angle) ) 短暂移动后,精灵会变得非常模糊,并很快弹出一个错误,指出内存不足 (在此行: eagle_img = pygame.transform.rotate(eagle_img,new_angle-angle)

My code: 我的代码:

# 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)

Please tell me what's wrong here and how i can improve it. 请告诉我这里有什么问题,以及我该如何改进。 Feel free to try the code out using a sprite named "eagle1.png". 随时使用名为“ eagle1.png”的精灵尝试代码。 Thanks! 谢谢!

the reason your eagle picture is becoming blurred is because your continually rotating the same png and not making a copy and rotating that copy while always keeping a non edited picture. 老鹰图片变得模糊的原因是因为您不断旋转相同的png而不复制并旋转该副本,同时始终保留未编辑的图片。 here is a function i have always used when rotating square images 这是旋转方形图像时我一直使用的功能

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

here is one for any shape of picture 这是任何形状的图片

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

source from here 这里来

as for the "out of memory" error i don't know for sure but i assume the leak is being caused by the rotation of the same png file over and over again. 至于“内存不足”错误,我不确定,但我认为泄漏是由相同png文件一遍又一遍的旋转引起的。 From some research that seems to occur often with other people. 从一些研究看来,这似乎经常发生在其他人身上。

hope this helps clear some things up:) 希望这有助于清理一些东西:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM