1
votes

I am experiencing a problem when it comes to drawling a sprite in Group(). I plan to later draw multiple sprites when this issue is resolved.

I tried a couple of things to resolve, such as renaming the class name, simplifing the code to the fundamentals (having the image just be added as a sprite in Group()). I even went on to check my pygame practice program and confirmed that I indeed messed something up with this project. I do not think that reinstalling Pygame libary will resolve the issue either.

In all cases, here are my codes:

main.py

import sys
from pygamerectangle import Background
from settings import settings
import GFunctions as gf
from pygame.sprite import Group

if __name__ == "__main__":
    pygame.init()
    setting = settings()
    screen = pygame.display.set_mode((setting.width, setting.height))
    pygame.display.set_caption("Pygame Theory")
    background = 60,60,60

    BackgroundGroup = Background()
    BackgroundGroup = Group()

    gf.BackgroundRepeat(BackgroundGroup)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        screen.fill(background)
        BackgroundGroup.draw(screen)
        pygame.display.flip() 

pygamerectangle.py

import pygame
from pygame.sprite import Sprite

class Background(Sprite):
    def __init__(self):
        super().__init__()
        self.img = pygame.image.load("background.png")
        self.rect = self.img.get_rect()

GFunctions.py

import pygame
from pygamerectangle import Background

def BackgroundRepeat(BackgroundGroup):
    BackgroundSample = Background()


    BackgroundGroup.add(BackgroundSample) # Used as part of the fundamentals to add a single sprite.
                                          # The code after this contains my plan to repeat the image;
                                          # similar to old-school websites that had repeating
                                          # backgrounds.

  #  BackgroundSampleWidth = BackgroundSample.rect.width
 #   ScreenAvailableSpace = setting.width - 2 * BackgroundSampleWidth
#    NumberOfBackgrounds = int(ScreenAvailableSpace / (2 * BackgroundSampleWidth))

   # for BackgroundRepeating in range(NumberOfBackgrounds):
   #     Backgrounds = Background(setting, screen)
   #     Backgrounds.x = BackgroundSampleWidth + 2 * BackgroundSampleWidth * BackgroundRepeating
   #     Backgrounds.rect.x = Backgrounds.x
  #      BackgroundGroup.add(Backgrounds)

Note: Under GFunctions.py, I understand that some things will not work, once I uncomment the codes, as they are missing arguements such as settings. I did this temporary as I understood that it was not part of the problem.

And here is the error I am getting:

AttributeError: 'Background' object has no attribute 'image'


For those who are wondering what is my background image: It's an 8x8 image.


Thanks in advance!

2
It looks like in your Background class, the image is stored in self.img, not self.image. Making sure you're using self.img in your code - schwartz721
@schwartz721, to my surprise, that actually fixed the error. All I had to do was to rename self.img to self.image (And some other minor tweaks that is associated with it). Either I assume that Pygame made the code be taking a variable that is extactly named image, or this was just simply a wrong naming that I just did. Either way, thank you very much for your solution! Is there any way that I or someone can mark your comment as an answer to this question that I made? - ATOPL
I wrote up an answer so you can accept it - schwartz721

2 Answers

0
votes

The program is raising an AttributeError on 'image', which means that your Background object doesn't have an 'image' attribute. From looking at your code, I can see that Background objects do have an 'img' attribute:

self.img = pygame.image.load("background.png")

So somewhere in your code you must have accidentally typed self.image instead of self.img. Make sure these match, and the error should disappear.

0
votes

While class names should normally use the CapWords convention, variables names should be lowercase. See Style Guide for Python Code.

Th issue are the lines

BackgroundGroup = Background()
BackgroundGroup = Group()

After this lines, BackgroundGroup is an instance of Group and has no attribute .image.

Change the names of the variables. Use different and lowercase names. e.g.:

backgroundSprite = Background()
backgroundGroup = Group()

gf.BackgroundRepeat(backgroundSprite)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill(background)
    backgroundGroup.draw(screen)
    pygame.display.flip() 
def BackgroundRepeat(backgroundGroup):

    backgroundSample = Background()
    backgroundGroup.add(backgroundSample)