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!