[英]How to rotate my image using Keys in pygame?
我試圖讓圖像(透明圖像)在我按下的鍵上旋轉。 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()
所以我會使用這段代碼,因為你的代碼實際上不會旋轉它,因為你讓它在循環外旋轉(只發生一次)。
我希望您嘗試使用它:
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
還
# add this if you want it to rotate by one each time it calls the function.
angle += 1
它應該使表面/矩形以其設置的角度旋轉。 我希望這有助於並享受使用 pygame 編碼的樂趣!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.