[Tutor] pygame help please

Alan Gauld alan.gauld at yahoo.co.uk
Thu Apr 18 20:10:06 EDT 2019


On 18/04/2019 15:16, fatima butt wrote:

> however the following error is coming:
> Traceback (most recent call last):
>   File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 304,
> in <module>
>     draw_lives(screen,WIDTH - 100,5, player.lives, player_mini_img)
>   File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py", line 56,
> in draw_lives
>     img.rect.x = x + 30 * i
> AttributeError: 'pygame.Surface' object has no attribute 'rect'
>>>>
> 
> def draw_lives(surf,x,y,lives,img):
>     for i in range(lives):
>         img_rect = img.get_rect()
>         img.rect.x = x + 30 * i

Note that you store the rect in img_rect but then attempt to access it
via img.rect. The first form is a variable name. The second is an
attribute access. And as the error message says img does not have
a rect attribute. You need to use your variable.



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list