I'm trying to implement unit selection on a large, scrollable surface.
# Arbitrary values for window size
self.w, self.h = 1600, 900
# Game world surface, everything gets blitted to this, which is then drawn to the screen:
self.mapSurf = pygame.Surface((3600, 3600))
The general process looks like this:
# draw the floor first
for e in self.map_layout.fixed_list:
if e.type == 1:
self.mapSurf.blit(self.map_sprite_list['floor_red'], e.rect)
# then draw the walls
for e in self.map_layout.fixed_list:
if e.type == 2:
self.mapSurf.blit(self.map_sprite_list['wall'], e.rect)
# add agents (units) to the mapSurf
for a in self.agent_list:
self.mapSurf.blit(self.map_sprite_list['proto_unit'], a.pos)
# draw everything to screen
self.screen.blit(self.mapSurf, self.mapSurfRect)
I have a method for getting the coordinates of a click on the screen. And tried to use this to select units.
def mouseUp(self, button, pos):
# select agent
if button==1:
for a in self.agent_list:
if a.pos.get_distance(vec2d(pos)) < 24:
self.selected.append(a)
The problem is that the value pos does not correspond to the mapSurf, but rather to the self.screen surface, so when comparing a.pos to the pygame event.pos returned from mouseUp, this function can only return coordinates within the window size, and I can't select agents which are spawned outside of the top left area of mapSurf.
What would be the general method of selecting units I need to implement to fix this program?
For some more details, here are three relevant links, all of which describe different aspects of how my program was built:
Trying to make a python map editor