在鼠标按钮上旋转图像,在pygame中单击

时间:2015-10-04 16:36:49

标签: pygame

我试图在激活MOUSEBUTTONCLICK时不断旋转此图像,但是当我运行MOUSEBUTTONCLICK时,没有任何事情发生。这是我尝试过的代码。

while True:
    'RotatingImg'
    image_surf = pygame.image.load('imagefile')
    image_surf = pygame.transform.scale(image_surf, (810,810))
    image_surfpos = image_surf.get_rect()
    screen.blit(image_surf, image_surfpos)
    degree = 0
    pygame.display.update()
    for event in pygame.event.get():
        '''Quit Button'''
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            ## if mouse is pressed get position of cursor ##
            screen.blit(target1, target1pos)

            'RotatingImg'
            image_surf = pygame.image.load('C:\\Users\Leyton\Documents\Coding Stuff\Python\Apps\Fan_blades.png')
            image_surf = pygame.transform.scale(image_surf, (810,810))
            image_surfpos = image_surf.get_rect()
            screen.blit(image_surf, image_surfpos)
            degree = 0
            blittedRect = screen.blit(image_surf, image_surfpos)
            'Get center of surf for later'
            oldCenter = blittedRect.center
            'rotate surf by amount of degrees'
            rotatedSurf = pygame.transform.rotate(image_surf, degree)
            'Get the rect of the rotated surf and set its center to the old center'
            rotRect = rotatedSurf.get_rect()
            rotRect.center = oldCenter
            'Draw rotatedSurf with the corrected rect so it gets put in proper spot'
            screen.blit(rotatedSurf, rotRect)

            'Change the degree of rotation'
            degree += 5

            if degree > 0:
                degree = 0

            'Show the screen Surface'
            pygame.display.flip()
            'Wait 0 ms for loop to restart'
            pygame.time.wait(0)



        elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
            screen.blit(target, targetpos)

如果名称 ==' 主要':     游戏()

1 个答案:

答案 0 :(得分:0)

1:不要在事件循环中加载图像。您需要保留非旋转版本,然后根据需要旋转它。

2:pygame.MOUSEBUTTONDOWN仅在您第一次单击鼠标按钮时触发。按住按钮不会继续触发事件。

3:你重置学位两次;首先,将其设置为0,然后将其设置为0,如果它大于0。

也可能存在其他问题。