使用 pygame.transform.rotate 时内存不足
Out of memory when using pygame.transform.rotate
我写了一个脚本,允许用户控制老鹰的精灵飞来飞去学习 pygame。直到我实现了一个旋转功能,使精灵根据它飞行的方向旋转,它看起来很好。精灵在移动了一会儿后变得非常模糊,很快就会弹出一个错误,说 内存不足(在这一行: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" 的 sprite 尝试代码。谢谢!
你的老鹰图片变得模糊的原因是因为你不断旋转相同的 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
来自 here
至于 "out of memory" 错误,我不确定,但我认为泄漏是由同一 png 文件反复旋转引起的。来自一些似乎经常发生在其他人身上的研究。
希望这有助于澄清一些事情:)
我写了一个脚本,允许用户控制老鹰的精灵飞来飞去学习 pygame。直到我实现了一个旋转功能,使精灵根据它飞行的方向旋转,它看起来很好。精灵在移动了一会儿后变得非常模糊,很快就会弹出一个错误,说 内存不足(在这一行: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" 的 sprite 尝试代码。谢谢!
你的老鹰图片变得模糊的原因是因为你不断旋转相同的 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
来自 here
至于 "out of memory" 错误,我不确定,但我认为泄漏是由同一 png 文件反复旋转引起的。来自一些似乎经常发生在其他人身上的研究。
希望这有助于澄清一些事情:)