首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问组对象的属性?AttributeError:'Group‘对象没有属性'risk_level’

如何访问组对象的属性?AttributeError:'Group‘对象没有属性'risk_level’
EN

Stack Overflow用户
提问于 2018-08-07 16:19:58
回答 1查看 598关注 0票数 0

我有两个类GeoFenceWorker。类GeoFence有一个属性risk_level,该属性被随机分配给该类的所有对象。

我想从类risk_level访问这个属性Worker。特别是,当我检查这两个类的对象之间的冲突时(请参阅Worker中的函数risk_level ),我希望打印工作者与之碰撞的特定地理位置的risk_level

下面我提供了我的代码,但是错误消息失败了:

AttributeError:'Group‘对象没有属性'risk_level’

因此,我理解访问组的对象是必要的,因为组本身没有属性risk_level。但我该怎么做呢?

代码语言:javascript
复制
import pygame

class GeoFence(pygame.sprite.Sprite):
    def __init__(self, rect, risk_level, *groups):
        # we set a _layer attribute before adding this sprite to the sprite groups
        self._layer = 1
        pygame.sprite.Sprite.__init__(self, groups)
        self.image = pygame.surface.Surface((rect.width, rect.height))
        self.image.fill(GREEN)
        self.rect = rect
        self.risk_level = risk_level


class Worker(pygame.sprite.Sprite):

  # we introduce to possible states: RUNNING and IDLE
  RUNNING = 0
  IDLE = 1
  NUMBER_OF_ACCIDENTS = 0

  def __init__(self, image_running, image_idle, location, *groups):

    # each state has it's own image
    self.images = {
        Worker.RUNNING: pygame.transform.scale(get_image(image_running), (45, 45)),
        Worker.IDLE: pygame.transform.scale(get_image(image_idle), (20, 45))
    }

    # we set a _layer attribute before adding this sprite to the sprite groups
    # we want the workers on top
    self._layer = 2
    pygame.sprite.Sprite.__init__(self, groups)

    # let's keep track of the state and how long we are in this state already            
    self.state = Worker.IDLE
    self.ticks_in_state = 0

    self.image = self.images[self.state]
    self.rect = self.image.get_rect(topleft=location)

    self.direction = pygame.math.Vector2(0, 0)
    self.speed = random.randint(1, 3)
    self.set_random_direction()


  def set_random_direction(self):
    # random new direction or standing still
    vec = pygame.math.Vector2(random.randint(-100,100), random.randint(-100,100)) if random.randint(0, 5) > 1 else pygame.math.Vector2(0, 0)

    # check the new vector and decide if we are running or fooling around
    length = vec.length()
    speed = sum(abs(int(v)) for v in vec.normalize() * self.speed) if length > 0 else 0

    if length == 0 or speed == 0:
        new_state = Worker.IDLE
        self.direction = pygame.math.Vector2(0, 0)
    else:
        new_state = Worker.RUNNING
        self.direction = vec.normalize()

    self.ticks_in_state = 0
    self.state = new_state

    # use the right image for the current state
    self.image = self.images[self.state]


  def update(self, screen):
    self.ticks_in_state += 1
    # the longer we are in a certain state, the more likely is we change direction
    if random.randint(0, self.ticks_in_state) > 70:
        self.set_random_direction()

    # now let's multiply our direction with our speed and move the rect
    vec = [int(v) for v in self.direction * self.speed]
    self.rect.move_ip(*vec)

    if any(s for s in pygame.sprite.spritecollide(self, fences, False) if s != self):
        print("RISK_LEVEL: ",fences.risk_level)


all_sprites = pygame.sprite.LayeredUpdates()
workers = pygame.sprite.Group()
fences = pygame.sprite.Group()

screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("TEST")

# create multiple workers
for pos in ((30,30), (50, 400), (200, 100), (700, 200)):
    Worker("w1.png", "w2.png", pos, all_sprites, workers, fences)

# create multiple geo-fences
risks = ["HIGH","MEDIUM","LOW"]
for rect in (pygame.Rect(510,150,75,52), pygame.Rect(450,250,68,40), pygame.Rect(450,370,68,48)):
    risk = risks[random.randint(0,2)]
    GeoFence(rect, risk, all_sprites, fences)

更新:

For @Prune:包含整个代码(如果出现任何问题)的my previous thread

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-07 18:20:47

此错误将在此处引发:

代码语言:javascript
复制
if any(s for s in pygame.sprite.spritecollide(self, fences, False) if s != self):
    print("RISK_LEVEL: ", fences.risk_level)

fences是一个精灵组,它没有risk_level属性,只有这个组中的GeoFence精灵拥有这个属性。因此,您需要找出哪些精灵与工作人员发生了冲突,然后访问这些精灵的risk_level。最简单的解决方案是使用for循环迭代碰撞后的精灵(用以下代码替换上面的两行):

代码语言:javascript
复制
for s in pygame.sprite.spritecollide(self, fences, False):
    print("RISK_LEVEL: ", s.risk_level)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51731348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档