コード例 #1
0
ファイル: panels.py プロジェクト: yczhangsjtu/Python-RA2
    def __init__(self,gotoBackController):
        super(GameController,self).__init__(gotoBackController)
        
        self.minimap = None
        self.groupOne = Set()
        self.groupTwo = Set()
        self.groupThree = Set()
        self.selected = Set()

        self.createProgress = [SimpleAnimation(images["progress"],\
                0,0,60,48,55,1,0,0) for i in range(4)]

        self.groupOneButton = GameCtrlButton(0)
        self.groupOneButton.setMouseListener(self.setGroupOne)
        self.buttons.add(self.groupOneButton)
        self.addSprite(self.groupOneButton,self.lendcap.right(),gamectrlbuttony)

        self.groupTwoButton = GameCtrlButton(1)
        self.groupTwoButton.setMouseListener(self.setGroupTwo)
        self.buttons.add(self.groupTwoButton)
        self.addSprite(self.groupTwoButton,self.groupOneButton.right(),gamectrlbuttony)

        self.groupThreeButton = GameCtrlButton(2)
        self.groupThreeButton.setMouseListener(self.setGroupThree)
        self.buttons.add(self.groupThreeButton)
        self.addSprite(self.groupThreeButton,self.groupTwoButton.right(),gamectrlbuttony)

        self.selectSameTypeButton = GameCtrlButton(3)
        self.selectSameTypeButton.setMouseListener(self.selectSameType)
        self.buttons.add(self.selectSameTypeButton)
        self.addSprite(self.selectSameTypeButton,self.groupThreeButton.right(),gamectrlbuttony)

        self.deployButton = GameCtrlButton(4)
        self.deployButton.setMouseListener(self.deploy)
        self.buttons.add(self.deployButton)
        self.addSprite(self.deployButton,self.selectSameTypeButton.right(),gamectrlbuttony)

        self.guardButton = GameCtrlButton(6)
        self.guardButton.setMouseListener(self.guard)
        self.buttons.add(self.guardButton)
        self.addSprite(self.guardButton,self.deployButton.right(),gamectrlbuttony)

        self.setpathButton = GameCtrlButton(9)
        self.setpathButton.setMouseListener(self.setpath)
        self.buttons.add(self.setpathButton)
        self.addSprite(self.setpathButton,self.guardButton.right(),gamectrlbuttony)

        self.player = None
        self.characters = None

        self.powere = ImageSprite(images["powerp"],5,1,0)
        self.powerh = ImageSprite(images["powerp"],5,1,1)
        self.powerl = ImageSprite(images["powerp"],5,1,2)
        self.powern = ImageSprite(images["powerp"],5,1,3)
        self.moneyfont = pygame.font.Font(None,15)

        self.createButtons = {}
        for unitname in requisite:
            self.createButtons[unitname] = CreateButton(unitname)
            self.createButtons[unitname].setMouseListener(self.addToCreateList)
            self.createButtons[unitname].setRightMouseListener(self.cancelCreateList)

        self.selectBuildingPosition = ""
        self.pointerset = None
コード例 #2
0
ファイル: panels.py プロジェクト: yczhangsjtu/Python-RA2
class GameController(BattleFieldController):
    def __init__(self,gotoBackController):
        super(GameController,self).__init__(gotoBackController)
        
        self.minimap = None
        self.groupOne = Set()
        self.groupTwo = Set()
        self.groupThree = Set()
        self.selected = Set()

        self.createProgress = [SimpleAnimation(images["progress"],\
                0,0,60,48,55,1,0,0) for i in range(4)]

        self.groupOneButton = GameCtrlButton(0)
        self.groupOneButton.setMouseListener(self.setGroupOne)
        self.buttons.add(self.groupOneButton)
        self.addSprite(self.groupOneButton,self.lendcap.right(),gamectrlbuttony)

        self.groupTwoButton = GameCtrlButton(1)
        self.groupTwoButton.setMouseListener(self.setGroupTwo)
        self.buttons.add(self.groupTwoButton)
        self.addSprite(self.groupTwoButton,self.groupOneButton.right(),gamectrlbuttony)

        self.groupThreeButton = GameCtrlButton(2)
        self.groupThreeButton.setMouseListener(self.setGroupThree)
        self.buttons.add(self.groupThreeButton)
        self.addSprite(self.groupThreeButton,self.groupTwoButton.right(),gamectrlbuttony)

        self.selectSameTypeButton = GameCtrlButton(3)
        self.selectSameTypeButton.setMouseListener(self.selectSameType)
        self.buttons.add(self.selectSameTypeButton)
        self.addSprite(self.selectSameTypeButton,self.groupThreeButton.right(),gamectrlbuttony)

        self.deployButton = GameCtrlButton(4)
        self.deployButton.setMouseListener(self.deploy)
        self.buttons.add(self.deployButton)
        self.addSprite(self.deployButton,self.selectSameTypeButton.right(),gamectrlbuttony)

        self.guardButton = GameCtrlButton(6)
        self.guardButton.setMouseListener(self.guard)
        self.buttons.add(self.guardButton)
        self.addSprite(self.guardButton,self.deployButton.right(),gamectrlbuttony)

        self.setpathButton = GameCtrlButton(9)
        self.setpathButton.setMouseListener(self.setpath)
        self.buttons.add(self.setpathButton)
        self.addSprite(self.setpathButton,self.guardButton.right(),gamectrlbuttony)

        self.player = None
        self.characters = None

        self.powere = ImageSprite(images["powerp"],5,1,0)
        self.powerh = ImageSprite(images["powerp"],5,1,1)
        self.powerl = ImageSprite(images["powerp"],5,1,2)
        self.powern = ImageSprite(images["powerp"],5,1,3)
        self.moneyfont = pygame.font.Font(None,15)

        self.createButtons = {}
        for unitname in requisite:
            self.createButtons[unitname] = CreateButton(unitname)
            self.createButtons[unitname].setMouseListener(self.addToCreateList)
            self.createButtons[unitname].setRightMouseListener(self.cancelCreateList)

        self.selectBuildingPosition = ""
        self.pointerset = None

    def addToCreateList(self,name):
        t = typeofunit[name]
        if t in ["building","defence"]:
            builded = self.player.getUnitInFactory(t)
            if builded != None:
                if builded[2] == name:
                    if builded[0] == 0:
                        self.selectBuildingPosition = name
                    elif self.player.createStop[t]:
                        self.player.createStop[t] = False
                    else:
                        self.cannotApply()
                else:
                    self.cannotApply()
                return
        self.player.addToCreateList(name)

    def cancelCreateList(self,name):
        t = typeofunit[name]
        builded = self.player.getUnitInFactory(t)
        if builded != None:
            if builded[2] == name:
                self.player.cancelCreateList(t)

    def takeOverGame(self,game,player):
        self.characters = game
        self.player = player

    def draw(self,screen):
        if self.pointerset != None:
            for col,row in self.pointerset:
                if self.goodToPutBuilding(self.selectBuildingPosition,col,row):
                    self.map.drawGreenPointer(screen,col,row)
                else:
                    self.map.drawRedPointer(screen,col,row)
        for unit in self.selected:
            if unit.inarea(self.characters):
                unit.drawBloodBar(screen)
                if unit in self.groupOne:
                    unit.drawGroup(screen,1)
                if unit in self.groupTwo:
                    unit.drawGroup(screen,2)
                if unit in self.groupThree:
                    unit.drawGroup(screen,3)

        super(GameController,self).draw(screen)

        moneyimg = self.moneyfont.render(str(self.player.money),False,WHITE)
        moneyrect = moneyimg.get_rect()
        moneyrect.center = self.credits.rect.center
        screen.blit(moneyimg,moneyrect)

        if self.mousedrag:
            x = min(self.mousedownx,self.mousex)
            y = min(self.mousedowny,self.mousey)
            w = abs(self.mousedownx-self.mousex)
            h = abs(self.mousedowny-self.mousey)
            rect = pygame.Rect(x,y,w,h)
            pygame.draw.rect(screen,WHITE,rect,1)

        self.drawPower(screen)

    def drawCreateButtonSet(self,buttonSet,screen):
        super(GameController,self).drawCreateButtonSet(buttonSet,screen)
        if buttonSet.overgroup != None:
            buttonSet.overgroup.draw(screen)

    def drawMinimap(self,screen):
        super(GameController,self).drawMinimap(screen)

        for unit in self.characters.unitSet:
            x,y = self.map.transformMini(unit.offsetx,unit.offsety)
            pygame.draw.rect(screen,colorofplayer[unit.player],pygame.Rect(x,y,1,1),2)

    def drawPower(self,screen):
        ppowern = int((1-exp(-max(self.player.powergen,self.player.powerload)*log(2)/200)) * powern)
        if self.player.powergen > self.player.powerload:
            powergenn = ppowern
            powerloadn = self.player.powerload*ppowern/self.player.powergen
        else:
            powerloadn = ppowern
            if self.player.powerload == 0:
                powergenn = 0
            else:
                powergenn = self.player.powergen*ppowern/self.player.powerload
        for i in range(powern):
            screen.blit(self.powere.image,(powerx,powery-i*2))
        if self.player.powerhigh:
            for i in range(powergenn):
                screen.blit(self.powerh.image,(powerx,powery-i*2))
        elif self.player.powerlow:
            for i in range(powergenn):
                screen.blit(self.powerl.image,(powerx,powery-i*2))
        for i in range(powerloadn):
            screen.blit(self.powern.image,(powerx,powery-i*2))

    def setGroupOne(self):
        if len(self.groupOne) == 0:
            self.groupOne = self.selected.copy()
            for unit in self.selected:
                if unit in self.groupTwo:
                    self.groupTwo.remove(unit)
                if unit in self.groupThree:
                    self.groupThree.remove(unit)
        else:
            self.selected = self.groupOne.copy()
    
    def setGroupTwo(self):
        if len(self.groupTwo) == 0:
            self.groupTwo = self.selected.copy()
            for unit in self.selected:
                if unit in self.groupOne:
                    self.groupOne.remove(unit)
                if unit in self.groupThree:
                    self.groupThree.remove(unit)
        else:
            self.selected = self.groupTwo.copy()
    
    def setGroupThree(self):
        if len(self.groupThree) == 0:
            self.groupThree = self.selected.copy()
            for unit in self.player.units:
                if unit in self.groupOne:
                    self.groupOne.remove(unit)
                if unit in self.groupTwo:
                    self.groupTwo.remove(unit)
        else:
            self.selected = self.groupThree.copy()
    
    def selectSameType(self):
        selectedTypes = Set()
        for unit in self.selected:
            selectedTypes.add(unit.name)
        original = len(self.selected)
        for unit in self.player.units:
            if unit.name in selectedTypes and unit.inarea(self.characters):
                self.selected.add(unit)
        if len(self.selected) == original:
            for unit in self.player.units:
                if unit.name in selectedTypes:
                    self.selected.add(unit)
    
    def deploy(self):
        pass
    
    def guard(self):
        pass
    
    def setpath(self):
        pass

    def cannotApply(self):
        pass

    def update(self):
        for i,t in enumerate(["building","defence","infantry","vehicle"]):
            self.updateCreateButtons(i,t)
        self.cleanupRemovedUnits()

    def cleanupRemovedUnits(self):
        self.groupOne.intersection_update(self.player.units)
        self.groupTwo.intersection_update(self.player.units)
        self.groupThree.intersection_update(self.player.units)
        self.selected.intersection_update(self.player.units)

    def updateCreateButtons(self,index,t):
        builded = self.player.getUnitInFactory(t)
        visible = self.tabbtn[index].children.visible
        self.tabbtn[index].children = ButtonSet()
        buttonset = self.tabbtn[index].children
        buttonset.visible = visible
        buttonset.scroll = 0
        buttonset.group = pygame.sprite.Group()
        buttonset.overgroup = pygame.sprite.Group()
        for i,unit in enumerate(self.player.createButtonList[t]):
            buttonset.add(self.createButtons[unit])
            self.createButtons[unit].index = i
            buttonset.group.add(self.createButtons[unit])
            if builded != None:
                if builded[2] != unit:
                    self.createButtons[unit].disable()
                else:
                    x,y = self.createButtons[unit].getpos()
                    buttonset.createProgress = self.createProgress[index]
                    buttonset.createProgress.setIndex(54*(builded[1]-builded[0])/builded[1])
                    buttonset.createProgress.setpos(x,y)
                    buttonset.overgroup.add(buttonset.createProgress)
            else:
                self.createButtons[unit].recover()

    def onMouseMove(self,x,y,button1=None,button2=None,button3=None):
        super(GameController,self).onMouseMove(x,y,button1,button2,button3)
        if self.selectBuildingPosition != "":
            if self.get_battle_rect().contains(pygame.Rect(x,y,1,1)) and\
                    not button1 and not button2:
                pointerx,pointery = self.map.getGridPos(x,y)
                self.pointerset = [(addPos(pointerx,pointery,col,row))\
                        for col,row in pointerset[self.selectBuildingPosition]]
            return
        if button1 != None and button1 and self.mousedown:
            self.mousex = min(x,battlewidth)
            self.mousey = min(y,battleheight)
            self.mousedrag = True

    def onMouseDown(self,x,y,button):
        super(GameController,self).onMouseDown(x,y,button)
        if button == 1:
            if self.pointerset != None:
                for col,row in self.pointerset:
                    if not self.goodToPutBuilding(self.selectBuildingPosition,col,row):
                        return
                col,row = self.pointerset[0]
                t = typeofunit[self.selectBuildingPosition]
                self.player.createFixUnit(col,row,t,self.characters)
                self.pointerset = None
                self.selectBuildingPosition = ""
                return
            if x >= 0 and x <= battlewidth and\
               y >= 0 and y <= battleheight:
                self.mousedownx = x
                self.mousedowny = y
                self.mousedown = True
        elif button == 3:
            self.selectBuildingPosition = ""
            self.pointerset = None
            self.mousedrag = False
            if self.get_battle_rect().contains(pygame.Rect(x,y,1,1)):
                for unit in self.selected:
                    unit.target = (unit.offsetx+(x-unit.x),unit.offsety+(y-unit.y))

    def onMouseUp(self,x,y,button):
        super(GameController,self).onMouseUp(x,y,button)
        if button == 1:
            if self.mousedrag:
                x = min(self.mousedownx,self.mousex)
                y = min(self.mousedowny,self.mousey)
                w = abs(self.mousedownx-self.mousex)
                h = abs(self.mousedowny-self.mousey)
                rect = pygame.Rect(x,y,w,h)
                self.selected = Set()
                for unit in self.player.units:
                    if rect.contains(unit.get_rect()):
                        if unit.regionselectable:
                            self.selected.add(unit)
            else:
                if self.get_battle_rect().contains(pygame.Rect(x,y,1,1)):
                    nounit = True
                    for unit in self.selected:
                        if unit.get_rect().contains(pygame.Rect(x,y,1,1)):
                            nounit = False
                            unit.onDoubleClick()
                            break
                    self.selected = Set()
                    if nounit:
                        for unit in self.player.units:
                            if unit.get_rect().contains(pygame.Rect(x,y,1,1)):
                                self.selected = Set([unit])
                                break
            self.mousedrag = False
            self.mousedown = False