简体   繁体   English

pygame随着时间的推移旋转图像

[英]pygame spin an image over time

I am trying to use a class to make an image spin over time after it has been created but I keep getting an error: 我试图使用一个类在创建后随时间旋转图像,但我不断收到错误:

"pygame.error: Width or height is too large". “pygame.error:宽度或高度太大”。

game_display=pygame.display.set_mode((display_width,display_height))
    display_rect=game_display.get_rect()
    forest_img=pygame.image.load("forest.jpg")
    game_display.blit(forest_img,(0,0)
    kunai=pygame.image.load("Kunai.png")
    kunai=pygame.transform.scale(kunai, (13,55))
    kunai_rect=kunai.get_rect(center=display_rect.center)
    angle = 0
    done = False

    global done
    global kunai
    global angle
    global kunai_rect

    class Knife():
        def __init__(self, vx):
            self.x=display_width//2+30
            self.y=int(ground+65)
            self.vx=vx
        def update(self):
            self.x+=self.vx

        def draw(self):
            global done
            global kunai
            global angle
            global kunai_rect

            while not done:
                kunai=pygame.transform.rotate(kunai, angle)
                kunai_rect=kunai.get_rect(center=kunai_rect.center)
                game_display.blit(kunai, kunai_rect)
                clock.tick(60)
                angle+=1

    knifes=[]
    while True:
        for event in pygame.event.get():
            if event.type==pygame.KEYDOWN:
               if event.key == pygame.K_SPACE:
                    knifes.append(Knife(20))
        for knife in knifes:
            knife.draw()
            knife.update()

Could someone help me? 有人能帮助我吗? Thanks. 谢谢。

The surface that you rotate, kunai is a global variable. 你旋转的表面, kunai是一个全局变量。 When it has been rotated, it will be stored into the same variable again: 旋转后,它将再次存储到同一个变量中:

kunai=pygame.transform.rotate(kunai, angle)

Each time you rotate a surface it will become larger. 每次旋转曲面时,它都会变大。 You can imagine rotating an rectangle, and if you want to store the rotated rectangle in a unrotated rectangle, you will in most cases have to make the unrotated rectangle larger. 你可以想象旋转一个矩形,如果你想将旋转的矩形存储在一个未旋转的矩形中,你在大多数情况下都必须使未旋转的矩形变大。

So what happens is that the kunai rectangle becomes larger and larger for each iteration. 所以会发生的是, kunai矩形对于每次迭代变得越来越大。 You could try to print out the size to see this. 您可以尝试打印出尺寸以查看此内容。

You should rotate the original image instead. 您应该改为旋转原始图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM