简体   繁体   中英

Using data from a numpy array in a function

I'm trying to make a simply solar system simulation as practice, but I'm running into a small problem.

I want to store simple planet data in a numpy array, then use the data from that array to draw the planets. However, I can't seem to get my custom functions to properly use the data.

For example, this is the way the data is stored.

# shape as type, size, orbitradius, startx, starty
solsystem = np.array([
                     ['sun', 2, 0, (screenx // 2), (screeny // 2)],
                     ['planet', 1, 200, 200, 200]
                     ])

The function I'm trying to use the data in.

class makebody():
    def new(btype, size, orbitradius, x, y):
        nbody = body()
        nbody.type = btype
        nbody.size = size
        nbody.orbitradius = orbitradius
        nbody.x = x
        nbody.y = y
        nbody.color = (0, 0, 255) #blue

        if (btype == 'sun'):
            nbody.color = (255, 255, 0)  #yellow

        return nbody

I've tried

bvec = np.vectorize(makebody.new)
    body = bvec(solsystem)

And

for t, size, orbitradius, x, y in np.ndindex(solsystem.shape):
        body = makebody.new(t, size, orbitradius, x, y)

But none of these deliver the desired result, or work at all for that matter. How would I go about doing this, or is numpy not even the right tool for this job?

I would use a dictionary or like in this example a list of tuples. Simple and effective. Then you can feed your list to a class an generate your solar system and then you can easily access your planets and attributes with the . notation:


class Body:

    def __init__(self, data):
        # data represents one element of solsystem
        self.btype, self.size, self.orbitradius, self.x, self.y, self.color = data

    def __str__(self):
        return f'Name:\t{self.btype}\nSize:\t{self.size}\n' \
               f'Orbit Radius:\t{self.orbitradius}\nPosition:\t{self.x}, {self.y}\nColor:\t{self.color}'


class SolarSystem:

    def __init__(self, solsystem):
        self.bodies = dict()
        for data in solsystem:
            b = Body(data)
            self.bodies[b.btype] = b


if __name__ == '__main__':
    screenx = 600
    screeny = 600

    solsystem = [('sun', 2, 0, (screenx // 2), (screeny // 2), (255, 255, 0)),
                 ('earth', 1, 200, 200, 200, (0, 0, 255)),
                 ('mars', 1, 200, 200, 200, (255, 0, 0))]

    ss = SolarSystem(solsystem)

    print(ss.bodies['sun'])

    # Access attributes with e.g. ss.bodies['sun'].size for the size, etc...

The print statement at the end will return the content of the str method:

Name:   sun
Size:   2
Orbit Radius:   0
Position:   300, 300
Color:  (255, 255, 0)

ss.bodies is just a dictionary with all your planets in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM