iiiiiam_liuli
2022-06-27 01:36
采纳率: 0%
浏览 19

pygame图像旋转问题

问题遇到的现象和发生背景

pygame图像旋转为什么会报错导致我内存爆炸
进行拐弯的时候

问题相关代码,请勿粘贴截图
    new_robot.image = pygame.transform.rotate(self.image, self.angle)

然后拐弯的时候就会爆炸
这是次级函数
import pygame
from pygame.sprite import Sprite
import math

class Robot(Sprite):

def __init__(self, screen):
    # initialize robot and its location
    self.screen = screen
    # load image and get rectangle
    self.image = pygame.image.load('images/robot.png').convert_alpha()
    self.image = pygame.transform.scale(self.image, (100, 100))
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()
    # put sweeper on the center of window
    self.rect.center = self.screen_rect.center
    self.angle = 90
    self.moving_speed = [5, 5]
    self.moving_pos = [self.rect.centerx, self.rect.centery]
    self.moving_right = False
    self.moving_left = False



def blitme(self):
    # buld the sweeper at the specific location
    self.screen.blit(self.image, self.rect)
def update(self,new_robot):
    new_robot.image = pygame.transform.rotate(self.image, self.angle)
    new_robot.rect = new_robot.image.get_rect(center=self.rect.center)
    self.moving_pos[0] -= math.sin(self.angle / 180 * math.pi) * self.moving_speed[0]
    self.moving_pos[1] -= math.cos(self.angle / 180 * math.pi) * self.moving_speed[1]
    self.rect.centerx = self.moving_pos[0]
    self.rect.centery = self.moving_pos[1]

    if self.moving_right:
        self.angle -= 1
    if self.angle < -180:
        self.angle = 360 + self.angle
    if self.moving_left:
        self.angle +=1
    if self.angle >180:
        self.angle = self.angle - 360
    #
    if (self.rect.top <= 0 and -90 < self.angle < 90) or (
            self.rect.bottom >= self.screen_rect.height and (self.angle > 90 or self.angle < -90)):
        self.angle = 180 - self.angle
    if self.rect.right > self.screen_rect.width:
        self.angle= 0 - self.angle
    if self.rect.left < 0:
        self.angle = 0 - self.angle

主函数
from robot import Robot
import pygame
from settings import Settings
from pygame.sprite import Group
import game_functions as gf
from detector import Detector

def run_game():

# initialize game and create a dispaly object
pygame.init()
# initialize settings
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Sweeping Robot")
# initialize the group of dusts
dusts = Group()
# Create the fleet of dusts.

new_robot = Robot(screen)
detector = Detector(screen, new_robot)
gf.create_room(ai_settings, screen, dusts)
clock = pygame.time.Clock()
# game loop
while True:
    # supervise keyboard and mouse item
    gf.check_events(new_robot)
    # fill color

    new_robot.update(new_robot)
    detector.update(new_robot)
    # 按间距中的绿色按钮以运行脚本。
    gf.update_screen(ai_settings, screen, dusts,new_robot,detector)

    clock.tick(100)  # 100 ms

if name == 'main':
run_game()

运行结果及报错内容

Traceback (most recent call last):
File "E:\Sweeping_robot\Sweeping_robot.py", line 40, in
run_game()
File "E:\Sweeping_robot\Sweeping_robot.py", line 31, in run_game
new_robot.update(new_robot)
File "E:\Sweeping_robot\robot.py", line 29, in update
new_robot.image = pygame.transform.rotate(self.image, self.angle)
pygame.error: Out of memory

我的解答思路和尝试过的方法
我想要达到的结果

2条回答 默认 最新

相关推荐 更多相似问题