Example #1
0
File: main.py Project: mn22nw/IU
class MyView(Widget):
    # properties that needs to be accessed in the .kv file are placed outside of the constructor
    lives = NumericProperty(5)
    level = NumericProperty(1)
    points = NumericProperty(0)
    layoutPositionY = NumericProperty(0)

    # def __init__(self,vc):
    def __init__(self, vc=None, **kwargs):
        super(MyView, self).__init__(**kwargs)
        
        # properties of the view 
        self.app = App.get_running_app()
        self.vc = vc
        self.settingsPopup = None
        self.helpPopup = None
        self.settingsPopupDismissed = False
        self.helpPopupDismissed = False
        self.bubble = None
        self.upcomingBubble = None
        self.angle = NumericProperty()
        self.rowspaceY = 0
        self.bubbleSpaceX = 0
        self.bubbleList = []
        self.bubbleGridList = []
        self.threatList = []
        self.threatListCopy = []
        self.layoutPositionY = self.y + (self.height * 1.5) # * (0.045 * 4.6) 
        self.startPositionY =   self.y + (self.height * 1.5)
        self.stepsMovedDown = NumericProperty()
       
    
    # loading the view (called in the controller)
    def loadView(self):
        self.bubble = Bubble()
        # create all the bubbles and threats for the startup
        self.createObsticles()

        # create the grid for the bubbles to fit in
        self.createBubbleGrid()

        # set the bubbles in the grid to taken if there's an bubble in it's place
        self.setTakenGridPositions()

        # add the first upcoming bubble to the view
        self.addUpcomingBubbletoView()

    def resetView(self):
        self.layoutPositionY = self.startPositionY 
        self.bubble = None
        self.rowspaceY = 0
        self.bubbleSpaceX = 0
        self.bubbleList = []
        self.bubbleGridList = []
        self.bubbleLayout.clear_widgets()
        self.bubbleGridLayout.clear_widgets()
        self.nextBubbleLayout.clear_widgets()

        self.loadView()
        self.points = 0
        self.lives = 5
        self.level = 1       

    def changeUpcomingBubbleColor(self):
        self.upcomingBubble.setRandomColor()
        self.upcomingBubble.source = 'graphics/bubbles/' + self.upcomingBubble.getColor() + '.png'


    def addUpcomingBubbletoView(self):
        self.upcomingBubble = self.createBubble(0,0)
        self.upcomingBubble.pos_hint={'x': 0.55, 'center_y': .5}
        # add the upcomingBubble to the preview-window
        self.nextBubbleLayout.add_widget(self.upcomingBubble)

    def createFiredBubble(self):
        self.bubble = Bubble(pos=(500,500)) 
        # set the shooting bubble to the same color as the upcoming previewd bubble
        self.bubble.bubbleColor = self.upcomingBubble.getColor()
        self.bubble.source = 'graphics/bubbles/' + self.bubble.getColor() + '.png'
        return self.bubble
    

    # setters and getters for the properties  
    
    def setLevel(self, value):
        self.level = value
     
    def setPoints(self, value):
        self.points += value

    def getPoints(self, value): # TODO - not using this?
        return self.points

    def setLives(self, value):
        self.lives += value
     
    def setBubbleStartPosition(self, bubble):
        bubble.center = self.shooter.center

    # popups
    def displaySettingsScreen(self):
        self.settingsPopupDismissed = False
        # the first time the setting dialog is called, initialize its content.
        if self.settingsPopup is None:
            
            self.settingsPopup = Popup(attach_to=self,
                                       title= 'DBShooter Settings'
                                       )
                      
            self.settingDialog = SettingDialog(root=self)
            
            self.settingsPopup.content = self.settingDialog
            
            self.settingDialog.music_slider.value = boundary(self.app.config.getint('General', 'Music'), 0, 100)
            self.settingDialog.sound_slider.value = boundary(self.app.config.getint('General', 'Sound'), 0, 100)
            
        self.settingsPopup.open()

    def displayHelpScreen(self):
        self.helpPopupDismissed = False
        if self.helpPopup is None:
            self.helpPopup = Popup(title = 'Rules', attach_to=self)
                      
            self.helpContent = HelpScreen(root=self)
            
            self.helpPopup.content = self.helpContent
            
            
        self.helpPopup.open()

    #TODO - make one standard popup for theese and just change the title and image!  to remove DRY (Had no time to do this)
    def displayLifeIsLostScreen(self):
        lifeIsLostScreen = Popup( title='Life is lost', auto_dismiss=False,
                            attach_to=self,
                            size_hint=(None,None), pos_hint={'center_x': 0.5, 'center_y': .6} 
                            )
        layout = BoxLayout(orientation = 'vertical')
        image = Image(source='graphics/lifeLost.png', pos_hint={'center_x': 0.5, 'center_y': 0.4})
        layout.add_widget(image)
        lifeIsLostScreen.content = layout
        lifeIsLostScreen.open()
        Clock.schedule_once(lifeIsLostScreen.dismiss, 2.5)
        Clock.schedule_once(self.removeLife, 2.6)

    def displayGameOverScreen(self):
        gameOverScreen = Popup( title='Game Over!', auto_dismiss=False,
                            attach_to=self,
                            size_hint=(None,None), pos_hint={'center_x': 0.5, 'center_y': .6} 
                            )
        layout = BoxLayout(orientation = 'vertical')
        image = Image(source='graphics/gameOver.png', pos_hint={'center_x': 0.5, 'center_y': 0.4})
        layout.add_widget(image)
        gameOverScreen.content = layout
        gameOverScreen.open()
        Clock.schedule_once(gameOverScreen.dismiss, 2.5)

    def displayCollisionScreen(self):
        collisionScreen = Popup( title='Collision!', auto_dismiss=False,
                            attach_to=self,
                            size_hint=(None,None), pos_hint={'center_x': 0.5, 'center_y': .6} 
                            )
        layout = BoxLayout(orientation = 'vertical')
        image = Image(source='graphics/onCollision.png', pos_hint={'center_x': 0.5, 'center_y': 0.4})
        layout.add_widget(image)
        collisionScreen.content = layout
        collisionScreen.open()
        Clock.schedule_once(collisionScreen.dismiss, 2.5)     

    def displayVictoryScreen(self):
        victoryScreen = Popup( title='EPIC WIN!', auto_dismiss=True,
                            attach_to=self,
                            size_hint=(None,None), pos_hint={'center_x': 0.5, 'center_y': .6} 
                            )
        layout = BoxLayout(orientation = 'vertical')
        image = Image(source='graphics/victory.png', pos_hint={'center_x': 0.5, 'center_y': 0.4})
        layout.add_widget(image)
        victoryScreen.content = layout
        victoryScreen.open()     

    def removeLife(self, instance):
        self.setPoints(-self.points)
        self.setLives(-1)

    def moveDownAllBubbles(self): 
        newPosition = self.layoutPositionY - (self.height * 0.045) 
        self.layoutPositionY = newPosition

    def createBubble(self, x, y):
        b = Bubble(pos_hint={'x': x, 'center_y': y}) 
        b.setRandomColor()
        b.source = 'graphics/bubbles/' + b.getColor() + '.png'
        return b

    def createBubbleRow(self, spaces, bubblesLeft, bubblesRight, x, y):       
        bubbleSizeX =  0.08333333333333 
        
        # create bubbles to the left
        for i in range(bubblesLeft):
            b = self.createBubble(x, y)
            self.bubbleLayout.add_widget(b)
            self.bubbleList.append(b)
            x += bubbleSizeX
        
        # add empty space for the threat
        x += (bubbleSizeX * spaces)
        
        # create bubbles to right
        for i in range(bubblesRight):
            b = self.createBubble(x, y)
            self.bubbleLayout.add_widget(b)
            self.bubbleList.append(b)
            x += bubbleSizeX
    
    def createBubbleGridRow(self, numberOfBubbles):  
        if (numberOfBubbles % 2 == 1): # odd
            self.bubbleSpaceX = 0.041666666666665    
        else:
            self.bubbleSpaceX = 0
        
        # create bubbles to the left
        for i in range(numberOfBubbles):
            b = self.createBubble(self.bubbleSpaceX, self.rowspaceY)
            self.bubbleGridLayout.add_widget(b)
            self.bubbleGridList.append(b)
            self.bubbleSpaceX += self.bubble.bubbleSizeX
        
        self.rowspaceY +=self.bubble.bubbleSizeY
           
    def createThreat(self, x, y):
        if len(self.threatList) > 0:
            # get a random threat from the list
            threatIndex = random.randint(0, len(self.threatList)-1)            
            threat = self.threatList.pop(threatIndex)
            threat.pos_hint={'x': x, 'center_y': y}
            # b.setRandomColor()
            self.bubbleLayout.add_widget(threat)
            self.threatListCopy.append(threat)

    def createObsticles(self):    

        # each block contains one threat and 3 rows of bubbles
        numberOfBlocks = 6  
        # setting procentual values for bubblesize, to be able to make the game responsive
        bubbleSizeX =  0.08333333333333 
        bubbleSizeY = 0.045
        x = 0
        y = bubbleSizeY * 10
        threatPosY = y + bubbleSizeY *1.3
        rowCount = 0
        numberOfBubbles = 12        
        xOdd = 0.041666666666665

        # the range is number of rows
        for r in range(numberOfBlocks):
            numberOfBubbles = 12
            # number of spaces that's needed for the threat to fit within the bubbles
            spaces = 3
            # set startnumber for threat
            startNumber = random.randint(1,9)

            # bubbles and posistion for first row of threat
            bubblesLeft = startNumber -1
            bubblesRight = numberOfBubbles - startNumber - spaces + 1

            # this will create 3 rows of bubbles with a threat inside at a random position
            for i in range(3): # remember! first time in range i will be equal to 0            
                # create a threat only on first row of block
                if i == 0:
                    threatPosX = (startNumber - 1) * bubbleSizeX + 0.005
                     # if threat starts at an uneven row, add half a bubble of extra space on it's left
                    if (rowCount % 2 == 1): 
                        threatPosX += xOdd

                    # create a threat
                    self.createThreat(threatPosX, threatPosY)

                    # increase the y-value for the threat position
                    threatPosY += bubbleSizeY * 3
                if (rowCount % 2 == 0): # even 
                    if i == 1:  # if row is even for every fourth row then add one bubble to left, and remove one right
                        bubblesLeft += 1
                        bubblesRight -= 1
                    if i == 2: 
                        bubblesRight -= 1
                    self.createBubbleRow(spaces, bubblesLeft, bubblesRight, x, y)
                    numberOfBubbles -= 1
                else: # odd
                    if i == 2:  # if row is uneven for every third row then take one bubble off left
                        bubblesLeft -= 1                     
                    self.createBubbleRow(spaces, bubblesLeft, bubblesRight -1, xOdd, y)
                    numberOfBubbles += 1
                spaces -= 1
                # bubbles and posistion for second row of threat
                if i == 1:
                    bubblesLeft +=1
                    
                bubblesRight += 1 
                y+=bubbleSizeY
                rowCount +=1 
     
    def createBubbleGrid(self):    
            # each block contains one threat and 3 rows of bubbles
            numberOfBlocks = 10  # needs to be even  
            numberOfBubbles = 12
            # the range is number of rows
            for i in range(numberOfBlocks):

                # set if the block should start with a row of 11 or 12 bubbles
                if (numberOfBlocks % 2 == 0): # even 
                    # STARTONEVEN 
                    self.createBubbleGridRow(numberOfBubbles)
                    self.createBubbleGridRow(numberOfBubbles-1)
                    self.createBubbleGridRow(numberOfBubbles)
                else: # odd
                    # STARTONODD
                    self.createBubbleGridRow(numberOfBubbles-1)
                    self.createBubbleGridRow(numberOfBubbles)
                    self.createBubbleGridRow(numberOfBubbles-1)        

                numberOfBlocks -= 1    

    def setTakenGridPositions(self):       
        for gridBubble in self.bubbleGridList:
            gridBubble.posTaken = False
            for bubble in self.bubbleList:
                if gridBubble.pos_hint == bubble.pos_hint:
                    gridBubble.posTaken = True
Example #2
0
File: main.py Project: mn22nw/IU
 def createBubble(self, x, y):
     b = Bubble(pos_hint={'x': x, 'center_y': y}) 
     b.setRandomColor()
     b.source = 'graphics/bubbles/' + b.getColor() + '.png'
     return b