Blit method bug?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Blit method bug?

babaliaris
This post was updated on .
Hello!

i have a problem with blit. When i try to blit a lot of images using a for loop (for example 800) after a certain amount, the blit is messing up the drawing.

See the following video i made: Blit Problem Video

No matter how many images i'm blitting, because of my code, none of this images should be on top of another image. But as you can see on the video, after a certain amount, blit is messing up.

You can clearly see all my code in the video.

Can anyone tell me if this is a bug or if not, what is the problem?

I'm using pygame 1.9.3 , and python 3.6.1 (Installed via pip).

Thank you.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Blit method bug?

Martin Kühne
On Thu, May 25, 2017 at 1:06 PM, babaliaris <[hidden email]> wrote:

> Hello!
>
> i have a problem with blit. When i try to blit a lot of images using a for
> loop (for example 800) after a certain amount, the blit is messing up the
> drawing.
>
> See the following video i made:  Blit Problem Video
> <https://www.youtube.com/watch?v=3L2tL5TW1S0>
>
> You can clearly see all my code in the video.
>
> Can anyone tell me if this is a bug or if not, what is the problem?
>


Let's be generous and say that this is not a bug, but rather a
technical limitation. Obviously pygame uses 16 bit integers for
coordinates internally, so that when you use coordinates higher than
65535, they wrap around to 0 again.
The question is, whether silly input values deserve silly output.
Personally, I see nothing wrong with that. I'm not sure you even could
attach more than 4 4k screens in a row to a modern computer, but that
would only amount to maximally quarter of the 65536 line which you
crossed in your video.

You can make sure that is the case by passing sensible values to your
blit function. For example, you could not blit at all if the
coordinates are outside the screen rectangle with the following
addition (to replace the blit line you already have):

image_pos = (x + i, y + i)
if screen.get_rect().colliderect(pygame.Rect(image_pos, image.get_size())):
    screen.blit(image, image_pos)

cheers!
mar77i
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Blit method bug?

Noel Garwick
In reply to this post by babaliaris
Hi babaliaris,

Glad to see you're trying out pygame.  Just a suggestion, but even though you can see the code in the video, it's still a good idea to paste or provide a link to it in your email.

This is something I've encountered before. It doesn't really have anything to do with the number of images you're blitting (only a very small percentage of those are even being written to the screen, see?) It's a limitation due to the coordinates being 16-bit integers.  When those go past  32,767 (for signed, which allow negative and positive values), it starts back at -32,767 and continues until we hit positive values you would see on the screen.  Since those are starting at an odd number, you end up with the 'offset' images you see in your output.

Python 3 supports much larger numbers.  iirc, the default for an integer is as much as a full Long -- so it should match the maximum allowable value for your environment.  Pygame is still using 16-bit integers for blitting, though.  One thing to remember is that all of those very large values are wayyyy outside the coordinates you are actually using in your display 600x600 window.  Anyway, you will want to do something like storing the absolute location of an object and also the location (rect) of a 'camera'.  Then you can do a calculation to find where you should display the object (if at all) by finding out where it is in relation to your camera rect.  I usually keep track of 2 rectangles per object; a hrect (as in "hit" rect) for absolute value / collisions, and a rect that is its location in relation to the camera (and named 'rect' so it will be used by the sprite group's draw method). Something like..

class Thing(pygame.sprite.Sprite):
def __init__(self, x, y):
    self.x, self.y = x, y
    self.image = pygame.image.load("dude.png")
    self.rect = self.image.get_rect()

    self.hrect = self.rect.copy()
    
    self.hrect.topleft = self.x, self.y
...
def update(self, camera):
    # logic to update self.x and self.y goes here

    self.hrect.topleft = self.x, self.y
    self.rect.topleft = self.x - camera.x, self.y - camera.y



Hope that helps.

On Thu, May 25, 2017 at 7:06 AM, babaliaris <[hidden email]> wrote:
Hello!

i have a problem with blit. When i try to blit a lot of images using a for
loop (for example 800) after a certain amount, the blit is messing up the
drawing.

See the following video i made:  Blit Problem Video
<https://www.youtube.com/watch?v=3L2tL5TW1S0>

You can clearly see all my code in the video.

Can anyone tell me if this is a bug or if not, what is the problem?

Thank you.



--
View this message in context: http://pygame-users.25799.x6.nabble.com/Blit-method-bug-tp3039.html
Sent from the pygame-users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Blit method bug?

babaliaris
This post was updated on .
In reply to this post by babaliaris
Thank's both of you! I didn't know that pygame uses 16 bits integers (I thought it was 32 bits every integer i put in the blit coordinates).

I already thought to blit only images which the rectangle of it is at least inside the screen resolution.
(Excuse me for my english).

For example (How do i input code by the way?):

[code]
for sprite in all_game_objects:
    if sprite.onScreen():
        sprite.render(self.pygame_screen_surface)
[/code]

I'm just trying to create a wrapper for pygame that will work as a game engine similarly to the Unity Game Engine (but only for 2d games creation), thats why i'm trying to master pygame because i had problems like fps drop, and this one.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Blit method bug?

Martin Kühne
On Thu, May 25, 2017 at 6:54 PM, babaliaris <[hidden email]> wrote:
> Thank's both of you! I didn't know that pygame uses 16 bits integers (I
> thought it was 32 bits every integer i put in the blit coordinates).
>

How comes you'd think such a thing? Python itself already ships
variable sized integers and keeps track of their size...

> I already thought to blit only images which the rectangle of it is at least
> inside the screen resolution.
> (Excuse me for my english).
>

Read my email again. I think I covered that already [0].

cheers!
mar77i

[0] https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect
Loading...