#πŸ”’ AttributeError: 'Player' object has no attribute 'image'

19 messages Β· Page 1 of 1 (latest)

dense swift
#

trying to draw a sprite to a screen using pygame, and keep running into this error:

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

here's my code:

import pygame

pygame.init()
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.knight_image = pygame.image.load(r'pkmnclone\brackeys_platformer_assets\sprites\knight - Copy.png')
        self.knight_rect = self.knight_image.get_rect(center = (960, 540))


screen = pygame.display.set_mode((1920, 1080))
clock = pygame.time.Clock()
pygame.display.set_caption('Pokemon Clone')

game_active = True
bg = pygame.image.load(r'pkmnclone\Glacial-mountains-parallax-background_vnitti\background_glacial_mountains.png')
bg = pygame.transform.scale(bg, (1920,1080))


#groups
knight = pygame.sprite.GroupSingle()
knight.add(Player())
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit

    if game_active:
        screen.blit(bg, (0,0))
        knight.draw(screen)
        pygame.display.update() #updates screen to show new elements
        clock.tick(60)
twin trailBOT
#

@dense swift

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fickle goblet
#

!traceback

twin trailBOT
#
Traceback

Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.

A full traceback could look like:

Traceback (most recent call last):
  File "my_file.py", line 5, in <module>
    add_three("6")
  File "my_file.py", line 2, in add_three
    a = num + 3
        ~~~~^~~
TypeError: can only concatenate str (not "int") to str

If the traceback is long, use our pastebin.

dense swift
dense swift
# fickle goblet Can you send the full traceback?
Traceback (most recent call last):
  File "c:\Users\probr\VS Studio Code Projects\pkmnclone\pkmnclone.py", line 31, in <module>
    knight.draw(screen)
  File "C:\Users\probr\VS Studio Code Projects\.venv\Lib\site-packages\pygame\sprite.py", line 572, in draw
    surface.blits(
  File "C:\Users\probr\VS Studio Code Projects\.venv\Lib\site-packages\pygame\sprite.py", line 573, in <genexpr>
    (spr.image, spr.rect, None, special_flags) for spr in sprites
     ^^^^^^^^^
AttributeError: 'Player' object has no attribute 'image'
#

here u go sorry for the delay

fickle goblet
#

Yeah that's what I suspected

#

self.knight_image / self.knight_rect should be self.image / self.rect instead

dense swift
#

but why?

#

do sprite attributes specifically need to be named those things?

fickle goblet
dense swift
#

okay that clears a lot up

#

thank you!

#

!close

twin trailBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.