1

i'm trying to use subsurfaces of Pygame to draw multiple cartesian plans, each one with your shapes inside your space.

At first, i call display method on init

self.screen = pygame.display.set_mode((width, height))
self.screen.fill(Colors.WHITE)

I have a surfaces dict, that storage all surfaces created.

At this part, i'm creating each subsurface, based on mainscreen, that takes all screen space.

 for canvas in canvas_list:
     surface = self.screen.subsurface(pygame.Rect(canvas.start.x, canvas.start.y,
                                                                         canvas.width, canvas.height))
     surface.fill(Colors.WHITE)
     self.surfaces[canvas.tag] = surface

I also tried to create using this way

surface = pygame.Surface([canvas.width, canvas.height])
surface.fill(Colors.WHITE)
self.surfaces[canvas.tag] = surface

And at render moment, i'm using this code to draw plan lines

half_height = canvas.height / 2
half_width = canvas.width / 2
pygame.draw.line(surface, Colors.BLACK, (0, half_height), (canvas.width, half_height))
pygame.draw.line(surface, Colors.BLACK, (half_width, 0), (half_width, canvas.height))
pygame.draw.circle(surface, Colors.BLACK, (half_width, half_height), 2, 0)

The problem is that, at to call method to render one more surface, only last surface is rendered, using different start points to Rect. I tried to use Surface.blit to fix position too, but only work when i used a new Surface, and not a surface created apart from another surface.

Follow about main code that call Pygame methods

canvas1 = Canvas(250, 250, Point(x=200, y=200), "canvas1")
canvas2 = Canvas(260, 260, Point(x=450, y=450), "canvas2")
canvas3 = Canvas(270, 270, Point(x=710, y=710), "canvas3")

Result that i got

I also tried change display.flip method location, if i call this method, after each surface object rendered, i see all surfaces blinking on each one position.

This way:

 for canvas in self.canvas_list:
     self._draw_at_canvas(canvas)
     pygame.display.flip()

Any suggestion to solve the problem

  • You actually try to define multiple clipping regions ([`set_clip`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_clip)). However, a subsurface does not define an area of the target _Surface_. A subsurface is for specifying a rectangular area of a _Surface_ and use it further. – Rabbid76 Apr 15 '22 at 04:48
  • So, one way to do that would be to define my "subsurfaces" as rects? @Rabbid76 – João Paulo de Souza Apr 17 '22 at 18:25
  • No. I don't think that subsurface is the right choice here. Subsurface are no clipping regions. I think you are trying to use them in an inappropriate way. Anyway I have no idea what you are trying to achieve, your question is completely unclear. – Rabbid76 Apr 17 '22 at 18:27
  • I understood. I'll need to find another way to do it, thanks for the tip. – João Paulo de Souza Apr 17 '22 at 18:31

0 Answers0