Advertisement

PyGame subsurfaces displays undesired duplicated imagry.

Started by September 06, 2018 02:14 AM
2 comments, last by joeldesante 6 years, 4 months ago

I hope this is the correct place to post this, but I have been having some issues. I am new to this site and to python/pygame so please humor me.

My game is suppose to have a four way split screen so that four players can compete against each other at once. The way I went about creating this is by making an "action_surface" that acts as the main canvas and four subsurface's that are positioned into four quadrants. As I create each subsurface object, I store it into a list so that each "camera" (aka. subsurface) can be easily accessed. Here is the code for that:

Camera.py


import pygame, random

class Camera:
	cameras = []

	def __init__(self, screen, action_surface, screen_size):

		self.screen = screen
		self.surface = action_surface.subsurface(pygame.Rect(0,0,screen_size[0]/2,screen_size[1]/2))

		Camera.cameras.append(self.surface)

		cam_count = len(Camera.cameras)
		print(cam_count)
		print(Camera.cameras)

	@staticmethod
	def update(screen, screen_size):

		for s in range(0,100):
			pygame.draw.rect(Camera.cameras[1], (0,255,255), pygame.Rect(s*100, s*100, 10, 10))
			pygame.draw.rect(Camera.cameras[3], (0,0,255), pygame.Rect(s*110, s*110, 10, 10))

		if len(Camera.cameras) > 0:
			for i in range(0, len(Camera.cameras)):
				if (i%2==0):
					screen.blit(Camera.cameras[i], (0, ((i/2)*(screen_size[1]/(len(Camera.cameras)/2)))))
				else:
					screen.blit(Camera.cameras[i], ((screen_size[0]/2), (((i/2)-0.5)*(screen_size[1]/(len(Camera.cameras)/2)))))

In theory, what this should allow me to do is access each camera from their index and draw to the individually. Unfortunately, this is not what happens.

In reality, when I select any of the camera's and attempt to draw art, all the cameras (surfaces) are updated with a duplicate copy of such art!

As you might be able to tell, this is not the desired effect I am going for. But, none the less this has happened. I am hoping that maybe somebody with more experience than me can give some insight on how to fix this problem.

Thanks.

-Joel DeSante

You're always passing a Rect(0, 0, W/2, H/2) into the call to `subsurface()`. That means all cameras are going to use the same (0,0, W/2, H/2) sub-rectangle of the action_surface for their blitting. You'll want to vary the X,Y position of the rect for each camera if you want them to target separate areas.

Also, in your update() method, you seem to be blitting each camera's surface to its portion of the main screen surface... why? If you're using subsurfaces of action_surface, you can just copy the entire action_surface over to the screen at once, saving you the overhead of multiple blit calls and position re-calculations.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Advertisement

Thank You

-Joel DeSante

This topic is closed to new replies.

Advertisement