用python翻转图像(pygame)

时间:2018-02-03 16:24:12

标签: python image rotation

我正在尝试学习python,所以我正在搞乱pygame。我是一个完全的初学者。

到目前为止,我已经做到了这一点,我可以控制图像,用箭头键在2d中移动它。

然而,我画了左边的图像,它总是朝左,我想让它面向它移动的方向。当我向右按时,我设法让它旋转180度,但那是每次我向右按,所以我一直在以错误的方式翻转。

当向左移动时左右移动时我需要面向右边。

Image附上。

谢谢

import pygame
import time
pygame.init()
display_width = 1000
display_height = 800
black=(0,0,0)
white=(255,255,255)
gamedisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('metal gear python')
clock = pygame.time.Clock()

snakeimg= pygame.image.load('snake.png')

snake_width = 96
snake_height= 79
def snake(x,y):
    gamedisplay.blit(snakeimg, (x,y))


discovered = False
while not discovered:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            discovered = TRUE
        print (event)




    x = (display_width * 0.45)
    y = (display_height * 0.8)
    x_change=0
    y_change=0
    snake_speed=0


    gameExit= False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            print(event)    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -3
                elif event.key == pygame.K_RIGHT:
                    x_change = 3



                elif event.key == pygame.K_RIGHT:
                    x_change = 3


            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:  
                    x_change = 0
                elif event.key == pygame.K_RIGHT:
                    x_change = 0
        x += x_change     

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                    y_change = -3
            elif event.key == pygame.K_DOWN:
                    y_change = 3
        if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        y += y_change

        gamedisplay.fill(white)
        snake(x,y)


        if x > display_width-snake_width or x < 0:
            gameExit=True

        if y > display_height-snake_height or y < 0:
            True


        pygame.display.update()
        clock.tick(60)

pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

主要是我改变蛇形功能,以便计算正确的旋转然后旋转图像。我也改变了一点逻辑,特别是在事件处理时,因此更容易阅读

import pygame
import time

pygame.init()
display_width = 1000
display_height = 800
black = (0, 0, 0)
white = (255, 255, 255)
gamedisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('metal gear python')
clock = pygame.time.Clock()

snakeimg = pygame.image.load('snake.png')

snake_width = 96
snake_height = 79


def snake(x, y):
    if x_change == 0 and y_change == 0:
        rotation = -90
    elif x_change > 0 and y_change == 0:
        rotation = 180
    elif x_change < 0 and y_change == 0:
        rotation = 0
    elif x_change == 0 and y_change > 0:
        rotation = 90
    elif x_change == 0 and y_change < 0:
        rotation = -90
    elif x_change < 0 and y_change < 0:
        rotation = -45
    elif x_change < 0 and y_change > 0:
        rotation = 45
    elif x_change > 0 and y_change < 0:
        rotation = -135
    elif x_change > 0 and y_change > 0:
        rotation = 135
    gamedisplay.blit(pygame.transform.rotate(snakeimg, rotation), (x, y))


discovered = False
while not discovered:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            discovered = True
        print(event)

    x = (display_width * 0.45)
    y = (display_height * 0.8)
    x_change = 0
    y_change = 0
    snake_speed = 0

    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                gameExit = True
                discovered = True # so you can actually quit the game
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -3
                elif event.key == pygame.K_RIGHT:
                    x_change = 3
                elif event.key == pygame.K_UP:
                    y_change = -3
                elif event.key == pygame.K_DOWN:
                    y_change = 3
            elif event.type == pygame.KEYUP:
                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    x_change = 0
                elif event.key in (pygame.K_UP, pygame.K_DOWN):
                    y_change = 0

        x += x_change
        y += y_change

        gamedisplay.fill(white)
        snake(x, y)

        if x > display_width - snake_width or x < 0:
            gameExit = True

        if y > display_height - snake_height or y < 0:
            gameExit = True

        pygame.display.update()
        clock.tick(60)

pygame.quit()
quit()

我仍然有一个问题:为什么2个游戏在彼此内部循环?