0

So I'm working on a hobby game, and i'm not able to get one class to inherit another properly. The file structure is as shown below:

main.py
Enemy
    |walker.py
    |genericEnemy.py

main.py calls walker.py, whose main class inherits from genericEnemy.py. Both of their contents are below:

walker.py

import pygame
from .genericEnemy import generic

class walker(generic):
    pass    

genericEnemy.py

'''
    This class controls generic capabilities for all enemies.
    Specific abilities are in the enemy type's class in the superfolder
'''

import pygame

class generic:
    def __init__(self, speed, pos, size):
        '''
            speed - The entity speed, an int greater than 0
            pos - The (x,y) position of the entity, a list of length 2
            size - The entity hitbox, a list with length 2
        '''

        #Movement Variables
        self.speed = speed
        self.currDir = 1
        self.isMoving = True

        #Drawing Variables
        self.pos = pos
        self.size = size

        #Gravity Variables
        self.isJumping = False
        self.fallCounter = 0
        self.gravityTimer = 0

#==================================================

        def draw(self, surface):
            pygame.draw.rect(surface, (255, 0, 0), (self.pos[0], self.pos[1], self.size[0], self.size[1]))

#==================================================

        def updateGravity(self):
            self.fallCounter += 1
            
            if self.fallCounter == 8:
                self.fallCounter = 0
                self.gravityTimer += 1

#==================================================

        def walk(self):
            if self.isMoving:
                self.pos[0] += self.speed * self.currDir

The issue I'm having is that in main when I say:

SCREEN = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
ENEMY = walker(6, [120, 1000], [10, 30])

and then later on

ENEMY.draw(SCREEN)

I get the error: AttributeError: 'walker' object has no attribute 'draw'

Any help would be greatly appreciated, like I said this is a hobbyist project, so i'm fairly inexperienced in python/pygame

2
  • 4
    The indentation in the generic class is wrong. You have all your other methods inside the __init__ method. De-indent them one level. Commented Jul 14, 2021 at 23:40
  • This is not an inheritance problem. You would have the same problem if you created an instance of generic without inheriting. Commented Jul 14, 2021 at 23:41

2 Answers 2

1

User Barmar was correct. I, as an absolute walnut, had my functions indented too far.

Sign up to request clarification or add additional context in comments.

Comments

0

Use class generic(pygame.sprite.Sprite):
and

all_sprites = pygame.sprite.Group()
all_sprites.add(ENEMY)
all_sprites.draw(SCREEN)

pygame sprite group has draw function
And maybe it need sprite.image!
It can be surface
self.image = pygame.Surface([self.size[0], self.size[1]])
self.image.fill((255, 0, 0))

or image file

self.image = pygame.transform.scale(pygame.image.load(imgname).convert_alpha(), (self.size[0], self.size[1]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.