我正在嘗試在 PyGame 中旋轉影像,但我不知道如何描述它,但它非常愚蠢。這是我的代碼。
import sys, pygame
pygame.init()
size = width, height = 600, 600
green = 50, 168, 82
screen = pygame.display.set_mode(size)
class Capybara:
def __init__(self, size_multiplier):
self.capybara = pygame.image.load('capybara.gif')
self.x = 300
self.y = 300
self.o_size = 92, 206
self.new_size = (self.o_size[0] * size_multiplier,self.o_size[1] * size_multiplier)
self.capybara = pygame.transform.scale(self.capybara, self.new_size)
self.base_capybara = self.capybara
self.rotation = 0
def rotate(self, amount):
self.rotation = amount
if self.rotation == 361:
self.rotation = 0
elif self.rotation == -1:
self.rotation = 360
print(self.rotation)
self.capybara = pygame.transform.rotate(self.base_capybara, self.rotation)
capybara = Capybara(0.8)
fpsClock = pygame.time.Clock()
rotate_l = False
rotate_r = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rotate_l = True
if rotate_r:
rotate_r = False
if event.key == pygame.K_RIGHT:
rotate_r = True
if rotate_l:
rotate_l = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
rotate_l = False
if event.key == pygame.K_RIGHT:
rotate_r = False
if rotate_l:
capybara.rotate(-1)
if rotate_r:
capybara.rotate(1)
screen.fill(green)
screen.blit(capybara.capybara, (capybara.x - int(capybara.new_size[0]/2), capybara.y - int(capybara.new_size[1]/2)))
pygame.display.update()
fpsClock.tick(60)
這就是我所說的愚蠢的意思:
uj5u.com熱心網友回復:
它看起來很“愚蠢,因為您沒有圍繞影像的中心旋轉。請參閱
import sys, pygame
pygame.init()
size = width, height = 600, 600
green = 50, 168, 82
screen = pygame.display.set_mode(size)
class Capybara:
def __init__(self, size_multiplier):
self.capybara = pygame.image.load('capybara.gif')
self.o_size = 92, 206
self.new_size = (self.o_size[0] * size_multiplier,self.o_size[1] * size_multiplier)
self.capybara = pygame.transform.scale(self.capybara, self.new_size)
self.base_capybara = self.capybara
self.rotation = 0
self.rect = self.capybara.get_rect(center = (300, 300))
def rotate(self, amount):
self.rotation = amount
if self.rotation == 361:
self.rotation = 0
elif self.rotation == -1:
self.rotation = 360
print(self.rotation)
self.capybara = pygame.transform.rotate(self.base_capybara, self.rotation)
self.rect = self.capybara.get_rect(center = self.rect.center)
capybara = Capybara(0.8)
fpsClock = pygame.time.Clock()
rotate_l = False
rotate_r = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rotate_l = True
if rotate_r:
rotate_r = False
if event.key == pygame.K_RIGHT:
rotate_r = True
if rotate_l:
rotate_l = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
rotate_l = False
if event.key == pygame.K_RIGHT:
rotate_r = False
if rotate_l:
capybara.rotate(-1)
if rotate_r:
capybara.rotate(1)
screen.fill(green)
screen.blit(capybara.capybara, capybara.rect)
pygame.display.update()
fpsClock.tick(60)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490390.html
標籤:Python python-3.x 数学 游戏 回转
