Python 如何在pygame中使用关键点旋转图像?
我试图让图像(透明图像)在我按下的键上旋转。也就是说,我想上下旋转我的球员的头部(他是从他的头部射击),但我真的没有一个像样的指导方针如何编码。如果有人能帮助我,我将不胜感激。我还希望当我按住键时,头部能够平稳地上下旋转(瞄准)。代码如下:Python 如何在pygame中使用关键点旋转图像?,python,image,rotation,pygame,key,Python,Image,Rotation,Pygame,Key,我试图让图像(透明图像)在我按下的键上旋转。也就是说,我想上下旋转我的球员的头部(他是从他的头部射击),但我真的没有一个像样的指导方针如何编码。如果有人能帮助我,我将不胜感激。我还希望当我按住键时,头部能够平稳地上下旋转(瞄准)。代码如下: import pygame pygame.init() white = (255,255,255) BLACK = (0,0,0) red = (255,0,0) gameDisplay = pygame.display.set_mode((640,
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()
你必须
blit()
inside而在gameloop()
insidehead()
你必须使用global playerhead
或return playerhead
因为现在你创建了局部变量。你只想在两个图像之间切换吗,正常的和旋转的?你能把你最新的代码和图片添加到问题中吗?并更详细地解释动画的外观。