Ejemplo n.º 1
0
 def setUp(self):
     pygame.init()
     screen = pygame.display.set_mode((800, 600))
     self.gamevents = Gamevents(800, 600)
     self.score = Score(screen, 800, 600, 5)
     self.ball = Ball(380, 280, 800, 600)
     self.bat1 = Bat(5, 280, 800, 600)
     self.bat2 = Bat(775, 280, 800, 600)
Ejemplo n.º 2
0
class TestBall(unittest.TestCase):
    def setUp(self):
        self.ball = Ball(580, 280, 800, 600)
        self.bat = Bat(775, 250, 800, 600)
        self.computer = ComputerAi(self.ball, self.bat, 800, "average")

    def test_maila_ei_liiku_kun_pallo_menossa_pois(self):
        self.ball.set_velocity(-2, -3)
        self.ball.update()
        self.computer.ai_player_move(False)
        self.assertEqual((self.bat.rect.y), 250)

    def test_maila_liikkuu_ylos_kun_pallo_tulossa_kohti_ylos(self):
        self.ball.set_velocity(5, -3)
        self.ball.update()
        self.computer.ai_player_move(False)
        self.assertEqual((self.bat.rect.y), 246)

    def test_maila_liikkuu_alas_kun_pallo_tulossa_kohti_alas(self):
        self.bat.set_y_position(200)
        self.ball.set_velocity(5, 3)
        self.ball.update()
        self.computer.ai_player_move(False)
        self.assertEqual((self.bat.rect.y), 204)

    def test_ai_lyo_palloa_kun_x_nopeus_alle_7(self):
        self.ball.rect.x = 720
        self.ball.set_velocity(4, 4)
        self.computer.ai_player_move(False)
        self.assertEqual(self.computer.ai_player_move(False), True)

    def test_ai_ei_lyo_palloa_kun_x_nopeus_yli_7(self):
        self.ball.rect.x = 720
        self.ball.set_velocity(8, 4)
        self.computer.ai_player_move(False)
        self.assertEqual(self.computer.ai_player_move(False), False)

    def test_ai_ei_lyo_palloa_kun_maila_ei_ole_kohdalla(self):
        self.ball.rect.x = 720
        self.ball.rect.y = 100
        self.ball.set_velocity(4, 4)
        self.computer.ai_player_move(False)
        self.assertEqual(self.computer.ai_player_move(False), False)

    def test_maila_liikkuu_alas_kun_pallo_tulossa_kohti_easy(self):
        self.computer = ComputerAi(self.ball, self.bat, 800, "easy")
        self.bat.set_y_position(200)
        self.ball.set_velocity(4, 3)
        self.ball.update()
        self.computer.ai_player_move(False)
        self.assertEqual((self.bat.rect.y), 202)

    def test_maila_liikkuu_ylos_kun_pallo_tulossa_kohti_easy(self):
        self.computer = ComputerAi(self.ball, self.bat, 800, "easy")
        self.ball.set_velocity(6, -3)
        self.ball.update()
        self.computer.ai_player_move(False)
        self.assertEqual((self.bat.rect.y), 248)
Ejemplo n.º 3
0
    def _normal_game(self, screen, game_type, backgrd1):
        '''initializes and starts 2 player game or game against computer.

        Args:
            screen: pygame display mode settings
            game_type: String, selected game type: "pvp" or "computer"
            backgrd1: background image according to screen size
        '''
        sprites = pygame.sprite.Group()
        background = pygame.image.load(
            os.path.join(dirname, "assets", backgrd1))
        bat1 = Bat(5, self.scr_height / 2, self.scr_width, self.scr_height)
        bat2 = Bat(self.scr_width - 25, self.scr_height / 2, self.scr_width,
                   self.scr_height)
        ball = Ball(320, 240, self.scr_width, self.scr_height)

        sprites.add(bat1)
        sprites.add(bat2)
        sprites.add(ball)

        game = Game(ball, bat1, bat2, self.scr_width, self.scr_height,
                    self.sound)
        game.main(game_type, sprites, background, screen, 0, self.ai_lvl)
Ejemplo n.º 4
0
    def _against_wall(self, screen, game_type, hiscore, backgrd2):
        '''initializes and starts against the wall game.

        Args:
            screen: pygame display mode settings
            game_type: String, selected game type: "wall"
            hiscore: Current hiscore in selected screen size.
            backgrd2: background image according to screen size
        '''
        sprites = pygame.sprite.Group()
        background = pygame.image.load(
            os.path.join(dirname, "assets", backgrd2))
        bat1 = Bat(5, self.scr_height / 2, self.scr_width, self.scr_height)
        ball = Ball(320, 240, self.scr_width, self.scr_height)

        sprites.add(bat1)
        sprites.add(ball)

        game = Game(ball, bat1, None, self.scr_width, self.scr_height,
                    self.sound)
        hiscore = game.main(game_type, sprites, background, screen, hiscore,
                            self.ai_lvl)
        return hiscore
Ejemplo n.º 5
0
 def setUp(self):
     self.bat = Bat(5, 250, 800, 600)
Ejemplo n.º 6
0
class TestBat(unittest.TestCase):
    def setUp(self):
        self.bat = Bat(5, 250, 800, 600)

    def test_konstruktori_luo_mailan_haluttuun_kohtaan(self):
        self.assertEqual((self.bat.rect.x), 5)
        self.assertEqual((self.bat.rect.y), 250)

    def test_sijoita_maila(self):
        self.bat.set_y_position(455)
        self.assertEqual((self.bat.rect.y), 455)

    def test_mailaa_ei_voi_sijoittaa_liian_alas(self):
        self.bat.set_y_position(490)
        self.assertEqual((self.bat.rect.y), 250)

    def test_mailaa_ei_voi_sijoittaa_liian_ylos(self):
        self.bat.set_y_position(-25)
        self.assertEqual((self.bat.rect.y), 250)

    def test_liikuta_mailaa_ylos(self):
        self.bat.move_up()
        self.assertEqual((self.bat.rect.y), 248)

    def test_liikuta_mailaa_alas(self):
        self.bat.move_down()
        self.assertEqual((self.bat.rect.y), 252)

    def test_maila_ei_saa_y_arvoksi_pienempaa_kuin_0(self):
        self.bat.set_y_position(2)
        self.bat.move_up()
        self.bat.move_up()
        self.assertEqual((self.bat.rect.y), 0)

    def test_maila_ei_saa_y_arvoksi_suurempaa_kuin_455(self):
        self.bat.set_y_position(453)
        self.bat.move_down()
        self.bat.move_down()
        self.assertEqual((self.bat.rect.y), 455)

    def test_vasen_maila_liikkuu_eteen_kun_lyo(self):
        self.bat.hit()
        self.assertEqual((self.bat.rect.x), 25)

    def test_vasen_maila_palaa_takaisin_paikalleen(self):
        self.bat.hit()
        self.bat.move_up()
        self.assertEqual((self.bat.rect.x), 5)

    def test_oikea_maila_liikkuu_eteen_kun_lyo(self):
        self.bat.rect.x = 775
        self.bat.hit()
        self.assertEqual((self.bat.rect.x), 755)

    def test_oikea_maila_palaa_takaisin_paikalleen(self):
        self.bat.rect.x = 775
        self.bat.hit()
        self.bat.move_up()
        self.assertEqual((self.bat.rect.x), 775)
Ejemplo n.º 7
0
class TestGamevents(unittest.TestCase):
    def setUp(self):
        pygame.init()
        screen = pygame.display.set_mode((800, 600))
        self.gamevents = Gamevents(800, 600)
        self.score = Score(screen, 800, 600, 5)
        self.ball = Ball(380, 280, 800, 600)
        self.bat1 = Bat(5, 280, 800, 600)
        self.bat2 = Bat(775, 280, 800, 600)

    def test_tietokone_odotuksen_jalkeen_palauttaa_false(self):
        self.assertEqual(self.gamevents.wait(3), False)

    def test_tietokone_laukaisee_pallon(self):
        self.assertEqual(self.gamevents.launch(3, self.ball), True)

    def test_pallo_lahtee_liikkeelle_kun_peli_alkaa(self):
        self.assertEqual(self.gamevents.launch(0, self.ball), True)

    def test_against_wall_pallo_pysyy_poydalla_hiscore_ei_muutu(self):
        run, hiscore = self.gamevents.ball_on_table(self.ball, self.score, 4,
                                                    5, True)
        self.assertEqual(run, True)
        self.assertEqual(hiscore, 5)

    def test_pelaaja_saa_pisteen_tietokonetta_vastaan(self):
        self.ball.rect.x = 805
        run, score = self.gamevents.is_p1_score(self.ball, 1, self.score, True,
                                                False, None)
        self.assertEqual(run, True)
        self.assertEqual(score, 2)

    def test_tietokone_voittaa_pelin(self):
        self.ball.rect.x = -44
        run, score = self.gamevents.is_p2_score(self.ball, 4, self.score, True,
                                                3, None)
        self.assertEqual(run, False)
        self.assertEqual(score, 5)

    def test_pallo_kimpoaa_mailasta_against_wall(self):
        self.ball.rect.x = 28
        self.ball.set_velocity(-6, 1)
        self.bat1.set_y_position(275)
        self.ball.update()
        score = self.gamevents.collision_wall(self.ball, self.bat1, 1, None)
        self.assertGreater(self.ball.get_x_velocity(), 3)
        self.assertEqual(score, 2)

    def test_pallo_kimpoaa_taka_seinasta_against_wall(self):
        self.ball.rect.x = 0.75 * 800 - 50
        self.ball.set_velocity(7, 1)
        self.ball.update()
        self.gamevents.collision_wall(self.ball, self.bat1, 2, None)
        self.assertLess(self.ball.get_x_velocity(), 0)

    def test_pallo_vaihtaa_suuntaa_pelaajan1_mailasta(self):
        self.ball.set_position(31, 120)
        self.ball.set_velocity(-8, 3)
        self.bat1.set_y_position(100)
        self.ball.update()
        self.gamevents.collision(self.bat1, self.bat2, self.ball, None, True)
        self.assertGreater(self.ball.get_x_velocity(), 6)

    def test_pallo_vaihtaa_suuntaa_pelaajan2_mailasta(self):
        self.ball.set_position(735, 120)
        self.ball.set_velocity(8, 3)
        self.bat2.set_y_position(100)
        self.ball.update()
        self.gamevents.collision(self.bat1, self.bat2, self.ball, None, False)
        self.assertLess(self.ball.get_x_velocity(), -6)
Ejemplo n.º 8
0
 def setUp(self):
     self.ball = Ball(580, 280, 800, 600)
     self.bat = Bat(775, 250, 800, 600)
     self.computer = ComputerAi(self.ball, self.bat, 800, "average")