为什么这艘船向右移动但向左,上下移动(不优化的方式)?

发布于 2025-02-06 14:25:55 字数 1387 浏览 0 评论 0原文

class Ship():

    def __init__(self, ai_setting, screen):
        self.screen = screen
        self.ai_setting = ai_setting
        self.image = pygame.image.load("images/ship.bmp")
        self.image = pygame.transform.scale(self.image, (200, 200))

        self.image_rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        self.image_rect.bottom = self.screen_rect.bottom
        self.image_rect.centerx = self.screen_rect.centerx

        self.center_xaxis =float( self.image_rect.centerx)
        #move_flags
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False


    def update(self):
        if self.moving_right:
            self.center_xaxis += self.ai_setting.ship_speed_factor
        if self.moving_left:
            self.image_rect.centerx -= 10
        if self.moving_up:
            self.image_rect.centery -=10
        if self.moving_down:
            self.image_rect.centery +=10

    def blitme(self):
        self.screen.blit(self.image, self.image_rect)

我尝试打印self.center_xaxisself.image_rect.centerx的值 但是self.center_xaxis并不相同。

我需要self.center的值来通过我在我制作的设置文件中控制的因素进行更改。 game_function文件根据按键将更改的移动标志处理为true或返回false

class Ship():

    def __init__(self, ai_setting, screen):
        self.screen = screen
        self.ai_setting = ai_setting
        self.image = pygame.image.load("images/ship.bmp")
        self.image = pygame.transform.scale(self.image, (200, 200))

        self.image_rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        self.image_rect.bottom = self.screen_rect.bottom
        self.image_rect.centerx = self.screen_rect.centerx

        self.center_xaxis =float( self.image_rect.centerx)
        #move_flags
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False


    def update(self):
        if self.moving_right:
            self.center_xaxis += self.ai_setting.ship_speed_factor
        if self.moving_left:
            self.image_rect.centerx -= 10
        if self.moving_up:
            self.image_rect.centery -=10
        if self.moving_down:
            self.image_rect.centery +=10

    def blitme(self):
        self.screen.blit(self.image, self.image_rect)

I tried printing the value of self.center_xaxis and self.image_rect.centerx
but self.center_xaxis isn't the same.

I need the value of self.center to change by a factor I'll control in the settings file I made. The game_function file handles the changing movement flags according to the keypress to True or back to False.

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

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

发布评论

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

评论(1

淡墨 2025-02-13 14:25:55

您可能想以浮点精度移动对象。因为 pygame.Rect 应该代表一个屏幕上的区域,pygame.Rect对象只能存储积分数据。参见 pygame.Rect

rect对象的坐标都是整数。 [...]

如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量属性中,并同步pygame.Rect.Rect对象。 round> rounh 坐标并将其分配给矩形的位置:

 class Ship():

    def __init__(self, ai_setting, screen):
        # [...]

        self.center_xaxis = self.image_rect.centerx
        self.center_yaxis = self.image_rect.centery

    def update(self):
        if self.moving_right:
            self.center_xaxis += self.ai_setting.ship_speed_factor
        if self.moving_left:
            self.center_xaxis -= self.ai_setting.ship_speed_factor
        if self.moving_up:
            self.center_yaxis -=10
        if self.moving_down:
            self.center_yaxis +=10

        self.image_rect.centerx = round(self.center_xaxis)
        self.image_rect.centery = round(self.center_yaxis)

    def blitme(self):
        self.screen.blit(self.image, self.image_rect)

You probably want to move the object with floating point precision. Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data. See pygame.Rect:

The coordinates for Rect objects are all integers. [...]

If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect object. round the coordinates and assign it to the location of the rectangle:

 class Ship():

    def __init__(self, ai_setting, screen):
        # [...]

        self.center_xaxis = self.image_rect.centerx
        self.center_yaxis = self.image_rect.centery

    def update(self):
        if self.moving_right:
            self.center_xaxis += self.ai_setting.ship_speed_factor
        if self.moving_left:
            self.center_xaxis -= self.ai_setting.ship_speed_factor
        if self.moving_up:
            self.center_yaxis -=10
        if self.moving_down:
            self.center_yaxis +=10

        self.image_rect.centerx = round(self.center_xaxis)
        self.image_rect.centery = round(self.center_yaxis)

    def blitme(self):
        self.screen.blit(self.image, self.image_rect)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文