def generate_platforms(): try: if self.completion < 100: player_x = self.test_player.x() free_to_spawn = True platforms_to_add = [] num_to_spawn = randint(2, 10) counter = 0 for p in self.platforms: if player_x + self.window_width / 4 < p.x < player_x + self.window_width: free_to_spawn = False if free_to_spawn: while counter < num_to_spawn: can_be_added = True platform_to_add = Platform( randint( player_x + self.window_width / 2, player_x + self.window_width / 2 + 150), randint(100, 500), int(self.platform_specs[0]), int(self.platform_specs[1]), self.enemy_specs) if self.rightKeydown: platform_to_add.set_x_velocity( -self.test_player.speed) for plat in platforms_to_add: if -40 < platform_to_add.x - plat.x < 40 or -80 < platform_to_add.y - plat.y < 80: can_be_added = False if can_be_added: platforms_to_add.append(platform_to_add) if platform_to_add.enemy != None: self.enemies.append( platform_to_add.enemy) counter = counter + 1 for p in platforms_to_add: self.platforms.append(p) self.scene.addItem(p) if p.health_package != None: self.scene.addItem(p.health_package) for e in self.enemies: if self.rightKeydown: e.x_velocity = -int(self.test_player.speed) e.set_p_speed(self.test_player.speed) self.scene.addItem(e) else: if not self.goal_added: goalPost1 = Platform(self.window_width + 600, 300 - 60, 20, 60) goalPost2 = Platform(self.window_width + 700, 300 - 60, 20, 60) goalBar = Platform(self.window_width + 600, 300 - 70, 120, 20) goalPost1.goal = True goalPost2.goal = True goalBar.goal = True plat_to_add = Platform(self.window_width, 300, 800, 25) plat_to_add.setBrush( QtGui.QBrush(QtGui.QColor(214, 255, 48), QtCore.Qt.SolidPattern)) goalPost1.setBrush( QtGui.QBrush(QtGui.QColor(196, 255, 0), QtCore.Qt.SolidPattern)) goalPost2.setBrush( QtGui.QBrush(QtGui.QColor(196, 255, 0), QtCore.Qt.SolidPattern)) goalBar.setBrush( QtGui.QBrush(QtGui.QColor(196, 255, 0), QtCore.Qt.SolidPattern)) if self.rightKeydown: goalPost1.set_x_velocity( -self.test_player.speed) goalPost2.set_x_velocity( -self.test_player.speed) goalBar.set_x_velocity(-self.test_player.speed) plat_to_add.set_x_velocity( -self.test_player.speed) self.scene.addItem(plat_to_add) self.scene.addItem(goalPost1) self.scene.addItem(goalPost2) self.scene.addItem(goalBar) self.platforms.append(goalPost1) self.platforms.append(goalPost2) self.platforms.append(goalBar) self.platforms.append(plat_to_add) self.goal_added = True except Exception as e: print(e)
def test_platform_and_enemy(self): self.specs = [[30, 3], [25, 4], [100, 30]] "PLATFORM" test_platform = Platform(100, 100, 100, 100) init_x = test_platform.x 'x vel for platform' test_platform.set_x_velocity(-2) "Platform movement when not scrolling (should be no movement)" 'Player not moving' test_platform.update_x(False, False) self.assertEqual(init_x, test_platform.x, 'Platform should not move when player is stationary') 'Player moving but not scrolling' test_platform.update_x(True, False) self.assertEqual( init_x, test_platform.x, 'Platform should not move when player is not scrolling') 'Player scrolling' test_platform.update_x(True, True) self.assertEqual(init_x + test_platform.x_velocity, test_platform.x, 'Platform should move to the left') "ENEMY" 'Enemy for the platform above' test_enemy = Enemy(test_platform.x, test_platform.y - 30, 25, 25, test_platform, self.specs[1]) 'Health' self.assertEqual(test_enemy.health, 100, 'Health not what it should be on init') 'Damage' test_enemy.take_damage() self.assertEqual(test_enemy.health, 95, "Wrong damage") "Dying" test_enemy.set_health(2) self.assertEqual(test_enemy.health, 2, "set_health not giving the wanted result") self.assertEqual(test_enemy.alive, True, "Enemy should be alive") test_enemy.take_damage() self.assertEqual(test_enemy.alive, False, "Enemy should be dead") "Movement" init_x = test_enemy.x test_enemy.update_x(False, False) self.assertGreater(test_enemy.x, init_x, "When on left edge the x should raise") print(test_enemy.x) "On right edge" test_enemy.set_x(test_platform.x + test_platform.width) self.assertEqual(test_enemy.x, test_platform.x + test_platform.width, "set_x not working") init_x_3 = test_enemy.x test_enemy.update_x(False, False) self.assertGreater(init_x_3, test_enemy.x, "Enemy should be moving left")