Пример #1
0
class TestTimer(unittest.TestCase):
    """
    A test class for the Timer module.
    """

    def setUp(self):
        """
        set up data used in the tests.
        setUp is called before each test function execution.
        """
        pygame.init()
        self.teams= []
        self.team1 = Team("team1")
        self.team2 = Team("team2")

        self.team1.addSnails(3)
        self.team2.addSnails(3)

        self.teams.append(self.team1)
        self.teams.append(self.team2)

        self.turnManager = TurnManager()
        self.turnManager.setTeams(self.teams)

    def testInitialized(self):
        """
        Test if init goes good
        """
        self.assertEqual(self.turnManager.teams, self.teams)
        self.assertEqual(self.turnManager.teams[0], self.team1)
        self.assertEqual(self.turnManager.teams[1], self.team2)

        self.assertEqual(self.turnManager.status, TurnStatus.BREAK)
        self.assertEqual(self.turnManager.startTime, Settings.TIMER_STARTTIME)
        self.assertEqual(self.turnManager.size, Settings.TIMER_SIZE)
        self.assertEqual(self.turnManager.position, Settings.TIMER_POSITION)
        self.assertEqual(self.turnManager.font_size, Settings.TIMER_FONT_SIZE)

    def testSingleton(self):
        """
        Test if the singleton works
        """
        turnmanager2 = TurnManager()
        self.assertEqual(id(self.turnManager), id(turnmanager2))
        self.assertEqual(self.turnManager._instance, turnmanager2._instance)

    def testTimer(self):
        """
        Test if the turnManager's status switches from CurrentTurn to Break when counter is 0
        """
        self.assertEqual(self.turnManager.status, TurnStatus.BREAK)
        #print self.turnManager.started
        self.turnManager.startTimer()

        time.sleep(Settings.TIMER_BREAKTIME + 2)

        self.assertEqual(self.turnManager.status, TurnStatus.CURRENTTURN)

        time.sleep(Settings.TIMER_STARTTIME + 2)
        self.assertEqual(self.turnManager.status, TurnStatus.BREAK)
        self.turnManager.stopTimer()

    def testCurrentTurn(self):
        """
        Test if the turnManager's status switches from Break to CurrentTurn when counter is 0
        """
        self.assertEqual(self.turnManager.status, TurnStatus.BREAK)
        #print self.turnManager.started
        self.turnManager.startTimer()

        time.sleep(Settings.TIMER_BREAKTIME + 1)

        self.assertEqual(self.turnManager.status, TurnStatus.CURRENTTURN)
        self.turnManager.stopTimer()
    
    def testChangeTurn(self):
        self.assertEqual(self.team1.hasTurn, True)
        self.turnManager.changeTurn()
        self.assertEqual(self.team1.hasTurn, False)
        self.assertEqual(self.team2.hasTurn, True)
        se


    def testChangeTurnTeam(self):
        """
        Test if the next time get's the turn
        """
        self.turnManager.startTimer()
        self.assertEqual(self.turnManager.teams[0].hasTurn, True)

        time.sleep(Settings.TIMER_BREAKTIME + Settings.TIMER_STARTTIME + 5)

        self.assertEqual(self.turnManager.teams[0].hasTurn, False)
        self.assertEqual(self.turnManager.teams[1].hasTurn, True)
        self.turnManager.stopTimer()


    def testChangeTurnSnail(self):
        """
        Test if the next snail get's the turn
        """
        a = 1

    def testFirstTurnTeam(self):
        """
        Test if the first team in the team list got the turn when the turnManager starts
        """
        self.turnManager.startTimer()
        time.sleep(2)
        self.assertEqual(self.turnManager.teams[0].hasTurn, True)
        self.assertEqual(self.turnManager.teams[1].hasTurn, False)
        self.turnManager.stopTimer()


    def testFirstTurnSnail(self):
        """
        Test if the first snail of the team who got the turn has the turn
        """
        # the first snail who gets the turn is the first snail of team 1
        firstSnailID = self.turnManager.teams[0].currentSnailTurn
        # the 2nd snail who gets the turn is the first snail of team 2
        secondSnailID = self.turnManager.teams[1].currentSnailTurn

        self.turnManager.startTimer()
        self.assertEqual(self.turnManager.teams[0].hasTurn, True)
        self.assertEqual(self.turnManager.teams[0].currentSnailTurn, firstSnailID)

#        time.sleep(Settings.TIMER_BREAKTIME + Settings.TIMER_STARTTIME + 5)
#
#        self.assertEqual(self.turnManager.teams[0].hasTurn, False)
#        self.assertEqual(self.turnManager.teams[1].hasTurn, True)
#        self.assertEqual(self.turnManager.teams[1].currentSnailTurn, secondSnailID)

        self.turnManager.stopTimer()
Пример #2
0
class Game(Scene):
    """
    This is the game class, this is the most important scene, and keeps track of the whole game
    """
    def __init__(self):
        """ Initializes the game scene """
        self.mainmenu = None
        self.winscreen = None
        self.initEvents()
        self.initTerrain()
        self.initTeams()

        self.createGameObjects()

        self.startNewGame()

    def initEvents(self):
        """ Initializes the eventreader """
        SceneManager().registerEventReader(self.do_action)

    def do_action(self, event):
        """
        Check the events, and do something when needed
        @param event: The event
        """
        
        # check events
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                for snail in TurnManager().currentTeam.orderedSnailList:
                    if snail.hasTurn:
                        snail.shoot()
            if event.key == pygame.K_ESCAPE:
                SceneManager().setScene(self.mainmenu)
    def clean(self):
        """ Clean everything up, so that application doesn't crash """
        self.turnManager.stopTimer()
        SceneManager().unregisterEventReader(self.do_action)

    def initTeams(self):
        """ Initializes the team """
        self.teams = []
        self.teamsAlive = 0

    def initTerrain(self):
        """ Initializes the terrain """
        self.terrain = Terrain()

    def createGameObjects(self):
        """ Create the terrain """
        self.terrain.create(15)

    def addTeam(self, name, numberOfSnails, gravity_direction):
        """
        Add a team to the game
        @param name: The name of the team
        @param numberOfSnails: The amount of snails the team has
        @param gravity_direction: The gravity direction of the team
        """
        team = Team(name)
        team.setGravity(gravity_direction)
        team.addSnails(numberOfSnails)
        team.setTeamImage((gravity_direction+1))
        self.teams.append(team)
        
    def startNewGame(self):
        """ Start a new game """
        for i in range(0, Settings.GAME_PLAYERS):
            self.addTeam('team '+str(i+1), Settings.GAME_SNAILS, i)
        self.turnManager = TurnManager()
        self.turnManager.setTeams(self.teams)

        self.gamemode = GameModes.GAME_PLACING_SNAILS

    def stopGame(self):
        """ Stop the game """
        self.turnManager.timer.cancel()
        teamColors = {1:'green', 2:'red', 3:'yellow', 4:'blue'}
        livingTeamColor = str(teamColors[self.teams[0].colorIndex])
        SceneManager().setScene(WinScreen(self.mainmenu, livingTeamColor))
        #SceneManager().scene = self.mainmenu

    def update(self, input):
        """
        Update the game
        @param input: The input class
        """
        self.terrain.update()
        self.updateTeams(input)

        self.updateGameMode()

    def updateTeams(self, input):
        """
        Update every team
        @param input: The input class
        """
        self.teamsAlive = 0
        for team in self.teams:
            team.update(input, self.terrain)
            self.teamsAlive += 1

    def updateGameMode(self):
        """ Update the gamemodes """
        if self.gamemode == GameModes.GAME_PLACING_SNAILS:
            for team in self.teams:
                for snail in team.sprites():
                    if snail.isPlaced == False:
                        return
            self.turnManager.startTimer()
            self.gamemode = GameModes.GAME_PLAYING
        if self.gamemode == GameModes.GAME_PLAYING:
            if self.teamsAlive <= 1:
                self.stopGame()

    def draw(self, surface):
        """
        Draw the game on a surface
        @param surface: The surface the game should be drawed on
        """
        self.terrain.draw(surface)
        for team in self.teams:
            team.draw(surface)

        #self.bullets.draw(surface)
        if self.gamemode == GameModes.GAME_PLAYING:
            self.turnManager.draw(surface)