TypeError : kollision() missing 1 required positional argument: 'self'

I have a error in the class "Baum" i dont know how to fix it. I want to code a collision so if one object of the Baum class is on a certain position , the other class stop to draw the object of the "Schlitten" class.

#Laden der Pygame Bibliothek
import pygame
import time
import random
#Initialisierung der Pygame Bibliothek
pygame.init()

# Spiel-Fenster erstellen
size = [700, 500]
screen = pygame.display.set_mode(size)
screen.fill((255,255,255))
# Noetig um die fps zu begrenzen
clock = pygame.time.Clock()

# Speichert ob das Spiel-Fenster geschlossen wurde
done = False


class Schlitten():
    def __init__(self, px, py, pscreen):
        self.FARBE1 = (139,87,66)
        self.FARBE2 = (139,90,43)
        self.braun = (104,73,71)
        self.x = px
        self.grau = (118,122,121)
        self.y = py
        self.red = (255,0,0)
        self.screen = pscreen
        self.treffer = False    

Here is the code for the undraw

    def zeichne_dich(self):
        if self.treffer == False:
            pygame.draw.rect(self.screen, self.FARBE2, [self.x,self.y,5,75])
            pygame.draw.rect(self.screen, self.FARBE2, [self.x+29,self.y,5,75])
            pygame.draw.rect(self.screen, self.braun, [self.x+5,self.y+20,24,3])
            pygame.draw.rect(self.screen, self.braun, [self.x+5,self.y+55,24,3])
            pygame.draw.rect(self.screen, self.FARBE1, [self.x+6,self.y+15,3,50])
            pygame.draw.rect(self.screen, self.FARBE1, [self.x+12,self.y+15,3,50])
            pygame.draw.rect(self.screen, self.FARBE1, [self.x+18,self.y+15,3,50])
            pygame.draw.rect(self.screen, self.FARBE1, [self.x+24,self.y+15,3,50])
            pygame.draw.rect(self.screen, self.grau, [self.x+5,self.y+10,24,2])

Here is the code for the collision

    def kollision(self):
        self.treffer = True

    def movemint(self):
        keys = pygame.key.get_pressed()
        if keys [pygame.K_LEFT] :
            self.x -= 4

        if keys [pygame.K_RIGHT] :
            self.x += 4

        if self.x < 0:
            self.x += 4

        if self.x > 665:
            self.x -= 4


    def links(self):
        return self.x

    def rechts(self):
       return self.x+34

    def oben(self):
        return self.y

    def unten(self):
        return self.y+75

class Baum():
    def __init__(self ,pos_x , pos_y ,pscreen ,pschlitten):
        self.green = (0,100,0)
        self.braun = (139,69,19)
        self.red = (255,0,0)
        self.x = pos_x
        self.y = pos_y
        self.screen = pscreen
        self.Schlitten = pschlitten



    def zeichne(self):
        pygame.draw.polygon(self.screen ,self.green , [(self.x+50 ,self.y-95),(self.x+0 , self.y-10),            
         (self.x+100,self.y-10)])
        pygame.draw.rect(self.screen , self.braun , [self.x+43,self.y-10,15,30])
        pygame.draw.polygon(self.screen , self.green , [(self.x+50 , self.y-95), (self.x+5 , self.y- 
           25),(self.x+95,self.y-25)]) 
        pygame.draw.polygon(self.screen , self.green , [(self.x+50 , self.y-95), (self.x+10 , 
         self.y-40), (self.x+90,self.y-40)]) 
        pygame.draw.polygon(self.screen , self.green , [(self.x+50 , self.y-95), (self.x+15, self.y- 
         53), (self.x+85,self.y-53)])



    def bewegung(self):
        self.y += 5


    def spawn(self):
        if self.y > 600:
            self.y = -50
            self.x = random.randrange(0,700)

And here is the code for the collision on the certain y coordinate

    def collision(self):
        if self.y > 385:
            self.Schlitten.kollision()

class score():
    def __init__(self, pbaum):
        schrift = pygame.font.SysFont("OCR A" , 30 , True ) 
        self.Baum = pbaum

    def anzeigen(self):
        score = 0

        self.text = schrift.render("Score: " + str(score)  , 0 , (0,0,0))
        screen.blit(self.text , (550 , 10))  

#Objekt der Klasse Schlitten erzeugen
spieler1 = Schlitten(350,400,screen)
Score = score(Baum)
#Objekt der Klasse Baum erzeugen
Baum1 = Baum(500,0 ,screen , Schlitten)
Baum2 = Baum(300,-525 , screen , Schlitten)
Baum3 = Baum(100,-1050 , screen, Schlitten)



schrift = pygame.font.SysFont("comicsans" , 30 , True ) 
# -------- Haupt-Schleife -----------
while not done:
    # Ändert den Wert von done auf True, falls Spiel-Fenster geschlossen wird
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True



    # --- hier Zeichenbefehle ergänzen---

    # Screen mit weiß fuellen
    screen.fill((255,255,255))

    pygame.mixer.music.set_volume(0.1)



    Score.anzeigen()


    # Schlitten zeichnen
    spieler1.zeichne_dich()
    spieler1.movemint()




    # Baeume zeichnen
    Baum1.zeichne()
    Baum1.bewegung()
    Baum1.spawn()
    Baum1.collision()




    Baum2.zeichne()
    Baum2.bewegung()
    Baum2.spawn()
    Baum2.collision()




    Baum3.zeichne()
    Baum3.bewegung()
    Baum3.spawn()
    Baum3.collision()









    # Maximale fps angeben
    clock.tick(60)

    # Bildschirm updaten um gezeichnete Objekte darzustellen
    pygame.display.flip()

# Pygame beenden, nachdem Haupt-Schleife beendet wurde
pygame.quit()

1 answer

  • answered 2020-03-25 14:33 Rabbid76

    kollision is an instance method. The method has to be invoked by an instance object of the class Schlitten. See further Method Objects and Instance Objects.
    You have to pass an instance of the class Schlitten to the constructor of Baum, to solve the issue. e.g.:

    spieler1 = Schlitten(350,400,screen)
    
    Baum1 = Baum(500, 0, screen, spieler1)
    Baum2 = Baum(300, -525, screen, spieler1)
    Baum3 = Baum(100, -1050, screen, spieler1)