Pygame - keydown

时间:2016-08-29 14:46:51

标签: python-2.7 pygame

当按下左/右箭头键时,如何将图像/精灵旋转到左/右? ......像一个轮子。

所以这是运行游戏循环的代码:

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10
        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10

        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

我曾尝试在key_pressed条件下添加它,但没有任何反应,我按照这个人的说法做了:https://stackoverflow.com/a/19316827/6256879

精灵被称为cancer_cell,正如我上面提到的,我希望它在左/右键被按下时旋转到左/右。

1 个答案:

答案 0 :(得分:0)

如果你想在每次点击左箭头或右箭头时旋转一度,这样的东西可能会有效。如果您希望图像连续旋转,则需要使用get_pressed()作为感兴趣的关键点,以便在每一帧都应用角度。

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        angle = 0 #reset the angle at each iteration
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10
            angle += 1.0
        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10
            angle -= 1.0

        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        cancer_cell = pygame.transform.rotate(cancer_cell,angle)
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

可以进行连续旋转:

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        angle = 0 #reset the angle at each iteration
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10

        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10


        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]: #do the same for the other key
               angle += 1              #and put -= instead of +=

        cancer_cell = pygame.transform.rotate(cancer_cell,angle)
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)