Vladimir Shevyakov
Vladimir Shevyakov

Reputation: 2841

Strange pygame Rect glitch in python 3

I'm currently making a game on pygame in Python 3. One part of my code is:

if (ballrect.bottom >= brick.top) and (ballrect.top <= brick.bottom) and (ballrect.left >= brick.right) and (ballrect.right <= left):

where ballrect and brick are pygame.Rect variables. When ran, the program crashes and I get the following:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "E:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "E:/Comp_Sci/game-balls/pad_bounce_3.py", line 158, in <module>
if (ballrect.bottom >= brick.top) and (ballrect.top <= brick.bottom) and (ballrect.left >= brick.right) and (ballrect.right <= left):
NameError: name 'left' is not defined

and 'left' is supposed to be an attribute of the pygame.Rect class. Even more interestingly, I use the 'left' attribute several times in my code and it works fine...

What am I doing terribly wrong?

EDIT: I found the issue. What was happening is that, at times, ballrect would sometimes not be defined.

Upvotes: 0

Views: 126

Answers (2)

Vladimir Shevyakov
Vladimir Shevyakov

Reputation: 2841

I found the issue. What was happening is that, at times, ballrect would sometimes not be defined.

Upvotes: 0

Oisin
Oisin

Reputation: 781

The variable 'left' at the end of the condition, is it defined? and (ballrect.right <= left): Did you mean brick.left? Try: if (ballrect.bottom >= brick.top) and (ballrect.top <= brick.bottom) and (ballrect.left >= brick.right) and (ballrect.right <= brick.left): From what I can tell it might be better to use brick.collidepoint(ballrect)

Upvotes: 1

Related Questions