0

I have the Margin class that I want to inject into the Triangle class

I'm using pygame and I'm really new to it so I don't know how to correctly inject the class

when executing the code it does not show me any error, but the image does not stay in the established margins

this is the Margin class I want to inject

# screen size
ANCHO = 600
ALTO = 600

class Margen(pygame.sprite.Sprite):
   def __init__(self):
       super().__init__()
       self.image = pygame.image.load("trian.png").convert()
       # Obtiene el rectángulo (sprite)
       self.rect = self.image.get_rect()
   def update(self, izquierda, derecha):
       if izquierda < 0:
           self.rect.left = 0
       if derecha > ANCHO:
           self.rect.right = ANCHO

this is the triangle class where I want to inject the margins

class Triangulo(pygame.sprite.Sprite):
    # sprite del jugador
    def __init__(self, margen:Margen):
        self.margen_ = margen
        # Heredamos el init de la clase Sprite de Pygame
        super().__init__()
        # Rectángulo (jugador)
        # self.image = pygame.Surface((200, 200)) #dibujar un cuadrado en la superficie
        self.image = pygame.image.load("trian.png").convert()
        # Obtiene el rectángulo (sprite)
        self.rect = self.image.get_rect()
        # Centra el rectángulo (sprite)
        self.rect.center = (ANCHO // 2, 600)
        # velocidad del personaje  (inicial)
        self.velocidad_x = 0
        self.velocidad_y = 0

    # mover el triangulo
    def update(self):
        # velocidad predeterminada cada vuelta del bucle
        self.velocidad_x = 0
        self.velocidad_y = 0

        teclas = pygame.key.get_pressed()  # teclas pulsadas
        if teclas[pygame.K_LEFT]:
            self.velocidad_x = -6
            # print(self.velocidad_x)
            # print(self.rect.x)
        if teclas[pygame.K_RIGHT]:
            self.velocidad_x = 6
        if teclas[pygame.K_UP]:
            self.velocidad_y = -6
        if teclas[pygame.K_DOWN]:
            self.velocidad_y = 6

        # disparo

        if teclas[pygame.K_SPACE]:
            jugador.disparo()

        # guardar nueva posicion
        self.rect.x += self.velocidad_x
        self.rect.y += self.velocidad_y

        # this is where I want to inject the Margin class
        izquierda = self.rect.left
        derecha = self.rect.right

        self.margen_.update(izquierda,derecha)
        '''
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > ANCHO:
            self.rect.right = ANCHO'''

        # margen abajo

        if self.rect.bottom > ALTO:
            self.rect.bottom = ALTO

        # limite superior
        if self.rect.top < 200:
            self.rect.top = 200

    def disparo(self):
        bala = Disparos(self.rect.centerx, self.rect.top)  # posición en centro del rectangulo jugador
        balas_sprite.add(bala)
BYCR
  • 1
  • 1
  • we can't run it to see how it works and what makes problem - so I can only suggest to use `print()` to see which part of code is executed and what you have in variables. It is called `"print debuging"` and it helps to see what code is doing. OR learn how to use real debuger. – furas Mar 19 '22 at 00:16
  • I have no idea what you want to do with this margin. You don't set position on start, you load the same image `trangle.png` (why?), so finally I have no idea what for is this call, and maybe you should do all directly in `Triangle. We can't run it, we can't read in your mind - you have to describe all details in question (not in comments) – furas Mar 19 '22 at 00:21
  • sorry, here is the complete code to make me understand better. https://github.com/bycr/DisparosJuego.git I should implement dependency injection but I really don't know-how – BYCR Mar 19 '22 at 00:42
  • Can you try to translate the variable names into English? It's way harder to understand the code for us now. – The_spider Mar 19 '22 at 09:34
  • yes of course, I just updated the repository with the translation – BYCR Mar 19 '22 at 12:44

0 Answers0