大家好,当我运行以下代码时,图像" Car1.jpg"从屏幕上快速放大然后因错误而崩溃:
追踪(最近一次通话): 文件" C:\ Users \ Adam \ Desktop \ Indy 500 \ Indev V0 \ Indy 500.py",第72行,in Car1.update(的DeltaTime) 文件" C:\ Users \ Adam \ Desktop \ Indy 500 \ Indev V0 \ Indy 500.py",第45行,更新中 self.image = pygame.transform.rotate(self.image,self.direction) pygame.error:宽度或高度太大
#Necessary imports to make the game run.
import math
import os
import pygame
from pygame.locals import *
import random
import sys
import time
#This sets up the Display, Framerate and Caption.
pygame.init()
screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
pygame.display.set_caption("Indy 500 Remastered")
class Car (pygame.sprite.Sprite):
#Initalises the 10 cars in game with their default stats.
def __init__ (self, image, position, MaxSpeed, Acceleration, Handling):
pygame.sprite.Sprite.__init__(self)
self.image = os.path.join("Graphics", image)
self.image = pygame.image.load(self.image)
self.position = position
self.speed = self.direction = 0
self.MaxSpeed = MaxSpeed
self.acceleration = Acceleration
self.handling = Handling
self.k_left = self.k_right = self.k_down = self.k_up = 0
def update(self, deltaTime):
self.direction = 0
self.speed += (self.k_up + self.k_down)
if self.speed > self.MaxSpeed:
self.speed = self.MaxSpeed
if self.speed < -self.MaxSpeed:
self.speed = self.MaxSpeed
self.direction += (self.k_right + self.k_left)
print(self.direction)
dx, dy = self.position
radian = self.direction * (math.pi /180)
dx += -self.speed * (math.sin(radian))
dy += -self.speed * (math.cos(radian))
print (dx)
print (dy)
self.position = ((dx, dy))
self.image = pygame.transform.rotate(self.image, self.direction)
self.rect = self.image.get_rect()
screen.blit(self.image, self.rect)
self.rect.center = self.position
rect = screen.get_rect()
Car1 = Car("Car1.jpg", rect.center, 2, 10, 10)
#This manages animations to make sure that the screen will refresh.
while True:
#This section will get the framerate and print it into the shell.
pygame.event.get()
deltaTime = clock.tick(60)
for event in pygame.event.get():
if not hasattr(event, "key"): continue
down = event.type == KEYDOWN
if event.key == K_RIGHT:
Car1.k_right = down * -3
elif event.key == K_LEFT:
Car1.k_left = down * 3
elif event.key == K_UP:
Car1.k_up = down * 2
elif event.key == K_DOWN:
Car1.k_up = down * -2
elif event.key == K_ESCAPE:
sys.exit(0)
screen.fill ((0,0,170))
Car1.update(deltaTime)
pygame.display.flip()
有没有办法解决这个问题?当我打印dx,dy和self.direction时,只有self.direction更改为3或-3。每次旋转都会使图像放大太多吗?可以做些什么来解决这个问题?
谢谢, 亚当。
答案 0 :(得分:3)
你不应该继续旋转旋转。这有点像影印复印件。您应保留图像的一个主副本,未转换,然后从同一主控创建所有不同的旋转。
每当你在图像上调用旋转时,你得到的是一个更大的图像。这是必要的,因为所有图像都是矩形的 - 如果将方形图像旋转45度,避免切掉任何角落的唯一方法是使得到的图像比原始图像宽40%。您还会发现旋转的副本会失去一些质量,因为除非旋转是90度的倍数,否则方形像素不能完美排列。因此,如果你继续旋转最后一次旋转的结果,你将会变得越来越大(边缘周围有越来越多的空填充)和越来越扭曲的图像,直到它最终太大而无法处理并且程序崩溃或者你的电脑慢慢爬行。
我建议做这样的事情:
def __init__(self, image_filename, ...):
...
self.original_image = pygame.image.load(image_filename)
...
def render(self, screen):
...
rotated_image = pygame.transform.rotate(self.original_image, self.angle)
screen.blit(rotated_image, rotated_image.get_rect(center=(self.x, self.y)))
...
def turn(self, clockwise_angle):
'''
Rotate clockwise by the given angle in degrees.
(Or indeed counter-clockwise if the angle is negative.)
'''
# Note: self.angle is positive for counter-clockwise
# rotation, to match pygame.transform.rotate.
self.angle -= clockwise_angle