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)