Simple collision and updating a list (basic inventory)


I am just learning and my code is unelegant and clunky- but this worked for me and I had a hard time finding examples of how to do this in noob english.

Basically I wanted to take my character and run over a item- have that item vanish then update my inventory. Now in this picture I am just using a texture of a card that will be used later in the game- because I do not have any gem models and plus these were handy.

if look below you will see that the overall mana count increased, and the watergem count increased and that the card or item ralph touched is now gone. This can be applied to any type of item management in the game not just for mana.

here is what I did:

(I used roaming ralph as base)
I switched the enviroment map out for a flat one so my random items didn’t end up in a hill or something. You can change the map out or try to use the existing one- you will just need to adjust the items z-height to match the terrain.

Next I created a small array to create two items: watergem (self.mana[0]) and deathgem(self.mana[1]). BY the way all this code went inside of the

class World(DirectObject):   def __init__(self):
        for xyz in range(2):
            self.mana.append(loader.loadModel("models/plane")) #<<<< change this to your own model- iused a plane (flat card) to just hold a card graphic that I just had laying in my game file.
            myTexture = loader.loadTexture("cards/ac1.jpg")#<<<again change the graphic to your own
            self.mana[xyz].setPos(self.ralph.getX()+10+random.randint(10, 30) ,self.ralph.getY()+10, 0) #<<< this places the cards somewhat close to ralph.
        self.mana[0].setTag('type', "watergem") #<<<here I setup my tags to identify the item later
        self.mana[1].setTag('type', "deathgem")

next I needed a collision:

But I was already using collision ray on the ground and I was not liking the effects of using a collision solids (I kept getting weird ray problems) So I switched to just measuring the distance and acting out the collision:
This code went in the collission movement task:

# Also deals with grid checking and collision detection
    def move(self, task):
        for i in self.mana:
            lenVector = self.ralph.getPos() - self.mana[ii].getPos() #<<this measures the distance between my actor and the items
            if lenVector.length()<2: #<< if this item is very close then perform collision
                if self.mana[ii].getTag('type')=="watergem": #<< check to see what type of item it is
                    self.textObject2.setText(str(self.watergems)) #<< update the online gui text
                if self.mana[ii].getTag('type')=="deathgem": #same thing again for the second item
                del self.mana[ii] 
                self.textObject7.setText(str(self.firegems + self.windgems + self.deathgems+ self.watergems + self.earthgems)) #<< update my total mana and display on gui       
        ii+=1 #<< increments the count

You will need to create textOjects to display the texts of course. But what happens is if roaming ralph touches an item the item vanishes and the inventory variable is increased by one then displayed on a textObject.

Forgive my poor writing and worse good, I hope this helps someone.

JB SKaggs


now I see what you need (couldn’t have said it before? :wink: )
here you’ll find out everything you need to achieve that goal

PS: if you don’t mind please post smaller images (not > 800x600) 'cos you make the thread reading complicated (at least for me)


Sorry about the pics didn’t think about them being too big.

Thank you for the link, again you keep surprising me.

JB SKaggs


don’t even mention that, you’re welcome and plus I’m pretty interested in what your trying to do 'cos as a former Magic fanatic I’m still missing Magic The Gathering game for PC


I have an old demo I could email you- runs terribly on Ubuntu but is playable if your careful. At one point in time I owned a Rpg / card shop and I had almost twenty thousand loose Magic TG cards in stock. Some were valuable, especially the ones that became illegal as well as Pokemon.

When I sold my shop I lost all that stuff.

Now I think I might have two 100 cards decks and both are new.

I am not totally sure if what I visualize I can do- by I am sure gonna give my all. But I want to use the cards like a spell library and then at the end of the game have this huge chess tournement where wizards fight with chess and spells to be king / queen of this fantasy land.

Actually playing this game out physically was fun wth my kids especially the chess aspect- if you set you tactics right yu could have like four pieces versus one piece and each piece controlls a certain number cards.

It was great. I probably should setup a website and post the info about this stuff. Well one day later.