为什么我的pygame旋转看起来如此愚蠢?
我试图在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)
这就是我的意思是愚蠢的: 如果您想测试,这也是Capybara图像。
I am trying to rotate an image in PyGame, and I don't know how to describe it, but it's very goofy. Here's my code.
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)
And here's what I mean by goofy: https://gyazo.com/4dd3a679d963595849e0412307b95fa9
Here's the capybara image too, if you want to test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来很愚蠢,因为您不绕图像的中心旋转。请参见我如何使用Pygame在其中心周围旋转图像?。
旋转图像时,图像的大小发生了变化。创建图像的矩形大小,并设置矩形的中心点。当图像旋转并使用矩形到
blint
图像:< img src =“ https://i.sstatic.net/wcsyx.gif” alt =“”>
It looks so "goofy, because you do not rotate around the center of the image. See How do I rotate an image around its center using PyGame?.
The size of the image changes when the image is rotated. Create a rectangule size of the image and set the center point of the rectangle. Update the rectangle when the image rotates and use the rectangle to
blint
the image: