unit selection problem

Paul Pittlerson menkomigen6 at gmail.com
Mon Jan 14 21:29:17 CET 2013


Unit selection doesn't work properly. Pygames event.pos returns a tuple of the coords within the current window, so it's not possible to select units outside of the top left corner.

from pygamehelper import *
from pygame import *
from pygame.locals import *
from vec2d import *
from math import e, pi, cos, sin, sqrt
from random import uniform
from mapeditor import *
from entity import *
from spritesheet import *

# version alpha 0.1
# map drawing seems functional, albeit crude
    
class Starter(PygameHelper):
    def __init__(self):        
        self.w, self.h = 1600, 900
        PygameHelper.__init__(self, size=(self.w, self.h), fill=((255,255,255)))
        self.map_layout = map_constructor("map.png", 2400)
        self.object_counter = len(self.map_layout.all_map_objects)
        map_textures = get_sprites( (48, 48) ,"spritesheet.png" , (0, 0) )
        self.collision_map = []

        # defines all map objects in readable format
        self.map_sprite_list = {
                                'floor_red' : map_textures.sprites[0],
                                'floor_dark' : map_textures.sprites[2],
                                'wall' : map_textures.sprites[1],
                                'proto_unit' : map_textures.sprites[3],
                                }
        # map scrolling modifiers
        self.mod_w = 0
        self.mod_h = 0
        
        # introduce agents
        self.agent_list = []
        self.selected = []
        
        # TODO: make surface size relative to map size
        self.mapSurf = pygame.Surface( (3600, 3600) )
        
        # draw the floor once

        for e in self.map_layout.fixed_list:
            if e.type == 1:
                self.mapSurf.blit(self.map_sprite_list['floor_red'], e.rect)
                
        self.mapSurfRect = self.mapSurf.get_rect()
        
        # create a collision map based on wall positions
        for e in self.map_layout.all_map_objects:       
            if e.type == 2:
                wall_object = wall()
                wall_object.pos = vec2d((e.x_position + 24), (e.y_position + 30))
                wall_object.target = wall_object.pos  
                self.collision_map.append(wall_object)
                
        for e in self.map_layout.all_map_objects: # check map for agents
            if e.type == 3:
                a = agent()
                a.pos = vec2d(e.x_position, e.y_position)
                print a.pos
                a.target = a.pos
                self.agent_list.append(a)
    
    def update(self):
        # map scrolling
        self.mapSurfRect.center = (((self.w / 2) + self.mod_w), (((self.h / 2)) + self.mod_h))
        
        # update agent position
        for a in self.agent_list:
            dir = a.target - a.pos
            if dir.length >= 3:
                dir.length = 3
                a.pos = a.pos + dir

        # update the walls, so they are drawn above the floor
        for e in self.map_layout.fixed_list:       
            if e.type == 2:
                self.mapSurf.blit(self.map_sprite_list['wall'], e.rect)
                
        # add agents to the mapSurf
        for a in self.agent_list:
            self.mapSurf.blit(self.map_sprite_list['proto_unit'], a.pos)
                
                
    def keyUp(self, key):
        pass
        
    def keyDown(self, key):
        
        # move the mapSurf object
        # TODO: implement continuous camera movement while key is pressed
        if key == K_RIGHT:
            self.mod_w -= 100
        if key == K_LEFT:
            self.mod_w += 100
        if key == K_UP:
            self.mod_h += 100
        if key == K_DOWN:
            self.mod_h -= 100
  
        
    def mouseUp(self, button, pos):

        # select agent
        if button==1:
            print pos
            for a in self.agent_list:
                if a.pos.get_distance(vec2d(pos)) < 24:
                    self.selected.append(a)
                    print 'selected'
                    
        # right click to move selected agents
        if button==3:            
            for a in self.selected:
                a.target = vec2d(pos)

        
    def mouseMotion(self, buttons, pos, rel):
        pass

    def draw(self):
        #self.screen.fill((255,255,255))
        self.screen.fill((0,0,0))
        
        self.screen.blit(self.mapSurf, self.mapSurfRect)
        
        #for a in self.selected:
        #    pygame.draw.circle(self.mapSurf, (50,200,50), a.pos.inttup(), 18, 1) # represent selected units
                
s = Starter()
s.mainLoop(100)



More information about the Python-list mailing list