示例#1
0
    def __init__(self, size, xCell, yCell):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.add(allspritesGroup)
        self.add(botsGroup)
        
        self.size = size #0-2
        self.direction = 0 #0-16 clockwise 0=up
        self.index = 0 #0-4 start, 5-16 loop, 17-18 stop
        self.speed = 0

        self.gun = [0] * 3
        self.gun[0] = Gun(1) #0=none 1=cannon 2=laser 3=torch 4=rockets
        self.gun[1] = 0
        self.gun[2] = 0
        self.gunDirection = 0
        
        self.imageList = [0] * 19
        self.loadAnim(0)
        
        self.rect = self.imageList[self.index].get_rect()
        self.xy = util.grid_to_world((xCell, yCell)) #world x y coordinates
        self.hitRect = pygame.Rect(self.xy[0] - 10, self.xy[1] - 10, 20, 20) #hitbox
        
        self.path = None #grid waypoints for pathfinding
        self.pathPos = 0
        self.jumpCell((xCell, yCell))
        self.currentCell = (xCell, yCell)
        self.nextCell = (xCell, yCell)
        self.destCell = (xCell, yCell)
        allspritesGroup.change_layer(self, self.currentCell[1] * 10)
        botsGroup.change_layer(self, self.currentCell[1] * 10)
示例#2
0
    def update(self):
        self.currentCell = util.world_to_grid(self.xy)
        if self.currentCell != self.destCell:
            self.speed = 8 - (self.size)
            if self.currentCell == self.nextCell:
                #if self.pathBlocked():
                #    self.pathSet(self.destCell)
                #else:
                self.pathPos -= 1
                self.nextCell = self.path[self.pathPos - 1]
                self.setDirection(util.point_direction(self.xy, util.grid_to_world(self.nextCell)))
                    #print 'pathpos ' + str(self.pathPos)
                    #print 'current cell ' + str(self.currentCell)
                    #print 'next cell ' + str(self.nextCell)
                    #print 'new direction ' + str(self.direction)
                    #print ' '
        else: #we've reached the destination
            self.speed = 0
            self.index = 0
            if self in movingGroup:
                blocked.append(self.currentCell)
                self.remove(movingGroup)
        self.image = self.imageList[self.index]
        self.index = ((self.index + 1) % 17) + (5 * (self.index == 16))

        self.gunDirection = int(round(self.direction) % 16)

        self.xy = util.add2(self.xy, util.speedDir_xy(self.speed, self.direction)) #move xy
        self.rect.center = (self.xy[0], self.xy[1] - (12 + (6 * self.size))) #move rect to xy
        self.hitRect.center = self.xy #move hitbox to xy

        allspritesGroup.change_layer(self, self.currentCell[1] * 10)
        botsGroup.change_layer(self, self.currentCell[1] * 10)
示例#3
0
def main():
    step = 0
    shiftPressed = False
    mouseDrag = False
    mouseRect = 0
    #scrolling with the mouse
    lScrolling = False
    rScrolling = False
    uScrolling = False
    dScrolling = False
    
    while True:
        clock.tick(30)
        step = (step + 1) % 1000
    # destroy objects 
    #for obj in allsprites:
        #if obj.timer == 0:
        #    obj = None
        for event in pygame.event.get():
            if event.type == MOUSEMOTION:
                if mouseRect != 0:
                    mouseDrag = True
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return
                elif event.key == K_DOWN:
                    view.yspd += 10
                elif event.key == K_LEFT:
                        view.xspd -= 10
                elif event.key == K_UP:
                        view.yspd -= 10
                elif event.key == K_RIGHT:
                    view.xspd += 10
                elif event.key == K_LSHIFT:
                    shiftPressed = True
            elif event.type == KEYUP:
                if event.key == K_DOWN:
                    view.yspd -= 10
                elif event.key == K_LEFT:
                    view.xspd += 10
                elif event.key == K_UP:
                    view.yspd += 10
                elif event.key == K_RIGHT:
                    view.xspd -= 10
                elif event.key == K_LSHIFT:
                    shiftPressed = False
            elif event.type == MOUSEBUTTONDOWN:
                mouseRect = util.view_to_world(pygame.mouse.get_pos(), view)
                if event.button == 3: #right click
                    for i in selectedGroup.sprites(): #move bots
                        i.add(movingGroup)
                        i.pathSet(util.world_to_grid(mouseRect))
                    mouseRect = 0
                    mouseDrag = 0
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1 and mouseRect != 0: #left click
                    #dragging
                    if mouseDrag:
                        mouseDrag = False
                        if not shiftPressed:
                            selectedGroup.empty()
                        selectedList = pygame.Rect(util.add2(mouseRect, map(lambda x: min(0,x), util.sub2(pygame.mouse.get_pos(), util.world_to_view(mouseRect, view)))), #move top left if width/height are negative
                                                map(lambda x: abs(x), util.sub2(pygame.mouse.get_pos(), util.world_to_view(mouseRect, view)))).collidelistall([i.hitRect for i in botsGroup.sprites()])
                        selectedGroup.add([botsGroup.get_sprite(i) for i in selectedList])
                    else:
                        selectedList = pygame.Rect(mouseRect, (1,1)).collidelist([i.hitRect for i in botsGroup.sprites()]) #index of selected bot
                        if selectedList != -1: #we're clicking a bot
                            if not shiftPressed:
                                selectedGroup.empty()
                            selectedGroup.add(botsGroup.get_sprite(selectedList))
                        else:
                            selectedGroup.empty()
                    mouseRect = 0
            elif event.type == QUIT:
                return
                
        allspritesGroup.update()
        view.update()
                
        #screen scroll using mouse
        #left
        if pygame.mouse.get_pos()[0] < 30:
            if not lScrolling:
                view.xspd -= 10
                lScrolling = True
        elif lScrolling:
            view.xspd += 10
            lScrolling = False
            
        #right
        if pygame.mouse.get_pos()[0] > 610:
            if not rScrolling:
                view.xspd += 10
                rScrolling = True
        elif rScrolling:
            view.xspd -= 10
            rScrolling = False
            
        #up
        if pygame.mouse.get_pos()[1] < 30:
            if not uScrolling:
                view.yspd -= 10
                uScrolling = True
        elif uScrolling:
            view.yspd += 10
            uScrolling = False
            
        #down
        if pygame.mouse.get_pos()[1] > 450:
            if not dScrolling:
                view.yspd += 10
                dScrolling = True
        elif dScrolling:
            view.yspd -= 10
            dScrolling = False
        
        #draw floor tiles
        for w in range(view.x - (view.x % 80), view.x + 720, 80):
            for h in range(view.y - (view.y % 80), view.y + 560, 80):
                baseSurface.blit(sprFloor, (w, h))

        #draw blocked cells
        for i in blocked:
            pygame.draw.circle(baseSurface, pygame.Color(255, 0, 0, 0), util.grid_to_world(i), 4, 4)

        #draw bot selections
        for i in selectedGroup:
            baseSurface.blit(pygame.transform.scale(pygame.transform.rotate(sprBotSelect, step * 2), (77, 38)), (i.rect.centerx - 38, i.rect.centery + (i.size * 10)))
            #pygame.draw.ellipse(viewSurface, pygame.Color(int(70 + (math.sin(step / 100) * 70)), 170, int(70 + (math.sin(step / 100) * 70)), 0), i.rect.inflate(-(120 - (8 * i.size)),-(100 - (4 * i.size))).move(0, 30), 0)
                
        #draw allsprites
        allspritesGroup.draw(baseSurface)

        #draw bot weapons
        for i in botsGroup:
            baseSurface.blit(gunList[i.gunDirection + ((i.gun[0] - 1) * 16)], (i.rect.centerx - 100, i.rect.centery - (60 + (5 * i.size)) + gunOffset[i.size][i.index]))
            #baseSurface.blit(sprBotGunMount, (i.rect.centerx - 6, i.rect.centery - (25 + (5 * i.size))))

        #draw grid
        for i in range(0, 1000, constants.cellSize):
            pygame.draw.line(baseSurface, pygame.Color(0,0,0,0), (0, i), (1000, i), 1)
            pygame.draw.line(baseSurface, pygame.Color(0,0,0,0), (i, 0), (i, 1000), 1)
            
        #draw mouse selection box
        if mouseDrag:
            pygame.draw.rect(baseSurface, pygame.Color(50, 170, 0, 0), pygame.Rect(mouseRect, util.sub2(pygame.mouse.get_pos(), util.world_to_view(mouseRect, view))), 2)
            
        #draw bot paths
        for i in movingGroup:
            pygame.draw.line(baseSurface, pygame.Color(50, 170, 0, 0), i.xy, util.grid_to_world(i.destCell), 2)
            if i.pathPos > 1:
                pygame.draw.lines(baseSurface, pygame.Color(200, 0, 0, 0), False, [util.grid_to_world(p) for p in i.path[:i.pathPos]], 2)
            
        #draw to view
        viewSurface.blit(baseSurface, (-view.x, -view.y))
        pygame.display.flip()
示例#4
0
     
 #load animation for int direction d to imageList
 def loadAnim(self, _dir):
     for iLoop in range(19):
         self.imageList[iLoop] = load_image(os.path.join(str(self.size), str(_dir), str(iLoop + 1) + '.bmp'), -1)
         
 #set the path with dest=(x, y)
 def pathSet(self, (x, y)):
     self.destCell = (x, y)
     if self.currentCell in blocked:
         blocked.remove(self.currentCell)
     astar.findpath(self.currentCell, self.destCell, blocked)
     self.path = astar.path #POINTS STORED IN REVERSE ORDER
     self.pathPos = len(self.path) - 1
     self.nextCell = self.path[len(self.path) - 2]
     self.setDirection(util.point_direction(self.xy, util.grid_to_world(self.nextCell)))
     #print self.currentCell
     #print blocked
     
 #check if any path cells are blocked
 def pathBlocked(self):
     for i in self.path:
         if i in blocked:
             if i != self.currentCell:
                 print 'Path Blocked at ' + str(i)
                 return True
     return False
     
 #set the direction dir=0-16
 def setDirection(self, _dir):
     if _dir != self.direction: