微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以根据 screen_with 和屏幕高度调整 pygame 中的对象大小

如何解决是否可以根据 screen_with 和屏幕高度调整 pygame 中的对象大小

import pygame
from pygame.sprite import Sprite
import random

class Alien(Sprite):
    """A class to represent a single alien in the fleet"""

    def __init__(self,ai_settings,screen):
        """Initialize the alien and set its starting position."""
        super().__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        # load the alien image and set its rect attribute.
        image_number = random.randint(1,4)

        if image_number == 1:
            self.image = pygame.image.load('images/alien.png').convert_alpha()
            infoObject = pygame.display.Info()
            self.image = pygame.transform.scale(self.image,(infoObject.current_w / 10,infoObject.current_h / 10))
        if image_number == 2:
            self.image = pygame.image.load('images/alien 2.png').convert_alpha()
            infoObject = pygame.display.Info()
            self.image = pygame.transform.scale(self.image,infoObject.current_h / 10))
        if image_number == 3:
            self.image = pygame.image.load('images/alien 3.png').convert_alpha()
            infoObject = pygame.display.Info()
            self.image = pygame.transform.scale(self.image,infoObject.current_h / 10))
        if image_number == 4:
            self.image = pygame.image.load('images/alien 4.png').convert_alpha()
            infoObject = pygame.display.Info()
            self.image = pygame.transform.scale(self.image,infoObject.current_h / 10))
        if image_number == 5:
            self.image = pygame.image.load('images/alien.png').convert_alpha()
            infoObject = pygame.display.Info()
            self.image = pygame.transform.scale(self.image,infoObject.current_h / 10))
        self.rect = self.image.get_rect()

使用此代码随机化我的外星人,但我经常在显示器之间切换,这导致外星人的数量因屏幕而异。所以我想知道是否可以根据屏幕大小更改外星人的大小。目前我收到错误“预期的整数参数,得到浮动”

解决方法

pygame.transform.scale 采用 size 参数,即 (width,height)。这个宽度和高度必须是整数。

所以在你的情况下,而不是:

self.image = pygame.transform.scale(self.image,(infoObject.current_w / 10,infoObject.current_h / 10))

你可以这样做:(将结果转换为整数)

self.image = pygame.transform.scale(self.image,(int(infoObject.current_w / 10),int(infoObject.current_h / 10)))

不过,还有更简单的方法。 Python 内置了整数除法,它实现了相同的结果,即除法然后转换。这种除法使用两个“/”字符,并且总是输出整数。

self.image = pygame.transform.scale(self.image,(infoObject.current_w // 10),infoObject.current_h // 10))

从程序设计的角度来看,在每个 if 语句中创建一个显示 Info 对象,并且每次都重新计算大小有点麻烦。还有很多其他的一般性重复。

这是我尝试更简洁的部分:

        # load the alien image and set its rect attribute.
        image_number = random.randint(1,4)

        self.image = pygame.image.load(f"images/alien {image_number}.png") # this does require you to rename "alien.png" to "alien 1.png"
        w,h = screen.get_size() # you already have the screen surface,you can get size that way
        self.image = pygame.transform.scale(self.image,(w//10,h//10))

此外,它现在根本不会影响您,但我碰巧在前几天在 https://github.com/pygame/pygame/pull/2581 中为 pygame.transform.scalepygame.transform.smoothscale 添加了浮动支持。所以以后可能不需要整数除法部分了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。