为什么我的pygame旋转看起来如此愚蠢?

发布于 2025-02-06 06:38:59 字数 2299 浏览 1 评论 0原文

我试图在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.

capybara

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2025-02-13 06:38:59

看起来很愚蠢,因为您不绕图像的中心旋转。请参见我如何使用Pygame在其中心周围旋转图像?
旋转图像时,图像的大小发生了变化。创建图像的矩形大小,并设置矩形的中心点。当图像旋转并使用矩形到blint图像:

< img src =“ https://i.sstatic.net/wcsyx.gif” alt =“”>

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)

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:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文