파이썬 클래스 질문

파이썬 클래스 질문

QA

파이썬 클래스 질문

답변 1

본문

class Person:

    def __init__(self, n, x, y):

        self.x = x

        self.y = y

        self.life = 0

 

class World:

    def __init__(self, n):

        self.n = n

        self.map = [[0 for j in range(n)] for i in range(n)]

        self.person = []

    def genPerson(self):

        x = randint(0, self.n-1)

        y = randint(0, self.n-1)

        while self.map[y][x] != 0:

            x = randint(0, self.n-1)

            y = randint(0, self.n-1)

        newPerson = Person(self.n, x, y)

        self.person.append(newPerson)

        self.map[y][x] = 1

    def nGenPerson(self, n):

        for i in range(n):

            self.genPerson()

 

-----

이런 코드가 있고,

miniWorld = World(100)

miniWorld.nGenPerson(nPerson)으로 인스턴트 객체를 생성해서 person리스트에 넣은 것까지는 했습니다!

그런데 for i in range(miniWorld.person)을 해서 person리스트에 들어있는 인스턴트 객체를 하나씩 돌면서 self.life 이거를 조작하고 싶은데 어떻게 해야할지 모르겠습니다..

그리고 life가 80이 되면 그 객체를 없애고 싶은데 i를 통해 순회하면서 어떻게 없앨지 도무지 감이 안잡힙니다..

도와주세요ㅜㅠ

이 질문에 댓글 쓰기 :

답변 1

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 0
© SIRSOFT
현재 페이지 제일 처음으로