1
I know to get a color of a pixel in Pygame I use get_at
.
But I have a huge background image and a lot of it is outside the screen area delimited by pygame.
For example, a 1000x1000 image inside a 500x500 screen will be only 50% visible, the rest will be "outside" of the screen. So I need to access this part OFF the pygame screen.
But if I make reference to get_at
with a coordinate larger or smaller than the screen dimensions, Pygame gives an error:
Indexerror: pixel index out of range
import pygame
W = 600
H = 400
pygame.init()
screen = pygame.display.set_mode([W, H])
image = pygame.image.load('huge_background.png').convert_alpha()
screen.blit(image, [-5000, -420])
pygame.display.update()
screen.get_at([100, -5]) # gera o erro "IndexError: pixel index out of range"
Would anyone know how I can access a pixel beyond the visible area of the pygame screen?
It is a simulation of vehicles inside a track.
Here is the currently visible pygame screen (1600 x 600). The 3 lines (magenta) are the collision "detectors":
And here is the background (track) that will roll according to the position of the car (8000 x 8000):
This would be a view of the screen (quado in red) and the global plane with the background image (track):
Thus, collision detector lines may exceed pygame screen limits, but need to "read" external pixels to detect collision.
Thank you so much for the excellent explanation! Little by little I’ll get acquainted with the concepts of pygame. I understood that the concept of Surface, where the background image is a Surface and the screen as well and can be manipulated independently.
– Rogério Dec
I edited the original question and added some images and explanations. I understand that I can use the
get_at
independently on the background image and not on the screen. But in addition to the background image, there may be OTHER CARS and other previously 'blighted' objects. That is, it would be a composition of the background image with other objects on top. How to treat the collision in this case' composite'?– Rogério Dec
the collision with other cars has to be compared with the coordinates of the other cars in the game, not with respect to their pixels on the screen - for this we will use the concept of Sprites and groups, and the collision functions embedded in the pygame. I suggest you unfold the question, and make the doubt in the above comment another question here in stackoverflow - since they will be distinct concepts. I or someone else will answer.
– jsbueno