简体   繁体   English

如何在 pygame 中使用 Keys 旋转我的图像?

[英]How to rotate my image using Keys in pygame?

I'm trying to let an image (transparent image) Rotate on the keys that I press.我试图让图像(透明图像)在我按下的键上旋转。 IE I want to rotate my player's head up and down(he is shooting from his head), but I don't really have a decent guideline on how to code this. IE 我想上下旋转我的玩家的头部(他从他的头部射击),但我真的没有关于如何编码的体面指南。 If anyone could help me it would be greatly appreciated.如果有人可以帮助我,将不胜感激。 Also I wanted the head just to smoothly rotate(aim) up and down when I hold the keys down.我还希望当我按住按键时头部能够平滑地上下旋转(瞄准)。 Code below:代码如下:

import pygame

pygame.init()

white = (255,255,255)
BLACK = (0,0,0)
red = (255,0,0)


gameDisplay = pygame.display.set_mode((640,360))

background = pygame.image.load('background.jpg').convert()

player = pygame.image.load('BigShagHoofdz.png') #this must be rotateable

pygame.display.set_caption('Leslie is Lauw')
clock = pygame.time.Clock()


gameDisplay.blit(background, [0,0])
gameDisplay.blit(player, [-1,223])
pygame.display.update()

FPS = 60

direction = "Down"

def head():
    if direction == "Down":
        playerhead = player
    if direction == "Up":
        playerhead = pygame.transform.rotate(player, 60)



def gameLoop():
    global direction
    gameExit = False


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

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            gameExit = True

            if event.key == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                direction = "Up"
                elif event.key == pygame.K_DOWN:
                direction = "Down"

clock.tick(60)
pygame.quit()
quit()

It should only rotate up or down when I press the up or down key, maybe let it go smooth when it's held down or up.它应该只在我按下向上或向下键时向上或向下旋转,也许在按住或向上时让它变得平滑。

If you just want to switch between a normal and a rotated image, you can create the rotated version before the while loop and then just switch the image if a key gets pressed. 如果只想在普通图像和旋转图像之间切换,则可以在while循环之前创建旋转版本,然后只要按下某个键就可以切换图像。

import pygame

pygame.init()

gameDisplay = pygame.display.set_mode((640, 360))
clock = pygame.time.Clock()
# The normal image/pygame.Surface.
player_image = pygame.Surface((30, 50), pygame.SRCALPHA)
player_image.fill((0, 100, 200))
pygame.draw.circle(player_image, (0, 50, 100), (15, 20), 12)
# The rotated image.
player_rotated = pygame.transform.rotate(player_image, 60)

FPS = 60

def gameLoop():
    player = player_image
    player_pos = [100, 223]
    gameExit = False

    while gameExit == False: 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                # Assign the current image to the `player` variable.
                if event.key == pygame.K_UP:
                    player = player_image
                elif event.key == pygame.K_DOWN:
                    player = player_rotated

        gameDisplay.fill((30, 30, 30))
        gameDisplay.blit(player, player_pos)

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

gameLoop()
pygame.quit()

so I would be using this code since your code wont actually rotate it because You are making it rotate outside of the loop (Only occurs once).所以我会使用这段代码,因为你的代码实际上不会旋转它,因为你让它在循环外旋转(只发生一次)。

I would like you to try to use this instead:我希望您尝试使用它:

import pygame, sys

pygame.init()

clock = pygame.time.Clock()
screen = pygame.display.set_mode((width, height))

def rotateItem(image, angle, x,y):
    image = pygame.transform.rotate(image, angle)
    imageRect = image.get_rect(center=(x,y))
    screen.blit(image, imageRect)

angle = 0
while True:
    screen.fill((r, g, b)) # Red, Green, Blue
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                angle = -angle # I can't test right now so if this doesn't work switch -angle with abs(angle)
                rotateItem(playerImage, angle, playerXPosition,playerYPosition)
            if event.key == pygame.K_DOWN:
                angle = abs(angle) # Same with this but change it to -angle
                rotateItem(playerImage, angle, playerXPosition,playerYPosition)

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

Also

# add this if you want it to rotate by one each time it calls the function.
angle += 1

It should make the surface / rect rotate at the angle it is set to.它应该使表面/矩形以其设置的角度旋转。 I hope this helped and enjoy coding with pygame!我希望这有助于并享受使用 pygame 编码的乐趣!

SOURCES: ClearCode - Rotating Stuff来源: ClearCode - 旋转的东西

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

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