Pygame Screen. Blit Not Working

Pygame Screen. Blit Not Working

In a program I am creating in python/pygame, screen.blit is not working. In all the other programs I have made, screen.blit works.

Error:
    Traceback (most recent call last):
  File "C:...\Main.py", line 46, in <module>
    screen.blit(sight.image, sight.rect)
AttributeError: 'int' object has no attribute 'blit'
4

1 Answer

The error in question is caused by the screen variable being allocated to an integer instead of a pygame.Surface() object.Perhaps you accidentally rewrote the variable somewhere?

From what I can make of what you are doing, it also seems like you are stating that the position of the blit is a full rect instead of a coordinate.

when you do:

screen.blit(sight.image,sight.rect)

What the program is seeing is:

screen.blit(sight.image,(sight.rect.x,sight.rect.y,sight.rect.width,sight.rect.height))

I am assuming you don't intend to do this. Remember that a rect object is consists of position, width and height.

To correct your code, put (sight.rect.x,sight.rect.y) where the location goes instead like this:

screen.blit(sight.image,(sight.rect.x,sight.rect.y))

the .x and .y correspond to the rect's upper left corners location in the (x,y) pixel plane. The sight image will then be blitted in that location.

If you want to read up on the the pygame.Rect object, I'll post the link to it's documentation here

I hope this helps. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.