How do I assign the sprite layer in the function definition in pygame?

I am trying to code a game called ‘Scrolling Shooter’ with Pygame. I am trying to draw sprites over each other in the right order. In the initialization function of each sprite class, I assign an attribute called ‘layer’. According to the pygame docs, this should change the layer of the sprite in the sprite in the allSprites = pygame.sprite.LayeredUpdates() function and the sprites with a higher value of layer should be drawn on top of sprites with a lower value. Despite the parameter, if I use get_layer_of_sprite in the draw function, it just gives 0. Somebody on the r/pygame subreddit said that the attribute should be _layer instead of layer but that didn’t work either.

The allSprites.draw(display) function is in my draw() funtion. The draw() function is called in each iteration of the main loop.

You can find the relevant code here: https://github.com/CiriOfRivia/Pygame-Scrolling-Shooters/blob/master/hi

You can find the full code here: https://github.com/CiriOfRivia/Pygame-Scrolling-Shooters/blob/master/Layering%20issue

Pygame docs: “If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains ‘layer’ then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.”

Thanks in advance for helping me. I probably did one little thing wrong which messes up everything, which is so often the case when I’m coding.

If I add the layer to the sprites after initializing them, it seems to work fine. However, I want to be able to set the layer in the initialization function.

Expected result: The layer of the plane is higher than the layer of the island, thus the plane should be drawn over the island. The output should be: Plane 3 Island 1

Actual result: The layer of the plane is equal to the layer of the island. The drawing order is random and the island gets drawn over the plane The output is: Plane 0 Island 0

Aligning a sprite with a tilemap

I’m trying to create a hybrid system for my Unity game. I want to use tilemap for collision and use sprites for the world.

I drew a sprite onto a 40×40 checkerboard grid as can be seen here. I wanted an easy way to create a collider for this without spending 30+ minutes hand placing box colliders and I decided to use a tilemap to place red tiles and use a tilemap collider for collision geometry and just hide the red tiles in-game.

Issue is, I have no clue how to align the checkerboard with the Unity tilemap grid.

Here’s a screenshot of the sprite in the unity editor with the tilemap grid visible.

If it isn’t obvious, then the white lines is the grid for the tilemaps. The cellsize for each cell on the grid isn’t the same size as the cells on the checkerboard pattern and I have no idea how to fix it.

The PPU (pixels per unit) of the sprite is 140. The PPU for the tileset tile(singular since there is literally only 1 tile in the entire tileset) is also 140. The checkerboard in paint.net is 40×40 and so is the tile. How can I get the cellsize of the grid in unity to match the cell size of the checkerboard.

Why the touch to the screen is fixed lower than the sprite is located?

That’s how I clog an array of sprites, each of which is slightly higher than the previous one.

private void building(){     //тут будет забивать массив спрайтами     for( int i = 0; i < numberOfElements; i ++){         sprite = new Sprite(img);         sprite.setX(positionX);         sprite.setY(positionY);         caterpillar.add(sprite);         positionY -= 100;     } } 

This is how I draw these sprites.

public void demonstration(){     batch.begin();     for(Sprite i : caterpillar){ batch.draw(i, i.getX(), Gdx.graphics.getHeight()-i.getY());}      batch.end(); } 

That’s how I track the touch to the sprite.

public boolean touchDown(int screenX, int screenY, int pointer, int button) {     for(Sprite i : ourCaterpillar){ if(i.getBoundingRectangle().contains(screenX, screenY)) System.out.println("!!"); }     System.out.println(screenX +" "+screenY);     return true; } 

As a result, it turns out that the touch is fixed only if you poke lower than the sprite is located. What am I doing wrong?

How to scale a sprite on x axis to touch the point without changing pivot?

I have a sprite. The pivot is at random point. I want to increase a sprite scale on x axis without moving the pivot till touching a point at right. I have a method to set/add size. I calculate a distance from a sprite edge to a right point and add it as a size.

The problem is the sprite scale increases its size in both directions of x axis (left and right) considering the pivot is not at the left edge. How to solve the problem?

I have: https://imgur.com/kXe1E52 I want to get: https://imgur.com/Awijq3T

libgdx прописывание в параметрах или создание нового sprite

вот у меня есть класс, который вызывает два метода других классов, которым нужен Sprite, что мне лучше сделать? 1) прописать в классе, из которого вызываются методы 1 раз спрайт и передать его в параметры вызова методов? 2) в каждом классе, создать свой Sprite?

или способ лучше есть?

Is there a way for a sprite familiar to gain the senses of a Warlock such as darkvision?

A third level High Elf Pact of the Chain Warlock with an Archfey patron casts the find familiar spell and then chooses a sprite form for the spirit to take.

The Warlock commands the familiar to enter a dark cave, and can see with darkvision through the familiar’s eyes. The sprite does not have darkvision and, therefore, either must have a light source or be guided by the warlock’s commands (such as “go right 10′”). The sprite is blinded by the dark.

This was the implementation of the rules by the DM in the game situation which I agree with. After checking the Player’s Handbook, I can’t see any argument against this ruling. I want to keep the sprite familiar, and I rarely use the familiar in game, I keep it for specific situations, one of which could be the “dark cave” situation.

Is there a way for the sprite to gain the senses of the Warlock such as darkvision?

My question is about the possibility of a Plan B to enable a “ninja sprite” in this situation, either now or in the future.

Include abilities, magic items, feats and spells which can be accessed at higher levels (including multi-classing) that might help, and also include tactics for different ways to use the familiar in this situation. I would prefer to exclude the option for the familiar take another form such as pseudodragon to avoid the “choose a different familiar” for role-play reasons.

The DM already ruled on the question posed here, so I would need some more evidence before going to “ask the DM” again about the ruling. The best answer could be: “No, there is no Plan B, your sprite will always enter a dark cave blinded, go with it and enjoy the challenge!”.

Unity not redrawing sprite, after sprite change

I am making a game where the player mines dirt by walking on it: Too many squares So, I created a scene and added a bunch of dirt to a grid, from a dirt prefab. When the character walks on a square, it is supposed to change to dark dirt. If I only have a few hundred dirt squares, it works as expected. I step on the dirt, and it turns dark. But now that I have a few thousand dirt squares, unity renders incorrectly, and will only change the sprite for dirt squares that are in every 4 or 5 columns. See the following picture: Wrong Mining

The debugger outputs GetComponent<SpriteRenderer>().sprite.name for the dirt I walk on is DarkDirt even though it is showing up as “Dirt” in reality. The inspector in the scene view agrees that it should be rendering DarkDirt.

How can I get unity to re-draw sprites when they update?

No me aparece el sprite de la clase padre

Teniendo esta clase hija:

import galapagoo import random import drawlib 

class Radio_Galapago(galapagoo.Galapago):

def __init__(self):     drawlib.start_render()     self.sprite=drawlib.make_sprite("/home/Documentos/Proyecto2/microbit.png",0.1)     n= random.randint(0,768)     x=drawlib.set_center_x(self.sprite,n)     y=drawlib.set_center_y(self.sprite,n)     drawlib.set_angle(self.sprite,0)     galapagoo.Galapago.l_sprite.append(self.sprite)     drawlib.finish_render()     self.x= n     self.y= n 

Y este codigo de la clase padre:

import drawlib import colors import math 

class Galapago:

tortugas=[] l_line=drawlib.make_line_list() l_sprite= drawlib.make_sprite_list()  def __init__(self):     drawlib.start_render()     sprite=drawlib.make_sprite("/home/nuria/Documentos/Proyecto2/ship (6).png",0.5)     drawlib.set_center_x(sprite,0)     drawlib.set_center_y(sprite,0)     drawlib.set_angle(sprite,90)     drawlib.finish_render()     self.x= 0     self.y= 0     self.angle= 90     self.color= colors.BLACK     self.widht= 1     self.sprite=sprite     self.l_sprite.append(self.sprite)     self.pen= 1 #con el 1 pintará con el 0 no hara nada     tortuga={} #Crea el diccionario de una unica tortuga     tortuga['sprite']=self.sprite     self.tortugas.append(tortuga) #Lleva el diccionario de una unica tortuga a una lista donde estan todas las tortugas 

Cuando en otro fichero mando drawlib.draw_sprite(galapagoo.Galapago.l_sprite) unicamente consigo que me aparezca en pantalla el sprite de la hija. Alguna idea de por que esto sucede cuando estoy almacenando ambos sprites en la misma lista?

Drawing each sprite at different location

I can draw multiple sprites but they are all on top of each other at (0,0). How would I make each sprite appear after one another in a row?

My Rect class and Draw function:

class Rect(pygame.sprite.Sprite):     def __init__(self,color,width,height,value=0):         super().__init__()         self.image = pygame.Surface([width, height])         self.image.fill(color)         self.rect = self.image.get_rect()         self.value = value         self.x = 0         self.y = 0     def change_value(self,color,value):         self.image.fill(color)         self.value=value  def DrawRects():     rect1 = Rect(red, boxW, boxH)     rect2 = Rect(black, boxW, boxH)     rects.add(rect1, rect2)      rects.draw(screen)