#๐Ÿ”’ Pygame Rect AttributeError

17 messages ยท Page 1 of 1 (latest)

twilit ravine
#

Even though I have clearly set a rect for cheese it still gives me an error.
https://paste.pythondiscord.com/3VWQ

File "c:\Users\user\OneDrive\Desktop\My Things\Code\Python\cheese chase\main.py", line 186, in <module>
main_menu()
File "c:\Users\user\OneDrive\Desktop\My Things\Code\Python\cheese chase\main.py", line 177, in main_menu
play()
File "c:\Users\user\OneDrive\Desktop\My Things\Code\Python\cheese chase\main.py", line 89, in play
all_sprites.update()
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygame\sprite.py", line 555, in update
sprite.update(*args, **kwargs)
File "c:\Users\user\OneDrive\Desktop\My Things\Code\Python\cheese chase\main.py", line 21, in update
self.check_collisions()
File "c:\Users\user\OneDrive\Desktop\My Things\Code\Python\cheese chase\main.py", line 32, in check_collisions
if self.rect.collidepoint(cheese.rect):
^^^^^^^^^^^
AttributeError: type object 'cheese' has no attribute 'rect'

simple wharfBOT
#

@twilit ravine

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.

dapper nymph
#

cheese.rect
this refers to the type itself, not an instance, only instances of cheese have a rect

soft hinge
#

this, you probably meant to use self.rect there, in your check_collisions function

timid musk
#

pygame.rect.collidepoint() wants an (x, y) tuple as the point, not another rect

hard tulip
#

But that wouldn't cause this error.

timid musk
#

No but just saying, also there are other methods to collide rects if necessary

#

Suggest changing the names of your classes to uppercase words Mouse and Cheese and instancing the objects similar to this. Note that I'm passing the instanced cheese object into the mouse instance. In the Mouse __init__ method, that cheese should be assigned to an attribute so the check_collision() method has a reference to it py all_sprites = pygame.sprite.Group() cheese = Cheese() mouse = Mouse((ScreenSize.current_w * 0.5, ScreenSize.current_h - 150), cheese) all_sprites.add(mouse) all_sprites.add(cheese) cheese_group = pygame.sprite.GroupSingle() cheese_group.add(cheese) (may not be exactly what you need but the idea is to instance the objects into variables that can be referenced)

timid musk
#

Also, there is a pygame.event that triggers every time the window is resized. Suggest handling the button reassignments in the event loop only when that event happens. This can (will) improve performance considerably, also, the image loading shouldn't be in the main loop at all, those images should be loaded into global variables that are referenced in the main loop

twilit ravine
#

Thanks everything seems to be working

timid musk
#

It just looks like this might be starting to setup a resizable window, I don't know if that is the intention though. Just trying to understand why you have those button being created in the main loop insstead of before it (where you assign other things)

twilit ravine
#

Oh I have no idea lol

#

I'm going to switch that right now lol

#

!solved

simple wharfBOT
#
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.