gpt4 book ai didi

python - 如何使用 pygame 中的按键旋转我的图像?

转载 作者:行者123 更新时间:2023-12-01 02:23:39 24 4
gpt4 key购买 nike

我试图让图像(透明图像)在我按下的键上旋转。 IE。我想上下旋转我的玩家的头部(他是从头射击),但我真的没有关于如何编码的正确指南。如果有人可以帮助我,我将不胜感激。另外,我希望当我按住按键时,头部能够平滑地上下旋转(瞄准)。代码如下:

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

当我按下向上或向下键时,它应该只向上或向下旋转,也许在按住或向上键时让它平滑地旋转。

最佳答案

如果您只想在正常图像和旋转图像之间切换,可以在 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()

关于python - 如何使用 pygame 中的按键旋转我的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47661930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com