简体   繁体   English

Python pygame.transform.rotate

[英]Python pygame.transform.rotate

I am new to pygame and python itself. 我是pygame和python本身的新手。 But right now, i have to make a spinner. 但是现在,我必须做一个微调器。 It should stop at random times. 它应该在随机时间停止。 What i was thinking was that i can have random.randint(0, 360). 我当时在想我可以拥有random.randint(0,360)。 So i can make it stop at random times. 因此,我可以使其在随机时间停止。 But right now, I can only make it rotate 90, or else it moves of the screen. 但是现在,我只能使其旋转90度,否则它将在屏幕上移动。

Any help would be useful. 任何帮助将是有用的。

Thanks 谢谢

import sys, pygame, time

pygame.init()

screen = pygame.display.set_mode([600,600])
black = 255,255,0
ball = pygame.image.load("wheel.gif")
temp = pygame.image.load("wheel.gif")


ballrec = ball.get_rect().center

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill(black)
    ball = pygame.transform.rotate(ball, 45)
    ballrec = ball.get_rect().center
    screen.blit(ball, ballrec)
    pygame.display.flip()
    time.sleep(1)

I've noticed that if you keep rotate the rotated object repeatedly, as you're doing in ball = pygame.transform.rotate(ball, 45) , the sprite gets distorted and starts to move off screen. 我注意到,如果您不断重复旋转旋转的对象,如在ball = pygame.transform.rotate(ball, 45)所做的那样,则精灵会变形并开始移出屏幕。 I usually keep the original image and switch back to that when I rotate 360 degrees. 我通常会保留原始图像,并在旋转360度时切换回原始图像。

As noted here , rotating by angles that are not divisible by 90 requires the image being padded. 如所指出的在这里 ,由是不整除90的角度旋转,需要被填充的图像。 You should be able to just blit the image to always be centered at 128,128 or you can trim off the edges of the image as necessary. 您应该可以将图像变白以始终位于128,128的中心,或者可以根据需要修剪图像的边缘。

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

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