2015-12-19 51 views
0

大家好,當我運行下面的代碼圖像「Car1.jpg」擴大以極快的速度去關閉屏幕,然後與錯誤而崩潰:pygame的 - 旋轉圖像不會停止拉伸

Traceback (most recent call last): File "C:\Users\Adam\Desktop\Indy 500\Indev V0\Indy 500.py", line 72, in Car1.update(deltaTime) File "C:\Users\Adam\Desktop\Indy 500\Indev V0\Indy 500.py", line 45, in update self.image = pygame.transform.rotate(self.image, self.direction) pygame.error: Width or height is too large

#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。每次旋轉是否會放大圖像太多?可以做些什麼來解決這個問題?

謝謝, 亞當。

回答

2

你不應該繼續旋轉旋轉的旋轉。這有點像複印複印件。您應該保留圖像的一個主要副本,未轉換,然後從同一主機創建所有不同的旋轉。

無論何時您調用圖像上的旋轉,您返回的都是較大的圖像。這是必要的,因爲所有圖像都是矩形的 - 如果將方形圖像旋轉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 
+0

感謝您的回答!那麼你會怎麼建議模擬汽車轉彎?這是否是沿着這條線:圖像=「car1.jpg」旋轉= 0.說第一次通過使它旋轉10度,然後第二次20.它會旋轉原來的10,回到0,然後到20? –

+0

我已經編輯了一個可以採取的一般方法的建議。 – Weeble

+0

非常感謝!明天我會放棄這一點! Pygame和OOP很新。僅用於製作程序性程序和基於文本的冒險。 –