Example #1
0
 def __init__(self, environment):
     self.environment = environment
     self.images = Images(FilePath("data").child("img2"))
     self.images.load()
     self.actions = deque()
     self.action = None
     self.center = Vector2D((0, 0))
Example #2
0
 def __init__(self, environment):
     self.environment = environment
     self.images = Images(FilePath("data").child("img2"))
     self.images.load()
     self.actions = deque()
     self.action = None
     self.center = Vector2D((0,0))
Example #3
0
 def __init__(self, environment):
     self.environment = environment
     _initSounds()
     self.environment.view = self
     self.images = Images(FilePath("data").child("img2"))
     self.images.load()
     self.center = Vector2D((0,0))
     self.playerAnimations = {}
     self.buildingAnimations = {}
Example #4
0
class Window(object):
    def __init__(self, environment):
        self.environment = environment
        _initSounds()
        self.environment.view = self
        self.images = Images(FilePath("data").child("img2"))
        self.images.load()
        self.center = Vector2D((0,0))
        self.playerAnimations = {}
        self.buildingAnimations = {}

    def paint(self,tick): # Draw Background
        """
        Call C{paint} on all views which have been directly added to
        this Window.
        """
        bg = self.images.images["background"]
        bgWidth = bg.width
        bgHeight = bg.height
        x = -(self.center * 20).x
        y = -(self.center * 20).y
        while x > 0:
            x -= bgWidth;
        while y > 0:
            y -= bgHeight
        while x < self.screen.get_width():#800:#480:
            j = y
            while j < self.screen.get_height():#480:#800:
                self.screen.blit(bg._image, pygame.Rect(x, j, bgWidth, bgHeight))
                j += bgHeight
            x += bgWidth

        self.drawEnvironment(tick)
        self.drawHUD()
        
        pygame.display.flip()
        
    def isVisible(self, entity):
        if not self.environment.team:
            return True
        # See objects on your team
        if self.environment.team == entity.team:
            return True
        # Object in range of my sentries
        for b in self.environment.buildings.itervalues():
            if b.isSentry() and (b.team == self.environment.team) and (entity.position - b.position).length < b.SENTRY_RANGE*5.0:
                return True
            
        # object in range of a scanning player
        for p in self.environment.players.itervalues():
            if (self.environment.team == p.team):
                if (entity.position - p.position).length < p.getScanRadius()*5 :
                    return True
        
        return False

    

    def drawEnvironment(self,tick):
        '''Draw the state of the environment. This is called by view after drawing the background. 
           This function draws the timer and calls the drawing functions of the players/buildings/resource pool'''
        if(self.environment.ResourcePool<>None):
            self.environment.ResourcePool.draw(self,self.screenCoord(Vector2D(0,0)))

        for b in self.environment.buildings.itervalues():
                        self.drawBuilding(b,self.screenCoord(b.position),self.isVisible(b),tick)
            
        for p in self.environment.players.itervalues():
            
            self.drawPlayer(p,self.screenCoord(p.position),self.isVisible(p),tick)
            
    def updateBuildingAnimations(self,building,tick):
        
        if not str(building.building_id) in self.buildingAnimations:
            self.buildingAnimations[str(building.building_id)] = AnimatedActions(building)

        building_animatedActions =self.buildingAnimations[str(building.building_id)]
            
        for anim in building.animations:
            building_animatedActions.addAnimation(anim[0],anim[1],tick)
        building.animations = []    
        return     building_animatedActions
            
    
    def updatePlayerAnimations(self,player,tick):
        if not str(player.player_id) in self.playerAnimations:
            self.playerAnimations[str(player.player_id)] = AnimatedActions(player)
        
        player_animatedActions =self.playerAnimations[str(player.player_id)]
            
       #player_animatedActions.animation
        
        while (len(player.animations)>0):
            
            anim=player.animations.pop()
            if (self.environment.IsServer and anim[3] ):
                player_animatedActions.addAnimation(anim[0],anim[1],tick)                
                player.animations.insert(0,  (anim[0],anim[1],tick,False))
                
            elif self.environment.IsServer and not anim[3]:
                print 'EOL'
                break
            elif not self.environment.IsServer:
                
                player_animatedActions.addAnimation(anim[0],anim[1],tick)
                
                
        return     player_animatedActions
        
    def drawPlayer(self,player,position,isVisible,tick):

        if isVisible:
                image = self.images.images["Player", player.team, player.sides]
                image.draw(self.screen, position)
                
                    
                
                for i in range(0,player.resources):
                        self.images.images["Armor", player.sides, i+1].draw(self.screen, position)
                
               
        else:
                image = self.images.images["Enemy", player.team]
                image.draw(self.screen, position)
        
        self.updatePlayerAnimations(player,tick).drawAnimation(self,position,tick,isVisible)
        
    def drawBuilding(self,building,position,IsVisible,tick):
        
        #building.animations.drawAnimation(self, position,tick)
        
        self.updateBuildingAnimations(building,tick).drawAnimation(self,position,tick,IsVisible)
        if not (building.sides and building.resources):
                return 0

        if IsVisible:
                
                if building.sides:
                        self.images.images["Building", building.sides, building.team].draw(self.screen, position)
                if building.sides >= 3:
                        self.images.images["Building Zone", building.sides, building.team].draw(self.screen, position)
                        self.images.images["BuildingHealth", building.team, building.sides, building.resources].draw(self.screen, position)
                if building.isSentry():
                        self.images.images["SentryScan"].drawScaled(self.screen, position, building.SENTRY_RANGE)


    def drawHUD(self): 
        ''' Draw the HUD . It includes scores, time, and other info'''

        #Draw time left
        minRemaining = self.environment.TimeLeft / 60
        secRemaining = self.environment.TimeLeft % 60
        secStr = str(secRemaining)
        if secRemaining <= 9: secStr = "0" + secStr

        minStr = str(minRemaining)
        if minRemaining <= 9: minStr = "0" + minStr
        
        if(self.environment.IsServer):
            font = pygame.font.Font("data/Deutsch.ttf", 70)
            text = font.render(minStr + ":" + secStr, True, (255, 255, 255))
            
            textrect = text.get_rect(left = 15, top = 40)
        else:
            

            font = pygame.font.Font("data/Deutsch.ttf", 35)
            text = font.render(minStr + ":" + secStr, True, (255, 255, 255))
            text = pygame.transform.rotate(text, 270)
      
            textrect = text.get_rect(left = 15, bottom = 410)

        self.screen.blit(text,textrect)

        #Draw the scores
        
        fontColors = [(255, 0, 0), (0,255,255)]
        if(self.environment.IsServer): 
            
            font = pygame.font.Font("data/Deutsch.ttf", 35)
            text = font.render(str(self.environment.scores[0]), True, fontColors[0])
            textrect = text.get_rect(right =735, top = 40)
            self.screen.blit(text,textrect)

            text = font.render(str(self.environment.scores[1]), True, fontColors[1])
        
            textrect = text.get_rect(right =735, top = 80)
        else:
            font = pygame.font.Font("data/Deutsch.ttf", 35)
            text = font.render(str(self.environment.scores[0]), True, fontColors[0])
            text = pygame.transform.rotate(text, 270)
            textrect = text.get_rect(right =735, bottom = 410)
            self.screen.blit(text,textrect)
            text = font.render(str(self.environment.scores[1]), True, fontColors[1])
            text = pygame.transform.rotate(text, 270)
            textrect = text.get_rect(right = 775, bottom = 410)
        
        self.screen.blit(text,textrect)

        #GAMEOVER
        
        if self.environment.GameOver:
            endGameMessage = ""
            if self.environment.IsServer:
                scoreDifference = self.environment.scores[0] - self.environment.scores[1]
                if scoreDifference > 0:
                    endGameMessage = "RED WINS!"
                elif scoreDifference < 0:
                    endGameMessage = "BLUE WINS!"
                else:
                    endGameMessage = "DRAW!"
                font = pygame.font.Font("data/Deutsch.ttf", 140)
                
                text = font.render(endGameMessage, True, (255,255,255))
                textrect = text.get_rect(centery =240, centerx = 400)

            else:
                scoreDifference = self.environment.scores[self.environment.team-1] > self.environment.scores[self.environment.otherTeam-1]
                if scoreDifference > 0:
                    endGameMessage = "YOU WIN!"
                elif scoreDifference < 0:
                    endGameMessage = "YOU LOSE!"
                else:
                  endGameMessage = "DRAW!"
                font = pygame.font.Font("data/Deutsch.ttf", 70)

                text = font.render(endGameMessage, True, (255,255,255))
                text = pygame.transform.rotate(text, 270)
                textrect = text.get_rect(centery =240, centerx = 400)
            self.screen.blit(text,textrect)
        
        
    def setCenter(self, position):
        self.center = position

    def worldCoord(self, p):
        width = self.screen.get_width()
        height = self.screen.get_height()
        (cx, cy) = self.screen.get_rect().center
        return Vector2D(((p.x - cx) * self.environment.width) / width,
                        ((p.y - cy) * self.environment.height) / height)

    def screenCoord(self, p):
        width = self.screen.get_width()
        height = self.screen.get_height()
        (cx, cy) = self.screen.get_rect().center
        return Vector2D((((p.x - self.center[0]) / (self.environment.width / 2)) * width) + cx,
                        (((p.y - self.center[1]) / (self.environment.height / 2)) * height) + cy)

    def start(self, title):
        self.screen = pygame.display.get_surface()
        pygame.display.set_caption(title)
    
        
        (cx, cy) = self.screen.get_rect().center

    def stop(self):
        self._renderCall.stop()
Example #5
0
class Window(object):
    def __init__(self, environment):
        self.environment = environment
        self.images = Images(FilePath("data").child("img2"))
        self.images.load()
        self.actions = deque()
        self.action = None
        self.center = Vector2D((0,0))

    def addAction(self, action):
        # TODO We no longer have actions?
        pass

    def startAction(self):
        if self.action == None:
            try:
                self.action = self.actions.popleft()
                self.action.start(5).addCallback(self.stopAction)
            except:
                pass

    def stopAction(self, ign):
        self.action = None
        self.startAction()

    def paint(self):
        """
        Call C{paint} on all views which have been directly added to
        this Window.
        """
        bg = self.images.images["background"]
        bgWidth = bg.width
        bgHeight = bg.height
        x = -(self.center * 20).x
        y = -(self.center * 20).y
        while x > 0:
            x -= bgWidth;
        while y > 0:
            y -= bgHeight
        while x < 480:
            j = y
            while j < 800:
                self.screen.blit(bg._image, pygame.Rect(x, j, bgWidth, bgHeight))
                j += bgHeight
            x += bgWidth
        self.environment.paint(self)
        if self.action:
            pass
        pygame.display.flip()

    def setCenter(self, position):
        self.center = position

    def worldCoord(self, p):
        width = self.screen.get_width()
        height = self.screen.get_height()
        (cx, cy) = self.screen.get_rect().center
        return Vector2D(((p.x - cx) * self.environment.width) / width,
                        ((p.y - cy) * self.environment.height) / height)

    def screenCoord(self, p):
        width = self.screen.get_width()
        height = self.screen.get_height()
        (cx, cy) = self.screen.get_rect().center
        return Vector2D((((p.x - self.center[0]) / (self.environment.width / 2)) * width) + cx,
                        (((p.y - self.center[1]) / (self.environment.height / 2)) * height) + cy)

    def start(self, title):
        self.screen = pygame.display.get_surface()
        pygame.display.set_caption(title)
        self._renderCall = LoopingCall(self.paint)
        self._renderCall.start(0.03)

    def stop(self):
        self._renderCall.stop()
Example #6
0
class Window(object):
    def __init__(self, environment):
        self.environment = environment
        self.images = Images(FilePath("data").child("img2"))
        self.images.load()
        self.actions = deque()
        self.action = None
        self.center = Vector2D((0, 0))

    def addAction(self, action):
        # TODO We no longer have actions?
        pass

    def startAction(self):
        if self.action == None:
            try:
                self.action = self.actions.popleft()
                self.action.start(5).addCallback(self.stopAction)
            except:
                pass

    def stopAction(self, ign):
        self.action = None
        self.startAction()

    def paint(self):
        """
        Call C{paint} on all views which have been directly added to
        this Window.
        """
        bg = self.images.images["background"]
        bgWidth = bg.width
        bgHeight = bg.height
        x = -(self.center * 20).x
        y = -(self.center * 20).y
        while x > 0:
            x -= bgWidth
        while y > 0:
            y -= bgHeight
        while x < self.screen.get_width():  #800:#480:
            j = y
            while j < self.screen.get_height():  #480:#800:
                self.screen.blit(bg._image,
                                 pygame.Rect(x, j, bgWidth, bgHeight))
                j += bgHeight
            x += bgWidth
        self.environment.paint(self)
        if self.action:
            #   self.action.draw(self.screen, Vector2D((240, 400)))
            pass
        pygame.display.flip()

    def setCenter(self, position):
        self.center = position

    def worldCoord(self, p):
        width = self.screen.get_width()
        height = self.screen.get_height()
        (cx, cy) = self.screen.get_rect().center
        return Vector2D(((p.x - cx) * self.environment.width) / width,
                        ((p.y - cy) * self.environment.height) / height)

    def screenCoord(self, p):
        width = self.screen.get_width()
        height = self.screen.get_height()
        (cx, cy) = self.screen.get_rect().center
        return Vector2D((((p.x - self.center[0]) /
                          (self.environment.width / 2)) * width) + cx,
                        (((p.y - self.center[1]) /
                          (self.environment.height / 2)) * height) + cy)

    def start(self, title):
        self.screen = pygame.display.get_surface()
        pygame.display.set_caption(title)
        self._renderCall = LoopingCall(self.paint)
        self._renderCall.start(0.03)

        (cx, cy) = self.screen.get_rect().center
#        print "envirn: " + str(self.environment.width) + " x " + str(self.environment.height)
#        print "screen: " + str(self.screen.get_width()) + " x " + str(self.screen.get_height())
#        print "scrctr: " + str(cx) + ", " + str(cy)

    def stop(self):
        self._renderCall.stop()