大家好,當我運行下面的代碼圖像「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。每次旋轉是否會放大圖像太多?可以做些什麼來解決這個問題?
謝謝, 亞當。
感謝您的回答!那麼你會怎麼建議模擬汽車轉彎?這是否是沿着這條線:圖像=「car1.jpg」旋轉= 0.說第一次通過使它旋轉10度,然後第二次20.它會旋轉原來的10,回到0,然後到20? –
我已經編輯了一個可以採取的一般方法的建議。 – Weeble
非常感謝!明天我會放棄這一點! Pygame和OOP很新。僅用於製作程序性程序和基於文本的冒險。 –