使用 pygame.transform.rotate 时内存不足 [英] Out of memory when using pygame.transform.rotate

查看:108
本文介绍了使用 pygame.transform.rotate 时内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本,允许用户控制鹰的精灵飞来飞去学习pygame.看起来很好,直到我实现了一个旋转功能,使精灵根据它飞行的方向旋转.移动一小会后,精灵变得非常模糊,很快就会弹出一个错误,说内存不足(在这一行:eagle_img = pygame.transform.rotate(eagle_img,new_angle-angle))

I wrote a script that allows a user to control the sprite of an eagle to fly around to learn 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 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)

请告诉我这里有什么问题以及我如何改进它.随意使用名为eagle1.png"的精灵尝试代码.谢谢!

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". Thanks!

推荐答案

您的老鹰图片变得模糊的原因是因为您不断旋转相同的 png,而不是制作副本并旋转该副本,同时始终保留未编辑的图片.这是我在旋转方形图像时一直使用的功能

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. 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

这里是任何形状的图片

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 文件一遍又一遍地旋转引起的.来自一些似乎经常与其他人一起发生的研究.

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. From some research that seems to occur often with other people.

希望这有助于澄清一些事情:)

hope this helps clear some things up:)

这篇关于使用 pygame.transform.rotate 时内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆