Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Help with changing multiple sprite images
-
Trying to make card matching game
I have explored the game examples
I have created a 7x4 grid of paired cards ♥️ and two jokersNow I want switch the images to card backs so that selecting a card will show the card
No matter what I try I have found no way to change the image of a specific card.
I tried covering the cards with back sprites with intention of changing the z-position of the hidden card, and could not find a way to do that either.
Clearly I am not getting Sprites, and the documentation has not enlightened me..
This is my new game code
def new_game(self): self.root_node.run_action(A.sequence(A.fade_to(0, 0.35), A.remove())) self.score = 0 self.score_label.text = '0' self.start_time = self.t self.root_node = Node(parent=self, position=self.size/2) self.root_node.scale = 0 self.gamecards = [] self.gamecards = cardimages self.tiles = [] # Populate board with the 28 cards in gamecards for y, x in product(range(ROWS), range(COLS)): img_name = self.gamecards.pop () s = Tile(img_name, x, y) # s tuple "array' created to hold cards? # {{Post Solution Note: This was where I went wrong, I was seeing the cards I had created as an array, # and I assumed could address them as such}} self.root_node.add_child(s) self.tiles.append(s) self.root_node.run_action(A.scale_to(SCALE, 4, TIMING_EASE_OUT_2)) # This sets the board up and zooms out sound.play_effect('digital:ZapThreeToneUp') # Need to cover with back of card images
Any hints would be greatly appreciated.
-
You can change the texture of a sprite with
sprite.texture = Texture(“Image.png“)
im can’t remember if it’s sprite.texture or sprite.Texture, but I’m sure you can figure that out.
-
My issue is perhaps two fold, I used image rather than texture based on the match3 example.
Secondly I have not figured out how to select a single specific card sprite to change having added them in bulk using :
self.root_node.add_child(s)
self.tiles.append(s)Is it possible to use texture and img_name interchangeably?
As I said earlier, complete newby trying to run before I can breath!
-
sprites under the hood use textures. but when you instantiate using an image name or Image, the constructor automatically converts to a Texture.
-
OK after much head scratching ... I think this better describes my dilemma
I have multiple objects all named tile
Each added to the node with “add_child’
If I touch ONE of them how can I change just it’s texture
The basic issue is how to write the code to change only the ‘tile’ I just touched called tile
Put another way:
I have so many children and stupidly I named them all tile, now I want to call just one of them.w -
I think I have at last grokked it!
Thanks those that gave me the gentle nudges in the right direction!touched_tile.texture = Texture('card:HeartsA')
That said touched_tile.img_name = 'card:HeartsA' does not have any result, which is still very confusing to me!
The Object SpriteNode init
When called, returns a random identity that can be used to address the sprite on the screenThis identity must be stored for later use.
Normally this quietly happens when the coder calls the SpriteNode init Object?
eg self.player = SpriteNode('plf:AlienGreen_front') becomes an object at 0x1186a8e08
Self.player is storing 0x1186a8e08 for you...So if you create loads of aliens remember their random identity, that’s how you call them
I had foolishly got fixated on seeing the creation of a load of objects as similar to creating an array of aliens
And thus assumed that I could simply call the third alien I had created in a user friendly manner.For example self.alien(3)=Texture(‘new alien image.png) would easily change the image of said third alien.
I suppose I have learned a very simple lesson “not all languages operate at an intuitive user friendly level”, or more properly I suppose not all objects operate like I would like them to!
As I realised I was trying to magically make objects operate how I wanted them to operate.
Finally if the Manual was written for complete newby’s like me it would be much longer ( too long, and less readable) but probably would have helped me!
So perhaps the best thing would be heavily commented example code....
-
You could do
self.aliens=[] for i in range(5): self.aliens.append(SpriteNode('imagename') ) self.add_child(aliens[i])
and later, you can address a particular aluen
self.aliens[3].texture = Texture(...)
so, similar to whatnyou were trying to do.
or, for instance, you can iterate over aliens to check if your player and alien are touching
for alien in self.aliens: if self.player.bbox.intersects(alien.bbox): self.end_game()
(obviously, use the real image names)
-
btw, if you have not already, take a look in the "This Ipad" folder for the Examples folder. Inside you will find a Game Tutorial folder, which walks you through a pretty complete game from start to finish, well commented. It won't teach you python, but it hits a lot of the mechanics of the scene module
-
Thanks JonB
I’ll give that a go!Any pointers on why changing ‘img_name’ didn’t work?
@adrius42 said:
That said touched_tile.img_name = 'card:HeartsA' does not have any result, which is still very confusing to me!
-
Because img_name is not an attribute of SpriteNode! It is just a name you made up. Look back at the docs of SpriteNode.