简体   繁体   English

用python翻转图像(pygame)

[英]Flipping an image with python (pygame)

I'm trying to learn python so I'm messing around with pygame. 我正在尝试学习python,所以我在搞混pygame。 I'm a complete beginner. 我是一个完整的初学者。

so far I have made it so that I can control an image, moving it in 2d with the arrow keys. 到目前为止,我已经完成了它,以便可以控制图像,并使用箭头键将其移动2d。

However, I drew the image facing left and it is always facing left and I want to make it face the direction it's moving. 但是,我画的图像是朝左的,它总是朝左,所以我想使其朝着移动的方向。 I managed to make it rotate 180 degrees when I press right, but that was everytime I press right so I kept flipping the wrong way. 当我按向右时,我设法使其旋转了180度,但这是每次我按向右时,所以我一直以错误的方式翻转。

I need it to face right when moving right and left when moving left. 我需要它在向右移动时朝右,在向左移动时朝左。

Image attached. 图片已附上。

Thank you 谢谢

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()

Mainly i change the snake function, so that it computes the correct rotation and then rotates the image. 我主要是更改蛇功能,以便它计算正确的旋转,然后旋转图像。 Also I change a little bit of the logic, especially at event handling so it is easier to read 另外,我更改了一些逻辑,尤其是在事件处理方面,因此更易于阅读

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()

And still i have the question: Why 2 game loops inside each other? 但我仍然有一个问题:为什么2个游戏相互循环?

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

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